Deleting any node from a singly linked list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 31
Reputation: DemonSpeeding is an unknown quantity at this point 
Solved Threads: 0
DemonSpeeding DemonSpeeding is offline Offline
Light Poster

Deleting any node from a singly linked list

 
0
  #1
Dec 6th, 2007
I believe what I have written at the moment is a singly linked list, and I can't figure out how to delete whatever node the user specifies, I've toyed around with it but I'm stumped.

Here's my specification file:
  1. #include<string>
  2. using namespace std;
  3. class Node
  4. {
  5. Node *prev;
  6. Node *next;
  7. string data;
  8. public:
  9. //Function Declarations
  10. void print(Node *&);
  11. void insertLast(Node *&);
  12. void delAny(Node *&);
  13. };

And here's the implementation file:
  1. #include <iostream>
  2. #include "linkedlist.h"
  3. #include <string>
  4. using namespace std;
  5.  
  6. void Node::print(Node *&temp)
  7. {
  8. if(temp==NULL) {
  9. cout << "List is Empty " << endl;
  10. }
  11. else {
  12. Node *tmp=temp;
  13. while(tmp!=NULL)
  14. {
  15. cout<<tmp->data<<endl;
  16. tmp=tmp->next;
  17. }
  18. }
  19. }
  20.  
  21. void Node::insertLast(Node *&temp)
  22. {
  23. Node *newNode;
  24. newNode=new Node;
  25. if (temp==NULL)
  26. {
  27. cout<<"\nEnter Data :";
  28. cin>>newNode->data;
  29. newNode->next=temp;
  30. temp=newNode;
  31. }
  32. else
  33. {
  34. Node *curr;
  35. curr=temp;
  36. while(curr->next!=NULL)
  37. {
  38. curr=curr->next;
  39. }
  40. cout<<"\nEnter Data :";
  41. cin>>newNode->data;
  42. newNode->next=NULL;
  43. curr->next=newNode;
  44. }
  45. }
  46.  
  47. void Node::delAny(Node *&temp) {
  48. Node *newNode;
  49. newNode=new Node;
  50. if(temp==NULL) {
  51. cout << "List is Empty " << endl;
  52. }
  53. else
  54. {
  55. cout<<"\nEnter Data :";
  56. cin>>newNode->data;
  57. newNode->next=temp;
  58. temp=newNode;
  59.  
  60.  
  61.  
  62. }

I'm not sure what to do with the function to get it to delete whatever the user enters from the list.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,696
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 728
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Deleting any node from a singly linked list

 
0
  #2
Dec 6th, 2007
To delete any node from a single linked list you have the average case and one edge case. The average case is easy: loop through the list until the next node is the node you're trying to delete, and set the current node's link to the link of the node you want to delete:
  1. while ( iter->next != 0 && iter->next->data != key )
  2. iter = iter->next;
  3.  
  4. if ( iter->next != 0 )
  5. iter->next = iter->next->next;
This works for deleting every node except the first because there's no node that points to it. So you need to do reset the first node to point to its link:
  1. if ( head->data == key )
  2. head = head->next;
  3. else {
  4. //...
  5.  
  6. while ( iter->next != 0 && iter->next->data != key )
  7. iter = iter->next;
  8.  
  9. if ( iter->next != 0 )
  10. iter->next = iter->next->next;
  11. }
It really helps to draw this logic out on paper. What I like to do is simulate the logic with a handful of change.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC