Thread: stack program; revisited

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    stack program; revisited

    To me, it is not obvious how to set up the POP and PUSH functions.

    Now I changed some stuff around in my program and I get a new error I aint ever seen called a BUS error. What the heck is that?

    The menu of choices comes up. I choose 1 to add a number to my list; then it cycles again and asks for another choice then I choose 1 again to add a number; and then I get bumped with the BUS error.

    Please explain my problems with my key functions POP and PUSH and what am I missing/ doing wrong???



    Code:
           
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct node {
       int data;                      /*  1st item of node */
       struct node *next;              /*  2nd item of node */
    };
    
    struct node *first = NULL;        /*  global variable  */
    struct node *new_node;            /*  need pointers for
                                           
                                                              dynamic mem alloc  */
    
    void push(int insert_value);
    void pop(int take_away_value);
    void print_list();
    void choice_list(void);
    
    
    void push(int insert_value)
    {
       new_node=malloc(sizeof(struct node));
       new_node->data = insert_value;
       new_node->next = first;
       first = new_node;
    }
    
    void pop(int take_away_value)
    {
       if (take_away_value == new_node->data)  {
          first = new_node->next;
          free(new_node);
       }
    
       else
    
          printf("Your list is now empty.");
    }
    
    void print_list()
    {
      if (new_node == NULL)
         printf("Your list is empty.\n\n");
    
      else   {
         printf("Your list is: \n");
         while (new_node != NULL)   {
            printf("%d -> ",new_node->data);
            new_node = new_node->next;
         }
    
         printf("NULL\n\n");
      }
    }
    
    void choice_list(void)
    {
      printf("Choose one of the following.\n"
             "0   To end program\n"
             "1   To add a value to list\n"
             "2   To remove a value from list\n");
    }
    
    
    main()
    {
       int number,choice;
    
       for( ; ; )   {
         choice_list();
         printf("Enter your choice :");
         scanf("%d",&choice);
         printf("\n");
    
         switch(choice)   {
          case 0:
            printf("You have ended the program"
                   "Good-bye !!");
            break;
    
          case 1:
            printf("Enter a number integer : ");
            scanf("%d",&number);
            push(number);
            print_list();
            break;
    
          case 2:
            printf("Enter number to remove from your list :");
            scanf("%d",&number);
            pop(number);
            print_list();
            break;
    
          default:
            printf("That is an invalid choice\n\n");
            break;
    
        }
    
        printf(" ?");
        scanf("%d",choice);
      }
      printf("End of run.\n");
      return 0;
    }
    Sue B.

    dazed and confused


  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pop is incorrect. See my other reply.

    Basicly, pop takes the value (data) stored in the top node and sets it aside to return it later. Then you set the top to be that node's "next" entry, and then free that node. Then you return the value you set aside earlier.

    Quzah.

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Ok, got the POP function issue.

    But why am i having problems with the switch statement?
    I choose choice 1 - goes to PUSH function
    lists out my number entered
    cycles back to switch statement;
    I choose choice 1 again, then get a BUS port or BUS error

    I dont' know what error that is to know what to fix.

    Can you explain.
    Sue B.

    dazed and confused


  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
        printf(" ?");
        scanf("%d",choice);
    I'm pretty sure your crashes have to do with that scanf statement. Of curiosity, your compiler did send you a warning for this line, right?

  5. #5
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    changing POP function to remove nodes

    I removed the scanf line QuestionC mentioned.
    Crashes stopped.

    I changed the Pop function to having no arguments.
    However it is still not working properly.

    Choosing 1 to add nodes works okay.
    Choosing 2 to delete nodes not working okay. and still in loop
    Choosing 0 to stop ; okay but leaves me in the loop; doesn't end program
    Choosing any other number defaults in switch; works okay.

    What is wrong here???


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct node {
       int data;                      /*  1st item of node */
       struct node *next;              /*  2nd item of node */
    };
    
    struct node *first = NULL;        /*  global variable  */
    struct node *new_node;            /*  need pointers for
                                          dynamic mem alloc  */
    
    void push(int insert_value);
    void pop();
    void print_list();
    void choice_list(void);
    
    
    void push(int insert_value)
    {
       new_node=malloc(sizeof(struct node));
       new_node->data = insert_value;
       new_node->next = first;
       first = new_node;
    }
    
    void pop()
    {
       if (first = new_node)  {
          new_node->next = first;
          free(new_node->next) ;
       }
    
       else
    
          printf("Your list is now empty.");
    }
    
    void print_list()
    {
      if (new_node == NULL)
         printf("Your list is empty.\n\n");
    
      else   {
         printf("Your list is: \n");
         while (new_node != NULL)   {
            printf("%d -> ",new_node->data);
            new_node = new_node->next;
         }
    
         printf("NULL\n\n");
      }
    }
    
    void choice_list(void)
    {
      printf("Choose one of the following.\n"
             "0   To end program\n"
             "1   To add a value to list\n"
             "2   To remove a value from list\n");
    }
    
    main()
    {
       int number,choice;
    
       for( ; ; )   {
         choice_list();
         printf("Enter your choice :");
         scanf("%d",&choice);
         printf("\n");
    
         switch(choice)   {
          case 0:
            printf("You have ended the program"
                   "Good-bye !!");
            break;
    
          case 1:
            printf("Enter a number integer : \n");
            scanf("%d",&number);
            push(number);
            print_list();
            break;
    
          case 2:
            printf("Enter number to remove from your list :");
            scanf("%d",&number);
            pop(number);
            print_list();
            break;
    
          default:
            printf("That is an invalid choice\n\n");
            break;
    
        }
    
      }
      printf("End of run.\n");
      return 0;
    }
    Sue B.

    dazed and confused


  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Your pop function is still ****ed up. Why don't you take Quzah's suggestion? Your infinite loop must contain a condition that if met will break the loop.

    Run this little program to understand:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	bool x = true;
    	int i=0;
    	while(x)
    	{
    		printf("hello\n");
    		i++;
    		if (i == 4) x = false;
    	}
    	return 0;
    }

  7. #7
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok changed POP function to use Quzah's idea;

    the program is not removing the node.

    check out code and output I ran


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct node {
       int data;                      /*  1st item of node */
       struct node *next;              /*  2nd item of node */
    };
    
    struct node *first = NULL;        /*  global variable  */
    struct node *new_node;            /*  need pointers for
                                          dynamic mem alloc  */
    
    void push(int insert_value);
    int pop();
    void print_list();
    void choice_list(void);
    
    
    void push(int insert_value)
    {
       new_node=malloc(sizeof(struct node));
       new_node->data = insert_value;
       new_node->next = first;
       first = new_node;
    }
    
    int pop()
    {
       struct node *Node;
       int remove_this;
    
       if (new_node == NULL)
          return -1;            /* error ; at NULL   */
    
       Node = new_node;
       new_node = new_node->next;
       remove_this = Node->data;
       free(Node);
    
       return remove_this;
    }
    
    void print_list()
    {

    OUTPUT::::


    "stack.c" 110 lines, 2065 characters
    $ gcc stack.c
    $ a.out



    Choose one of the following.
    0 To end program
    1 To add a value to list
    2 To remove a value from list
    Enter your choice :1

    Enter a number integer :
    12
    Your list is:
    12 -> NULL

    Choose one of the following.
    0 To end program
    1 To add a value to list
    2 To remove a value from list
    Enter your choice :1

    Enter a number integer :
    14
    Your list is:
    14 -> 12 -> NULL

    Choose one of the following.
    0 To end program
    1 To add a value to list
    2 To remove a value from list
    Enter your choice :1

    Enter a number integer :
    16
    Your list is:
    16 -> 14 -> 12 -> NULL

    Choose one of the following.
    0 To end program
    1 To add a value to list
    2 To remove a value from list
    Enter your choice :2

    Enter number to remove from your list :12
    Your list is empty.

    Choose one of the following.
    0 To end program
    1 To add a value to list
    2 To remove a value from list
    Enter your choice :1

    Enter a number integer :
    10
    Your list is:
    10 -> 16 -> 14 -> 12 -> NULL

    Choose one of the following.
    0 To end program
    1 To add a value to list
    2 To remove a value from list
    Enter your choice :
    Sue B.

    dazed and confused


  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Enter a number to remove from your list?
    What nonsense is that? Why is it even there? Do you know what a stack is?

  9. #9
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Excuse me.

    I guess I don't know what a stack is.

    "The list" I refer to in my output as I understand it is this:

    first node points to NULL
    then enter a number; this is the data that now is part of first NODE, which is still pointing to NULL because there is no other Node to point yet.

    then cycling through to add a Node on top of /next to this Node, enter another number;

    then cycling through to add a Node on top of /next to this Node,
    until there is a layer or a chain of Nodes.

    to remove one; anyone you enter which one to remove; could be on top, could be in middle.
    then the list is squeezed together so to speak and still in the other it was before the removal.

    That is how I understand stacks.

    Troll King:
    I suppose you think your comment is helpful for a beginner at C???
    Sue B.

    dazed and confused


  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Stacks always remove the top node. Nothing else. That is why the function is called pop. Pop returns and integer, so you should capture it: int num = Pop();

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Reread my example of a stack of paper. You always take off the top item in a "stack". A "queue" is like a stack, but you always take off of the bottom of the pile. A "list" is like either one, except you remove from any point using a search function.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finished Stack
    By the pooper in forum C Programming
    Replies: 11
    Last Post: 02-02-2005, 10:52 AM
  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. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM