| | |
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++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






. small efforts required.