


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An example implementation of a singly linked list in c++. The code includes functions for creating and printing the list, as well as searching for and deleting nodes. Users can input data to be added to the list in ascending order or search for and delete existing nodes.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!
#include
while (inputData >= 0) // stop when a negative value is entered { found = SearchList(first, currPtr, predPtr, inputData); if (! found) // Node isn't there, so add it. currPtr is pointing at the { // node which will follow this one, and predPtr points to // the node which will precede this node newPtr = new node; // Create a new node. newPtr->data = inputData; newPtr->next = currPtr; // This node points to next one in order. if (currPtr == first) // This will be the new first node first = newPtr; // so make head point to this node; else // otherwise, make the previous node point predPtr->next = newPtr; // to this one. } else // Delete it. currPtr is pointing at it; predPtr points at the node { // before it (or is NULL if we're deleting the first node). if (first == currPtr) // If first node is to be deleted, first = currPtr->next; // make head point to the second node. else // else , make previous node point to the predPtr->next = currPtr->next; // one after this one. delete currPtr; // Get rid of this node. } PrintList(first); cout << "Next number: "; cin >> inputData; } // end of while more data system(“pause”); return 0; } // end of main bool SearchList (nodePtr first, nodePtr & currPtr, nodePtr & predPtr, int key) { currPtr = first; // Set currPtr to point to the first node. predPtr = NULL; // predPtr will track one node behind it; while (currPtr != NULL) // While there are more nodes to look at: { if (currPtr->data == key) // if this is the one we want, stop searching return true; // and report back that we found it. if (currPtr->data > key) // If this value is greater, then the return false; // one we want isn't here, so stop searching. predPtr = currPtr; // Otherwise advance to the next node. currPtr = currPtr->next; } // If loop ended without returning, then the return false; // value we're searching for isn't there but } // logically belongs at the end of the list
Here's a sample run of the program (with user entries in bold print): Type a series of positive integers (in ascending order) to be entered into a linked list. Type a negative integer to stop. Values may be typed one per line or all on the same line. The resulting list is displayed after each value is added. Sample input: 8 10 15 25 - 5 10 15 20 25 - The list contains: 5 The list contains: 5 -> 10 The list contains: 5 -> 10 -> 15 The list contains: 5 -> 10 -> 15 -> 20 The list contains: 5 -> 10 -> 15 -> 20 -> 25 Type one or more positive integers to be added to the list, typing -1 to stop. The new list is displayed after each entry. You can add new values or delete existing ones. 16 The list contains: 5 -> 10 -> 15 -> 16 -> 20 -> 25 Next number: 3 The list contains: 3 -> 5 -> 10 -> 15 -> 16 -> 20 -> 25 Next number: 42 The list contains: 3 -> 5 -> 10 -> 15 -> 16 -> 20 -> 25 -> 42 Next number: 17 The list contains: 3 -> 5 -> 10 -> 15 -> 16 -> 17 -> 20 -> 25 -> 42 Next number: 10 The list contains: 3 -> 5 -> 15 -> 16 -> 17 -> 20 -> 25 -> 42 Next number: 3 The list contains: 5 -> 15 -> 16 -> 17 -> 20 -> 25 -> 42 Next number: 42 The list contains: 5 -> 15 -> 16 -> 17 -> 20 -> 25 Next number: 16 The list contains: 5 -> 15 -> 17 -> 20 -> 25 Next number: 17 The list contains: 5 -> 15 -> 20 -> 25 Next number: 10 The list contains: 5 -> 10 -> 15 -> 20 -> 25