栈与队列的算法总结

栈与队列的算法总结

  • 栈与队列实现
  • 含有最小值的栈
  • 两个栈实现队列
  • 一个栈排序另一个栈

含有最小值的栈

思路:用一个辅助栈来记录主栈的最小值,对于主栈来说有两种操作:插入,弹出。

插入:当向主栈插入元素时,有三种情况:

  • 1.当向主栈插入元素后,如果辅助栈为空,则直接将此元素插入辅助栈顶。
  • 2.当向主栈插入元素后,辅助栈不为空,需要判断辅助栈栈顶元素与插入元素大小,如果辅助栈栈顶元素大于插入元素,则将插入元素插入到辅助栈栈顶。
  • 3.如果辅助栈栈顶元素小于或者等于插入元素,则把辅助栈栈顶元素插入到辅助栈的栈顶。

如图,stockData为主栈,stackMin为辅助栈,当向主栈插入元素时:

弹出:当弹出主栈元素时,辅助栈也需要弹出元素。

#include <stdio.h>
#include<malloc.h>

//声明链表节点类型
typedef struct LISTtag LISTtagNode;
struct    LISTtag{
    int value;
    LISTtagNode *next;
};
int size=0;
int sizeminor = 0;

//创建链表
LISTtagNode * create(){
    LISTtagNode *head;
    head = (LISTtagNode*)malloc(sizeof(LISTtagNode));
    head->next =  NULL;
    return head;
}


//在链表头新增节点
int push(LISTtagNode *head,int value){
    if(head == NULL){
        return -1;
    }
    //新建节点
    LISTtagNode *node;
    node = (LISTtagNode*)malloc(sizeof(LISTtagNode));
    //节点赋值,此节点跟在头节点后
    node->value = value;
    node->next = head->next;
    head->next = node;
    size++;
    return 1;
}


//打印链表
int PrindLinKList(LISTtagNode *head){
     if(head == NULL){
        return -1;
    }
    printf("\n ");
    LISTtagNode *t = head;
    while (t->next != NULL) {
        t = t->next;
        printf("%d ", t->value);
    }
    return 1;
}


int pop(LISTtagNode *head){
    if(head ==NULL){
        return -1;
    }
    LISTtagNode *node;
    int index = 0;
    while(head->next!=NULL&&index<1){
        node = head->next;
        index++;
    }
    if(index == 1){
         printf("\n主栈弹出元素:%d \n",node->value);
        if(head->next->next != NULL){
            head->next = head->next->next;  
        }else{
            head->next =NULL;
        }
        free(node);
        size--;
        return 1;
    }  
    return -1;
}

//打印栈顶元素
int top(LISTtagNode *head){
    if(head ==NULL){
        return -1;
    }
    LISTtagNode *node;
    int index = 0;
    while(head->next!=NULL&&index<1){
        node = head->next;
        index++;
    }
    if(index == 1){
        printf("\n栈顶元素:%d\n",node->value);
        return 1;
    }
    return -1;
}

int empty(LISTtagNode *head){
    if(head ==NULL){
        return -1;
     }
    if(head->next!=NULL){
        return 0;
    }
    return 1;
}

//链表长度
int length(LISTtagNode *head){
    if(head ==NULL){
        return -1;
     }
    return size;
}


//在链表头新增节点
int pushminor(LISTtagNode *head,int value){
    if(head == NULL){
        return -1;
    }
    if(head->next ==NULL){
          //新建节点
        LISTtagNode *node;
        node = (LISTtagNode*)malloc(sizeof(LISTtagNode));
        //节点赋值,此节点跟在头节点后
        node->value = value;
        node->next = head->next;
        head->next = node;
        sizeminor++;
        return 1;
    }else{
        if(head->next->value >= value){
            //新建节点
            LISTtagNode *node;
            node = (LISTtagNode*)malloc(sizeof(LISTtagNode));
            //节点赋值,此节点跟在头节点后
            node->value = value;
            node->next = head->next;
            head->next = node;
            sizeminor++;
            return 1;
        }else{
              //新建节点
            LISTtagNode *node;
            node = (LISTtagNode*)malloc(sizeof(LISTtagNode));
            //节点赋值,此节点跟在头节点后
            node->value = head->next->value;
            node->next = head->next;
            head->next = node;
            sizeminor++;
            return 1;
        }
        
        
    }
  
}


int popminor(LISTtagNode *head){
    if(head ==NULL){
        return -1;
    }
    LISTtagNode *node;
    int index = 0;
    while(head->next!=NULL&&index<1){
        node = head->next;
        index++;
    }
    if(index == 1){
         printf("辅助栈弹出元素:%d\n",node->value);
        if(head->next->next != NULL){
            head->next = head->next->next;  
        }else{
            head->next =NULL;
        }
        free(node);
        sizeminor--;
        return 1;
    }
    return -1;
}

int main () {
    //主栈
    LISTtagNode *head = create();
    //辅助栈
    LISTtagNode *headminor = create();
    
    //向链表插入元素
    push(head,7);
    pushminor(headminor,7);
    //PrindLinKList(headminor);
    top(headminor);
    
     //向主栈插入元素
    push(head,9);
     //向辅助栈插入元素
    pushminor(headminor,9);
    //打印辅助栈的栈顶元素,就是最小值
    top(headminor);
    
     //向链表插入元素
    push(head,23);
    pushminor(headminor,23);
    top(headminor);
    
     //向链表插入元素
    push(head,2);
    pushminor(headminor,2);
    top(headminor);
    
    //弹出链表元素
    pop(head);
    popminor(headminor);
    top(headminor);
    
    
    return 0;
}
实现代码

猜你喜欢

转载自www.cnblogs.com/-wenli/p/12641322.html