943,931 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9822
  • C++ RSS
Dec 6th, 2007
0

Deleting any node from a singly linked list

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
DemonSpeeding is offline Offline
31 posts
since Oct 2007
Dec 6th, 2007
0

Re: Deleting any node from a singly linked list

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: 'Grocery Counter' using Classes
Next Thread in C++ Forum Timeline: How to represent a space and a tab?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC