Thread: Epic System crash (quite funny)

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Epic System crash (quite funny)

    I'm making a delete function for a linked list, but it is not working correctly, it is causing massive problems when it reaches if statements. Can someone point me in the right direction please. (front is a global pointer)

    Code:
    void delEntry()
    {
            object *temp1=front;
            object *temp2=front;
            int delDate;
            int delTime;
            int i=1;
    
            cout<<"date: ";
            cin>>delDate;
            cout<<"time: ";
            cin>>delTime;
    
            while(i!=0)
            {
                    if(temp1==0)
                    {
                            cout<<"List Empty"<<"\n";
                            i=0;
                    }
    
                    else if(delDate==front->date)
                    {
                            if(front->time==delTime)
                            {
                                    delete front;
                                    front=0;
                            }
    
                            else
                            {
                                    temp1=temp1->next;
                                    temp2=temp1->next->next;
                            }
    
                    }
    
                    else if(delDate==temp2->date)
                    {
                          if(temp2->time==delTime)
                          {
                                temp1->next=temp2->next;
                                delete temp2;
                                temp1=temp2=0;
                                i=0;
                          }
    
                          else
                          {
                                temp1=temp1->next;
                                temp2=temp1->next->next;
                          }
                    }
    
                    else
                    {
                            temp1=temp1->next;
                            temp2=temp1->next->next;
                    }
            }
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    temp2=temp1->next->next;
    if temp1->next is null this will crash

    PS. instead of i=0 you can just use break; statement
    Last edited by vart; 11-16-2006 at 03:18 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Further you have quite a few cases of bad logic. If front is the node you want to delete, your code obliterates the entire linked list.

    Second, you update temp1, and then do temp2 = temp1->next->next. This means temp2 is always 2 steps ahead of temp1, not 1 like I believe you think it is.

    Third, don't name variables temp unless they are holding a value for a very short time; see how I used (and didn't use) temp. When I declared current and prev, without even looking at the code where they are used, it should be immediately obvious what the function of each variable is -- current will hold the node we are currently examining, and prev will hold the node just previous to the current one. In fact in general I wouldn't even use temp for the special case (I'd call it newFront) but I did to illustrate a situation in which it's not too bad to use a poorly descriptive name.

    Here's a simpler kind of code:

    Code:
    void delEntry()
    {
            // Check the empty case
            if (front == NULL) return;
    
            cout<<"date: ";
            cin>>delDate;
            cout<<"time: ";
            cin>>delTime;
    
            // Check the special case where we delete from the front of the list
            if ( front->equals(delDate, delTime) ) {
                    object * temp = front->next;
                    delete front;
                    front = temp;
                    return;
            }
    
            object * prev = front;
            object * current = front->next;
    
            while(current != NULL){
                    if (current->equals(delDate, delTime)){
                            prev->next = current->next;
                            delete current;
                            return;
                    }
                    prev = current;
                    current = current->next;
            }
    }
    Last edited by Cat; 11-16-2006 at 03:33 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  2. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  3. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  4. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM
  5. Problem Reporting System. Need Advide!
    By brunomiranda in forum Tech Board
    Replies: 9
    Last Post: 09-25-2003, 09:21 PM