Download presentation
Presentation is loading. Please wait.
1
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
Comparsion Linked Storage Contiguous 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 Head 1 2 3
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
7
Initialize List listPtr addr of list count headPtr NULL
headPtr NULL void initializeList(List* listPtr) { listPtr->headPtr = NULL; listPtr->count = 0; }
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 < 0 || 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 Head 1 2 2 position
11
Set Position Head 0x4000 0x2030 0x30a8 2 position i 0x4000 nodePtr
12
Set Position Head 0x4000 0x2030 0x30a8 2 position 1 i 0x2030 nodePtr
13
Set Position Head 0x4000 0x2030 0x30a8 2 position 2 i 0x30a8 nodePtr
14
Insert Head 1 2 If we insert it at position 0. 1 2 Head
15
Insert Head 1 2 If we insert it at position 0. 2 1 3 Head
16
Instead, suppose we insert it at position 1.
Head 1 2
17
Instead, suppose we insert it at position 1.
Head 3 2 1
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
Inserting – New List Head NULL 0x30a8 position 0x30a8 newNodePtr
20
Inserting – New List Head 0x30a8 position 0x30a8 newNodePtr
21
Inserting – Start of List
Head Head 0x30a8 0x2008 0x2000 newNodePtr 0x2000 position
22
Inserting – Start of List
Head Head 0x30a8 0x2008 0x2000 newNodePtr 0x2000 position
23
Inserting – Start of List
Head Head 0x30a8 0x2008 0x2000 newNodePtr 0x2000 position
24
Inserting – Inside the List
Head 0x3080 0x3050 nodePtr 0x3080 0x2000 newNodePtr 0x2000 1 position
25
Inserting – Inside the List
Head 0x3080 0x3050 nodePtr 0x3080 0x2000 newNodePtr 0x2000 1 position
26
Inserting – Inside the List
Head 0x3080 0x3050 nodePtr 0x3080 0x2000 newNodePtr 0x2000 1 position
27
Delete Head 1 2 3 If we delete the Node at position 0. 2 1 Head
28
If we delete the Node at position 2.
Head 1 2 3
29
If we delete the Node at position 2.
Head 2 1 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); fprintf(stderr, “List is empty or invalid position.\n”); exit(1);
31
Deleting – 1st Node Head 0x4000 0x2030 0x30a8 position 0x4000 nodePtr
32
Deleting – 1st Node 0x4000 0x2030 0x30a8 position Head 0x4000 nodePtr
33
Deleting – 1st Node 0x2030 0x30a8 position Head 0x4000 nodePtr
34
Deleting – Middle Node Head 0x4000 0x2030 0x30a8 1 position 0x2030
nodePtr 0x2030 OldNodePtr
35
Deleting – Middle Node Head 0x4000 0x2030 0x30a8 1 position 0x2030
nodePtr 0x2030 OldNodePtr
36
Deleting – Middle Node Head 0x4000 0x30a8 1 position 0x2030 nodePtr
OldNodePtr
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.
38
Double Linked List 1 2 3 4 Current
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
Insert at end currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 0x2000
NULL NULL NULL 0x4000 0x3080 NULL 0x2030 currentPtr prevPtr newPtr 0x2000
41
Insert at end currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 0x2000
NULL NULL 0x4000 0x3080 NULL 0x2030 currentPtr prevPtr newPtr 0x2000
42
Insert at end currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 0x2000
NULL NULL 0x4000 0x3080 0x2030 0x2030 currentPtr prevPtr newPtr 0x2000
43
Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030
NULL NULL 0x4000 0x3080 0x2000 0x3080 currentPtr prevPtr 0x2000 NULL newPtr NULL
44
Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030
NULL NULL 0x4000 0x3080 0x2000 0x3080 currentPtr prevPtr 0x2000 2030 newPtr NULL
45
Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030
NULL NULL 0x4000 0x3080 0x2000 0x3080 currentPtr prevPtr 0x2000 2030 newPtr 3080
46
Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030
NULL NULL 0x4000 0x2000 0x2000 0x3080 currentPtr prevPtr 0x2000 2030 newPtr 3080
47
Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030
NULL NULL 0x4000 0x2000 0x2000 0x3080 currentPtr prevPtr 0x2000 2030 newPtr 3080
48
Delete from end currentPtr oldPtr prevPtr 0x4000 0x3080 0x2030 0x3080
NULL NULL 0x4000 0x3080 oldPtr 0x2030 currentPtr 0x3080 prevPtr
49
Delete from end currentPtr oldPtr prevPtr 0x4000 0x3080 0x2030 0x3080
NULL NULL NULL 0x4000 0x3080 oldPtr 0x2030 currentPtr 0x3080 prevPtr
50
Delete from end currentPtr oldPtr prevPtr 0x4000 0x3080 0x3080 NULL
51
Delete from inside list
0x4000 0x3080 0x2030 0x3080 0x2030 NULL NULL 0x4000 0x3080 oldPtr 0x3080 currentPtr 0x4000 prevPtr
52
Delete from inside list
0x4000 0x3080 0x2030 0x2030 0x2030 NULL NULL 0x4000 0x3080 oldPtr 0x3080 currentPtr 0x4000 prevPtr
53
Delete from inside list
0x4000 0x3080 0x2030 0x2030 0x2030 NULL NULL 0x4000 0x4000 oldPtr 0x3080 currentPtr 0x4000 prevPtr
54
Delete from inside list
0x4000 0x2030 0x2030 NULL NULL 0x4000 oldPtr 0x3080 currentPtr Head 0x4000 prevPtr
55
Elementary Algorithms
Next Elementary Algorithms
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.