stack

This is a discussion on stack within the C Programming forums, part of the General Programming Boards category; hi jsut need a little help editing this new to forum and hope to stay around heard this is the ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    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, 01:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

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