Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

deleting nodes from a linked list structure

Posted on 2000-03-01
3
201 Views
Last Modified: 2012-05-04
My problem deals with deletions of nodes from a linked list structure. the nodes in the linked list are defined by the type definitions given as:
    typedef struct node
      {
       int value;             /*value of node*/
       struct node next;      /*points to next node in list*/
       struct node previous;  /*points to the previous node in list*/
      }NodeRec;

    typedef Noderec *node_ptr;

the forward pointers (next) for the nodes follow the node values in sorted order from smallest to largest. the backward pointer (previous) for each node points to its predecessor as determined by the sorted ordering.
a linked listed created  with this structure has both a head pointer which points to the first node (smallest valued node) in the list and a tail pointer pointing to the last node (largest valued node) in the list. I need to write a function that will remove a node from the linked list. the function will modify list pointers as necessary so as to leave the remainder of the list intact. a call to the function will be of the form:
            found = remove(item,head,tail);
where head points to the first node in the list, tail points to the last node in the list and item is the integer value of the node to be deleted from the list. head and tail are both of type node_ptr. the variable found is of type boolean. remove returns TRUE if the value to be deleted has been found in the list. remove returns FALSE if the value to be deleted could not be found in the list. make sure that the function considers three cases. one case will be the deletion of the head node of the list. a second case will be the deletion of the tail node of the list. a third case will be the deletion of a node somewhere in the middle of the list. if multiple occurences of a given value to be deleted occur in the list, delete only the node containing the first occurence of the value.........thanks in advance!
0
Comment
Question by:beachbumm
  • 3
3 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2573184
bool remove(item,head,tail)
{  node_ptr temp=head;
    while(temp){
        if(temp->value==item){
        if(temp==head){
        temp->next->previous=NULL;
        head=temp->next;
        return TRUE;
         }
        if(temp==tail){
        temp->previous->next=NULL;
        tail=temp->previous;
        return TRUE;
        }
        temp->previous->next=temp->next;
        temp->next->previous=temp->previous;
return TRUE;
}//if
        temp=temp->next;
}//while

     return FALSE;

}

Regards
W.Yinan

       
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2573186
bool remove(item,head,tail)
{  node_ptr temp=head;
    while(temp){
        if(temp->value==item){
        if(temp==head){
        temp->next->previous=NULL;
        head=temp->next;
        return TRUE;
         }
        if(temp==tail){
        temp->previous->next=NULL;
        tail=temp->previous;
        return TRUE;
        }
        temp->previous->next=temp->next;
        temp->next->previous=temp->previous;
return TRUE;
}//if
        temp=temp->next;
}//while

     return FALSE;

}

Regards
W.Yinan

       
0
 
LVL 5

Accepted Solution

by:
Wyn earned 50 total points
ID: 2573187
hmmm...
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Having a file "symlink" to a memory location (Windows) 6 180
SQL Statment to match two tables in c# 6 80
valid enum? 6 75
Digital Output does not have continuity to ground? 3 100
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question