Title: CS101 Slides
1Linked List
Chapter 17 Truly dynamic memory management
2Motivation
- A List is a very useful structure to hold a
collection of data. - Examples
- List of students marks
- List of temperatures for a period of time
- Implementation in C using
- Static memory (array) int marks10
- Note we need to know the size of the array
- Dynamic memory
- int n, marks
- Cin gtgt n
- Marks new intn
- Note once memory is allocated, the size of the
array is fixed (i.e. static) - Linked list truly dynamic
3Motivation
- Common operations on lists
- Traverse the list (useful in printing the list)
- Search for an item
- Add an item
- Delete an item
- Note
- Algorithms of these operations based on static
arrays are not very efficient. Adding or
deleting an item in the array involves a lot of
copying of items in the list. And the actual
memory size of the array remains fixed. - Using linked lists improves the efficiency. The
size of the list can grow and shrink as we add or
delete items from the list.
4Linked Lists Basic Idea
- A linked list is an ordered collection of data
- Each element of the linked list has
- Some data
- A link to the next element
- The link is used to chain (or connect) the data
- Example A linked list of integers
Link
Data
20
45
75
5Linked Lists Basic Idea
- The list can grow and shrink
List
20
45
add(75), add(85)
20
45
75
85
delete(85), delete(45), delete(75)
20
6Linked List Structure
- Node - Data Link
- Data - contains useful information
- Link chains(connects) the data together
- Head pointer - points to the first element
- Empty List - head pointer equals NULL
7Nodes
Data may be simple
or more complex
8Nodes in C
Define the node type struct NODE int
data NODE link
Simple
In the program void main() // p_new is of type
NODE NODE p_new // allocating memory for one
node p_new new NODE // assigning value to the
data field in the node p_new-gtdata 10 // link
points to NULL p_new-gtlink NULL
9Nodes in C
Define the node type struct DATA char
key int ID int tel struct NODE
DATA data NODE link
Complex node
In the program void main() // p_new is of type
NODE NODE p_new // allocating memory for one
node p_new new NODE // assigning values to
the data field in the node p_new-gtdata.key
a p_new-gtdata.ID 123 p_new-gtdata.tel
5678 // link points to NULL p_new-gtlink
NULL
10Linked List Order
- Nodes in a linked list has an order
- Nodes are usually ordered by a key, such as
studentID - Unlike arrays, nodes in a linked list may not be
stored in neighborhood memory cells - The first node of a list is kept by a head
pointer (pHead), which must be kept safely - The last node of a list is a node that contains a
NULL value in its link part
11Linked Lists Basic Operations
- Transverse visit each item in the list
- (useful for printing the list)
- Search for an item in the list
- Add an item to the list
- Delete an item from the list
12Linked List Traversal
traverse (list) 1. Set pointer (pWalker) to the
first node (pHead) in list 2. while (not end of
the list) 2.1 process (current node,
pWalker) 2.2 set pointer to next node end
traverse
When pWalker becomes NULL, it means that the end
of the list is reached
13Linked List Traversal
pHead
// Other statements NODE pWalker pWalker
pHead while (pWalker) cout ltlt
pWalker-gtdata ltlt endl pWalker
pWalker-gtlink // points to next node
14Search for an item (target) in the list
- target is in the list
- ? The search function returns true
- pCur points to the node which contains target
- pPre points to the preceding node
2 cases (i) pPreNULL, (ii) pPre!NULL
15Search for an item (target) in the list
- target is NOT in the list
- ?The search function returns false
- pCur points to the node which should be after
target - pPre points to the node which should be before
target
3 cases (i) pPreNULL, (ii) pCurNULL, (iii)
pPre!NULL AND pCur!NULL
16Algorithm for searching
pPre NULL pCur pHead // Search until
target lt list data key while (pCur (target gt
pCur-gtdata.key)) pPre pCur pCur
pCur-gtlink if (pCur (target
pCur-gtdata.key)) found true else
found false return found
17Add an item Array vs. Linked List
- Array it involves many copying operations
- (Remember Data must be ordered!)
955809 Peter 956498 Mary 956894 John
955809 Peter 955992 Ada 956498 Mary 956894
John
955992 Ada
- For linked list, no copying operation is necessary
Data can be anywhere in memory Only links are
altered
18Add an item, i.e. adding a Node
- Item to be added should NOT be in the list,
- ? Search for this item will return a false value
- pPre points to the item less than the one to be
added - pCur points to an item greater than the one to
be added
Two cases
19Add an item to an Empty List
pHeadNULL, means the list is empty pPreNULL,
means we are adding to an empty list, OR at the
beginning of a list
20Add an item at Beginning of the list
Now, pHead!NULL, so the list is not empty Also,
pPreNULL, so we are adding at the beginning of
the list
The code is identical!
21Add an item in Middle of the list
pPre ! NULL AND pPre-gtlink ! NULL, so we are
adding in the middle of the list
22Add an item at the End of the list
pPre ! NULL AND pPre-gtlink NULL, so we are
adding at the end of the list
The code is identical to that of adding to the
middle of the list
23Add an item to the list
- Node pNew
- pNew new Node // allocate memory to new node
- if (!pNew) exit(0) // Memory overflow
- pNew-gtdata item
- if (pPre NULL)
-
- pNew-gtlink pHead // add before the first
node - pHead pNew // or to an empty list
-
- else
-
- pNew-gtlink pPre-gtlink // add in the middle or
- pPre-gtlink pNew // at the end
24Delete an item (the first one) from the list
- Item to be deleted should be in the list
- ? Search returns a True value
- pCur points to the item and
- pPre points to the one before item.
pPre NULL, means we are deleting the first node
25Delete - General Case
pPre!NULL for all other cases
26Delete a Node from a linked list
// Delete a node from a linked list // Other
Statements if (pPre NULL) pHead
pCur-gtlink // delete first node else // delete
other node pPre-gtlink pCur-gtlink delete
(pCur) // return memory
27Next Topic Linked List Design
- We have completed the basic operations in a
linked list (Part A). - The last topic in the course (Part B of the
notes) is to implement linked list as objects.
To be covered in May after OO design and ADT.
28Go to Part B