Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views68 pages

08 Lists

The document provides an overview of linked lists, detailing their structure, operations, and comparisons with arrays. It covers types of linked lists such as singly, doubly, and multi-linked lists, as well as their applications. Additionally, it discusses node implementation, memory usage, and traversal methods within linked lists.

Uploaded by

機長小李
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views68 pages

08 Lists

The document provides an overview of linked lists, detailing their structure, operations, and comparisons with arrays. It covers types of linked lists such as singly, doubly, and multi-linked lists, as well as their applications. Additionally, it discusses node implementation, memory usage, and traversal methods within linked lists.

Uploaded by

機長小李
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Linked List

1. Linear Lists
2. List Operations
3. Arrays versus Linked Lists
4. Nodes
5. Sentinel (dummy) nodes
6. Types of Linked Lists
• Singly-Linked Lists
• Doubly-Linked Lists // CIS 22C
• Multi-Linked Lists // CIS 22C
7. Linked List Applications

1
A linear list is a sequence of n >= 0 elements:

x1, x2, x3, . . . xn

The first element is x1. The last element is xn.


Any element in the middle, xk , 1 < k < n, is
preceded by xk - 1 and followed by xk + 1.

2
A linear list is a sequence of n >= 0 elements:

x1, x2, x3, . . . xn

The first element is x1. The last element is xn.


Any element in the middle, xk , 1 < k < n, is
preceded by xk - 1 and followed by xk + 1.

Each element has a unique successor and


a unique predecessor, except
the last element with no successor and
the first element with no predecessor.

3
Non-linear data structures:

1. Tree: a data structure in which each element may have


multiple successors and one predecessor, except for
one node, that does not have a predecessor, called the
root of the tree. // CIS 22C, CIS 26B
2. Graph: a data structure in which each element may
have multiple successors and multiple predecessors.
// CIS 22C

4
Linear List Operations:

Create List - Creates an empty list (constructor)


Insert - Inserts a new data item anywhere in the list
Delete - Deletes any data item from the list
Search - Searches for a data item in the list
Traverse - Visits every data item once
Get Length - Returns the number of data items in the list
Is Empty - Checks if the list is empty
Destroy List - Releases the memory (destructor)

5
A linear list is a list in which each element has a
unique successor and a unique predecessor, except
the last node with no successor, and the first node
with no predecessor.

Array implementation // sequential allocation

Linked implementation // linked allocation

6
list A linked list of integers

20 70 10 50

data next
NULL
70

The elements in a linked list are called nodes.


Each node contains two parts:
data and next,
holding the location of the next node.

7
Operations
Arrays vs Linked Lists
1. Direct Access: list[i] 1. “Walk” to the node

2. Sorted list: binary search 2. Sorted list: linear search

3. Insert: 3. Insert:
• shift elements to make room • create a new node
• update pointers

4. Delete: 4. Delete:
• shift elements to eliminate gap • update pointers
• release memory

8
Operations
Arrays vs Linked Lists
1. Direct Access: list[i] // + 1. “Walk” to the node // -

2. Sorted list: binary search // + 2. Sorted list: linear search


// -
3. Insert: 3. Insert:
• shift elements to make room • create a new node
// - • update pointers
// +
4. Delete: 4. Delete:
• shift elements to eliminate gap • update pointers
// - • release memory
// +

9
Memory Usage
Arrays vs Linked Lists
1. Needs memory for data 1. Needs memory for both
data and pointers

2. Allocate the array once 2. Allocate memory as needed

3. Recycle the array once 3. Recycle as needed

10
Node

data next

70

The elements in a linked list are called nodes.


Each node contains two parts:
data and next,
holding the location of the next node.

A node in a linked list can be implemented as a


- structure, or a
- class

In the following examples we assume that the linked list node is a struct.
11
A linked list of City nodes.

state year_incorporated name

city
state year_incorporated name next

A node in a linked list can be implemented as a


- structure, or a
- class

City is either a struct or a class


12
A linked list of City nodes.

state year_incorporated name LinkedList

city
state year_incorporated name next Node

City

UML – diagram
- A LinkedList has Nodes.
- Each Node has a City object.
- is used to show “aggregation”
13
A linked list of City nodes.

list

This is a Data Structure Diagram for a Singly-Linked List of City nodes

Note: City name in this example is a dynamically allocated C-string

14
What is one advantage and one disadvantage of using B instead of A?

( A ) ( B )

state year_incorporated name state year_incorporated name next

city
state year_incorporated name next

Although (B) looks simpler, (A) is preferred because it makes it easier to


replace the city structure/object with a different type (Book, Stu, etc.)

15
UML Diagram for the Singly Linked List Class
SList
- head
- length
+ SList()
+ insertNode()
+ deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()

- head is a pointer to the first node


- length is storing the number of nodes in the linked list and must be
updated each time we add / remove a node from the list

The head of the list could be the only data member of a linked list class .
The length of the list is convenient to have: when needed, we can get it
without traversing the entire list.
16
Data Structure Diagram of a Singly Linked List.
head
5

40 90 50 30 80

NULL

17
list Data Structure Diagram of a Singly Linked List.
head
5

SList list; // list is a linked list object

40 90 50 30 80

NULL

18
list Data Structure Diagram of a Singly Linked List.
head
0

SList list; // empty list


NULL

// constructor
SList::SList()
{
head = NULL;
length = 0;
} SList
- head
- length
+ SList()
+ insertNode()
+ deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()

Creating an empty linked list.

19
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

// getter NULL
int SList::getLength()
{ SList
return length; - head
} - length
+ SList()
+ insertNode()
+ deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()

The getLength() member function of the SList class returns the number of
nodes in the list.
20
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL
int SList::isEmpty()
{ SList
return (length == 0); - head
} - length
+ SList()
The isEmpty() member function of the SList class returns true if the + insertNode()
+ deleteNode()
list is empty, false otherwise. + searchList()
+ traverse()
We could always use getLength() instead of isEmpty(). + getLength()
Why do we need this function? + isEmpty()
+ ~SList()
Convenience: it enhances the readability of the code.

Notice its elegant implementation with a single return statement.


21
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

SList
- head
- length
+ SList()
Traversing a linked list: visit every node once.
+ insertNode()
There are many algorithms based on traversal: + deleteNode()
- Printing a list + searchList()
- Calculating a sum/average + traverse()
+ getLength()
- Finding smallest/largest + isEmpty()
- Any/all type of inquires such as: + ~SList()
- Are all elements positive?
- Is there at least one odd value?
- Updating data in each node
22
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

SList
- head
- length
+ SList()
+ insertNode()
+ deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()
Traversing an array can be done using either an index or a pointer.
To traverse a linked list we have only one option: use a pointer!
23
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
+ deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()
pCurr – a pointer to the current element in the list.
We begin the traversal with the first node.
Assume the purpose of traversal is to display the list. 24
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
for (int i = 0; i < length; i++) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
// How do we move to the next node ?
+ isEmpty()
+ ~SList()
}
}

Since we have the length of the list, we can easily write a for loop to traverse it. 25
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
for (int i = 0; i < length; i++) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
// How do we move to the next node ?
+ isEmpty()
= pCurr->next; + ~SList()
}
}

What is the address of the next node? pCurr->next 26


list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
for (int i = 0; i < length; i++) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
// How do we move to the next node ?
+ isEmpty()
pCurr = pCurr->next; + ~SList()
}
}

How do we make pCurr point to the next node? (assign pCurr->next to pCurr) 27
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
+ deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()
How can we traverse the list without using its length?

28
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
+ deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()
How can we traverse the list without using its length?
Use a while loop controlled by a pointer

29
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()
How can we traverse the list without using its length?
Use a while loop controlled by a pointer: keep going as long as the pointer is not NULL.
The last node does not have a “next”: its next pointer is assigned NULL to mark the end
of the list. 30
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
pCurr = pCurr->next;
+ isEmpty()
} + ~SList()
}

31
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
pCurr = pCurr->next;
+ isEmpty()
} + ~SList()
}

32
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
pCurr = pCurr->next;
+ isEmpty()
} + ~SList()
}

Output: 40
33
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
pCurr = pCurr->next;
+ isEmpty()
} + ~SList()
}

Output: 40
34
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
pCurr = pCurr->next;
+ isEmpty()
} + ~SList()
}

Output: 40
35
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
pCurr = pCurr->next;
+ isEmpty()
} + ~SList()
}

Output: 40 90
36
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
pCurr = pCurr->next;
+ isEmpty()
} + ~SList()
}

Output: 40 90
37
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
pCurr = pCurr->next;
+ isEmpty()
} + ~SList()
}

Output: 40 90 // … and so on, until we get to the end of the list.


38
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

NULL

void SList::displayList() SList


{ - head
Node *pCurr; - length
pCurr = head; pCurr
+ SList()
+ insertNode()
while ( pCurr != NULL ) + deleteNode()
{ + searchList()
cout << pCurr->data << endl; + traverse()
+ getLength()
pCurr = pCurr->next;
+ isEmpty()
} + ~SList()
}

Output: 40 90 50 30 80
39
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

pCurr NULL

SList
- head
- length
+ SList()
Let’s say we want to delete the node that contains 50 and that we
+ insertNode()
already have a pointer, pCur, pointing to this node. To remove 50 + deleteNode()
from the list, we have to make “node 90” to point to “node 30”, and + searchList()
this cannot be done with only one pointer! + traverse()
+ getLength()
+ isEmpty()
+ ~SList()

40
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

pPre pCurr NULL

SList
- head
- length
+ SList()
Let’s say we want to delete the node that contains 50 and that we
+ insertNode()
already have a pointer, pCur, pointing to this node. To remove 50 + deleteNode()
from the list, we have to make “node 90” to point to “node 30”, and + searchList()
this cannot be done with only one pointer! + traverse()
+ getLength()
+ isEmpty()
Let’s add another pointer, pPre, to point to the current’s node + ~SList()
predecessor.

41
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

pPre pCurr NULL

SList
pPre->next = pCur->next;
- head
- length
+ SList()
Let’s say we want to delete the node that contains 50 and that we
+ insertNode()
already have a pointer, pCur, pointing to this node. To remove 50 + deleteNode()
from the list, we have to make “node 90” to point to “node 30”, and + searchList()
this cannot be done with only one pointer! + traverse()
+ getLength()
+ isEmpty()
Let’s add another pointer, pPre, to point to the current’s node + ~SList()
predecessor.

Finally, release the memory (delete the node).


42
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 30 80

pPre pCurr NULL

SList
pPre->next = pCur->next;
delete pCur; - head
- length
+ SList()
Let’s say we want to delete the node that contains 50 and that we
+ insertNode()
already have a pointer, pCur, pointing to this node. To remove 50 + deleteNode()
from the list, we have to make “node 90” to point to “node 30”, and + searchList()
this cannot be done with only one pointer! + traverse()
+ getLength()
+ isEmpty()
Let’s add another pointer, pPre, to point to the current’s node + ~SList()
predecessor.

Finally, release the memory (delete the node).


43
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

pPre pCurr NULL

SList
pPre->next = pCur->next;
- head
delete pCur; - length
+ SList()
Does this code work if we remove the last node in the list? + insertNode()
+ deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()

44
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

pPre pCurr NULL

SList
pPre->next = pCur->next; // NULL
// pPre->next = NULL; - head
delete pCur; - length
+ SList()
Does this code work if we remove the last node in the list? + insertNode()
+ deleteNode()
Since pCurr is the last node in the list, after removing it, pPre will + searchList()
+ traverse()
be the last node in the list:
+ getLength()
+ isEmpty()
+ ~SList()

45
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30

pPre pCurr NULL

SList
pPre->next = pCur->next; // NULL
// pPre->next = NULL; - head
delete pCur; - length
+ SList()
Does this code work if we remove the last node in the list? + insertNode()
+ deleteNode()
Since pCurr is the last node in the list, after removing it, pPre will + searchList()
+ traverse()
be the last node in the list: it works!
+ getLength()
+ isEmpty()
+ ~SList()

46
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

pPre pCurr NULL

SList
pPre->next = pCur->next;
delete pCur; - head
- length
+ SList()
Does this code work if we remove the first node in the list? + insertNode()
+ deleteNode()
+ searchList()
+ traverse()
+ getLength()
+ isEmpty()
+ ~SList()

47
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

pPre pCurr NULL

SList
pPre->next = pCur->next;
delete pCur; - head
- length
+ SList()
Does this code work if we remove the first node in the list? + insertNode()
+ deleteNode()
The first node does not have a predecessor (pPre is NULL) + searchList()
+ traverse()
Dereferencing a NULL pointer is not possible: it does NOT work!
+ getLength()
+ isEmpty()
+ ~SList()

48
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

pPre pCurr NULL

SList
head = pCur->next;
delete pCur; - head
- length
+ SList()
Does this code work if we remove the first node in the list? + insertNode()
+ deleteNode()
The first node does not have a predecessor (pPre is NULL) + searchList()
+ traverse()
Dereferencing a NULL pointer is not possible: it does not work!
+ getLength()
+ isEmpty()
When we remove the first node in the list, the head of the list must + ~SList()
be updated to point to the second node.

49
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

90 50 30 80

pPre pCurr NULL

SList
head = pCur->next;
delete pCur; - head
- length
+ SList()
Does this code work if we remove the first node in the list? + insertNode()
+ deleteNode()
The first node does not have a predecessor (pPre is NULL) + searchList()
+ traverse()
Dereferencing a NULL pointer is not possible: it does not work!
+ getLength()
+ isEmpty()
When we remove the first node in the list, the head of the list must + ~SList()
be updated to point to the second node.

50
list Data Structure Diagram of a Singly Linked List.
head
5

SList list = buildList(5);

40 90 50 30 80

pPre pCurr NULL

SList
- head
- length
+ SList()
To summarize, when we remove a node from a list we have to
+ insertNode()
consider two cases: + deleteNode()
1. removing the first node (must update head) + searchList()
2. removing any other node (head does not change) + traverse()
+ getLength()
+ isEmpty()
We can make delete simpler if we add a predecessor for the first + ~SList()
node, an “empty” data node: it does not carry any data, and it is
known as a “dummy node” or a “sentinel node”.
51
head
5

/////// 40 90 50 30 80

NULL

A sentinel node, also known as dummy or header node, is an extra node added
before the first data record.
This convention simplifies and accelerates some list-manipulation algorithms,
by making sure that all links can be safely dereferenced and that every list
(even one that contains no data elements) always has a "first" node.

52
head
5

/////// 40 90 50 30 80

NULL

A sentinel node, also known as dummy or header node, is an extra node added
before the first data record.
This convention simplifies and accelerates some list-manipulation algorithms,
by making sure that all links can be safely dereferenced and that every list
(even one that contains no data elements) always has a "first" node.

Should we change the counter to 6, to include the sentinel node? No, the
counter represents the number of data items we store in the list.
When/where should we create the sentinel node? This is the constructor’s job!

53
head
5

/////// 10 20 50 60 80

NULL

Assume the linked list is sorted in ascending order.


How does this change the delete function?
Unsuccessful searches are faster. Let’s say we intend to delete 15. When we
search for 15 we do not have to traverse the entire list: we stop at 20 (not found!)
54
head
5

/////// 10 20 50 60 80

bool SList::deleteNode(int target)


{
ListNode *pCur; // To traverse the list NULL
ListNode *pPre; // To point to the previous node
bool deleted = false; SList
pPre = head; // Initialize pointers - head
pCur = head->next; - length
// Find the node containing the target: Skip all nodes whose data element is less than the target
while (pCur != NULL && pCur->data < target){ + SList()
pPre = pCur; + insertNode()
pCur = pCur->next; + deleteNode()
} + searchList()
if (pCur != NULL && pCur->data == target) { // If found, delete the node
pPre->next = pCur->next;
+ traverse()
delete pCur; + getLength()
deleted = true; + isEmpty()
length--; + ~SList()
}
return deleted;
}

55
head
5

/////// 10 20 50 60 80

pPre pCurr
NULL

SList
- head
- length
+ SList()
When we insert a new element into a sorted list, the list + insertNode()
will be sorted after the insertion of the new element. + deleteNode()
+ searchList()
For instance, if we insert 25, + traverse()
- first we find the insertion point (between 20 and 50), + getLength()
+ isEmpty()
+ ~SList()

56
head pNew
5

25

/////// 10 20 50 60 80

pPre pCurr
NULL

SList
- head
- length
+ SList()
When we insert a new element into a sorted list, the list + insertNode()
will be sorted after the insertion of the new element. + deleteNode()
+ searchList()
For instance, if we insert 25, + traverse()
- first we find the insertion point between 20 and 50, + getLength()
- then we create a new node, + isEmpty()
+ ~SList()
- copy 25 into the new node,

57
head pNew
6

25

/////// 10 20 50 60 80

pPre pCurr
NULL

SList
pPre->next = pNew;
pNew->next = pCurr; - head
length++; - length
+ SList()
When we insert a new element into a sorted list, the list + insertNode()
will be sorted after the insertion of the new element. + deleteNode()
+ searchList()
For instance, if we insert 25, + traverse()
- first we find the insertion point between 20 and 50, + getLength()
- then we create a new node, + isEmpty()
+ ~SList()
- copy 25 into the new node,
- update the links and
- update the counter.
58
head pNew
6

25

/////// 10 20 50 60 80

pPre pCurr
NULL
void SList::insertNode(int target)
{
SList
ListNode *pCur; // To traverse the list - head
ListNode *pPre; // To point to the previous node
- length
ListNode *pNew; // To point to the new node
+ SList()
pPre = head; // Initialize pointers
pCur = head->next;
+ insertNode()
// Find location: Skip all nodes whose data element is less than the target + deleteNode()
while (pCur != NULL && pCur->data < target){ + searchList()
pPre = pCur; + traverse()
pCur = pCur->next; + getLength()
} + isEmpty()
// Insert new node between pPre and pCur
pPre->next = pNew;
+ ~SList()
pNew->next = pCur;
length++;
}
59
Linked List Applications

1. Linked list in an array (no pointers)


2. Operations on big numbers
3. Operations on polynomials
4. Collections of linked lists
• array of linked lists
• linked list of linked lists
5. Text processing
6. Storage of files on a disk
7. Heap management
8. Hash Tables (for solving collisions - CIS 22C)

60
1. Linked list in an array (no pointers)
data next
0

data next
1 70 4
data next
2
data next
3 10 6
data next
4 80 -1
data next
5
data next
6 20 1
data next
7

61
2. Operations on big numbers (tens of digits) 2019783 +
8905269
========
10925052

3 8 7 9 1 0 2

num_1

9 6 2 5 0 9 8

num_2

2 5 0 5 2 9 0 1

sum

62
3. Operations on polynomials
P: 3x4 + 2x + 5
Q: 7x3 + 5x2 + 4x + 6

P + Q?

P: 3x4 + 2x + 5
Q: 7x3 + 5x2 + 4x + 6
================================
P + Q: 3x4 + 7x3 + 5x2 + 6x + 11

63
4. Collections of linked lists
• array of linked lists (CIS 22C – Hashing)
• linked list of linked lists (CIS 22C – Graphs)

64
5. Text processing
text

This is the first line

This is the second line

The third

The fourth

The second from the last

This is the last line.

65
6. Storage of files on a disk

There are different ways of allocating disk blocks for files, such as:

- contiguous
- linked allocation
- indexed allocation

Linked Allocation: Each file is a linked list of disk blocks.

These disk blocks may be scattered through the disk.


A few bytes of each disk block contain the address of the next block.
The directory contains a pointer to the first (and last) blocks of the file.

66
7. Heap management

Operating Systems keep track of storage that is not in use: “free list”,
often implemented as a linked list.

The “free list” is used to store free blocks, it is searched on


each allocation, and also updated on each release operation,
when adjacent free blocks are merged.

67
Linked List

ü 1. Linear Lists
ü 2. List Operations
ü 3. Arrays versus Linked Lists
ü 4. Nodes
ü 5. Sentinel (dummy) nodes
ü 6. Types of Linked Lists
• Singly-Linked Lists
• Doubly-Linked Lists // CIS 22C
• Multi-Linked Lists // CIS 22C
ü 7. Linked List Applications

68

You might also like