Thread: Having problem deleting node in middle of stack

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

    Having problem deleting node in middle of stack

    all right. got my program working
    EXCEPT for when you want to delete a specific node (value in list)

    I can enter different values (choice 1 - case 1 of switch)
    I can delete the top one (Pop function)
    I can print the list
    I can have the count of the # of nodes list
    ALL this is fine.

    But if I delete a node in the middle of the list
    and reprint the list
    I get the original list BEFORE the deletion.

    How come???
    Sue B.

    dazed and confused


  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    In case 5 you forgot to call function DeleteNode...

    You should also check the if(number<0) that follows...
    Whats the point in this?? You don't want negative numbers to be deleted?!??

    case 5:
    printf("What value do you want to delete from"
    " your list : \n");
    scanf("%d",&number);
    deleteNode(number);
    if (number<0) {
    printf("Error deleting node; %d not found"
    "in list\n",number);
    }
    else {
    printf("Node containing %d deleted successfully\n",
    number);
    }
    break;

    ...One last thing In function deletenode
    ...
    if (prev == NULL)
    first = first->next; /* value is in the first node */


    With this way you don't deallocote memory first points to.
    You should use:

    if (prev == NULL){
    prev =first;
    first = first->next; /* value is in the first node */
    free(prev);
    }

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    I got my case 5 to call function (i copied and pasted from another issue of same program and failed to fix that part earlier - dummie me)

    and the code for function deleteNode is straight from my textbook, and it works just fine, thanks.

    here's my switch statement with all cases.
    Code:
         switch(choice)   {
          case 0:
            printf("You have ended the program.\n"
                   "Good-bye !!\n\n");
            exit(0);
    
          case 1:
            printf("Enter a number integer : ");
            scanf("%d",&number);
            push(number);
            break;
    
          case 2:
            pop();
            break;
    
          case 3:
            count_nodes();
            break;
    
          case 4:
            print_list();
    
           case 5:
            printf("What value do you want to delete from"
                   " your list : \n");
            scanf("%d",&number);
            deleteNode(number);
            printf("Node containing %d deleted successfully\n",
                       number);
            break;
     
          default:
            printf("That is an invalid choice\n\n");
            break;
     
        }
    What I wanted to check for is if value number wasn't in the list of values [data] in each node but I don't know how to compare that and to what value to compare it to. Seems like a loop goes in there somewhere. Or how does the stack work in keeping track of the list of values. first->data is the storage area for the value in the node....right???

    Any ideas???
    Sue B.

    dazed and confused


  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    First, I'll say that your code really does look kosher to me.. I mean, as long as your main function is using the code that you posted, and not the code from that txt file.

    Now, for checking whether number is a value in the list, the deleteNode function does that automatically.
    Code:
     for (cur = first, prev = NULL;
         cur != NULL && cur->data != value;
         prev = cur, cur = cur->next)
       ;
    
      if (cur == NULL)  {
        printf("That value wasn't found\n");
        return ;             /*  value was not found */
      }
    
      if (prev == NULL)
        first = first->next;   /*  value is in the first node */
    
      else
        prev->next = cur->next; /* value is in some other node */
    
      free(cur);
      return ;
    There are only two conditions on which the for loop will end, if curr points at the node, the data of which is the value you are seeking, or if curr points to the end of the list, NULL. If it points at NULL, then obviously the value is not in the list (since it's checked all the nodes starting at first), so the value must not be in the list.

    If you wanted to write a function to test whether or not a certain value was in the list, it would look something like this...
    Code:
    for(cur = first; cur != NULL && cur->data != value; curr = curr -> next);
    printf ((curr == NULL)?("Value not found.\n"):("Value found.\n"));
    Finally, I'm not sure if your instructor pointed this out, but removing a node from the middle of a stack is kinda like watching Saturday morning cartoons on a radio. A stack is defined by the fact that you can only push things on it, and pop things of it. Not a criticism of your question, just making sure that it's understood what a stack really is.

    Although, it is technically possible to remove a value from the middle of a stack if you have another stack to work with, but that's hardly efficient.
    Last edited by QuestionC; 10-29-2001 at 11:08 AM.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  2. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Problem deleting a node from a linked list
    By Dag_ in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 09:53 AM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM