Please Help Me With This Code Of Implementing Stacks

This is a discussion on Please Help Me With This Code Of Implementing Stacks within the C Programming forums, part of the General Programming Boards category; i want to implement multiple stacks using arrays..I am able to create ,insert and print them but not poping an ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    Please Help Me With This Code Of Implementing Stacks

    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
    Code:
    #include<stdio.h>
    #include<conio.h>
    int top[5];
    int bot[5];
    
    
    void main()
    {
    int a[50]={0} ;
    int length,num;
    int ch;
    int temp,x,st_no,st_no1;
    int b,i,j,k;
    clrscr();
    printf("Enter the length of the total stack: ");
    scanf("%d",&length);
    printf("\nEnter the number of stacks: ");
    scanf("%d",&num);
    //divinding the arrays into stacks
    b=(length)/(num);
    top[0]=0;
    bot[0]=0;
    for(i=1;i<num;i++)
      {
        top[i]=top[i-1]+b;
        bot[i]=bot[i-1]+b;
      }
    do
       {
        printf("\n1.PUSH 2.POP 3.PRINT 4.EXIT \n");
        printf("Enter the choice: ");
        scanf("%d",&ch);
        switch(ch)
          {
           case 1:
    	printf("Enter the stack number: ");
    	scanf("%d",&st_no);
    	if(top[st_no-1]==bot[st_no] || top[st_no-1]==length)
    	printf("\n\n\n!!THE %d STACK IS FULL\n",st_no);
    	else
    	{
    	  printf("ENTER THE ELEMENT TO BE PUSHED");
    	  scanf("%d",&x);
    	  a[top[st_no-1]]=x;
    	 top[st_no-1]=top[st_no-1]+1;
    	}
    
    
    	  break;
    	//-------------------------------------------------------
    	case 2:
    		printf("\nENTER THE STACK NUMBER");
    		scanf("%d",&st_no1);
    		printf("\nThe stack you have choosen is %d",st_no1-1);
    		printf("-----%d-----",a[top[(st_no-1)]]);
    		if(top[st_no-1]==bot[st_no-1])
    		printf("\n!STACK EMPTY!\n");
    		else
    		{
    
    		  printf("\nDELETED ELMENT IS: %d\n",a[top[st_no1-1]]);
    		  top[st_no1-1]=top[st_no1-1]-1;
    	       }
    
    	     break;
           //----------------------------------------------------------
    	case 3:
    	   k=0;
    	   for(i=0;i<num;i++)
    	    {
    	    printf("\nThe elements in stack %d are: ",i+1);
    
    		       for(j=0;j<b;j++)
    		 {
    		  printf("%d->",a[i+j+k]);
    		  }
    	       k=k+b-1;
    	     }
    
    
    	    break;
    	}
        }
        while(ch<4);
        getch();
    
    }

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    more info? (expected result when you pop, actual result given by the code)

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote Originally Posted by yxunomei
    more info? (expected result when you pop, actual result given by the code)
    when i push an item in to stack 1 i get the contents of the stack as item-0-0 etc....
    but when i pop the element my top and bottom(top,bot) of the stack are moving but i am unable to display the stack after poping....

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    int main()
    Also make sure that main() returns 0.

    Are you sure you want two variables called st_no and st_no1, it looks like you are getting confused as to which one to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21