Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSCI 270: Implementing a Singly Linked List - Prof. Sandra Huerter, Study notes of Data Structures and Algorithms

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

2009/2010

Uploaded on 03/28/2010

koofers-user-53q
koofers-user-53q 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI 270 Example of a Client-Managed Singly Linked List
#include <iostream>
using namespace std;
struct node; // forward declaration for the structure
typedef node* nodePtr; // so I can create a typedef for a pointer to it
struct node
{
int data;
nodePtr next; // or node * next;
};
void PrintList (nodePtr first);
bool SearchList (nodePtr first, nodePtr & currPtr, nodePtr & prevPtr, int key);
int main ()
{
nodePtr first, // points to the beginning of the list
currPtr, // used in a search to point to the current node
newPtr, // used to point to a newly-created node
lastPtr, // used to point to the last node in the list
predPtr; // trails one node behind currPtr in a search
int inputData;
bool found;
// Create a list from data values input in already-sorted order so that each
// new item is added at the end of the list – no searching is necessary.
first = NULL; // so I can test later to see if first is pointing to a real node
cout << "Type a series of positive integers (in ascending order)\n"
<< "to be entered into a linked list. Type a negative integer to stop.\n";
cout << "Values may be typed one per line or all on the same line.\n";
cout << "The resulting list is displayed after each value is added.\n";
cout << "Sample input: 8 10 15 25 -1\n";
cin >> inputData;
while (inputData >= 0) // for each valid input value:
{
newPtr = new node; // Create a new structure
newPtr->data = inputData; // and assign its data value.
newPtr->next = NULL; // Make new node the end of the list.
if (first == NULL) // If this is the first node in the list,
first = newPtr; // make first point to it;
else // otherwise make the previous node point
lastPtr->next = newPtr; // to this one.
lastPtr = newPtr; // Remember where the last node is.
PrintList(first); // Display list to be sure it's correct.
cin >> inputData; // get next value to be inserted into list
}
// Now let's add some nodes given to us in random order
cout << "Type one or more positive integers to be added to the list,\n";
cout << "typing -1 to stop. The new list is displayed after each entry.\n";
cout << "You can add new values or delete existing ones.\n";
cin >> inputData;
1
pf3
pf4

Partial preview of the text

Download CSCI 270: Implementing a Singly Linked List - Prof. Sandra Huerter and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CSCI 270 Example of a Client-Managed Singly Linked List

#include using namespace std; struct node; // forward declaration for the structure typedef node* nodePtr; // so I can create a typedef for a pointer to it struct node { int data; nodePtr next; // or node * next; }; void PrintList (nodePtr first); bool SearchList (nodePtr first, nodePtr & currPtr, nodePtr & prevPtr, int key); int main () { nodePtr first, // points to the beginning of the list currPtr, // used in a search to point to the current node newPtr, // used to point to a newly-created node lastPtr, // used to point to the last node in the list predPtr; // trails one node behind currPtr in a search int inputData; bool found; // Create a list from data values input in already-sorted order so that each // new item is added at the end of the list – no searching is necessary. first = NULL; // so I can test later to see if first is pointing to a real node cout << "Type a series of positive integers (in ascending order)\n" << "to be entered into a linked list. Type a negative integer to stop.\n"; cout << "Values may be typed one per line or all on the same line.\n"; cout << "The resulting list is displayed after each value is added.\n"; cout << "Sample input: 8 10 15 25 -1\n"; cin >> inputData; while (inputData >= 0) // for each valid input value: { newPtr = new node; // Create a new structure newPtr->data = inputData; // and assign its data value. newPtr->next = NULL; // Make new node the end of the list. if (first == NULL) // If this is the first node in the list, first = newPtr; // make first point to it; else // otherwise make the previous node point lastPtr->next = newPtr; // to this one. lastPtr = newPtr; // Remember where the last node is. PrintList(first); // Display list to be sure it's correct. cin >> inputData; // get next value to be inserted into list } // Now let's add some nodes given to us in random order cout << "Type one or more positive integers to be added to the list,\n"; cout << "typing -1 to stop. The new list is displayed after each entry.\n"; cout << "You can add new values or delete existing ones.\n"; cin >> inputData;

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