| | |
Deleting any node from a singly linked list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Solved Threads: 0
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:
And here's the implementation file:
I'm not sure what to do with the function to get it to delete whatever the user enters from the list.
Here's my specification file:
C++ Syntax (Toggle Plain Text)
#include<string> using namespace std; class Node { Node *prev; Node *next; string data; public: //Function Declarations void print(Node *&); void insertLast(Node *&); void delAny(Node *&); };
And here's the implementation file:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include "linkedlist.h" #include <string> using namespace std; void Node::print(Node *&temp) { if(temp==NULL) { cout << "List is Empty " << endl; } else { Node *tmp=temp; while(tmp!=NULL) { cout<<tmp->data<<endl; tmp=tmp->next; } } } void Node::insertLast(Node *&temp) { Node *newNode; newNode=new Node; if (temp==NULL) { cout<<"\nEnter Data :"; cin>>newNode->data; newNode->next=temp; temp=newNode; } else { Node *curr; curr=temp; while(curr->next!=NULL) { curr=curr->next; } cout<<"\nEnter Data :"; cin>>newNode->data; newNode->next=NULL; curr->next=newNode; } } void Node::delAny(Node *&temp) { Node *newNode; newNode=new Node; if(temp==NULL) { cout << "List is Empty " << endl; } else { cout<<"\nEnter Data :"; cin>>newNode->data; newNode->next=temp; temp=newNode; }
I'm not sure what to do with the function to get it to delete whatever the user enters from the 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:
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:
It really helps to draw this logic out on paper. What I like to do is simulate the logic with a handful of change.
C++ Syntax (Toggle Plain Text)
while ( iter->next != 0 && iter->next->data != key ) iter = iter->next; if ( iter->next != 0 ) iter->next = iter->next->next;
C++ Syntax (Toggle Plain Text)
if ( head->data == key ) head = head->next; else { //... while ( iter->next != 0 && iter->next->data != key ) iter = iter->next; if ( iter->next != 0 ) iter->next = iter->next->next; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Deleting Pointed node in Singly Linked List (C)
- Removing an item from head of linked list (C)
- Singly-Linked Lists: Ultimate (C++)
Other Threads in the C++ Forum
- Previous Thread: 'Grocery Counter' using Classes
- Next Thread: How to represent a space and a tab?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






