Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an...

44
Linear List Linked Representation

Transcript of Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an...

Page 1: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Linear ListLinked Representation

Page 2: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

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

Page 3: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Memory Layout

a b c d e

c a e d b

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.

Page 4: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Linked Representation

pointer (or link) in e is NULL

c a e d b

use a variable firstNode to get to the first element a

firstNode

Page 5: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Normal Way To Draw A Linked List

link or pointer field of node

data field of node

a b c d e

NULL

firstNode

Page 6: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

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.

a b c d e

NULL

firstNode

Page 7: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Node Representation

template <class T>struct chainNode { // data members T element; chainNode<T> *next;  // constructors come here};

next

element

Page 8: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Constructors Of chainNode

chainNode() {}?

?

?

element

next

element

chainNode(const T& element)

{this->element = element;}

chainNode(const T& element, chainNode<T>* next)

{this->element = element;

this->next = next;}

Page 9: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

get(0)

checkIndex(0);

desiredNode = firstNode; // gets you to first node

return desiredNode->element;

a b c d e

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 inreturn desiredNode.element;When firstNode = null, there is no element whose index is 0.

Page 10: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

get(1)

checkIndex(1);

desiredNode = firstNode->next; // gets you to second node

return desiredNode->element;

a b c d e

NULL

firstNode

Page 11: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

get(2)

checkIndex(2);

desiredNode = firstNode->next->next; // gets you to third node

return desiredNode->element;

a b c d e

NULL

firstNode

Page 12: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

get(5)

checkIndex(5); // throws exception

desiredNode = firstNode->next->next->next->next->next;

// desiredNode = NULL

return desiredNode->element; // NULL.element

a b c d e

NULL

firstNode

Page 13: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Get(theIndex)

• Check theIndex• currentNode = firstNode• For (i=0; i<theIndex;i++)

– currentNode = currentNode->next;

• Return currentNode->element

Page 14: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Erase An Element

erase(0)

a b c d e

NULL

firstNode

deleteNode = firstNode;

firstNode = firstNode->next;

delete deleteNode;

Page 15: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

a b d e

NULL

firstNode

c

erase(2)

first get to node just before node to be removed

cc

beforeNode = firstNode->next;

b

beforeNode

Page 16: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

erase(2)

save pointer to node that will be deleted

deleteNode = beforeNode->next;

beforeNode

a b c d enull

firstNode

Page 17: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

erase(2)

now change pointer in beforeNode

beforeNode->next = beforeNode->next->next;

delete deleteNode;

beforeNode

a b c d enull

firstNode

Page 18: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

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--

Page 19: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

insert(0,’f’)

a b c d e

NULL

firstNode

f

newNode

Step 1: get a node, set its data and link fields

newNode = new chainNode<char>(theElement,

firstNode);

Page 20: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

insert(0,’f’)

a b c d e

NULL

firstNode

f

newNode

Step 2: update firstNode

firstNode = newNode;

Page 21: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

One-Step insert(0,’f’)

a b c d e

NULL

firstNode

f

newNode

firstNode = new chainNode<char>(‘f’, firstNode);

Page 22: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

insert(3,’f’)

• first find node whose index is 2

a b c d e

NULL

firstNode

fnewNode

beforeNode

c

• next create a node and set its data and link fields

chainNode<char>* newNode = new chainNode<char>( ‘f’,

beforeNode->next);

• finally link beforeNode to newNodebeforeNode->next = newNode;

Page 23: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Two-Step insert(3,’f’)

beforeNode = firstNode->next->next;

beforeNode->next = new chainNode<char>

(‘f’, beforeNode->next);

a b c d e

NULL

firstNode

fnewNode

beforeNode

c

Page 24: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

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++

Page 25: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
Page 26: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Other chain structures

Page 27: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Chain With Header Node

a b c d e

NULL

headerNode

a b c d e

NULL

firstNode

insert/erase code is simplified

Page 28: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Empty Chain With Header Node

headerNode

NULL

Page 29: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

The Class chainWithHeader

template<class T>class chainWithHeader : public linearList<T> { public:// other ADT methods defined here

protected: void checkIndex(int theIndex) const; chainNode<T>* HeaderNode; int listSize; }; 

Page 30: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Circular List

a b c d e

firstNode

Page 31: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Insert(0,’f’)insert(listSize,’g’)

erase(0)erase(listSize-1)

a b c d e

firstNode

Page 32: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Doubly Linked List

a b c d e

NULL

firstNode

NULL

lastNode

Page 33: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Node Representation

template <class T>struct doublyChainNode { // data members T element; chainNode<T> *previous; chainNode<T> *next; 

// constructors come here};

next

element

previous

Page 34: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Insert(0,’f’)insert(listSize,’g’)

erase(0)erase(listSize-1)

a b c d e

NULL

firstNode

NULL

lastNode

Page 35: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Doubly Linked Circular List

a b c d e

firstNode

Page 36: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Insert(0,’f’)insert(listSize,’g’)

erase(0)erase(listSize-1)

a b c e

headerNode

d

Page 37: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Doubly Linked Circular List With Header Node

a b c e

headerNode

d

Page 38: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Empty Doubly Linked Circular List With Header Node

headerNode

Page 39: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

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.

Page 40: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
Page 41: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
Page 42: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
Page 43: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
Page 44: Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Lab 2

• Check the note and download your lab material before lab, bring your labtop with you in class!