Stack and queue algorithm summary

 

Stack and queue algorithm summary

  • Stack and queue implementation
  • Minimum stack comprising
  • Two stacks queue
  • Sorting a stack to another stack

 

Minimum stack comprising

Thinking: recording with a minimum of auxiliary stack to the main stack with two operation to the main stack is: insert, eject.

Insert: insert elements when the main stack, there are three cases:

  • 1. When the stack is inserted into the main elements, if the auxiliary stack is empty, this element directly inserted auxiliary stack.
  • 2. When the stack is inserted into the main element, the secondary stack is not empty, the top element is necessary to determine the auxiliary stack and the size of the insertion element, the top element is greater than if the auxiliary stack insertion element will be inserted into the auxiliary insertion element stack stack.
  • 3. If the auxiliary stack top element less than or equal to insert elements, put the auxiliary stack top element of the stack is inserted into the auxiliary stack.

As shown, the main stack StockData, stackMin auxiliary stack, when the element is inserted into the main stack:

Pop: pop up when the main elements of the stack, the stack have to pop the auxiliary elements.

#include <stdio.h> 
#include < the malloc .h> // declaration list node type 
typedef struct listtag LISTtagNode;
 struct     listtag {
     int value; 
    LISTtagNode * Next; 
}; int size = 0 ;
 int sizeminor = 0 ; // Create list 
LISTtagNode * Create () { 
    LISTtagNode * head; 
    head = (LISTtagNode *) the malloc ( the sizeof (LISTtagNode)); 
    head -> Next =   NULL;
    




return head; 
} 


// the new node list head 
int Push (LISTtagNode head *, int value) {
     IF (head == NULL) {
         return - . 1 ; 
    } 
    // new node 
    LISTtagNode * Node; 
    Node = (LISTtagNode *) the malloc ( the sizeof (LISTtagNode));
     // node assigned with the node at the head node 
    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 ; 
} 

// chain length 
int length (* LISTtagNode head) {
     IF (head == NULL) {
         return - . 1 ; 
     } 
    return size; 
} 


// add nodes to the beginning of the list 
int pushminor (LISTtagNode head *, int value ) {
     IF (head == NULL) {
         return - . 1 ; 
    } 
    IF (head-> Next == NULL) {
           // new node 
        LISTtagNode * node; 
        node = (LISTtagNode *)the malloc ( the sizeof (LISTtagNode));
         // node assigned with the node at the head node 
        node-> value = value; 
        Node -> Next = head-> Next; 
        head -> Next = Node; 
        sizeminor ++ ;
         return  . 1 ; 
    } the else {
         IF (head-> next-> value> = value) {
             // new node 
            LISTtagNode * node; 
            node = (LISTtagNode *) the malloc ( the sizeof (LISTtagNode));
             //Node assigned with the node at the head node 
            node-> value = value; 
            Node -> Next = head-> Next; 
            head -> Next = Node; 
            sizeminor ++ ;
             return  . 1 ; 
        } the else {
               // new node 
            LISTtagNode * node; 
            node = (LISTtagNode *) the malloc ( the sizeof (LISTtagNode));
             // node assigned with the node at the head node 
            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 ();
     // auxiliary stack 
    LISTtagNode headminor = * Create (); 
    
    // insert elements into the list 
    Push (head, . 7 ); 
    pushminor (headminor, . 7 );
     // PrindLinKList (headminor); 
    Top (headminor); 
    
     // element is inserted into the main stack 
    Push (head, . 9 );
      // insert elements into the auxiliary stack 
    pushminor (headminor, . 9 );
     // print the auxiliary stack top element, is the minimum 
    Top (headminor); 
    
     // insert elements into the list 
    Push (head, 23 is ); 
    pushminor (headminor, 23 is ); 
    Top (headminor);
    
     // inserted into the list element 
    Push (head, 2 ); 
    pushminor (headminor, 2 ); 
    Top (headminor); 
    
    // pop-up list element 
    POP (head); 
    popminor (headminor); 
    Top (headminor); 
    
    
    return  0 ; 
}
Implementation code

 

Guess you like

Origin www.cnblogs.com/-wenli/p/12641322.html