By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

help me ....implementing multiple stacks using arrray

P: 5
i want to implement multiple stacks using arrays..I am able to create ,insert and print them but not poping an element form any of the stack..This is what i have
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. int top[5];
  4. int bot[5];
  5.  
  6.  
  7. void main()
  8. {
  9. int a[50]={0} ;
  10. int length,num;
  11. int ch;
  12. int temp,x,st_no,st_no1;
  13. int b,i,j,k;
  14. clrscr();
  15. printf("Enter the length of the total stack: ");
  16. scanf("%d",&length);
  17. printf("\nEnter the number of stacks: ");
  18. scanf("%d",&num);
  19. //divinding the arrays into stacks
  20. b=(length)/(num);
  21. top[0]=0;
  22. bot[0]=0;
  23. for(i=1;i<num;i++)
  24.   {
  25.     top[i]=top[i-1]+b;
  26.     bot[i]=bot[i-1]+b;
  27.   }
  28. do
  29.    {
  30.     printf("\n1.PUSH 2.POP 3.PRINT 4.EXIT \n");
  31.     printf("Enter the choice: ");
  32.     scanf("%d",&ch);
  33.     switch(ch)
  34.       {
  35.        case 1:
  36.     printf("Enter the stack number: ");
  37.     scanf("%d",&st_no);
  38.     if(top[st_no-1]==bot[st_no] || top[st_no-1]==length)
  39.     printf("\n\n\n!!THE %d STACK IS FULL\n",st_no);
  40.     else
  41.     {
  42.       printf("ENTER THE ELEMENT TO BE PUSHED");
  43.       scanf("%d",&x);
  44.       a[top[st_no-1]]=x;
  45.      top[st_no-1]=top[st_no-1]+1;
  46.     }
  47.  
  48.  
  49.       break;
  50.     //-------------------------------------------------------
  51.     case 2:
  52.         printf("\nENTER THE STACK NUMBER");
  53.         scanf("%d",&st_no1);
  54.         printf("\nThe stack you have choosen is %d",st_no1-1);
  55.         printf("-----%d-----",a[top[(st_no-1)]]);
  56.         if(top[st_no-1]==bot[st_no-1])
  57.         printf("\n!STACK EMPTY!\n");
  58.         else
  59.         {
  60.  
  61.           printf("\nDELETED ELMENT IS: %d\n",a[top[st_no1-1]]);
  62.           top[st_no1-1]=top[st_no1-1]-1;
  63.            }
  64.  
  65.          break;
  66.        //----------------------------------------------------------
  67.     case 3:
  68.        k=0;
  69.        for(i=0;i<num;i++)
  70.         {
  71.         printf("\nThe elements in stack %d are: ",i+1);
  72.  
  73.                for(j=0;j<b;j++)
  74.          {
  75.           printf("%d->",a[i+j+k]);
  76.           }
  77.            k=k+b-1;
  78.          }
  79.  
  80.  
  81.         break;
  82.     }
  83.     }
  84.     while(ch<4);
  85.     getch();
  86.  
  87. }
  88.  
Oct 15 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.