Linked Lists: Understanding Syntax and Semantics in C++, Study notes of Computer Science

An introduction to linked lists in c++ with a focus on syntax and semantics. It covers the creation of linked lists using structures and pointers, and includes examples and diagrams. Additionally, it discusses a hybridlist class implementation in c++.

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-3qn-1
koofers-user-3qn-1 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Linked Lists
Our goal in this lecture is to develop an understanding of the syntax and semantics of linked lists in C+
+.
We will be working with structures where each element consists of two parts. One part holds the data
or information associated with an element and the other part holds the address of another item of the
same form. Except in the case that the address is null, we have one item referring to another item of
exactly the same type.
A diagram is
Head
The syntax used in the book is similar to each of the following
typedef float ComponentType;
struct NodeType
{
ComponentType info;
NodePtr link;
};
typedef NodeType * NodePtr;
NodePtr head;
NodePtr currPtr;
NodePtr nextPtr;
typedef float ComponentType;
struct NodeType; // forward declaration
typedef NodeType * NodePtr;
struct NodeType // complete the declaration
{
ComponentType info;
NodePtr link;
};
NodePtr head;
NodePtr currPtr;
NodePtr nextPtr;
Some program statements
head = new NodeType;
head ->info = 12.8;
nextPtr = new NodeType;
nextPtr -> info = 45.2;
head -> link = nextPtr;
currPtr = nextPtr;
nextPtr = new NodeType;
nextPtr -> info = 70.1;
currPtr ->link = nextPtr;
nextPtr->link = NULL;
currPtr = nextPtr;
draw the diagram for each statement.
info link
nenext
info link
nenext
info link
nenext
info link
nenext
info NULL
pf3
pf4
pf5
pf8

Partial preview of the text

Download Linked Lists: Understanding Syntax and Semantics in C++ and more Study notes Computer Science in PDF only on Docsity!

Linked Lists Our goal in this lecture is to develop an understanding of the syntax and semantics of linked lists in C+ +. We will be working with structures where each element consists of two parts. One part holds the data or information associated with an element and the other part holds the address of another item of the same form. Except in the case that the address is null, we have one item referring to another item of exactly the same type. A diagram is Head The syntax used in the book is similar to each of the following typedef float ComponentType; struct NodeType { ComponentType info; NodePtr link; }; typedef NodeType * NodePtr; NodePtr head; NodePtr currPtr; NodePtr nextPtr; typedef float ComponentType; struct NodeType; // forward declaration typedef NodeType * NodePtr; struct NodeType // complete the declaration { ComponentType info; NodePtr link; }; NodePtr head; NodePtr currPtr; NodePtr nextPtr; Some program statements head = new NodeType; head ->info = 12.8; nextPtr = new NodeType; nextPtr -> info = 45.2; head -> link = nextPtr; currPtr = nextPtr; nextPtr = new NodeType; nextPtr -> info = 70.1; currPtr ->link = nextPtr; nextPtr->link = NULL; currPtr = nextPtr; draw the diagram for each statement. info link nenext info link nenext info link nenext info link nenext info NULL

Code to create a list, p 855 typedef int ComponentType; struct NodeType; // forward declaration typedef NodeType * NodePtr; struct NodeType // complete the declaration { ComponentType component; NodePtr * link; }; NodePtr head; NodePtr currPtr; NodePtr newNodePtr; ComponentType inputVal; head = new NodeType; cin >> head->component; currPtr = head; cin >>inputVal; while (cin) { newNodePtr = new NodeType; newNodePtr ->component = inputVal; currPtr -> link = newNodePtr; currPtr = newNodePtr; cin >> inputVal; } currPtr->link = NULL; Draw diagrams for this too. Input 32,78, 99, 21

// First occurrence of item is no longer in list // && List components are in ascending order HybridList(); // Constructor // Postcondition: // Empty list is created HybridList( const HybridList& otherList ); // Copy-constructor // Postcondition: // List is created as a duplicate of otherList ~HybridList(); // Destructor // Postcondition: // List is destroyed private: NodeType* head; }; The implementation //****************************************************************** // IMPLEMENTATION FILE (Hybrid.cpp) // This file implements the HybridList class member functions // List representation: a linked list of dynamic nodes //****************************************************************** #include "HybridList.h" #include #include // For NULL using namespace std; typedef NodeType* NodePtr; struct NodeType { ComponentType component; NodePtr link; }; // Private members of class: // NodePtr head; External pointer to linked list

HybridList::HybridList() // Constructor // Postcondition: // head == NULL { head = NULL; } //****************************************************************** HybridList::HybridList( const HybridList& otherList ) // Copy-constructor // Postcondition: // IF otherList.head == NULL (i.e., the other list is empty) // head == NULL // ELSE // head points to a new linked list that is a copy of // the linked list pointed to by otherList.head { NodePtr fromPtr; // Pointer into list being copied from NodePtr toPtr; // Pointer into new list being built if (otherList.head == NULL) { head = NULL; return; } // Copy first node fromPtr = otherList.head; head = new NodeType; head->component = fromPtr->component; // Copy remaining nodes toPtr = head; fromPtr = fromPtr->link; while (fromPtr != NULL) { toPtr->link = new NodeType; toPtr = toPtr->link; toPtr->component = fromPtr->component; fromPtr = fromPtr->link; } toPtr->link = NULL; }

void HybridList::InsertAsFirst( /* in / ComponentType item ) // Precondition: // component members of list nodes are in ascending order // && item < component member of first list node // Postcondition: // New node containing item is at top of linked list // && component members of list nodes are in ascending order { NodePtr newNodePtr = new NodeType; // Temporary pointer newNodePtr->component = item; newNodePtr->link = head; head = newNodePtr; } //****************************************************************** void HybridList::Insert( / in */ ComponentType item ) // Precondition: // component members of list nodes are in ascending order // && item is assigned // Postcondition: // New node containing item is in its proper place // in linked list // && component members of list nodes are in ascending order { NodePtr currPtr; // Moving pointer NodePtr prevPtr; // Pointer to node before *currPtr NodePtr newNodePtr; // Pointer to new node // Set up node to be inserted newNodePtr = new NodeType; newNodePtr->component = item; // Find previous insertion point prevPtr = NULL; currPtr = head; while (currPtr != NULL && item > currPtr->component) { prevPtr = currPtr; currPtr = currPtr->link; } // Insert new node newNodePtr->link = currPtr; if (prevPtr == NULL) head = newNodePtr; else prevPtr->link = newNodePtr; }

void HybridList::RemoveFirst( /* out / ComponentType& item ) // Precondition: // Linked list is not empty (head != NULL) // && component members of list nodes are in ascending order // Postcondition: // item == component member of first list node at entry // && Node containing item is no longer in linked list // && component members of list nodes are in ascending order { NodePtr tempPtr = head; // Temporary pointer item = head->component; head = head->link; delete tempPtr; } //****************************************************************** void HybridList::Delete( / in */ ComponentType item ) // Precondition: // item == component member of some list node // && component members of list nodes are in ascending order // Postcondition: // Node containing first occurrence of item is no longer in // linked list // && component members of list nodes are in ascending order { NodePtr delPtr; // Pointer to node to be deleted NodePtr currPtr; // Loop control pointer // Check if item is in first node if (item == head->component) { // Delete first node delPtr = head; head = head->link; } else { // Search for node in rest of list currPtr = head; while (currPtr->link->component != item) currPtr = currPtr->link; // Delete *(currPtr->link) delPtr = currPtr->link; currPtr->link = currPtr->link->link; } delete delPtr; }