c++ - Deleting by Index? -
i created linked-list program using following source code,
#include <iostream> using namespace std; struct node { int data; node *next; }; struct headnode { int count; node *headptr; }; class linkedlist { public: linkedlist(); ~linkedlist(); void addtohead( int ); bool removefromhead(); // bool addtotail ( int ); // bool removefromtail(); void addnode ( int ); bool deletenode ( int ); void deleteallnodes(); bool isempty(); int getnoofnodes(); void displayallnodes(); private: int datacmp ( int, int ); void displaynode ( node* ); headnode head; }; linkedlist::linkedlist() { head.count = 0; head.headptr = null; return; } linkedlist::~linkedlist() { deleteallnodes(); return; } void linkedlist::addtohead ( int newdata ) { node *pnew = new node; pnew -> data = newdata; pnew -> next = head.headptr; head.headptr = pnew; head.count++; } bool linkedlist::removefromhead() { bool exit; node *temp; if ( head.headptr ) { temp = head.headptr; head.headptr = head.headptr -> next; delete temp; head.count--; exit = true; // returns true if it's successful } else exit = false; // returns false if it's not successful return exit; } /* bool linkedlist::addtotail( int ) { } bool linked::removefromtail(); { } */ void linkedlist::addnode ( int newdata ) { node *pnew = new node, *ppre = null, *pcur = head.headptr; pnew -> data = newdata; while ( pcur && datacmp( pnew -> data, pcur -> data ) >= 0 ) { ppre = pcur; pcur = pcur -> next; } if ( ppre ) { pnew -> next = ppre -> next; ppre -> next = pnew; head.count++; } else { pnew -> next = head.headptr; head.headptr = pnew; head.count++; } } bool linkedlist::deletenode( int data ) { bool exit; node *ppre = null, *pcur = head.headptr; while ( pcur && datacmp( pcur -> data, data ) < 0 ) { ppre = pcur; pcur = pcur -> next; } if ( pcur && datacmp( pcur -> data, data ) == 0 ) { if ( ppre ) { ppre -> next = pcur -> next; delete pcur; head.count--; exit = true; // return true if successful } else { head.headptr = pcur -> next; delete pcur; head.count--; exit = true; // return true if successful } } else exit = false; // return false if unsuccessful return exit; } void linkedlist::deleteallnodes() { node *temp; while ( head.headptr ) { temp = head.headptr; head.headptr = head.headptr -> next; delete temp; head.count--; } return; } bool linkedlist::isempty() { return head.count == 0; } int linkedlist::getnoofnodes() { return head.count; } void linkedlist::displayallnodes() { node *pcur = head.headptr; int nodecount = 1; while ( pcur ) { cout << "node " << nodecount << ": "; displaynode( pcur ); cout << endl; nodecount++; pcur = pcur -> next; } return; } int linkedlist::datacmp( int value0, int value1 ) { int exit = 0; if ( value0 < value1 ) exit = -1; else if ( value0 > value1 ) exit = 1; return exit; } void linkedlist::displaynode( node *node ) { cout << node -> data; return; } void printmenu() { cout << "1. add head" << endl; cout << "2. remove head" << endl; cout << "3. add node " << endl; cout << "4. delete node" << endl; cout << "5. delete nodes" << endl; cout << "6. list empty?" << endl; cout << "7. number of nodes" << endl; cout << "8. display nodes" << endl; cout << "9. quit" << endl; } int getchoice() { int choice; cout << "select choice: "; cin >> choice; cin.clear(); cin.ignore( 200, '\n' ); return choice; } int getdata() { int data; cout << "enter data: "; cin >> data; cin.clear(); cin.ignore( 200, '\n' ); return data; } void processchoice( int choice, linkedlist& list ) { int data; bool opstatus; switch ( choice ) { case 1: data = getdata(); list.addtohead( data ); break; case 2: if ( list.removefromhead() ) { cout << "removed node head" << endl; } else cerr << "list empty" << endl; break; case 3: data = getdata(); list.addnode( data ); cout << "node " << data << " added"; cout << endl; break; case 4: if ( !list.isempty() ) { data = getdata(); if ( list.deletenode( data ) ) { cout << "node " << data << " deleted"; cout << endl; } else cerr << "node not found" << endl; } else cerr << "list empty" << endl; break; case 5: list.deleteallnodes(); cout << "all nodes deleted" << endl; break; case 6: cout << ( list.isempty() ? "list empty" : "list not empty" ); cout << endl; break; case 7: cout << "no. of nodes: " << list.getnoofnodes(); cout << endl; break; case 8: list.displayallnodes(); break; default: cout << "invalid choice" << endl; } } int main() { linkedlist list; int choice; { printmenu(); choice = getchoice(); if ( choice != 9 ) processchoice( choice, list ); } while ( choice != 9 ); return 0; }
instead of deleting value, how can modify code can delete nodes index?
add deletenodebyindex
function takes in index value, can perform same delete operation except delete item @ given index instead of testing value matches.
edit:
you're deleting node data, half way there. difference is, instead of iterating through each item , testing data value, counting how many items you've iterated through until reach index want remove.
bool linkedlist::deletenodebyindex( int index ) { bool exit; node *ppre = null, *pcur = head.headptr; int currentindex = 0; while ( pcur ) { // here loop until reach our desired index. if (currentindex == index) { break; } // increment current index , pcur next item. currentindex++; ppre = pcur; pcur = pcur -> next; } // if pcur still valid @ point, means broke @ // proper place , pcur should @ proper index. if ( pcur ) { if ( ppre ) { ppre -> next = pcur -> next; delete pcur; head.count--; exit = true; // return true if successful } else { head.headptr = pcur -> next; delete pcur; head.count--; exit = true; // return true if successful } } else exit = false; // return false if unsuccessful return exit; }
Comments
Post a Comment