need help with assignment due tonite

This is a discussion on need help with assignment due tonite within the C Programming forums, part of the General Programming Boards category; this assignment is due tonite, i have a small glitch here's the code: #include <stdio.h> #include <stdlib.h> struct node { ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    need help with assignment due tonite

    this assignment is due tonite, i have a small glitch
    here's the code:

    #include <stdio.h>
    #include <stdlib.h>

    struct node {
    int value;
    struct node *next;
    };

    struct node *first = NULL;
    struct node *new_node;
    void push( int input_value);
    int pop();
    void instructions(void);
    void printlist();
    main()
    {
    int input, value, number, lv;

    instructions();
    printf("?");
    scanf("%d", &input);
    while (input != 0) {
    switch (input) {
    case 1:
    printf("Enter an integer: ");
    scanf("\n%d", &number);
    push(number);
    printlist();
    break;
    case 2:
    pop();
    break;
    default:
    printf("Invalid choice.\n\n");
    break;
    }
    printf("?");
    scanf("%d", &input);
    }
    printf("End of run.\n");
    return 0;
    }

    void push( int input_value)
    {
    new_node = malloc(sizeof(struct node));
    new_node->value = input_value;
    new_node->next = first;
    first = new_node;
    }

    int pop()
    {
    struct node *n;
    int lv;
    if (lv == NULL)
    printf("List is empty");
    else
    n = first; /* or n = first */
    first = first->next; /* or first = " " */
    lv = n->value;
    printf(" %d returned\n", lv);
    free( n );
    }

    void instructions (void)
    {
    printf("Enter your choice:\n"
    " 0 to end program.\n"
    " 1 to add a value to the list.\n"
    " 2 to remove a value from the list.\n");
    }

    void printlist()
    {
    if (free == NULL)
    printf("List is empty.\n\n");
    else {
    printf("The list is:\n");

    while (new_node != NULL) {

    printf("%d --> ", new_node->value);
    new_node = new_node->next;
    }


    printf("NULL\n\n");
    }
    }


    here's the glitch, I want to print "list is empty" after everything is popped off.

    admiral% gcc stack2.c
    admiral% a.out
    Enter your choice:
    0 to end program.
    1 to add a value to the list.
    2 to remove a value from the list.
    ?1
    Enter an integer: 4
    The list is:
    4 --> NULL

    ?1
    Enter an integer: 5
    The list is:
    5 --> 4 --> NULL

    ?2
    5 returned
    ?2
    4 returned
    ?2
    Segmentation Fault (core dumped)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > ?2
    > 5 returned
    > ?2
    > 4 returned
    > ?2
    > Segmentation Fault (core dumped)

    Well this is an absolute no brainer. You're trying to free an empty list. Add a check to fix this.

    Quzah.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    First, please use code tags!

    Second, your fault (rather obviously) is in your Pop(). Look:

    Code:
    int pop()
    {
    	struct node *n;
    	int lv;
    	if (lv == NULL)
    		printf("List is empty");
    	else
    		n = first; /* or n = first */
    	first = first->next; /* or first = " " */
    	lv = n->value;
    	printf(" %d returned\n", lv);
    	free( n );
    }
    Look at the third line. You're comparing an uninitialized integer to NULL. I think you really mean if(first == NULL) which is your "empty" condition, no?

    You also might want brackets around that else, because you want to do more than one thing only if n is valid! An alternative is brackets after the if, and then returning from there.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    9
    Quzah, I know you because Sue and I have worked to gether a lot.

    What kind of check are your referring to, I was trying to use if lv == NULL but it didn't work

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    9
    Thanks so much for all ypur help, i got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. please help..linked list homework assignment due tomorrow..
    By rocketman03 in forum C Programming
    Replies: 2
    Last Post: 11-23-2008, 05:32 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. Need help with a C assignment due on 9/23 (Friday)
    By JohnnyBoy268 in forum C Programming
    Replies: 6
    Last Post: 09-23-2005, 12:59 AM

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