| | |
deleting a random node from a singly linked list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 0
Hello,
I'm sorry if this has been asked before, but I didn't seem to find any answers. I'm trying to write a function that would delete a specified number from a singly linked list.
This is what i've got so far, but it's not working:
I'm sorry if this has been asked before, but I didn't seem to find any answers. I'm trying to write a function that would delete a specified number from a singly linked list.
This is what i've got so far, but it's not working:
c++ Syntax (Toggle Plain Text)
void remove(list*& start, int s) { list* pom=NULL; list* pom2=NULL; while(pom!=NULL) { pom=start; if(pom->next->value==s) { pom2=pom->next; pom->next=pom->next->next; delete pom2; } start=start->next; } }
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
Deleting a node is not difficult.
let me tell you the algorithm.
Algorithm.
Delete (value)
1. Check if head is null return
2. if the value matches head
set temp = head
set head = head->next
free head.
3. Link previous = head;
4. Link temp = head->next;
5. while (temp->data != value)
do
temp = temp->next
previous = previous->next
6. if temp is not null
set previous->next = temp->next
free temp.
Hope this helps
let me tell you the algorithm.
Algorithm.
Delete (value)
1. Check if head is null return
2. if the value matches head
set temp = head
set head = head->next
free head.
3. Link previous = head;
4. Link temp = head->next;
5. while (temp->data != value)
do
temp = temp->next
previous = previous->next
6. if temp is not null
set previous->next = temp->next
free temp.
Hope this helps
![]() |
Other Threads in the C++ Forum
- Previous Thread: total array
- Next Thread: class
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






. small efforts required.