implementing a stack by using a double linked list

Discussion in 'C' started by fame ndongo, Aug 25, 2008.

Thread Status:
Not open for further replies.
  1. fame ndongo

    fame ndongo New Member

    Joined:
    Aug 25, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    please,is there anybody who could help me urgently to implement a stack by using two ways linked list. here's the program that i have tried to do:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    #define Stack_size 5
    #define Queue_size 5
    
    void stack();
    void queue();
    void insert_stack(int n);
    void output_stack();
    int delete_stack();
    
    void insert_queue(int n);
    void output_queue();
    int delete_queue();
    void cir_queue();
    
    void insert_cir_queue(int n);
    void output_cir_queue();
    int delete_cir_queue();
    void cir_cir_queue();
    
    
    int STACK[Stack_size];
    int QUEUE[Queue_size];
    int CIR_QUEUE[Queue_size];
    
    
    int top;
    int front,rear;
    int cir_front,cir_rear;
    
    
    int main()
    {
      char ch;
      top=0;
      front=0;
      rear=0;
      cir_front=0;
      cir_rear=0;
    
    
      clrscr();
    
      do{
      start:
          printf("\n\nPress\n\n");
          printf("(1) for stack operation.\n");
          printf("(2) for queue operation.\n");
          printf("(3) for circular queue operation.\n");
          printf("(4) for exit.\n");
    
          ch=getch();
    
          switch(ch)
          {
           case '1': stack();
    		 break;
    
           case '2': queue();
    		 break;
    
           case '3': cir_queue();
    		 break;
    
           case '4': exit(0);
    		 break;
          }
    
      }while(1);
    
    
     return 0;
    }
    
    
    void stack()
    {
      char ch;
      int n;
      do{
          printf("\n\nPress\n\n");
          printf("(1) to insert number in the stack.\n");
          printf("(2) to delete number in the stack.\n");
          printf("(3) to exit operation\n\n");
    
          ch=getch();
    
          switch(ch)
          {
           case '1': printf("insert the number.\n");
    		 scanf("%d",&n);
    		 insert_stack(n);
    		 output_stack();
    		 break;
    
    
           case '2': n=delete_stack();
    		 if(n==-999)
    		   printf("Underflow situation has come.\n");
    		 else{
    		   printf("%d is deleted from the stack",n);
    		   output_stack();
    		   }
    		 break;
    
    
           case '3': return;
    
    
          }
    
    
    
    
      }while(1);
    
    }
    
    
    
    
    void queue()
    {
      char ch;
      int n;
    
      do{
          printf("\n\nPress\n\n");
          printf("(1) to insert number in the queue.\n");
          printf("(2) to delete number in the queue.\n");
          printf("(3) to exit operation\n\n");
    
          ch=getch();
    
          switch(ch)
          {
           case '1': printf("insert the number.\n");
    		 scanf("%d",&n);
    		 insert_queue(n);
    		 output_queue();
    		 break;
    
    
           case '2': n=delete_queue();
    		 if(n==-999)
    		   printf("Underflow situation has come.\n");
    		 else{
    		   printf("%d is deleted from the queue",n);
    		   output_queue();
    		   }
    		 break;
    
    
           case '3': return;
    
    
          }
    
          }while(1);
    
    
    
    
    }
    
    
    
    void insert_stack(int n)
    {
        if(top==Stack_size)
          printf("Overflow situation has occured.\n");
    
        else{
    	STACK[top]=n;
    	top++;
    	}
    }
    
    int delete_stack()
    {
       int number;
    
       if(top==NULL){
         return -999;
         }
    
       else{
       number=STACK[--top];
       return number;
       }
    
    
    }
    
    void output_stack()
    {
    
      printf("\n\nStack status.\n\n");
    
      for(int i=0;i<top;i++)
       printf("%d\t",STACK[i]);
    
    }
    
    void insert_queue(int n)
    {
       if(rear==Queue_size)
        printf("Overflow situation.\n");
       else{
        QUEUE[rear]=n;
        rear++;
       }
    
    }
    
    void output_queue()
    {
    
      printf("\n\nQueue status.\n\n");
    
      for(int i=front;i<rear;i++)
       printf("%d\t",QUEUE[i]);
    
    }
    
    
    int delete_queue()
    {
       int number;
    
       if(front==rear){
         return -999;
         }
    
       else{
       number=QUEUE[front++];
       return number;
       }
    }
    
    void cir_queue()
    {
      char ch;
      int n;
    
      do{
          printf("\n\nPress\n\n");
          printf("(1) to insert number in the circular queue.\n");
          printf("(2) to delete number in the circular queue.\n");
          printf("(3) to exit operation\n\n");
    
          ch=getch();
    
          switch(ch)
          {
           case '1': printf("insert the number.\n");
    		 scanf("%d",&n);
    		 insert_cir_queue(n);
    		 output_cir_queue();
    		 break;
    
    
           case '2': n=delete_cir_queue();
    		 if(n==-999)
    		   printf("Underflow situation has come.\n");
    		 else{
    		   printf("%d is deleted from the queue",n);
    		   output_cir_queue();
    		   }
    		 break;
    
    
           case '3': return;
    
          }
         }while(1);
    
    }
    
    void insert_cir_queue(int n)
    {
    
    
       if((cir_front>cir_rear)&&((cir_front-cir_rear)==1)||(cir_front==0&&cir_rear==Queue_size))
        printf("Overflow situation.\n");
    
       else
       {
         if((cir_rear==Queue_size)&&(cir_front!=0))
           cir_rear=0;
    
           CIR_QUEUE[cir_rear]=n;
           printf("Front =%d rear =%d\n",cir_front,cir_rear);
           cir_rear++;
    
       }
    
    }
    
    void output_cir_queue()
    {
      int i;
      printf("\n\nCircular Queue status.\n\n");
    
      if(cir_rear>=cir_front){
        for(i=cir_front;i<cir_rear;i++)
         printf("%d\t",CIR_QUEUE[i]);
       }
      else{
    
    
        for(i=cir_front;i<Queue_size;i++)
         printf("%d\t",CIR_QUEUE[i]);
    
        for(i=0;i<cir_rear;i++)
         printf("%d\t",CIR_QUEUE[i]);
    
    
        }
    
    
    
    }
    
    
    int delete_cir_queue()
    {
       int number;
    
       if(cir_front==cir_rear){
         return -999;
         }
    
       else
       {
       if ((cir_front==Queue_size)&&(cir_rear!=0))
         cir_front=0;
    
         number=CIR_QUEUE[cir_front++];
         printf("Front =%d rear =%d\n",cir_front,cir_rear);
    
    
         return number;
       }
    
    }
    thanks a lot.
     
    Last edited by a moderator: Aug 25, 2008
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice