Thread: homework assignment problem

  1. #1
    Unregistered
    Guest

    Unhappy homework assignment problem

    OK, I'm glad you guys are there. Here's my problem, I have been working with Sue Ballew in St. Louis. I have this stack program that is supposed to do a bunch of stuff, push, pop, count, print list, and "delete". I can't get the delete function to work, I took the code right from my King textbook, p 378 but it doesn't work. I've tried different forms for the function, e.g.,

    void delete( int input_value)
    {
    struct node *cur, *prev;

    for (cur = first, prev = NULL;
    cur != NULL && cur->value != input_value;
    prev = cur, cur = cur->next)
    ;

    if (cur == NULL)
    printf("value not found\n");
    return;
    if (prev == NULL)
    first = first->next;
    else
    prev->next = cur->next;
    free (cur);
    return;
    }
    but that didn't work either.

    I am including the whole program here, if any one can figure out what I am sure is some dumb mistake on my part, I will be very grateful. I am 56 and much too old for this ****.

    #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);
    void pop();
    /* struct node *delete(struct node *first, int input_value);
    */
    void delete(struct node *list, int input_value);
    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();
    printlist();
    break;
    case 3:
    printf("Enter value to be removed: ");
    scanf("\n%d", &number);
    delete(first, number);
    printlist();
    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;
    }

    void pop()
    {
    int lv;
    if (first == NULL)
    return;
    lv = first->value;
    printf(" %d returned\n", lv);
    first = first->next;
    return;
    }

    void delete(struct node *list,int input_value)
    {
    struct node *cur, *prev, *q;
    for (cur = list, prev = NULL;
    cur != NULL && cur->value != input_value;
    prev = cur, cur = cur->next)
    ;

    if (cur == NULL)
    printf("value not found\n");
    return;
    if (prev == NULL) {
    list = list->next;

    printf(" First value removed"); }
    else
    prev->next = cur->next;
    free (cur);

    return;
    }

    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"
    " 3 to delete a value from the list.\n");
    }

    void printlist()
    {
    struct node *p;
    if (first == NULL)
    printf("List is empty.\n\n");
    else {
    printf("The list is:\n");
    p = first;
    while (p != NULL) {

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


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

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

    homework problem difficulty

    OK, I'm glad you guys are there. Here's my problem, I have been working with Sue Ballew in St. Louis. I have this stack program that is supposed to do a bunch of stuff, push, pop, count, print list, and "delete". I can't get the delete function to work, I took the code right from my King textbook, p 378 but it doesn't work. I've tried different forms for the function, e.g.,

    void delete( int input_value)
    {
    struct node *cur, *prev;

    for (cur = first, prev = NULL;
    cur != NULL && cur->value != input_value;
    prev = cur, cur = cur->next)
    ;

    if (cur == NULL)
    printf("value not found\n");
    return;
    if (prev == NULL)
    first = first->next;
    else
    prev->next = cur->next;
    free (cur);
    return;
    }
    but that didn't work either.

    I am including the whole program here, if any one can figure out what I am sur#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);
    void pop();
    /* struct node *delete(struct node *first, int input_value);
    */
    void delete(struct node *list, int input_value);
    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();
    printlist();
    break;
    case 3:
    printf("Enter value to be removed: ");
    scanf("\n%d", &number);
    delete(first, number);
    printlist();
    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;
    }

    void pop()
    {
    int lv;
    if (first == NULL)
    return;
    lv = first->value;
    printf(" %d returned\n", lv);
    first = first->next;
    return;
    }

    void delete(struct node *list,int input_value)
    {
    struct node *cur, *prev, *q;
    for (cur = list, prev = NULL;
    cur != NULL && cur->value != input_value;
    prev = cur, cur = cur->next)
    ;

    if (cur == NULL)
    printf("value not found\n");
    return;
    if (prev == NULL) {
    list = list->next;

    printf(" First value removed"); }
    else
    prev->next = cur->next;
    free (cur);

    return;
    }

    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"
    " 3 to delete a value from the list.\n");
    }

    void printlist()
    {
    struct node *p;
    if (first == NULL)
    printf("List is empty.\n\n");
    else {
    printf("The list is:\n");
    p = first;
    while (p != NULL) {

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


    printf("NULL\n\n");
    }
    }e is some dumb mistake on my part, I will be very grateful. I am 56 and much too old for this ****.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    void delete( int input_value)
    {
    ...

    if (cur == NULL)
    printf("value not found\n");
    return;
    if (prev == NULL)
    first = first->next;
    else
    prev->next = cur->next;
    free (cur);
    return;
    }

    Just looking that this I can see that it will always display: value not found or else nothing, it will return instead. You need to use braces { ... } so that you form code blocks. Without code blocks conditions con only execute one line of code following the test.

    Overall this code is semi okay. You probably have a poor textbook. It's not totally wrong, but it's not too good either.

  4. #4
    Unregistered
    Guest
    Thanks so much for your reply, but there was only one area where I could have added a block after the else statement at the end, I tried it but it didn't help. I have tried everything I could think of and it just doesn't work. As I said most of the code comes right from the text in a delete a node discussion. They make a big deal of the liberal use of comma operators and the empty body of the for statement.

    void delete(struct node *list,int input_value)
    {
    struct node *cur, *prev, *q;
    for (cur = list, prev = NULL;
    cur != NULL && cur->value != input_value;
    prev = cur, cur = cur->next)
    ;

    if (cur == NULL)
    printf("value not found\n");
    return;
    if (prev == NULL) {
    list = list->next;

    printf(" First value removed"); }
    else
    prev->next = cur->next;
    free (cur);

    return;
    }

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You have not used code blocks yet. See there is a difference between these statements:

    Code:
    if ( 10 > 9)  x = 4; //one statement executes if true
    
    if ( 10 > 9) 
    {
        x = 4;    //two or more statements execute
        y = 10;  //if the condition is true
    }
    If you can understand this than you will see your error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. A homework problem about C++ (Pointer)
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2005, 04:28 PM
  5. problem on assignment
    By rodney1001 in forum C Programming
    Replies: 1
    Last Post: 10-29-2001, 03:49 PM