Linked Lists CSE1303 Part A Data Structures and Algorithms
Download
Report
Transcript Linked Lists CSE1303 Part A Data Structures and Algorithms
Linked Lists
CSE1303 Part A
Data Structures and Algorithms
Overview
• Operations for Lists.
• Implementation of Linked Lists.
• Double Linked Lists.
5/27/2016
CSE1303 Part A
2
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.
5/27/2016
CSE1303 Part A
3
Linked List
Head
0
5/27/2016
1
2
CSE1303 Part A
3
4
#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
5
Initialize List
listPtr
addr of list
count
headPtr
0
NULL
void initializeList(List* listPtr)
{
listPtr->headPtr = NULL;
listPtr->count = 0;
}
5/27/2016
CSE1303 Part A
6
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
5/27/2016
CSE1303 Part A
7
Set Position
Head
0
2
5/27/2016
1
2
position
CSE1303 Part A
8
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;
}
5/27/2016
CSE1303 Part A
9
Set Position
Head
0x4000
2
position
0
i
0x4000
5/27/2016
0x2030
0x30a8
NodePtr
CSE1303 Part A
10
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;
}
5/27/2016
CSE1303 Part A
11
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;
}
5/27/2016
CSE1303 Part A
12
Set Position
Head
0x4000
2
position
0
i
0x4000
5/27/2016
0x2030
0x30a8
NodePtr
CSE1303 Part A
13
Set Position
Head
0x4000
2
position
1
i
0x2030
5/27/2016
0x2030
0x30a8
NodePtr
CSE1303 Part A
14
Set Position
Head
0x4000
2
position
2
i
0x30a8
5/27/2016
0x2030
0x30a8
NodePtr
CSE1303 Part A
15
Insert
Head
0
1
2
If we insert it at position 0.
0
1
2
Head
5/27/2016
CSE1303 Part A
16
Insert
Head
0
1
2
If we insert it at position 0.
0
1
2
3
Head
5/27/2016
CSE1303 Part A
17
Instead, suppose we insert it at position 1.
Head
1
0
5/27/2016
CSE1303 Part A
2
18
Instead, suppose we insert it at position 1.
Head
0
2
3
1
5/27/2016
CSE1303 Part A
19
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++;
}
5/27/2016
CSE1303 Part A
20
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++;
}
5/27/2016
CSE1303 Part A
21
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++;
}
5/27/2016
CSE1303 Part A
22
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++;
}
5/27/2016
CSE1303 Part A
23
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++;
}
5/27/2016
CSE1303 Part A
24
Inserting – New List
Head
0
0x30a8
5/27/2016
0x30a8
NULL
position
newNodePtr
CSE1303 Part A
25
Inserting – New List
0x30a8
Head
0
0x30a8
5/27/2016
position
newNodePtr
CSE1303 Part A
26
Inserting – Start of List
Head
0x2008
0x30a8
0x2000
newNodePtr
0x2000
position
0
5/27/2016
CSE1303 Part A
27
Inserting – Start of List
Head
0x2008
0x30a8
0x2000
newNodePtr
0x2000
position
0
5/27/2016
CSE1303 Part A
28
Inserting – Start of List
Head
0x2008
0x30a8
0x2000
newNodePtr
0x2000
position
0
5/27/2016
CSE1303 Part A
29
Inserting – Inside the List
Head
0x3080
0x3050
0x2000
newNodePtr
0x2000
position
0
5/27/2016
CSE1303 Part A
30
Inserting – Inside the List
Head
0x3080
0x3050
0x2000
newNodePtr
0x2000
position
0
5/27/2016
CSE1303 Part A
31
Inserting – Inside the List
Head
0x3080
0x3050
0x2000
newNodePtr
0x2000
position
0
5/27/2016
CSE1303 Part A
32
Head
Delete
0
1
2
3
If we delete the Node at position 0.
0
1
2
Head
5/27/2016
CSE1303 Part A
33
If we delete the Node at position 2.
Head
0
5/27/2016
1
2
CSE1303 Part A
3
34
If we delete the Node at position 2.
Head
0
5/27/2016
1
2
CSE1303 Part A
2
35
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);
}
else {
fprintf(stderr, “List is empty or invalid position.\n”);
exit(1);
}
}
36
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);
}
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->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);
}
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->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);
}
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->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);
}
else {
fprintf(stderr, “List is empty or invalid position.\n”);
exit(1);
}
}
40
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/27/2016
CSE1303 Part A
41
Deleting – 1st Node
Head
0x4000
0
position
0x4000
NodePtr
5/27/2016
0x2030
CSE1303 Part A
0x30a8
42
Deleting – 1st Node
0x4000
0
position
0x4000
NodePtr
5/27/2016
0x2030
0x30a8
Head
CSE1303 Part A
43
Deleting – 1st Node
0x2030
0
position
0x4000
NodePtr
5/27/2016
0x30a8
Head
CSE1303 Part A
44
Deleting – Middle Node
Head
0x4000
0
position
0x2030
NodePtr
5/27/2016
0x2030
CSE1303 Part A
0x30a8
45
Deleting – Middle Node
Head
0x4000
0
position
0x2030
NodePtr
5/27/2016
0x2030
CSE1303 Part A
0x30a8
46
Deleting – Middle Node
Head
0x4000
0
position
0x2030
NodePtr
5/27/2016
0x30a8
CSE1303 Part A
47
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.
5/27/2016
CSE1303 Part A
48
Double Linked List
0
1
2
3
4
Current
5/27/2016
CSE1303 Part A
49
struct DoubleLinkNodeRec
{
float
struct DoubleLinkNodeRec*
struct DoubleLinkNodeRec*
};
value;
nextPtr;
previousPtr;
typedef struct DoubleLinkNodeRec Node;
struct DoubleLinkListRec
{
int
count;
Node*
currentPtr;
int
position;
};
typedef struct DoubleLinkListRec DoubleLinkList;
5/27/2016
CSE1303 Part A
50
Insert at end
0x4000
0x3080
NULL
0x3080
0x2030
0x4000
0x2030
0x2000
NULL
NULL
0x3080
NULL
0x2030
Head
5/27/2016
prevPtr
CSE1303 Part A
newPtr
0x2000
51
Insert at end
0x4000
0x3080
NULL
0x3080
0x2030
0x4000
0x2030
0x2000
0x2000
NULL
0x3080
NULL
0x2030
Head
5/27/2016
prevPtr
CSE1303 Part A
newPtr
0x2000
52
Insert at end
0x4000
0x3080
NULL
0x3080
0x2030
0x4000
0x2030
0x2000
0x2000
NULL
0x3080
0x2030
0x2030
Head
5/27/2016
prevPtr
CSE1303 Part A
newPtr
0x2000
53
Insert inside the list
0x4000
0x3080
NULL
0x3080
0x2030
0x2030
NULL
0x4000
0x3080
0x2000
0x3080
Head
0x2000
prevPtr
NULL
newPtr
NULL
5/27/2016
CSE1303 Part A
54
Insert inside the list
0x4000
0x3080
NULL
0x3080
0x2030
0x2030
NULL
0x4000
0x3080
0x2000
0x3080
Head
0x2000
prevPtr
2030
newPtr
NULL
5/27/2016
CSE1303 Part A
55
Insert inside the list
0x4000
0x3080
NULL
0x3080
0x2030
0x2030
NULL
0x4000
0x3080
0x2000
0x3080
Head
0x2000
prevPtr
2030
newPtr
3080
5/27/2016
CSE1303 Part A
56
Insert inside the list
0x4000
0x3080
NULL
0x3080
0x2030
0x2030
NULL
0x4000
0x2000
0x2000
0x3080
Head
0x2000
prevPtr
2030
newPtr
3080
5/27/2016
CSE1303 Part A
57
Insert inside the list
0x4000
0x3080
NULL
0x3080
0x2030
0x2000
NULL
0x4000
0x2000
0x2000
0x3080
Head
0x2000
prevPtr
2030
newPtr
3080
5/27/2016
CSE1303 Part A
58
Delete from end
0x4000
0x3080
NULL
Head
5/27/2016
0x3080
0x2030
0x2030
NULL
0x4000
0x3080
oldPtr
0x2030
CSE1303 Part A
59
Delete from end
0x4000
0x3080
0x2030
0x3080
NULL
NULL
0x4000
0x3080
NULL
Head
5/27/2016
oldPtr
0x2030
CSE1303 Part A
60
Delete from end
0x4000
0x3080
0x3080
NULL
NULL
Head
5/27/2016
0x4000
oldPtr
0x2030
CSE1303 Part A
61
Delete from inside list
0x4000
0x3080
NULL
Head
5/27/2016
0x3080
0x2030
0x4000
0x3080
0x2030
NULL
0x3080
oldPtr
CSE1303 Part A
62
Delete from inside list
0x4000
0x2030
NULL
Head
5/27/2016
0x3080
0x2030
0x4000
0x3080
0x2030
NULL
0x3080
oldPtr
CSE1303 Part A
63
Delete from inside list
0x4000
0x2030
NULL
Head
5/27/2016
0x3080
0x2030
0x4000
0x3080
0x2030
NULL
0x4000
oldPtr
CSE1303 Part A
64
Delete from inside list
0x4000
0x2030
0x2030
NULL
NULL
Head
5/27/2016
0x4000
0x3080
oldPtr
CSE1303 Part A
65
Revision
• Linked List.
• Benefits of different implementations.
• Double Linked List.
Preparation
• Read Chapter 6 in Kruse et al.
5/27/2016
CSE1303 Part A
66