Download presentation
Presentation is loading. Please wait.
1
CSE1303 Part A Data Structures and Algorithms 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 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
5 Linked List headPtr 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 headPtr 021 2 position 0x30a80x20300x4000 0 i nodePtr
11
11 Set Position 2 position 0x2030 1 i nodePtr 0x30a80x20300x4000 021 headPtr
12
12 Set Position 2 position 0x30a8 2 i nodePtr 0x30a80x20300x4000 021 headPtr
13
13 Insert 021 If we insert it at position 0. headPtr newNodePtr 103 headPtr 2
14
14 Instead, suppose we insert it at position 1. 02 1 headPtr newNodePtr 0 3 1 headPtr 2 newNodePtr
15
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
16 Inserting – New List headPtr 0 position 0x30a8 NULL newNodePtr 0x30a8
17
17 Inserting – New List 0 position 0x30a8 newNodePtr 0x30a8 headPtr
18
18 Inserting – Start of List 0x30a8 0x2008 0x2000 0 position 0x2000 newNodePtr headPtr
19
19 Inserting – Start of List 0x30a8 0x2008 0x2000 0 position 0x2000 newNodePtr headPtr
20
20 Inserting – Start of List 0x30a8 0x2008 0x2000 0 position 0x2000 newNodePtr headPtr
21
21 Inserting – Inside the List 0x2000 1 position 0x2000 newNodePtr 0x3050 0x3080 prevPtr 0x3080 headPtr
22
22 Inserting – Inside the List 0x2000 1 position 0x2000 newNodePtr 0x3050 0x3080 prevPtr 0x3080 headPtr
23
23 Inserting – Inside the List 0x2000 1 position 0x2000 newNodePtr 0x3050 0x3080 prevPtr 0x3080 headPtr
24
24Delete 2310 If we delete the Node at position 0. headPtr 210
25
25 If we delete the Node at position 2. headPtr 2310 210 oldNodePtr
26
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
27 Deleting – 1 st Node 0 position 0x4000 oldNodePtr 0x30a80x20300x4000 headPtr
28
28 0 position 0x4000 oldNodePtr 0x30a80x20300x4000 Deleting – 1 st Node headPtr
29
29 0 position 0x4000 oldNodePtr 0x30a80x2030 Deleting – 1 st Node headPtr
30
30 Deleting – Middle Node 1 position 0x2030 oldNodePtr 0x30a80x20300x4000 0x2030 prevPtr headPtr
31
31 0x30a80x20300x4000 Deleting – Middle Node 1 position 0x2030 oldNodePtr 0x2030 prevPtr headPtr
32
32 0x30a80x4000 Deleting – Middle Node 1 position 0x2030 oldNodePtr 0x2030 prevPtr headPtr
33
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
34 Double Linked List currentPtr 01234
35
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
36 Insert at end currentPtr newNodePtr 0x40000x30800x20300x2000 0x40000x3080 0x2030 NULL 0x2000 prevPtr 0x2030
37
37 Insert at end 0x40000x30800x20300x2000 0x40000x3080 0x2030 0x2000 NULL 0x2000 prevPtr 0x2030 currentPtr newNodePtr
38
38 Insert at end 0x40000x30800x20300x2000 0x40000x3080 0x2030 0x2000 NULL 0x2030 0x2000 prevPtr 0x2030 currentPtr newNodePtr
39
39 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 0x2000 prevPtr 0x3080 currentPtr newNodePtr
40
40 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 2030 NULL 0x2000 prevPtr 0x3080 currentPtr newNodePtr
41
41 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 2030 3080 0x2000 prevPtr 0x3080 currentPtr newNodePtr
42
42 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x2000 0x30800x2030 NULL 2030 3080 0x2000 prevPtr 0x3080 currentPtr newNodePtr
43
43 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x2000 0x30800x2000 NULL 2030 3080 0x2000 prevPtr 0x3080 currentPtr newNodePtr
44
44 Delete from end oldNodePtr 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x2030 currentPtr prevPtr 0x3080
45
45 Delete from end 0x40000x30800x2030 0x40000x3080 NULL 0x2030 currentPtr prevPtr 0x3080 oldNodePtr
46
46 Delete from end 0x40000x3080 0x4000 0x3080NULL 0x2030 currentPtr prevPtr 0x3080 oldNodePtr
47
47 Delete from inside list 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr
48
48 Delete from inside list 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr
49
49 Delete from inside list 0x40000x30800x2030 0x4000 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr
50
50 Delete from inside list 0x40000x2030 0x4000 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr
51
51Revision Linked List. Benefits of different implementations. Double Linked List. 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.