simple stack

This is a discussion on simple stack within the C Programming forums, part of the General Programming Boards category; Hi all i am learning C now and i want to make a program that for a simple stack (an ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    simple stack

    Hi all i am learning C now and i want to make a program that for a simple stack (an integer array) i will insert elements (integers) in the stack until it will be full or remove them until it will be empty.Here is my code:
    Code:
    #define N 2
    int stack[N];
    int head=0;
    
    #include<stdio.h>
    void push(int);
    int pop(void);
    
    int main()
    {
    	int s,k;
    
    	do
    	{
    		printf("Give your selection\n");
    		printf("1.Push\n");
    		printf("2.Pop\n");
    		printf("3.Finish\n");
    		scanf("%d",&s);
    		switch(s)
    		{
    			case 1:
    			{
    				printf("give an element to push in the stack:\n");
    				r=scanf("%d",&k);
    				if (r==1)
    					push(k);
    				else
    				{
    					printf("You don't give a valid number");
    					return 0;
    				}
    				break;
    			}
    			case 2:
    			{
    				pop();
    				break;
    			}
    		}
    	}
    	while(s!=3);
    
    }
    
    void push(int e)
    {
    	if (head<N)
    		stack[head++]=e;
    	else
    		printf("STACK IS FULL\n");
    }
    
    
    int pop(void)
    {
    	if(head>0)
    		return stack[--head];
    	else
    		printf("STACK IS EMPTY\n");
    }
    My problem is that here i declare the array and the index of the stack(head) as global variables but when i try to declare them as local variables in main and pass them to push and pop functions i can't get it to work.Any ideas?

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok this is probably because you're not passing your variables by reference to you function, and therefore head++ increments the copy of head that you're sending to the function, so head(in main) always stays at 0.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    by reference
    because it is c - not a reference but pointers should be used...

    I think the better way is to use a struct
    Code:
    typedef struct
    {
       int items[N];
       unsigned int head=0;
    }stack;
    in main declare the variable of this type

    stack myStack;

    and pass the pointer to it in the push/pop functions

    push(&myStack, newValue);

    value = pop(&myStack);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    because it is c - not a reference but pointers should be used...
    Agreed. But from what I remember, sending a variable to a function using a pointer is what's called passing the argument by reference. When you don't use a pointer, it's passing the argument by value.

    And I agree that ideally you'd want a structure for the stack, but if the OP wants to stick with his array, those small changes will get him what he desires.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 09:32 AM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  3. Rough cut stack manipulation
    By Thantos in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 11:49 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. HEap and stack, I'm confused
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-31-2002, 09:59 AM

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