| | |
linked list question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
Im having real trouble seeing this, how do I delete something out of a linked list if it is already there? My current delete function just deletes everything out of the list.
the text file contains numbers like this:
75
85
95
25
35
75
85
95
25
the text file contains numbers like this:
75
85
95
25
35
75
85
95
25
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; class List { public: void Insert(int); void Print(); // void Delete(int); int Length(int); }; struct node { int item; node *nxt; }; node *start_ptr = 0; node *current; void List::Delete(int item) { if(start_ptr==NULL) cout<<"NULL"; node* temp=start_ptr; if(temp->item==item) { start_ptr=temp->nxt; delete temp; } } void List::Insert(int item) { node *temp, *temp2; // Temporary pointers // Reserve space for new node and fill it with data temp = new node; temp->item=item; temp->nxt = NULL; // Set up link to this node if (start_ptr == NULL) { start_ptr = temp; current = start_ptr; } else { temp2 = start_ptr; while (temp2->nxt != NULL) { temp2 = temp2->nxt; // Move to next link in chain } temp2->nxt = temp; } } void List::Print() { node *temp; temp = start_ptr; if (temp == NULL) cout << "The list is empty" << endl; else { while (temp != NULL) { // Display details for what temp points to cout << temp->item << " "; if (temp == current) cout << " <--"; cout << endl; temp = temp->nxt; } cout << "End of list" << endl; } } int List::Length(int item) { node *temp; temp=start_ptr; temp->item=item; int count=0; while(temp != 0) { temp=temp->nxt; count++; } return count; } void main() { List display; fstream inFile; node number; inFile.open("numbers.txt"); while(inFile>>number.item) { display.Insert(number.item); display.Delete(number.item); } display.Print(); //cout<<display.Length(number.item); }
Its done something like this: Note: not compiled or tested.
C++ Syntax (Toggle Plain Text)
node* temp=start_ptr; node* prev = NULL; while( temp ) { if( temp->item == item) { // delete this node if( prev == NULL) { // we are at the head of the linked list, for remove the head start_ptr = start_ptr->nxt; } else { // we're somewhere in the middle of the linked list prev->nxt = temp->nxt; } delete temp; break; // all done } prev = hold; hold = hold->next; }
Last edited by Ancient Dragon; Oct 5th, 2008 at 7:49 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; class List { public: void Insert(int); void Print(); // void Delete(int); int Length(int); }; struct node { int item; node *nxt; }; node *start_ptr = 0; node *current; void List::Delete(int item) { node* temp=start_ptr; node* prev = NULL; node* hold; while( temp ) { if( temp->item == item) { // delete this node if( prev == NULL) { // we are at the head of the linked list, for remove the head start_ptr = start_ptr->nxt; } else { // we're somewhere in the middle of the linked list prev->nxt = temp->nxt; } delete temp; break; // all done } prev = hold; hold = hold->nxt; } } void List::Insert(int item) { node *temp, *temp2; // Temporary pointers // Reserve space for new node and fill it with data temp = new node; temp->item=item; temp->nxt = NULL; // Set up link to this node if (start_ptr == NULL) { start_ptr = temp; current = start_ptr; } else { temp2 = start_ptr; while (temp2->nxt != NULL) { temp2 = temp2->nxt; // Move to next link in chain } temp2->nxt = temp; } } void List::Print() { node *temp; temp = start_ptr; if (temp == NULL) cout << "The list is empty" << endl; else { while (temp != NULL) { // Display details for what temp points to cout << temp->item << " "; if (temp == current) cout << " <--"; cout << endl; temp = temp->nxt; } cout << "End of list" << endl; } } int List::Length(int item) { node *temp; temp=start_ptr; temp->item=item; int count=0; while(temp != 0) { temp=temp->nxt; count++; } return count; } void main() { List display; fstream inFile; node number; inFile.open("numbers.txt"); while(inFile>>number.item) { display.Insert(number.item); display.Delete(number.item); } display.Print(); //cout<<display.Length(number.item); }
The problem is in main(), not that delete function. First it calls insert to insert a new number then turns right around and deletes it. Delete that line
C++ Syntax (Toggle Plain Text)
while(inFile>>number.item) { display.Insert(number.item); //display.Delete(number.item); <<<<< delete this line }
Last edited by Ancient Dragon; Oct 5th, 2008 at 9:07 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
But I want the delete function to delete any duplicates in the linked list. If I just delete that function then I still have the whole Linked list, duplicates included.
the text file contains numbers like this:
75
85
95
25
35
75
85
95
25
I want it to display these numbers only out of that list:
75
85
95
25
35
the text file contains numbers like this:
75
85
95
25
35
75
85
95
25
I want it to display these numbers only out of that list:
75
85
95
25
35
Check to see if the number is already in the list before adding it. The delete() function is not the place to do that. Write another class method that does nothing but search the list for a number, return true if the number already exists in the list or false if it doesn't. Then if it doesn't call that Insert() function.
Last edited by Ancient Dragon; Oct 5th, 2008 at 9:23 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- connect linked list (C)
- Circular linked list (C)
- linked list question (C)
- Seg Fault ~ Linked List Delete (C)
- Linked List & Objects (C++)
- "doubly linked list" question (C)
- linked stack question. (C)
Other Threads in the C++ Forum
- Previous Thread: help me..pls...
- Next Thread: link list with copy and delete elements
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






