| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Removing node from a linked list
Hi
So far i have come up with this code for a linked list but i need to know how to remove a node from the linked list, i know i have to search for the node then remove it by setting the current array pointer to the previous one but im not sure which way to go about it! #include <iostream> #include <string> using namespace std; const int MAXSIZE = 10; int size = 0; int headOfList = -1; int freeList = 0; typedef struct { string name; int next; }node; node list[MAXSIZE]; void addToList(); void displayList(); void removefromlist(); bool isFull() { return (size==MAXSIZE); } bool isEmpty() { return (size==0); } void init() { for(int x=0; x<MAXSIZE;x++) list[x].next = x+1; list[MAXSIZE-1].next = -1; } void main() { int choice = 0; init(); while (choice != 4) { cout << "\nMain Menu\n\n1. Add to List\n2. Remove from List\n3. Display list"; cout << "\n4. Exit\n\n Select: "; cin >> choice; cin.get(); switch(choice) { case 1 : addToList(); break; case 2 : removefromlist(); break; case 3 : displayList(); break; case 4 : cout << "\nEnd of Program\n\n"; break; default : cout << "\nInvalid selection. Try again: \n"; } } cout << "\n\n"; } void addToList() { int current, previous; bool found; if (isFull()) { cout << "\nList is full\n"; } else { cout << "\nEnter a name: "; getline(cin, list[freeList].name); int newSlot = freeList; freeList=list[freeList].next; if (isEmpty()) { headOfList = newSlot; list[newSlot].next = -1; } else { current = headOfList; previous = current; found = false; while ((current != -1) && (found != true)) { if (list[newSlot].name < list[current].name) { if (current == headOfList) { list[newSlot].next = current; headOfList = newSlot; } else { list[previous].next = newSlot; list[newSlot].next = current; } found = true; } else { previous = current; current = list[current].next; } } // of while if (found != true) { list[previous].next = newSlot; list[newSlot].next = -1; } } // of else size++; } // of first else } // of function void displayList() { int current; if (isEmpty()) { cout << "\nList is empty. Nothing to display!\n\n"; } else { current = headOfList; cout << endl; while (current != -1) { cout << list[current].name << endl; current = list[current].next; } cout << endl; } } |
|
#2
|
|||
|
|||
|
Try this
In keeping with your line of thought, try what's below. Just a note though,
you might want to have a look at pointers, if you're not familiar with them. They provide a cleaner, and I'd say easier solution, once you understand them. void removefromlist() { string nameToRemove = string(); if (isEmpty()) { cout << "\nNothing to remove.\n\n"; } else { cout << "\nWhich name would you like to remove?\n"; displayList(); cout << "Select: "; getline(cin, nameToRemove); int current = headOfList; int previous = -1; while (current != -1) { if ( list[current].name == nameToRemove) { //need to adjust the "link" pointing to current //so that it now points to what current points to if (previous != -1) { //anything but the head of this list list[previous].next = list[current].next; } else { //move headOfList to point to what current points to headOfList = list[current].next; } //done break; } previous = current; current = list[current].next; } //while loop } //else } //removefromlist() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Removing node from a linked list |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|