doubly sorted linked list problem

This is a discussion on doubly sorted linked list problem within the C++ Programming forums, part of the General Programming Boards category; I got a problem to delete the last node, what might be the problem? Code: typedef struct node { char ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    25

    doubly sorted linked list problem

    I got a problem to delete the last node, what might be the problem?
    Code:
    typedef struct node
    {
      char data;
      int count;
      node *next;
      node *pre;
    } NODE;

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    In C++, you don't need the extra typedef and NODE:
    Code:
    struct node
    {
      char data;
      int count;
      node *next;
      node *pre;
    };
    Your problem is probably in the code that tries to do the delete that you haven't posted. If you haven't written it yet, just start from the beginning node and move on to the next node until the next node is null, then you have found the last node.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    Here's the remove function, each node can be deleted without a problem except the last one.
    Code:
          void delete(char the_node)
          {
            NODE *target;
              if((target=find(the_node))!=NULL)
              {
                if(target->next->count>1)
                  target->next->count--;
                else
                {
                  NODE* temp;
                  temp=target->next;
                  target->next=temp->next;
                  if (target->next->next != NULL) target->next->next->pre=target;
                  if(temp==head)
                  {
                    head=temp->next;
                    temp->next->pre = NULL;
                  }
                  delete(temp);
                }
                howMany--;
              }
          }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> if(target->next->count>1)

    If the last one is the one you are going to delete, won't target->next be null? In both your if and your else, you seem to assume that target->next is valid. Try checking for null and handle that case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. doubly linked list problem
    By YevGenius in forum C Programming
    Replies: 4
    Last Post: 05-02-2004, 08:54 PM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  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