/
Linked Lists Linked Lists

Linked Lists - PowerPoint Presentation

test
test . @test
Follow
399 views
Uploaded On 2016-07-13

Linked Lists - PPT Presentation

part 1 CS 244 Brent M Dingle PhD Game Design and Development Program Department of Mathematics Statistics and Computer Science University of Wisconsin Stout 2014 Chapter 5ish Previously ID: 402849

list node tail head node list head tail linked singly point sheldon leonard howard raj front null element elem

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Linked Lists" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Linked Listspart 1

CS 244

Brent M. Dingle, Ph.D.Game Design and Development ProgramDepartment of Mathematics, Statistics, and Computer ScienceUniversity of Wisconsin – Stout

2014

Chapter 5-ishSlide2

PreviouslyStandard Template LibraryMakefiles and GeanyNow…Moving on to Linked ListsSlide3

Marker SlideAny General Questions ?Next up

Singly Linked ListsClass FunDefinition and DescriptionImplementation ExamplesSlide4

The SetupHow to create a list usingPointersNodesSlide5

Group FunThe ceiling is NULLEach of you will be a NODEWe shall now make a LINKED LIST

Who wants to be the HEAD OF THE CLASS (list) ?Excellent. Point at the ceiling.Slide6

CurrentlyHave a list of ONE nodeThe HEAD of the listSlide7

Add a Node

Point at someone near you

Now we have 2 nodes in our listSlide8

An Another NodeNode added last time (2nd person)Now you point at someone near youKeep going…Slide9

End Group FunBack to Computer ScienceSlide10

Marker SlideAny Questions On:Singly Linked ListsClass Fun

Next upSingly Linked ListsDefinition and DescriptionImplementation ExamplesDoubly Linked ListsCircularly Linked ListsSlide11

Singly Linked List

A singly linked list is a structure consisting of a sequence of nodesA singly linked list stores a pointer to the first node (head) and last (tail)

Each node storeselementlink to the next node

next

elem

node

Leonard

head

tailSlide12

Singly Linked List

A singly linked list is a structure consisting of a sequence of nodesA singly linked list stores a pointer to the first node (head) and last (tail)

Each node storeselementlink to the next node

next

elem

node

Leonard

Sheldon

Howard

head

tailSlide13

Singly Linked List

A singly linked list is a structure consisting of a sequence of nodesA singly linked list stores a pointer to the first node (head) and last (tail)

Each node storeselementlink to the next node

next

elem

node

Leonard

Sheldon

Howard

head

tailSlide14

Singly Linked List

A singly linked list is a structure consisting of a sequence of nodesA singly linked list stores a pointer to the first node (head) and last (tail)

Each node storeselementlink to the next node

next

elem

node

Leonard

Sheldon

Howard

Raj

head

tailSlide15

Singly Linked List

A singly linked list is a structure consisting of a sequence of nodesA singly linked list stores a pointer to the first node (head) and last (tail)

Each node storeselementlink to the next node

next

elem

node

Leonard

Sheldon

Howard

Raj

head

tail

NULLSlide16

Singly Linked List

A singly linked list is a structure consisting of a sequence of nodesA singly linked list stores a pointer to the first node (head) and last (tail)

Each node storeselementlink to the next node

next

elem

node

Leonard

Sheldon

Howard

Raj

head

tail

NULLSlide17

Singly Linked List

A singly linked list is a structure consisting of a sequence of nodesA singly linked list stores a pointer to the first node (head) and last (tail)

Each node storeselementlink to the next node

next

elem

node

Leonard

Sheldon

Howard

Raj

head

tailSlide18

Singly Linked List

A singly linked list is a structure consisting of a sequence of nodesA singly linked list stores a pointer to the first node (head) and last (tail)

Each node storeselementlink to the next node

next

elem

node

Leonard

Sheldon

Howard

Raj

head

tailSlide19

Singly Linked List Node

next

elem

node

template

<

typename

Type

>

class

SLinkedListNode

{

public

:

Type

elem

;

SLinkedListNode

<Type> *next;

};

Leonard

Sheldon

Howard

Raj

Example code behind the scenesSlide20

Singly Linked List: Operations

A singly linked list is a structure consisting of a sequence of nodesTypical Operations

insertFront(e): inserts an element on the front of the listremoveFront(): returns and removes the element at the front of the listinsertBack(e): inserts an element on the back of the list

removeBack(): returns and removes the element at the end of the listSlide21

Inserting at the FrontAllocate a new nodeHave new node point to old head

Update head to point to new node

Leonard

Sheldon

Howard

Raj

head

tailSlide22

Inserting at the FrontAllocate a new nodeHave new node point to old head

Update head to point to new node

Leonard

Sheldon

Howard

Raj

head

Penny

tailSlide23

Inserting at the FrontAllocate a new nodeHave new node point to old head

Update head to point to new node

Leonard

Sheldon

Howard

Raj

head

Penny

tailSlide24

Inserting at the FrontAllocate a new nodeHave new node point to old head

Update head to point to new node

Leonard

Sheldon

Howard

Raj

head

Penny

tailSlide25

Inserting at the Front: Special CaseAllocate a new node

Have new node point to old headUpdate head to point to new node

head

tail

Slide26

Inserting at the Front: Special CaseAllocate a new node

Have new node point to old headUpdate head to point to new node

Raj

head

tail

Slide27

Inserting at the Front: Special CaseAllocate a new node

Have new node point to old headUpdate head to point to new node

Raj

head

tail

Done trivially, already points to NULLSlide28

Inserting at the Front: Special CaseAllocate a new node

Have new node point to old headUpdate head to point to new node

Raj

head

tailSlide29

Inserting at the Front: Special CaseAllocate a new node

Have new node point to old headUpdate head to point to new nodeIf tail is NULL, update tail to point to the head node

Raj

head

tail

Slide30

Singly Linked List: Operations

A singly linked list is a structure consisting of a sequence of nodesTypical Operations

insertFront(e): inserts an element on the front of the listremoveFront(): returns and removes the element at the front of the listinsertBack(e): inserts an element on the back of the list

removeBack(): returns and removes the element at the end of the listSlide31

Removing at the FrontUpdate head to point to next node in the list

Return elem of previous head and delete the node

Leonard

Sheldon

Howard

Raj

head

tailSlide32

Removing at the FrontUpdate head to point to next node in the list

Return elem of previous head and delete the node

Leonard

Sheldon

Howard

Raj

head

tailSlide33

Removing at the FrontUpdate head to point to next node in the list

Return elem of previous head and delete the node

Leonard

Sheldon

Howard

Raj

head

tailSlide34

Removing at the FrontUpdate head to point to next node in the list

Return elem of previous head and delete the node

Leonard

Sheldon

Howard

Raj

head

tailSlide35

Removing at the FrontUpdate head to point to next node in the list

Return elem of previous head and delete the node

Sheldon

Howard

Raj

head

tailSlide36

Removing at the Front: Special CaseUpdate head to point to next node in the list

Return elem of previous head and delete the node

Sheldon

head

tailSlide37

Removing at the Front: Special CaseUpdate head to point to next node in the list

Return elem of previous head and delete the node

Sheldon

head

tail

Slide38

Removing at the Front: Special CaseUpdate head to point to next node in the list

Return elem of previous head and delete the node

Sheldon

head

tail

Slide39

Removing at the Front: Special CaseUpdate head to point to next node in the list

Return elem of previous head and delete the node

Sheldon

head

tail

Slide40

Removing at the Front: Special CaseUpdate head to point to next node in the list

Return elem of previous head and delete the nodeIf head is NULL, update tail to NULL

head

tail

Slide41

Singly Linked List: Operations

A singly linked list is a structure consisting of a sequence of nodesTypical Operations

insertFront(e): inserts an element on the front of the listremoveFront(): returns and removes the element at the front of the listinsertBack(e): inserts an element on the back of the list

removeBack(): returns and removes the element at the end of the listSlide42

Inserting at the BackAllocate a new nodeIf tail is NULL, update head and tail to point to the new node; otherwise

Have the old tail point to the new nodeUpdate tail to point to new node

Leonard

Sheldon

Howard

head

tailSlide43

Inserting at the BackAllocate a new nodeIf tail is NULL, update head and tail to point to the new node; otherwise

Have the old tail point to the new nodeUpdate tail to point to new node

Leonard

Sheldon

Howard

head

tail

RajSlide44

Inserting at the BackAllocate a new nodeIf tail is NULL, update head and tail to point to the new node; otherwise

Have the old tail point to the new nodeUpdate tail to point to new node

Leonard

Sheldon

Howard

head

tail

RajSlide45

Inserting at the BackAllocate a new nodeIf tail is NULL, update head and tail to point to the new node; otherwise

Have the old tail point to the new nodeUpdate tail to point to new node

Leonard

Sheldon

Howard

head

tail

RajSlide46

Singly Linked List: Operations

A singly linked list is a structure consisting of a sequence of nodesTypical Operations

insertFront(e): inserts an element on the front of the listremoveFront(): returns and removes the element at the front of the listinsertBack(e): inserts an element on the back of the list

removeBack(): returns and removes the element at the end of the listSlide47

Removing at the BackNo efficient way of doing so (O

(n))Must walk a curPtr

to end of list along with a prevPtrTypically would not use a singly linked-list if this operation is commonly used

Leonard

Sheldon

Howard

Raj

head

tailSlide48

Removing at the BackNo efficient way of doing so (O

(n))Must walk a curPtr

to end of list along with a prevPtrTypically would not use a singly linked-list if this operation is commonly used

Leonard

Sheldon

Howard

Raj

head

tail

curPtr

prevPtr

Slide49

Removing at the BackNo efficient way of doing so (O

(n))Must walk a curPtr

to end of list along with a prevPtrTypically would not use a singly linked-list if this operation is commonly used

Leonard

Sheldon

Howard

Raj

head

tail

curPtr

prevPtrSlide50

Removing at the BackNo efficient way of doing so (O

(n))Must walk a curPtr

to end of list along with a prevPtrTypically would not use a singly linked-list if this operation is commonly used

Leonard

Sheldon

Howard

Raj

head

tail

curPtr

prevPtrSlide51

Removing at the BackNo efficient way of doing so (O

(n))Must walk a curPtr

to end of list along with a prevPtrTypically would not use a singly linked-list if this operation is commonly used

Leonard

Sheldon

Howard

Raj

head

tail

curPtr

prevPtrSlide52

Removing at the BackNo efficient way of doing so (O

(n))Must walk a curPtr

to end of list along with a prevPtrTypically would not use a singly linked-list if this operation is commonly used

Leonard

Sheldon

Howard

Raj

head

tail

curPtr

prevPtrSlide53

Removing at the BackNo efficient way of doing so (O

(n))Must walk a curPtr

to end of list along with a prevPtrTypically would not use a singly linked-list if this operation is commonly used

Leonard

Sheldon

Howard

Raj

head

tail

curPtr

prevPtr

Slide54

Removing at the BackNo efficient way of doing so (O

(n))Must walk a curPtr

to end of list along with a prevPtrTypically would not use a singly linked-list if this operation is commonly used

Leonard

Sheldon

Howard

Raj

head

tail

curPtr

prevPtr

Slide55

Removing at the BackNo efficient way of doing so (O

(n))Must walk a curPtr

to end of list along with a prevPtrTypically would not use a singly linked-list if this operation is commonly used

Leonard

Sheldon

Howard

head

tailSlide56

Singly Linked List: Operations

A singly linked list is a structure consisting of a sequence of nodesTypical Operations

insertFront(e): inserts an element on the front of the listremoveFront(): returns and removes the element at the front of the listinsertBack(e): inserts an element on the back of the list

removeBack(): returns and removes the element at the end of the listSlide57

Marker SlideAny Questions On:Singly Linked ListsClass Fun

Definition and DescriptionNext upSingly Linked ListsImplementation Examples

Check TimeMay Stop Here Implementation Examples is reviewed in next presentationSlide58

Linked List – Definition A linked list is a data structure which is built from nodes and pointers. A list forms a

“chain of nodes” With pointers representing the links of the chain and holding the entire list together. Slide59

Linked List – Example This linked list has four nodes in itEach with a link to the next node in the series. The last node has a link to the

value NULLThere is also another special pointer, called Start which points to the first link in the chain so that we can keep track of it.Slide60

Linked List – Implementation Key part of a linked list is the node structureWhich holds the data for each nodename, age, height, pointer to next node

class Node {

public: string m_name; int m_age; // age in years

double m_height; // height in meters

Node*

mp_next

; // pointer to next node

};

Node*

startPtr

= NULL;

// global variable to keep track

// of beginning of the list

Others may

call

startPtr

start

,

head

,

headPtr

root

,

rootPtrSlide61

Adding a NodeTo the End of the ListFirstCreate a new nodeAsk user for information to fill

in the node’s dataNode *

tempPtr = new Node;cout << "Please enter the name of the person -> ";cin >> tempPtr->m_name

;cout << “Enter the age of the person -> “;

cin >> tempPtr

->

m_age

;

cout

<< "Enter the height of the person

";

cin

>>

tempPtr

->height;

tempPtr

->

mp_next

= NULL;

tempPtr

???

Bob

Bob

22

Bob

22

1.8

A class constructor would likely do this last line for us

NULLSlide62

Initialize the Start PointerAssuming that was the first node in the listHow would we initialize the global variable startPtr ?

Node *startPtr = NULL;

?????startPtr = tempPtr;

tempPtr

NULL

startPtr

NULLSlide63

Moving Through a ListIt is common to use a currentPtrTo keep track of what node is “currently” being examinedIt too, usually begins at the beginning

startPtr =

tempPtr; ?????Node* currentPtr = startPtr

;

startPtr

NULL

currentPtr

NULLSlide64

Moving ExampleAssume we have a list with more than 1 node

Node* currentPtr = startPtr

;

NULL

startPtr

currentPtr

while (

currentPtr

->next != NULL )

{

currentPtr

=

currentPtr

-

>

mp_next

}

This will move the

currentPtr

to point to the last node in the list

currentPtr

currentPtr

currentPtr

Useful for outputting a list

Useful for appending to a listSlide65

Removing the HeadHow to remove the first element

NULL

startPtr

oldHeadPtr

removeFront

()

{

Node*

oldHeadPtr

=

startPtr

;

startPtr

=

oldHeadPtr

->

mp_next

;

delete

oldHeadPtr

;

}

startPtr

Calling this repeatedly until

startPtr

== NULL

will delete the entire list.

Useful for de-constructorsSlide66

Example: Linked List Class

class MyLinkedList{

public: MyLinkedList(); // constructor ~MyLinkedList(); // destructor

bool

isEmpty() const

;

// returns true if list is empty

Node

*

findNode

(string

findName

);

// returns null or node w/ match

void

addNode

(

const

Node&

newNode

);

// add node to list

void

removeFront

();

// remove first node of list

private:

Node*

mp_startPtr

;

// pointer to head of list

};

class Node

{

public:

string

m_name

; int m_age; // age in years Node*

mp_next; // pointer to next node};Slide67

Summary ReviewLinked Lists are similar to arraysWhen compared to arrays Linked Lists haveThe bad:You cannot access the

i-th element unless you walk to it through the i-1 elements that come before itThe good:You can INSERT an element into a list WITHOUT moving/shifting all the other elementsSlide68

Free Play – Things to Work OnHomework 4

Homework 5Slide69

The EndOr is it?