Download presentation
Presentation is loading. Please wait.
Published byElmer Bryan Modified over 9 years ago
1
Linear List Linked Representation
2
Linked Representation 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. The figures show computer memory. An array uses a contiguous chunk of memory.
4
Linked Representation pointer (or link) in e is NULL caedb use a variable firstNode to get to the first element a firstNode
5
Normal Way To Draw A Linked List link or pointer field of node data field of node abcde NULL firstNode
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 pointer. abcde NULL firstNode
7
Node Representation template struct chainNode { // data members T element; chainNode *next; // constructors come here }; next element
8
Constructors Of chainNode chainNode() {} ? ? ? element next element chainNode(const T& element) {this->element = element;} chainNode(const T& element, chainNode * next) {this->element = element; this->next = next;}
9
get(0) checkIndex(0); desiredNode = firstNode; // gets you to first node return desiredNode element; abcde NULL firstNode Do a checkIndex first. Then move to desired node. Once you are at the desired node, you can return the element in that node as in return desiredNode.element; When firstNode = null, there is no element whose index is 0.
10
get(1) checkIndex(1); desiredNode = firstNode next; // gets you to second node return desiredNode element; abcde NULL firstNode
11
get(2) checkIndex(2); desiredNode = firstNode next next; // gets you to third node return desiredNode element; abcde NULL firstNode
12
get(5) checkIndex(5); // throws exception desiredNode = firstNode next next next next next; // desiredNode = NULL return desiredNode element; // NULL.element abcde NULL firstNode
13
Get(theIndex) Check theIndex currentNode = firstNode For (i=0; i<theIndex;i++) –currentNode = currentNode->next; Return currentNode->element
14
Erase An Element erase(0) abcde NULL firstNode deleteNode = firstNode; firstNode = firstNode next; delete deleteNode;
15
abde NULL firstNode c erase(2) first get to node just before node to be removed c c beforeNode = firstNode next ; b beforeNode
16
erase(2) save pointer to node that will be deleted deleteNode = beforeNode next; beforeNode abcde null firstNode
17
erase(2) now change pointer in beforeNode beforeNode next = beforeNode next next; delete deleteNode; beforeNode abcde null firstNode
18
Erase(theIndex) Check theIndex If theIndex == 0 –Delete firstNode –firstNode = firstNode->next Else –Find beforeNode –deleteNode = beforeNode->next; –beforeNode->next = beforNode->next->next –Delete deleteNode listSize--
19
insert(0,’f’) abcde NULL firstNode f newNode Step 1: get a node, set its data and link fields newNode = new chainNode (theElement, firstNode);
20
insert(0,’f’) abcde NULL firstNode f newNode Step 2: update firstNode firstNode = newNode;
21
One-Step insert(0,’f’) abcde NULL firstNode f newNode firstNode = new chainNode (‘f’, firstNode);
22
insert(3,’f’) first find node whose index is 2 abcde NULL firstNode f newNode beforeNode c next create a node and set its data and link fields chainNode * newNode = new chainNode ( ‘f’, beforeNode next); finally link beforeNode to newNode beforeNode next = newNode;
23
Two-Step insert(3,’f’) beforeNode = firstNode next next; beforeNode next = new chainNode (‘f’, beforeNode next); abcde NULL firstNode f newNode beforeNode c
24
Insert(theIndex, theElement) Check theIndex (0-listSize) If theIndex == 0 – create newNode –newNode = firstNode Else –Create newNode –Find the beforeNode –newNode->next = beforeNode->next –beforeNode->next = newNode listSize++
26
Other chain structures
27
Chain With Header Node abcde NULL headerNode abcde NULL firstNode insert/erase code is simplified
28
Empty Chain With Header Node headerNode NULL
29
The Class chainWithHeader template class chainWithHeader : public linearList { public: // other ADT methods defined here protected: void checkIndex(int theIndex) const; chainNode * HeaderNode; int listSize; };
30
Circular List abcde firstNode
31
Insert(0,’f’) insert(listSize,’g’) erase(0) erase(listSize-1) abcde firstNode
32
Doubly Linked List abcde NULL firstNode NULL lastNode
33
Node Representation template struct doublyChainNode { // data members T element; chainNode *previous; chainNode *next; // constructors come here }; next element previous
34
Insert(0,’f’) insert(listSize,’g’) erase(0) erase(listSize-1) abcde NULL firstNode NULL lastNode
35
Doubly Linked Circular List abcde firstNode
36
Insert(0,’f’) insert(listSize,’g’) erase(0) erase(listSize-1) abce headerNode d
37
Doubly Linked Circular List With Header Node abce headerNode d
38
Empty Doubly Linked Circular List With Header Node headerNode
39
The STL Class list/vector Linked implementation of a linear list. Doubly linked circular list with header node. Has many more methods than chain. Similar names and signatures.
44
Lab 2 Check the note and download your lab material before lab, bring your labtop with you in class!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.