Thread: Stack Problem

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Stack Problem

    Hi, I need your help with this one.
    I created a stack program but there is a problem.
    The program doesn't seem to add data into the push function.


    Code:
    /*
     *Stack
     *C
     *
     */ 
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    typedef struct stack Stack;
    
    
    struct stack{
       int data;
       Stack *next;     
    };
    
    
    
    
    void push(Stack *, int);
    int pop(Stack *);
    int is_empty(Stack *); //
    void print_stack(Stack *);
    void instructions(void);
    
    
    int main(void)
    {
       Stack *node = NULL;
       int choice;
       int value;
       
       instructions();
       printf("?");
       scanf("%d", &choice);
       
       while(choice != 3)
       {
          switch(choice)
          {
             case 1:
                printf("\nEnter an integer => ");
                scanf("%d", &value); 
                push(node,value); 
                print_stack(node); 
                break; 
                 
             case 2:
                if(!is_empty(node))
                {
                   printf("\nThe popped value => %d", pop(node));                   
                }      
                print_stack(node);
                break;
                
             default:
                printf("\nInvalid choice\n\n");
                instructions();
                break;              
          }
       printf("?");   
       scanf("%d ", &choice);         
       }
       printf("End Of Run!\n");
       return 0;   
    }
    
    
    void push(Stack *node, int value)
    {
       Stack *new_node;
       new_node = (Stack *)malloc(sizeof(Stack));
       
       if(new_node != NULL)
       {
          new_node->data = value;
          new_node->next = node;
          node = new_node;            
       }    
       else printf("No available memory fo %d\n", value);
    }
    int pop(Stack *node)
    {
        Stack *temp;
        int value;
        
        temp = node;
        value = node->data;
        node = node->next;
        free(temp);
        
        return value;
    }
    
    
    int is_empty(Stack *node)
    {
       return node == NULL;   
    }
    
    
    void print_stack(Stack *node)
    {
       if(node == NULL)
       {
          printf("The stack is empty.\n\n");        
       }
       else
       {
          printf("Tha stack is: \n");    
          while(node != NULL)
          {
             printf("%d --> ", node->data);
             node = node->next;
          }
          printf("NULL\n\n");
       }
    }
    
    
    void instructions(void)
    {
       printf("Enter choice => \n"
                     "1 push\n"
                     "2 pop\n"
                     "3 end\n");    
    }

    Can somebody tell me what's wrong?

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Code:
    node = new_node;
    That changes value of node pointer only in push() function.

    The are 2 ways to fix it:
    1. By passing pointer to Stack pointer
    Code:
    void push(Stack **node, int value)
    {
       Stack *new_node;
       new_node = malloc(sizeof(Stack)); // no type-cast needed here
       if(new_node != NULL)
       {
          new_node->data = value;
          new_node->next = *node;
          *node = new_node;
       }
       else printf("No available memory fo %d\n", value);
    }
    2. By returning new pointer.
    Code:
    Stack *push(Stack *node, int value)
    {
       Stack *new_node;
       new_node = malloc(sizeof(Stack)); // no type-cast needed here
       if(new_node != NULL)
       {
          new_node->data = value;
          new_node->next = node;
       }
       else printf("No available memory fo %d\n", value);
       return new_node;
    }

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    i understand now, you've been a great help.

    on your comment, it says no type-cast needed.
    why and when do i need to do type-casting ?
    i need clarifications!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by noroi View Post
    on your comment, it says no type-cast needed.
    why and when do i need to do type-casting ?
    in C

    void* pointer could be assigned to any other pointer without cast, and the opposite.

    since malloc returns void* its result could be assigned to any pointer without cast
    Removing cast also provides additional help in verifying that required include is not missing
    FAQ > Casting malloc - Cprogramming.com
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem Stack in c++
    By magician89 in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2010, 08:32 AM
  2. need help for stack problem again
    By bahareh in forum C++ Programming
    Replies: 20
    Last Post: 11-24-2006, 10:01 AM
  3. need help for stack problem
    By bahareh in forum C++ Programming
    Replies: 15
    Last Post: 11-19-2006, 04:29 PM
  4. stack problem in c++
    By galmca in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2005, 12:49 PM
  5. stack problem
    By anu in forum C Programming
    Replies: 12
    Last Post: 10-15-2001, 03:38 PM