Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists.

Similar presentations


Presentation on theme: "Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists."— Presentation transcript:

1 Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists

2 2 Overview Operations for Lists. Implementation of Linked Lists. Double Linked Lists.

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

4 4 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 5 Linked List Head 0123

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

7 7 void initializeList(List* listPtr) { listPtr->headPtr = NULL; listPtr->count = 0; } Initialize List count headPtr 0 NULL listPtr addr of list

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

9 9 Node* setPosition(const List* listPtr, int position) { int i; Node* nodePtr = listPtr->headPtr; if (position = listPtr->count) { fprintf(stderr, “Invalid position\n”); exit(1); } else { for (i = 0; i < position; i++) { nodePtr = nodePtr->nextPtr; } return nodePtr; }

10 10 Set Position Head 021 2 position

11 11 Set Position Head 2 position 0x4000 0 i nodePtr 0x30a80x20300x4000

12 12 Set Position Head 2 position 0x2030 1 i nodePtr 0x30a80x20300x4000

13 13 Set Position Head 2 position 0x30a8 2 i nodePtr 0x30a80x20300x4000

14 14 Insert Head 021 If we insert it at position 0. Head 102

15 15 Insert Head 021 If we insert it at position 0. 0213 Head

16 16 Instead, suppose we insert it at position 1. Head 02 1

17 17 Head Instead, suppose we insert it at position 1. 032 1

18 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++; }

19 19 Inserting – New List Head 0 position 0x30a8 NULL newNodePtr 0x30a8

20 20 Inserting – New List Head 0 position 0x30a8 newNodePtr 0x30a8

21 21 Inserting – Start of List Head 0x30a8 0x2008 0x2000 0 position 0x2000 newNodePtr

22 22 Inserting – Start of List Head 0x30a8 0x2008 0x2000 0 position 0x2000 newNodePtr

23 23 Inserting – Start of List Head 0x30a8 0x2008 0x2000 0 position 0x2000 newNodePtr

24 24 Inserting – Inside the List 0x2000 1 position 0x2000 newNodePtr Head 0x3050 0x3080 nodePtr 0x3080

25 25 Inserting – Inside the List 0x2000 1 position 0x2000 newNodePtr Head 0x3050 0x3080 nodePtr 0x3080

26 26 Inserting – Inside the List 0x2000 1 position 0x2000 newNodePtr Head 0x3050 0x3080 nodePtr 0x3080

27 27 Delete 2 Head 310 If we delete the Node at position 0. Head 210

28 28 Head If we delete the Node at position 2. 2310

29 29 Head If we delete the Node at position 2. 2210

30 30 void deleteNode(List* listPtr, int position) { Node* oldNodePtr = NULL; Node* nodePtr = NULL; if (listPtr->count > 0 && position 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 31 Deleting – 1 st Node Head 0 position 0x4000 nodePtr 0x30a80x20300x4000

32 32 Head 0 position 0x4000 nodePtr 0x30a80x20300x4000 Deleting – 1 st Node

33 33 Head 0 position 0x4000 nodePtr 0x30a80x2030 Deleting – 1 st Node

34 34 Deleting – Middle Node Head 1 position 0x2030 OldNodePtr 0x30a80x20300x4000 0x2030 nodePtr

35 35 Head 0x30a80x20300x4000 Deleting – Middle Node 1 position 0x2030 OldNodePtr 0x2030 nodePtr

36 36 Head 0x30a80x4000 Deleting – Middle Node 1 position 0x2030 OldNodePtr 0x2030 nodePtr

37 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. in both directionsTraverse a list, in both directions.

38 38 Double Linked List Current 01234

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

40 40 Insert at end currentPtr newPtr 0x40000x30800x20300x2000 0x40000x3080 0x2030 NULL 0x2000 prevPtr 0x2030

41 41 Insert at end newPtr 0x40000x30800x20300x2000 0x40000x3080 0x2030 0x2000 NULL 0x2000 prevPtr 0x2030 currentPtr

42 42 Insert at end newPtr 0x40000x30800x20300x2000 0x40000x3080 0x2030 0x2000 NULL 0x2030 0x2000 prevPtr 0x2030 currentPtr

43 43 Insert inside the list newPtr 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 0x2000 prevPtr 0x3080 currentPtr

44 44 Insert inside the list newPtr 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 2030 NULL 0x2000 prevPtr 0x3080 currentPtr

45 45 Insert inside the list newPtr 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 2030 3080 0x2000 prevPtr 0x3080 currentPtr

46 46 Insert inside the list newPtr 0x40000x3080 0x2030 0x2000 0x4000 0x2000 0x30800x2030 NULL 2030 3080 0x2000 prevPtr 0x3080 currentPtr

47 47 Insert inside the list newPtr 0x40000x3080 0x2030 0x2000 0x4000 0x2000 0x30800x2000 NULL 2030 3080 0x2000 prevPtr 0x3080 currentPtr

48 48 Delete from end oldPtr 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x2030 currentPtr prevPtr 0x3080

49 49 Delete from end 0x40000x30800x2030 0x40000x3080 NULL oldPtr 0x2030 currentPtr prevPtr 0x3080

50 50 Delete from end 0x40000x3080 0x4000 0x3080NULL oldPtr 0x2030 currentPtr prevPtr 0x3080

51 51 Delete from inside list 0x40000x30800x2030 0x40000x3080 0x2030 NULL oldPtr 0x3080 currentPtr prevPtr 0x4000

52 52 Delete from inside list 0x40000x30800x2030 0x40000x3080 0x2030 NULL oldPtr 0x3080 currentPtr prevPtr 0x4000

53 53 Delete from inside list 0x40000x30800x2030 0x4000 0x2030 NULL oldPtr 0x3080 currentPtr prevPtr 0x4000

54 54 Delete from inside list Head 0x40000x2030 0x4000 0x2030 NULL oldPtr 0x3080 currentPtr prevPtr 0x4000

55 55 Revision Linked List. Benefits of different implementations. Double Linked List. Preparation Read Chapter 6 in Kruse et al.

56 56 Revision: Reading Kruse 5.2.2 - 5.2.5 Standish 2.4 – 2.5 Preparation Next lecture: Elementary Algorithms Read Chapter 6 in Kruse et al.


Download ppt "Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists."

Similar presentations


Ads by Google