Thread: Stack program

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Stack program

    Hey i really need help for this stack project im working on. im just trying to make a simple stack that adds values,in total there will be five values and then im trying to get the stack to pop the values at the top and take in new values at the bottom. Can someone please tell me what im doing wrong heres my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 5
    #define newbos 0
    
    /*create a stack size of 50*/
    void push(int i);
    /*create a fucntion called push which returns value i*/
    int pop (void);
    /*create a function called pop which has no return value*/
    int  *tos, *p1, stack[SIZE],*p2,loop;
    /*make tos an address that points to top of stack,p1 
    as push 1 value, and stack with an array size*/
    
    int main(int argc, char *argv[])
    {
      int value;
      /*create a integer number called value its undeclared ehre*/
      
    tos = stack; /* tos points to the top of stack */
      p1 = stack; /* initialize p1 */
    /*make top of stack point to int stack*/
      do {
        printf("Enter value: ");
        scanf("%d", &value);
    
        if(value != 0) push(value);
        else printf("value on top is %d\n", pop());
    
      } while(value != -1);
    /*do enter value and take value until, if value is not = to 0
    push (value) or else print value on the top of the stack and pop it*/
      return 0;
    }
    
    void push(int i)
    {
        
      p1++;
      if(p1 == (tos+SIZE)) {
        printf("Maximum stack size,trying to pop and add one new value.\n");
        getch();
         if(p1 ==(tos+SIZE)){
        p1 == 0; 
        loop++;
        overlap();
        printf("value of stack is %i",p1);   
      }
        exit(1);
      }
      *p1 = i;
    }
    int overlap()
    {
     if(loop == 5)
     {
         *tos == newbos;    
             
     }
    }
    int isempty()
    {
       if(tos <= 0)
       {
              printf("stack is empty");
       } 
        
    }
    int pop(void)
    {
      if(p1 == tos) {
        printf("Stack Underflow.\n");
        getch();
     
      }
      p1--;
      return *(p1+1);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A stack doesn't take in new values at the top and old values at the bottom -- that's a queue.

    I don't know what you're seeing that you don't want to see.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Stacks
    By Cmuppet in forum C Programming
    Replies: 19
    Last Post: 10-13-2004, 02:32 PM
  3. linked list stack question
    By Drew in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2003, 05:05 AM
  4. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM
  5. Accessing data in the program stack
    By dajur in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 10:15 PM