Thread: stack

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    stack

    hi jsut need a little help editing this new to forum and hope to stay around heard this is the best one around ,
    i want to use arrays eg house
    dog
    cat

    only problem is i can only use stdio.h

    when cat is entered a second time it pops off the list ??
    ive started building the stack but can only do it with numbers if anyone can edit this for me will be a great help

    all the best dave

    Code:
     #include <stdio.h>
    #include<ctype.h>
    # define MAXSIZE 200
    
    int stack[MAXSIZE];
    int top;	//index pointing to the top of stack
    
    void push(int y)
    {
    
    if(top>MAXSIZE)
           {
           printf("STACK FULL");
           return;
           }
    else
    	{
    	top++;
    	stack[top]=y;
    	}
    }
    
    int pop()
    {
    int a;
    if(top<=0)
    	{
    	printf("STACK EMPTY");
    	return 0;
    	}
    else
    	{
    	a=stack[top];
    	top--;
    	}
    return(a);
    
    }
    
    int main()
    {
    void push(int);
    {
    int pop();
    int i,num;
    
    int value;
    
    {
    
    {
    
      do {
        printf("Enter value: ");
        scanf("%d", &value);
    
        if(value !=0) push(value);
        else printf("value on top is %d\n", pop());
    
      } while(value != -1);
    
      return 0;
    }
    
    
    	
    	system("PAUSE");
    	return 0;
    	
    }
    }
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    starting to look through it, i see you never initialized 'top'. initialize it to something, probably in main, or on the same line it is declared.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also you have problem with bounds...
    indexes you can access in your stack are from 0 to MAXSIZE -1
    you are accessing items from 1 to MAXSIZE +1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You have redefined your push() in main. You have a couple of stray return 0's in there.

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Also, global variables are not very good coding style. You should declare your top and stack[] variables in the main function and pass a pointer to them to your pop and push functions...
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM