cse1303 part a data structures and algorithms lecture a9 – linked lists

51
CSE1303 Part A CSE1303 Part A Data Structures and Data Structures and Algorithms Algorithms Lecture A9 – Linked Lists Lecture A9 – Linked Lists

Post on 21-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

CSE1303 Part ACSE1303 Part AData Structures and AlgorithmsData Structures and Algorithms

Lecture A9 – Linked ListsLecture A9 – Linked Lists

Page 2: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

2

OverviewOverview

• Operations for Lists.

• Implementation of Linked Lists.

• Double Linked Lists.

Page 3: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

3

List OperationsList Operations

• Go to a position in the list.

• Insert an item at a position in the list.

• Delete an item from a position in the list.

• Retrieve an item from a position.

• Replace an item at a position.

• Traverse a list.

Page 4: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

4

ComparisonComparison

Linked Storage– Unknown list size.– Flexibility is needed.

Contiguous Storage– Known list size.– Few insertions and deletions are made within the list.– Random access

Page 5: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

5

Linked ListLinked List

headPtr

0 1 2 3

Page 6: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

6

#ifndef LINKEDLISTH#define LINKEDLISTH#include <stdbool.h>#include "node.h"

struct LinkedListRec{ int count; Node* headPtr;};

typedef struct LinkedListRec List;

void intializeList(List* listPtr);bool listEmpty(const List* listPtr);Node* setPosition(const List* listPtr, int position);void insertItem(List* listPtr, float item, int position);float deleteNode(List* listPtr, int position);

#endif

Page 7: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

7

void initializeList(List* listPtr){ listPtr->headPtr = NULL; listPtr->count = 0;}

Initialize ListInitialize List

count

headPtr

0

NULL

listPtr addr of list

Page 8: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

8

Set PositionSet Position

• check if position is within range

• start with address of head node

• set count to 0

• while count is less than position

– follow link to next node

– increment count

• return address of current node

Page 9: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

9

Node* setPosition(const List* listPtr, int position){ int i; Node* nodePtr = listPtr->headPtr;

if (position < 0 || position >= listPtr->count) { fprintf(stderr, “Invalid position\n”); exit(1); } else { for (i = 0; i < position; i++) { nodePtr = nodePtr->nextPtr; } }

return nodePtr;}

Page 10: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

10

Set PositionSet Position

headPtr

0 21

2 position

0x30a80x20300x4000

0x4000

0 i

nodePtr

Page 11: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

11

Set PositionSet Position

2 position

0x2030

1 i

nodePtr

0x30a80x20300x4000

0 21

headPtr

Page 12: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

12

Set PositionSet Position

2 position

0x30a8

2 i

nodePtr

0x30a80x20300x4000

0 21

headPtr

Page 13: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

13

InsertInsert

0 21

If we insert it at position 0.

headPtr

newNodePtr

10 3

headPtr

2

Page 14: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

14

Instead, suppose we insert it at position 1.

0 21

headPtr

newNodePtr

0 3

1

headPtr

2

newNodePtr

Page 15: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

15

void insertItem(List* listPtr, float item, int position){ Node* newNodePtr = makeNode(item); Node* prevPtr = NULL;

if (position == 0) { newNodePtr->nextPtr = listPtr->headPtr; listPtr->headPtr = newNodePtr; } else { prevPtr = setPosition(listPtr, position-1); newNodePtr->nextPtr = prevPtr->nextPtr; prevPtr->nextPtr = newNodePtr; } listPtr->count++;}

Page 16: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

16

Inserting – New ListInserting – New List

headPtr

0 position

0x30a8

NULL

newNodePtr

0x30a8

Page 17: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

17

Inserting – New ListInserting – New List

0 position

0x30a8 newNodePtr

0x30a8headPtr

Page 18: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

18

Inserting – Start of ListInserting – Start of List

0x30a80x2008

0x2000

0position

0x2000newNodePtr

headPtr

Page 19: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

19

Inserting – Start of ListInserting – Start of List

0x30a80x2008

0x2000

0position

0x2000newNodePtr

headPtr

Page 20: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

20

Inserting – Start of ListInserting – Start of List

0x30a80x2008

0x2000

0position

0x2000newNodePtr

headPtr

Page 21: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

21

Inserting – Inside the ListInserting – Inside the List

0x2000

1position

0x2000newNodePtr

0x30500x3080

prevPtr 0x3080

headPtr

Page 22: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

22

Inserting – Inside the ListInserting – Inside the List

0x2000

1position

0x2000newNodePtr

0x30500x3080

prevPtr 0x3080

headPtr

Page 23: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

23

Inserting – Inside the ListInserting – Inside the List

0x2000

1position

0x2000newNodePtr

0x30500x3080

prevPtr 0x3080

headPtr

Page 24: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

24

DeleteDelete

2 310

If we delete the Node at position 0.

headPtr

210

headPtr

Page 25: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

25

If we delete the Node at position 2.headPtr

2 310

headPtr

210

oldNodePtr

Page 26: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

26

void deleteNode(List* listPtr, int position){ Node* oldNodePtr = NULL; Node* prevPtr = NULL;

if (listPtr->count > 0 && position < listPtr->count) { if (position == 0) { oldNodePtr = listPtr->headPtr; listPtr->headPtr = oldNodePtr->nextPtr; } else { prevPtr = setPosition(listPtr, position - 1); oldNodePtr = prevPtr->nextPtr; prevPtr->nextPtr = oldNodePtr->nextPtr; } listPtr->count--; free(oldNodePtr); } else { fprintf(stderr, “List is empty or invalid position.\n”); exit(1); }}

Page 27: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

27

Deleting – 1Deleting – 1stst Node Node

0 position

0x4000 oldNodePtr

0x30a80x20300x4000

headPtr

Page 28: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

28

0 position

0x4000 oldNodePtr

0x30a80x20300x4000

Deleting – 1Deleting – 1stst Node Node

headPtr

Page 29: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

29

0 position

0x4000 oldNodePtr

0x30a80x2030

Deleting – 1Deleting – 1stst Node Node

headPtr

Page 30: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

30

Deleting – Middle NodeDeleting – Middle Node

1 position

0x2030 oldNodePtr

0x30a80x20300x4000

0x2030 prevPtr

headPtr

Page 31: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

31

0x30a80x20300x4000

Deleting – Middle NodeDeleting – Middle Node

1 position

0x2030 oldNodePtr

0x2030 prevPtr

headPtr

Page 32: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

32

0x30a80x4000

Deleting – Middle NodeDeleting – Middle Node

1 position

0x2030 oldNodePtr

0x2030 prevPtr

headPtr

Page 33: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

33

Double Linked List OperationsDouble Linked List Operations

• Go to a position in the list.

• Insert an item in a position in the list.

• Delete an item from a position in the list.

• Retrieve an item from a position.

• Replace an item at a position.

• Traverse a list, in both directionsin both directions.

Page 34: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

34

Double Linked ListDouble Linked List

currentPtr

0 1 2 3 4

Page 35: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

35

struct DoubleLinkNodeRec{ float value; struct DoubleLinkNodeRec* nextPtr; struct DoubleLinkNodeRec* previousPtr;};

typedef struct DoubleLinkNodeRec Node;

struct DoubleLinkListRec{ int count; Node* currentPtr; int position;};

typedef struct DoubleLinkListRec DoubleLinkList;

Page 36: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

36

Insert at endInsert at end

currentPtrnewNodePtr

0x4000 0x3080 0x2030 0x2000

0x4000 0x3080

0x3080 0x2030 NULL

NULLNULL

NULL

0x2000prevPtr

0x2030

Page 37: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

37

Insert at endInsert at end

0x4000 0x3080 0x2030 0x2000

0x4000 0x3080

0x3080 0x2030 0x2000

NULLNULL

NULL

0x2000prevPtr

0x2030

currentPtrnewNodePtr

Page 38: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

38

Insert at endInsert at end

0x4000 0x3080 0x2030 0x2000

0x4000 0x3080

0x3080 0x2030 0x2000

NULLNULL

0x2030

0x2000prevPtr

0x2030

currentPtrnewNodePtr

Page 39: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

39

Insert inside the listInsert inside the list0x4000 0x3080 0x2030

0x2000

0x4000 0x3080

0x3080 0x2030NULL

NULL

NULL

NULL

0x2000prevPtr

0x3080

currentPtr

newNodePtr

Page 40: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

40

Insert inside the listInsert inside the list0x4000 0x3080 0x2030

0x2000

0x4000 0x3080

0x3080 0x2030NULL

NULL

2030

NULL

0x2000prevPtr

0x3080

currentPtr

newNodePtr

Page 41: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

41

Insert inside the listInsert inside the list0x4000 0x3080 0x2030

0x2000

0x4000 0x3080

0x3080 0x2030NULL

NULL

2030

3080

0x2000prevPtr

0x3080

currentPtr

newNodePtr

Page 42: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

42

Insert inside the listInsert inside the list0x4000 0x3080 0x2030

0x2000

0x4000 0x2000

0x3080 0x2030NULL

NULL

2030

3080

0x2000prevPtr

0x3080

currentPtr

newNodePtr

Page 43: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

43

Insert inside the listInsert inside the list0x4000 0x3080 0x2030

0x2000

0x4000 0x2000

0x3080 0x2000NULL

NULL

2030

3080

0x2000prevPtr

0x3080

currentPtr

newNodePtr

Page 44: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

44

Delete from endDelete from end

oldNodePtr

0x4000 0x3080 0x2030

0x4000 0x3080

0x3080 0x2030 NULL

NULL

0x2030currentPtr

prevPtr 0x3080

Page 45: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

45

Delete from endDelete from end

0x4000 0x3080 0x2030

0x4000 0x3080

0x3080 NULL NULL

NULL

0x2030currentPtr

prevPtr 0x3080

oldNodePtr

Page 46: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

46

Delete from endDelete from end

0x4000 0x3080

0x4000

0x3080 NULL

NULL

0x2030currentPtr

prevPtr 0x3080

oldNodePtr

Page 47: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

47

Delete from inside listDelete from inside list

0x4000 0x3080 0x2030

0x4000 0x3080

0x3080 0x2030 NULL

NULL

0x3080currentPtr

prevPtr 0x4000

oldNodePtr

Page 48: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

48

Delete from inside listDelete from inside list

0x4000 0x3080 0x2030

0x4000 0x3080

0x2030 0x2030 NULL

NULL

0x3080currentPtr

prevPtr 0x4000

oldNodePtr

Page 49: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

49

Delete from inside listDelete from inside list

0x4000 0x3080 0x2030

0x4000 0x4000

0x2030 0x2030 NULL

NULL

0x3080currentPtr

prevPtr 0x4000

oldNodePtr

Page 50: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

50

Delete from inside listDelete from inside list

0x4000 0x2030

0x4000

0x2030 NULL

NULL

0x3080currentPtr

prevPtr 0x4000

oldNodePtr

Page 51: CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

51

RevisionRevision• Linked List.• Benefits of different implementations.• Double Linked List.

Revision: ReadingRevision: Reading• Kruse 5.2.2 - 5.2.5• Standish 2.4 – 2.5

PreparationPreparationNext lecture: Elementary Algorithms• Read Chapter 6 in Kruse et al.