deleting an integer from a list

This is a discussion on deleting an integer from a list within the C Programming forums, part of the General Programming Boards category; Good morning, here's my problem. I have an assigment to create a list and do things to it like push, ...

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

    Question deleting an integer from a list

    Good morning, here's my problem. I have an assigment to create a list and do things to it like push, pop, count, printlist and delete item from list. Everything works well but I can't get the delete function to work. The code for it comes almost directly from my text but it doesn't work. When I run it and print the list afterwards, the list has not changed. However, if I put an incorrect value in the function, I do get the proper response from the function. Its the correct values that are the problem. I've tried everything. Originally I was using a void function with 1 argument, the input value. Now I have set it up with 2 arguments, the input value and the list, because this was more like the code in the text. I'm new to C and in every and all cases so far, I've always found that the problem was something very simple that I overlooked.

    Here's the function:

    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;
    }

    Its kind of long b#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");
    }
    }
    ut here is the complete program:

  2. #2
    Unregistered
    Guest
    /************************************/
    for (cur = list, prev = NULL;
    cur != NULL && cur->value != input_value;
    prev = cur, cur = cur->next)
    ;
    /************************************/
    These piece of code does NOTHING because of the
    last ";".
    May be the problem....

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    That last bit of code is fine.. it just traverses tot he end of the list. This part right after is incorrect however...

    if (cur == NULL)
    printf("value not found\n");
    return;


    You need to use brackets. As it is, the return statement is executing whether or nor the condition of the if is fulfulled, and I'm pretty sure you don't want to escape the function at this point unless cur == NULL.



    Callou collei, we'll code the way
    Of Prime Numbers and Pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 07:02 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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