Linked Lists - PowerPoint PPT Presentation

1 / 66
About This Presentation
Title:

Linked Lists

Description:

Delete an item from a position in the list. Retrieve an item from a position. ... void insertItem(List* listPtr, float item, int position) ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 67
Provided by: DavidAl87
Category:
Tags: linked | list | lists

less

Transcript and Presenter's Notes

Title: Linked Lists


1
Linked Lists
  • CSE1303 Part A
  • Data Structures and Algorithms

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
Linked List
Head
0
1
2
3
5
ifndef LINKEDLISTH define LINKEDLISTH include
ltstdbool.hgt 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
0
headPtr
NULL
void initializeList(List listPtr)
listPtr-gtheadPtr NULL listPtr-gtcount 0
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

8
Set Position
Head
0
2
1
2
position
9
Node setPosition(const List listPtr, int
position) int i Node nodePtr
listPtr-gtheadPtr if (position lt 0 position
gt listPtr-gtcount) fprintf(stderr,
Invalid position\n) exit(1) else
for (i 0 i lt position i) nodePtr
nodePtr-gtnextPtr return
nodePtr
10
Set Position
Head
0x30a8
0x2030
0x4000
2
position
0
i
0x4000
NodePtr
11
Node setPosition(const List listPtr, int
position) int i Node nodePtr
listPtr-gtheadPtr if (position lt 0 position
gt listPtr-gtcount) fprintf(stderr,
Invalid position\n) exit(1) else
for (i 0 i lt position i) nodePtr
nodePtr-gtnextPtr return
nodePtr
12
Node setPosition(const List listPtr, int
position) int i Node nodePtr
listPtr-gtheadPtr if (position lt 0 position
gt listPtr-gtcount) fprintf(stderr,
Invalid position\n) exit(1) else
for (i 0 i lt position i) nodePtr
nodePtr-gtnextPtr return
nodePtr
13
Set Position
Head
0x30a8
0x2030
0x4000
2
position
0
i
0x4000
NodePtr
14
Set Position
Head
0x30a8
0x2030
0x4000
2
position
1
i
0x2030
NodePtr
15
Set Position
Head
0x30a8
0x2030
0x4000
2
position
2
i
0x30a8
NodePtr
16
Insert
Head
0
2
1
If we insert it at position 0.
1
0
2
Head
17
Insert
Head
0
2
1
If we insert it at position 0.
18
Instead, suppose we insert it at position 1.
Head
1
0
2
19
Instead, suppose we insert it at position 1.
Head
20
void insertItem(List listPtr, float item, int
position) Node newNodePtr
makeNode(item) Node nodePtr NULL if
(position 0) newNodePtr-gtnextPtr
listPtr-gtheadPtr listPtr-gtheadPtr
newNodePtr else nodePtr
setPosition(listPtr, position-1)
newNodePtr-gtnextPtr nodePtr-gtnextPtr
nodePtr-gtnextPtr newNodePtr
listPtr-gtcount
21
void insertItem(List listPtr, float item, int
position) Node newNodePtr
makeNode(item) Node nodePtr NULL if
(position 0) newNodePtr-gtnextPtr
listPtr-gtheadPtr listPtr-gtheadPtr
newNodePtr else nodePtr
setPosition(listPtr, position-1)
newNodePtr-gtnextPtr nodePtr-gtnextPtr
nodePtr-gtnextPtr newNodePtr
listPtr-gtcount
22
void insertItem(List listPtr, float item, int
position) Node newNodePtr
makeNode(item) Node nodePtr NULL if
(position 0) newNodePtr-gtnextPtr
listPtr-gtheadPtr listPtr-gtheadPtr
newNodePtr else nodePtr
setPosition(listPtr, position-1)
newNodePtr-gtnextPtr nodePtr-gtnextPtr
nodePtr-gtnextPtr newNodePtr
listPtr-gtcount
23
void insertItem(List listPtr, float item, int
position) Node newNodePtr
makeNode(item) Node nodePtr NULL if
(position 0) newNodePtr-gtnextPtr
listPtr-gtheadPtr listPtr-gtheadPtr
newNodePtr else nodePtr
setPosition(listPtr, position-1)
newNodePtr-gtnextPtr nodePtr-gtnextPtr
nodePtr-gtnextPtr newNodePtr
listPtr-gtcount
24
void insertItem(List listPtr, float item, int
position) Node newNodePtr
makeNode(item) Node nodePtr NULL if
(position 0) newNodePtr-gtnextPtr
listPtr-gtheadPtr listPtr-gtheadPtr
newNodePtr else nodePtr
setPosition(listPtr, position-1)
newNodePtr-gtnextPtr nodePtr-gtnextPtr
nodePtr-gtnextPtr newNodePtr
listPtr-gtcount
25
Inserting New List
0x30a8
Head
NULL
0
position
0x30a8
newNodePtr
26
Inserting New List
0x30a8
Head
0
position
0x30a8
newNodePtr
27
Inserting Start of List
Head
Head
0x30a8
0x2008
0x2000
0x2000
newNodePtr
0
position
28
Inserting Start of List
Head
Head
0x30a8
0x2008
0x2000
0x2000
newNodePtr
0
position
29
Inserting Start of List
Head
Head
0x30a8
0x2008
0x2000
0x2000
newNodePtr
0
position
30
Inserting Inside the List
Head
0x3080
0x3050
0x2000
0x2000
newNodePtr
0
position
31
Inserting Inside the List
Head
0x3080
0x3050
0x2000
0x2000
newNodePtr
0
position
32
Inserting Inside the List
Head
0x3080
0x3050
0x2000
0x2000
newNodePtr
0
position
33
Delete
Head
2
3
1
0
If we delete the Node at position 0.
34
If we delete the Node at position 2.
Head
2
3
1
0
35
If we delete the Node at position 2.
Head
2
36
void deleteNode(List listPtr, int position)
Node oldNodePtr NULL Node nodePtr
NULL if (listPtr-gtcount gt 0 position lt
listPtr-gtcount) if (position 0)
oldNodePtr listPtr-gtheadPtr
listPtr-gtheadPtr oldNodePtr-gtnextPtr
else nodePtr setPosition(listPtr,
position - 1) oldNodePtr
nodePtr-gtnextPtr nodePtr-gtnextPtr
oldNodePtr-gtnextPtr
listPtr-gtcount-- free(oldNodePtr)
else fprintf(stderr, List is empty or
invalid position.\n) exit(1)
37
void deleteNode(List listPtr, int position)
Node oldNodePtr NULL Node nodePtr
NULL if (listPtr-gtcount gt 0 position lt
listPtr-gtcount) if (position 0)
oldNodePtr listPtr-gtheadPtr
listPtr-gtheadPtr oldNodePtr-gtnextPtr
else nodePtr setPosition(listPtr,
position - 1) oldNodePtr
nodePtr-gtnextPtr nodePtr-gtnextPtr
oldNodePtr-gtnextPtr
listPtr-gtcount-- free(oldNodePtr)
else fprintf(stderr, List is empty or
invalid position.\n) exit(1)
38
void deleteNode(List listPtr, int position)
Node oldNodePtr NULL Node nodePtr
NULL if (listPtr-gtcount gt 0 position lt
listPtr-gtcount) if (position 0)
oldNodePtr listPtr-gtheadPtr
listPtr-gtheadPtr oldNodePtr-gtnextPtr
else nodePtr setPosition(listPtr,
position - 1) oldNodePtr
nodePtr-gtnextPtr nodePtr-gtnextPtr
oldNodePtr-gtnextPtr
listPtr-gtcount-- free(oldNodePtr)
else fprintf(stderr, List is empty or
invalid position.\n) exit(1)
39
void deleteNode(List listPtr, int position)
Node oldNodePtr NULL Node nodePtr
NULL if (listPtr-gtcount gt 0 position lt
listPtr-gtcount) if (position 0)
oldNodePtr listPtr-gtheadPtr
listPtr-gtheadPtr oldNodePtr-gtnextPtr
else nodePtr setPosition(listPtr,
position - 1) oldNodePtr
nodePtr-gtnextPtr nodePtr-gtnextPtr
oldNodePtr-gtnextPtr
listPtr-gtcount-- free(oldNodePtr)
else fprintf(stderr, List is empty or
invalid position.\n) exit(1)
40
void deleteNode(List listPtr, int position)
Node oldNodePtr NULL Node nodePtr
NULL if (listPtr-gtcount gt 0 position lt
listPtr-gtcount) if (position 0)
oldNodePtr listPtr-gtheadPtr
listPtr-gtheadPtr oldNodePtr-gtnextPtr
else nodePtr setPosition(listPtr,
position - 1) oldNodePtr
nodePtr-gtnextPtr nodePtr-gtnextPtr
oldNodePtr-gtnextPtr
listPtr-gtcount-- free(oldNodePtr)
else fprintf(stderr, List is empty or
invalid position.\n) exit(1)
41
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

42
Deleting 1st Node
Head
0x30a8
0x2030
0x4000
0
position
0x4000
NodePtr
43
Deleting 1st Node
0x30a8
0x2030
0x4000
Head
0
position
0x4000
NodePtr
44
Deleting 1st Node
0x30a8
0x2030
Head
0
position
0x4000
NodePtr
45
Deleting Middle Node
Head
0x30a8
0x2030
0x4000
0
position
0x2030
NodePtr
46
Deleting Middle Node
Head
0x30a8
0x2030
0x4000
0
position
0x2030
NodePtr
47
Deleting Middle Node
Head
0x30a8
0x4000
0
position
0x2030
NodePtr
48
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.

49
Double Linked List
0
1
2
3
4
Current
50
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
51
Insert at end
0x4000
0x3080
0x2030
0x2000
0x3080
0x2030
NULL
NULL
NULL
0x4000
0x3080
NULL
0x2030
Head
prevPtr
newPtr
0x2000
52
Insert at end
0x4000
0x3080
0x2030
0x2000
0x3080
0x2030
0x2000
NULL
NULL
0x4000
0x3080
NULL
0x2030
Head
prevPtr
newPtr
0x2000
53
Insert at end
0x4000
0x3080
0x2030
0x2000
0x3080
0x2030
0x2000
NULL
NULL
0x4000
0x3080
0x2030
0x2030
Head
prevPtr
newPtr
0x2000
54
Insert inside the list
0x4000
0x3080
0x2030
0x3080
0x2030
NULL
NULL
0x4000
0x3080
0x2000
0x3080
Head
0x2000
prevPtr
NULL
newPtr
NULL
55
Insert inside the list
0x4000
0x3080
0x2030
0x3080
0x2030
NULL
NULL
0x4000
0x3080
0x2000
0x3080
Head
0x2000
prevPtr
2030
newPtr
NULL
56
Insert inside the list
0x4000
0x3080
0x2030
0x3080
0x2030
NULL
NULL
0x4000
0x3080
0x2000
0x3080
Head
0x2000
prevPtr
2030
newPtr
3080
57
Insert inside the list
0x4000
0x3080
0x2030
0x3080
0x2030
NULL
NULL
0x4000
0x2000
0x2000
0x3080
Head
0x2000
prevPtr
2030
newPtr
3080
58
Insert inside the list
0x4000
0x3080
0x2030
0x3080
0x2000
NULL
NULL
0x4000
0x2000
0x2000
0x3080
Head
0x2000
prevPtr
2030
newPtr
3080
59
Delete from end
0x4000
0x3080
0x2030
0x3080
0x2030
NULL
NULL
0x4000
0x3080
Head
oldPtr
0x2030
60
Delete from end
0x4000
0x3080
0x2030
0x3080
NULL
NULL
NULL
0x4000
0x3080
Head
oldPtr
0x2030
61
Delete from end
0x4000
0x3080
0x3080
NULL
NULL
0x4000
Head
oldPtr
0x2030
62
Delete from inside list
0x4000
0x3080
0x2030
0x3080
0x2030
NULL
NULL
0x4000
0x3080
Head
oldPtr
0x3080
63
Delete from inside list
0x4000
0x3080
0x2030
0x2030
0x2030
NULL
NULL
0x4000
0x3080
Head
oldPtr
0x3080
64
Delete from inside list
0x4000
0x3080
0x2030
0x2030
0x2030
NULL
NULL
0x4000
0x4000
Head
oldPtr
0x3080
65
Delete from inside list
0x4000
0x2030
0x2030
NULL
NULL
0x4000
Head
oldPtr
0x3080
66
Revision
  • Linked List.
  • Benefits of different implementations.
  • Double Linked List.

Preparation
  • Read Chapter 6 in Kruse et al.
Write a Comment
User Comments (0)
About PowerShow.com