comp103 - linked lists (part a)1 chapter 17 truly dynamic memory management
Post on 20-Dec-2015
225 Views
Preview:
TRANSCRIPT
COMP103 - Linked Lists (Part A)
2
Motivation 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 marks[10];
Note: we need to know the size of the array “Dynamic” memory –
int n, *marks;Cin >> n;Marks = new int[n];
Note: once memory is allocated, the size of the array is fixed (i.e. static)
Linked list – truly dynamic
COMP103 - Linked Lists (Part A)
3
Motivation 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.
COMP103 - Linked Lists (Part A)
4
Linked 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:
20 45 75 85
Data Link
COMP103 - Linked Lists (Part A)
5
Linked Lists: Basic Idea
The list can grow and shrink
20 45
20 45 75 85
20
add(75), add(85)
delete(85), delete(45), delete(75)
List
COMP103 - Linked Lists (Part A)
6
Linked 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
node
=NULL
COMP103 - Linked Lists (Part A)
8
Nodes in C++Simple: data Define the node type:
struct NODE{ int data; NODE *link;};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->data = 10;// link points to NULL
p_new->link = NULL;}
10
COMP103 - Linked Lists (Part A)
9
Nodes in C++Complex node:
Define the node type:struct DATA{ char key;
int ID;int tel;
};struct NODE{ DATA data; NODE *link;};
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->data.key = ‘a’;p_new->data.ID = 123;p_new->data.tel = 5678;
// link points to NULLp_new->link = NULL;
}
tel. no. ID key
5678 123 a
COMP103 - Linked Lists (Part A)
10
Linked 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
COMP103 - Linked Lists (Part A)
11
Linked 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
COMP103 - Linked Lists (Part A)
12
Linked 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 nodeend traverse
pWalker
When pWalker becomes NULL, it means that the end of the list is reached
COMP103 - Linked Lists (Part A)
13
Linked List Traversal
// Other statements NODE *pWalker;pWalker = pHead;while (pWalker) { cout << pWalker->data << endl; pWalker = pWalker->link; // points to next node}
pHead
COMP103 - Linked Lists (Part A)
14
Search for an item (target) in the listtarget 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) pPre==NULL, (ii) pPre!=NULL
COMP103 - Linked Lists (Part A)
15
Search for an item (target) in the listtarget 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) pPre==NULL, (ii) pCur==NULL, (iii) pPre!=NULL AND pCur!=NULL
COMP103 - Linked Lists (Part A)
16
Algorithm for searching
pPre = NULL;pCur = pHead;// Search until target <= list data keywhile (pCur && (target > pCur->data.key)) {
pPre = pCur; pCur = pCur->link;
} if (pCur && (target == pCur->data.key))
found = true; else
found = false;return found;
COMP103 - Linked Lists (Part A)
17
Add an item: Array vs. Linked List Array: it involves many copying operations
(Remember: Data must be ordered!)
955809 Peter956498 Mary956894 John
955809 Peter955992 Ada956498 Mary956894 John
955992 Ada+
955809 Peter 956498 Mary 956894 John
955992 Ada
For linked list, no copying operation is necessaryData can be anywhere in memory
Only links are altered
COMP103 - Linked Lists (Part A)
18
Add an item, i.e. adding a NodeItem 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
Tw
o c
ases:
COMP103 - Linked Lists (Part A)
19
Add an item to an Empty List
pHead==NULL, means the list is empty
pPre==NULL, means we are adding to an empty list, OR at the beginning of a list
COMP103 - Linked Lists (Part A)
20
Add an item at Beginning of the list
Now, pHead!=NULL, so the list is not empty
Also, pPre==NULL, so we are adding at the beginning of the list
The code is identical!
COMP103 - Linked Lists (Part A)
21
Add an item in Middle of the list
pPre != NULL AND pPre->link != NULL,
so we are adding in the middle of the list
COMP103 - Linked Lists (Part A)
22
Add an item at the End of the listpPre != NULL AND pPre->link == 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
COMP103 - Linked Lists (Part A)
23
Add an item to the list
Node *pNew;pNew = new Node; // allocate memory to new nodeif (!pNew) exit(0); // Memory overflowpNew->data = item;if (pPre == NULL) {
pNew->link = pHead; // add before the first node pHead = pNew; // or to an empty list
}else {
pNew->link = pPre->link; // add in the middle orpPre->link = pNew; // at the end
}
COMP103 - Linked Lists (Part A)
24
Delete 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
COMP103 - Linked Lists (Part A)
25
Delete - General Case
pPre->link = pCur->linkdelete (pCur);
pPre->link = pCur->link;delete (pCur);
pPre!=NULL for all other cases
COMP103 - Linked Lists (Part A)
26
Delete a Node from a linked list
// Delete a node from a linked list
// Other Statements
if (pPre == NULL) pHead = pCur->link; // delete first nodeelse // delete other node pPre->link = pCur->link; delete (pCur); // return memory
COMP103 - Linked Lists (Part A)
27
Next 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.
top related