Download presentation
Presentation is loading. Please wait.
Published byDennis McLaughlin Modified over 9 years ago
2
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next
3
Memory Layout abcdecaedb A linked representation uses an arbitrary layout. Layout of L = (a,b,c,d,e) using an array representation. (^denotes computer memory)
4
Linked Representation pointer (or link) in e is NULL caedb use a variable first to get to the first element a first
5
Normal Way To Draw A Linked List link or pointer field of node data field of node abcde NULL first
6
Chain A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL (or 0) pointer. abcde NULL first
7
Operations on a linked list Possible operations –Get an element –Remove an element –Insert an element We first discuss the codes for represent an node, then discuss the operations.
8
Node and Chain Representation (See Program 4.6 @ P186) template class ChainNode { private: T data; ChainNode *link; // constructors come here }; template class Chain {... private: ChainNode *first; } link data
9
Constructors Of ChainNode ChainNode() {} ? ? ? data link data ChainNode(const T& data) {this->data = data;} ChainNode(const T& data, chainNode * link) {this->data = data; this->link = link;}
10
Get(0) desiredNode = first; // gets you to first node return desiredNode data; abcde NULL first
11
Get(1) desiredNode = first link; // gets you to second node return desiredNode data; abcde NULL first
12
Get(2) desiredNode = first link link; // gets you to third node return desiredNode data; abcde NULL first
13
Get(5) desiredNode = first link link link link link; // desiredNode = NULL return desiredNode data; // NULL.element abcde NULL First
14
Delete An Element (Delete (0)) abcde NULL first deleteNode = first; first = first link; delete deleteNode; deleteNode
15
abde NULL first c Delete(2) first get to node just before node to be removed c c beforeNode = first link ; b beforeNode
16
Delete(2) save pointer to node that will be deleted deleteNode = beforeNode link; beforeNode abcde null first
17
Delete(2) now change pointer in beforeNode beforeNode link = beforeNode link link; delete deleteNode; beforeNode abcde null first
18
Insert(0,’f’) abcde NULL first f newNode Step 1: get a node, set its data and link fields newNode = new ChainNode (theElement, first);
19
Insert(0,’f’) abcde NULL first f newNode Step 2: update first first = newNode;
20
One-Step Insert(0,’f’) abcde NULL first f newNode first = new chainNode (‘f’, first);
21
Insert(3,’f’) first find node whose index is 2 abcde NULL first f newNode beforeNode c next create a node and set its data and link fields ChainNode * newNode = new ChainNode ( ‘f’, beforeNode link); finally link beforeNode to newNode beforeNode link = newNode;
22
Two-Step Insert(3,’f’) beforeNode = first link link; beforeNode link = new ChainNode (‘f’, beforeNode link); abcde NULL first f newNode beforeNode c
23
In Class Exercise Write down the pseudo code for the function insert(int n, char c)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.