CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists
2 Overview Operations for Lists. Implementation of Linked Lists. Double Linked Lists.
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 Comparison 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 Linked List headPtr 0123
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 void initializeList(List* listPtr) { listPtr->headPtr = NULL; listPtr->count = 0; } Initialize List count headPtr 0 NULL listPtr addr of list
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 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 Set Position headPtr position 0x30a80x20300x i nodePtr
11 Set Position 2 position 0x i nodePtr 0x30a80x20300x headPtr
12 Set Position 2 position 0x30a8 2 i nodePtr 0x30a80x20300x headPtr
13 Insert 021 If we insert it at position 0. headPtr newNodePtr 103 headPtr 2
14 Instead, suppose we insert it at position headPtr newNodePtr headPtr 2 newNodePtr
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++; }
16 Inserting – New List headPtr 0 position 0x30a8 NULL newNodePtr 0x30a8
17 Inserting – New List 0 position 0x30a8 newNodePtr 0x30a8 headPtr
18 Inserting – Start of List 0x30a8 0x2008 0x position 0x2000 newNodePtr headPtr
19 Inserting – Start of List 0x30a8 0x2008 0x position 0x2000 newNodePtr headPtr
20 Inserting – Start of List 0x30a8 0x2008 0x position 0x2000 newNodePtr headPtr
21 Inserting – Inside the List 0x position 0x2000 newNodePtr 0x3050 0x3080 prevPtr 0x3080 headPtr
22 Inserting – Inside the List 0x position 0x2000 newNodePtr 0x3050 0x3080 prevPtr 0x3080 headPtr
23 Inserting – Inside the List 0x position 0x2000 newNodePtr 0x3050 0x3080 prevPtr 0x3080 headPtr
24Delete 2310 If we delete the Node at position 0. headPtr 210
25 If we delete the Node at position 2. headPtr oldNodePtr
26 void deleteNode(List* listPtr, int position) { Node* oldNodePtr = NULL; Node* prevPtr = NULL; if (listPtr->count > 0 && position 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); }
27 Deleting – 1 st Node 0 position 0x4000 oldNodePtr 0x30a80x20300x4000 headPtr
28 0 position 0x4000 oldNodePtr 0x30a80x20300x4000 Deleting – 1 st Node headPtr
29 0 position 0x4000 oldNodePtr 0x30a80x2030 Deleting – 1 st Node headPtr
30 Deleting – Middle Node 1 position 0x2030 oldNodePtr 0x30a80x20300x4000 0x2030 prevPtr headPtr
31 0x30a80x20300x4000 Deleting – Middle Node 1 position 0x2030 oldNodePtr 0x2030 prevPtr headPtr
32 0x30a80x4000 Deleting – Middle Node 1 position 0x2030 oldNodePtr 0x2030 prevPtr headPtr
33 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.
34 Double Linked List currentPtr 01234
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;
36 Insert at end currentPtr newNodePtr 0x40000x30800x20300x2000 0x40000x3080 0x2030 NULL 0x2000 prevPtr 0x2030
37 Insert at end 0x40000x30800x20300x2000 0x40000x3080 0x2030 0x2000 NULL 0x2000 prevPtr 0x2030 currentPtr newNodePtr
38 Insert at end 0x40000x30800x20300x2000 0x40000x3080 0x2030 0x2000 NULL 0x2030 0x2000 prevPtr 0x2030 currentPtr newNodePtr
39 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 0x2000 prevPtr 0x3080 currentPtr newNodePtr
40 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 2030 NULL 0x2000 prevPtr 0x3080 currentPtr newNodePtr
41 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL x2000 prevPtr 0x3080 currentPtr newNodePtr
42 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x2000 0x30800x2030 NULL x2000 prevPtr 0x3080 currentPtr newNodePtr
43 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x2000 0x30800x2000 NULL x2000 prevPtr 0x3080 currentPtr newNodePtr
44 Delete from end oldNodePtr 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x2030 currentPtr prevPtr 0x3080
45 Delete from end 0x40000x30800x2030 0x40000x3080 NULL 0x2030 currentPtr prevPtr 0x3080 oldNodePtr
46 Delete from end 0x40000x3080 0x4000 0x3080NULL 0x2030 currentPtr prevPtr 0x3080 oldNodePtr
47 Delete from inside list 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr
48 Delete from inside list 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr
49 Delete from inside list 0x40000x30800x2030 0x4000 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr
50 Delete from inside list 0x40000x2030 0x4000 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr
51Revision Linked List. Benefits of different implementations. Double Linked List. Revision: Reading Kruse Standish 2.4 – 2.5 Preparation Next lecture: Elementary Algorithms Read Chapter 6 in Kruse et al.