Lecture A9 Linked Lists CSE1303 Part A Data Structures and Algorithms

Download Report

Transcript Lecture A9 Linked Lists CSE1303 Part A Data Structures and Algorithms

Lecture A9 Linked Lists

CSE1303 Part A Data Structures and Algorithms

Overview

• Operations for Lists.

• Implementation of Linked Lists.

• Double Linked Lists.

5/2/2020 CSE1303 Part A 2

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

5/2/2020 CSE1303 Part A 3

Comparsion

Linked Storage

– Unknown list size.

– Flexibility is needed.

Contiguous Storage

– Known list size.

– Few insertions and deletions are made within the list.

– Random access 5/2/2020 CSE1303 Part A 4

Linked List

Head 0 5/2/2020 1 CSE1303 Part A 2 3 5

#ifndef LINKEDLISTH #define LINKEDLISTH #include #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

6

Initialize List

listPtr

addr of list

count headPtr

0 NULL

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

5/2/2020 CSE1303 Part A 7

Set 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 5/2/2020 CSE1303 Part A 8

Head 0

2 position

Set Position

1 2 5/2/2020 CSE1303 Part A 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;

5/2/2020 CSE1303 Part A 10

Set Position

Head

0x4000 0x2030 2 0 0x4000 position i NodePtr

5/2/2020 CSE1303 Part A

0x30a8

11

Set Position

Head

0x4000 0x2030 2 0 0x4000 position i NodePtr

5/2/2020 CSE1303 Part A

0x30a8

12

Set Position

Head

0x4000 0x2030 2 1 0x2030 position i NodePtr

5/2/2020 CSE1303 Part A

0x30a8

13

Set Position

Head

0x4000 0x2030 2 2 0x30a8 position i NodePtr

5/2/2020 CSE1303 Part A

0x30a8

14

Insert

Head 0 If we insert it at position 0.

1 5/2/2020 0 Head CSE1303 Part A 1 2 2 15

Insert

Head 0 If we insert it at position 0.

1 0 Head 5/2/2020 1 CSE1303 Part A 2 2 3 16

Instead, suppose we insert it at position 1.

Head 0 1 2 5/2/2020 CSE1303 Part A 17

Instead, suppose we insert it at position 1.

Head 5/2/2020 0 CSE1303 Part A 1 2 3 18

void insertItem(List* listPtr, float item, int position) { Node* newNodePtr = makeNode(item); Node* nodePtr = NULL; } if (position == 0) { newNodePtr->nextPtr = listPtr->headPtr; listPtr->headPtr = newNodePtr; } else { nodePtr = setPosition(listPtr, position-1); newNodePtr->nextPtr = nodePtr->nextPtr; nodePtr->nextPtr = newNodePtr; } listPtr->count++;

5/2/2020 CSE1303 Part A 19

Head

Inserting – New List

NULL 0x30a8 0 position 0x30a8 newNodePtr

5/2/2020 CSE1303 Part A 20

Head

Inserting – New List

0x30a8 0 position 0x30a8 newNodePtr

5/2/2020 CSE1303 Part A 21

Inserting – Start of List

0x2008 newNodePtr 0x2000 position 0x2000 0

5/2/2020 CSE1303 Part A

0x30a8

22

Inserting – Start of List

0x2008 newNodePtr 0x2000 position 0x2000 0

5/2/2020 CSE1303 Part A

0x30a8

23

Inserting – Start of List

0x2008 newNodePtr 0x2000 position 0x2000 0

5/2/2020 CSE1303 Part A

0x30a8

24

Inserting – Inside the List

Head

0x3080 NodePtr 0x3080 newNodePtr 0x2000 position 1

5/2/2020 CSE1303 Part A

0x2000 0x3050

25

Inserting – Inside the List

Head

0x3080 NodePtr 0x3080 newNodePtr 0x2000 position 1

5/2/2020 CSE1303 Part A

0x2000 0x3050

26

Inserting – Inside the List

Head

0x3080 NodePtr 0x3080 newNodePtr 0x2000 position 1

5/2/2020 CSE1303 Part A

0x2000 0x3050

27

Head

Delete

0 1 2 If we delete the Node at position 0.

3 5/2/2020 Head 0 CSE1303 Part A 1 2 28

If we delete the Node at position 2.

Head 0 5/2/2020 1 CSE1303 Part A 2 3 29

If we delete the Node at position 2.

Head 0 5/2/2020 1 CSE1303 Part A 2 2 30

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

31

Deleting – 1 st Node

Head

0x4000 0 position 0x2030 0x30a8 0x4000 NodePtr

5/2/2020 CSE1303 Part A 32

Deleting – 1 st Node

0x4000 0x2030 0 position

Head

0x4000 NodePtr

5/2/2020 CSE1303 Part A

0x30a8

33

Deleting – 1 st Node

0x2030 0 position

Head

0x4000 NodePtr

5/2/2020 CSE1303 Part A

0x30a8

34

Deleting – Middle Node

Head

0x4000 0x2030 1 0x2030 0x2030 position NodePtr OldNodePtr

5/2/2020 CSE1303 Part A

0x30a8

35

Deleting – Middle Node

Head

0x4000 0x2030 1 0x2030 0x2030 position NodePtr OldNodePtr

5/2/2020 CSE1303 Part A

0x30a8

36

Deleting – Middle Node

Head

0x4000 1 0x2030 0x2030 position NodePtr OldNodePtr

5/2/2020 CSE1303 Part A

0x30a8

37

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

.

5/2/2020 CSE1303 Part A 38

0

Double Linked List

1 2 3 4 5/2/2020 Current CSE1303 Part A 39

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;

5/2/2020 CSE1303 Part A 40

Insert at end

0x4000 0x3080 0x2030 0x2000 0x3080 NULL 0x2030 0x4000 NULL 0x3080

currentPtr 5/2/2020

NULL NULL 0x2030 prevPtr newPtr

CSE1303 Part A

0x2000

41

Insert at end

0x4000 0x3080 0x2030 0x2000 0x3080 NULL 0x2030 0x4000 0x2000 0x3080

currentPtr 5/2/2020

NULL NULL 0x2030 prevPtr newPtr

CSE1303 Part A

0x2000

42

Insert at end

0x4000 0x3080 0x2030 0x2000 0x3080 NULL 0x2030 0x4000 0x2000 0x3080

currentPtr 5/2/2020

NULL 0x2030 0x2030 prevPtr newPtr

CSE1303 Part A

0x2000

43

0x4000

Insert inside the list

0x3080 0x2030 0x3080 NULL 0x2030 0x4000

currentPtr

0x3080 prevPtr 0x2000 NULL NULL

5/2/2020 CSE1303 Part A

NULL 0x3080 0x2000 newPtr

44

0x4000

Insert inside the list

0x3080 0x2030 0x3080 NULL 0x2030 0x4000

currentPtr

0x3080 prevPtr 0x2000 2030 NULL

5/2/2020 CSE1303 Part A

NULL 0x3080 0x2000 newPtr

45

0x4000

Insert inside the list

0x3080 0x2030 0x3080 NULL 0x2030 0x4000

currentPtr

0x3080 prevPtr 0x2000 2030 3080

5/2/2020 CSE1303 Part A

NULL 0x3080 0x2000 newPtr

46

0x4000

Insert inside the list

0x3080 0x2030 0x3080 NULL 0x2030 0x4000

currentPtr

0x3080 prevPtr 0x2000 2030 3080

5/2/2020 CSE1303 Part A

NULL 0x2000 0x2000 newPtr

47

0x4000

Insert inside the list

0x3080 0x2030 0x3080 NULL 0x2000 0x4000

currentPtr

0x3080 prevPtr 0x2000 2030 3080

5/2/2020 CSE1303 Part A

NULL 0x2000 0x2000 newPtr

48

Delete from end

0x4000 0x3080 0x2030 0x3080 NULL 0x2030 0x4000 NULL 0x3080

currentPtr 5/2/2020

oldPtr prevPtr 0x3080

CSE1303 Part A

0x2030

49

Delete from end

0x4000 0x3080 0x2030 0x3080 NULL NULL 0x4000 NULL 0x3080

currentPtr 5/2/2020

oldPtr prevPtr 0x3080

CSE1303 Part A

0x2030

50

Delete from end

0x4000 0x3080 0x3080 NULL NULL 0x4000

currentPtr 5/2/2020

oldPtr prevPtr 0x3080

CSE1303 Part A

0x2030

51

Delete from inside list

0x4000 0x3080 0x2030 0x3080 NULL 0x2030 0x4000 NULL 0x3080

currentPtr 5/2/2020

oldPtr prevPtr 0x4000

CSE1303 Part A

0x3080

52

Delete from inside list

0x4000 0x3080 0x2030 0x2030 NULL 0x2030 0x4000 NULL 0x3080

currentPtr 5/2/2020

oldPtr prevPtr 0x4000

CSE1303 Part A

0x3080

53

Delete from inside list

0x4000 0x3080 0x2030 0x2030 NULL 0x2030 0x4000 NULL 0x4000

currentPtr 5/2/2020

oldPtr prevPtr 0x4000

CSE1303 Part A

0x3080

54

0x4000 0x2030 NULL

Delete from inside list

0x2030 NULL 0x4000 oldPtr prevPtr 0x4000

CSE1303 Part A

0x3080

5/2/2020 55

Revision

• Linked List.

• Benefits of different implementations.

• Double Linked List.

Preparation

• Read Chapter 6 in Kruse et al.

5/2/2020 CSE1303 Part A 56