Lec4 Linked List
Lec4 Linked List
What is a pointer ?
• Creation
int *ptr=&variable Returns variable’s memory address
• Dereference
*ptr Returns contents stored at address
• Indirect assignment
*ptr=val Stores value at address
• Assignment
ptr=ptr1 Stores pointer in another variable
Using Pointers
int i1;
int i2;
int *ptr1; 0x1014 …
0x1000
int *ptr2; 0x1012 ptr2:
0x1010 …
0x1000
i1 = 1;
0x1008 ptr1:
i2 = 2;
0x1004 3
i2: 2
ptr1 = &i1;
0x1000 i1: 3
1
ptr2 = ptr1;
*ptr1 = 3;
i2 = *ptr2;
Pointers and Arrays in C
• Let L = (e1,e2,…,en)
– Each element ei is represented in a separate node
– Each node has exactly one link field that is used to locate the next
element in the linear list
– The last node, en, has no node to link to and so its link field is NULL.
• This structure is also called a chain.
first
Link Field …… 0
Data Field e1 e2 e3 en
Structure of a linked list
myList
a b c d
• We can define structures with pointer fields that refer to the structure type
containing them
int data;
struct list *next;
}
• a.next = &b;
• b.next = &c;
• a.next -> data has value 2
• a.next -> next -> data has value 3
• b.next -> next -> data error !!
a b c
1 2 3
Dynamic Memory Allocation
free(ptr);
?
ptr
Examples of the Nodes of a Linked List
int number;
struct node *link;
};
The Nodes of a Linked List – Examples
struct student{
char name[20];
int id;
double grdPts;
struct student *next;
};
The Nodes of a Linked List – Examples
next
struct person_node{
struct person data;
data
struct person_node *next;
};
Basic Operations on a Linked List
1. Add a node
2. Delete a node
3. Search for a node
4. Traverse (walk) the list
• Useful for counting operations or aggregate operations
Adding a node into a SLL
• There are many ways a new node can be inserted into a list:
– As the new first element
– As the new last element
– Before a given node (specified by a reference)
– After a given node
– Before a given value
– After a given value
• All these are possible, but differ in difficulty
Adding Nodes to a Linked List
There are four steps to add a node to a linked list:
• Allocate memory for the new node.
• Determine the insertion point
– You need to know only the new node’s predecessor (Pre)
• Point the new node to its successor.
• Point the predecessor to the new node.
Pointer to the predecessor (Pre) can be in one of two states:
• It can contain the address of a node
– i.e., you are adding somewhere after the first node – in the middle or at
the end
• It can be NULL
– i.e., you are adding either to an empty list or at the beginning of the list
Header Nodes
• The purpose is to keep the list from being null, and to point at
the first element
• There are two types of header list
– Grounded header list : is a header list where the last node contain the
null pointer.
– Circular header list : is a header list where the last node points back to
the header node
Head
Before: After:
New 39 NULL
New 39 NULL
Head
Head
Pre
Pre
Adding a Node to the Beginning of a Linked List
New 39
Head 75 124
Pre After:
New 39
Head
75 124
Pre
Adding/Inserting after
node 2.5
Head
Before:
New -> next = Pre -> next;
Pre -> next = New;
New 64
55 124
Pre
After:
New 64
55 124
Pre
Adding a Node to the End of a Linked List
Before: Code
New -> next = NULL;
New 144 Pre -> next = New;
55 124
Pre
After:
New 144
55 124
Pre
Inserting a Node Into a Linked List
• Given the head pointer (Head), the predecessor (Pre) and the data to be inserted (item).
Memory must be allocated for the new node (New) and the links properly set.
//insert a node into a linked list
struct node *New;
New = (struct node *) malloc(sizeof(struct node));
New -> data = item;
if (Pre == NULL){
//add before first logical node or to an empty list
New -> next = Head;
Head = New;
}
else {
//add in the middle or at the end
New -> next = Pre -> next;
Pre -> next = New;
}
Traversing a SLL (animation)
Walker
Head
thre
one two
e
Traversing a Linked List
• Deleting a node requires that we logically remove the node from the list
by changing various links and then physically deleting the node from the
list (i.e., return it to the heap).
• Any node in the list can be deleted.
– Note that if the only node in the list is to be deleted, an empty list will result. In this case the head
pointer will be set to NULL.
thre
one two
e
Deleting the First Node from a SLL
Before: Code:
Head = Cur -> next;
Head
free(Cur);
75 124
Pre Cur
After:
Pre Cur
Deleting an element from a SLL
Before: Code:
Pre -> next = Cur -> next;
75 96 124
free(Cur);
Pre Cur
After:
Recycled
75 124
Pre Cur
Deleting a Node From a Linked List
• Given the head pointer (Head), the node to be deleted (Cur), and its
predecessor (Pre), delete Cur and free the memory allocated to it.
Initialize: Pre = NULL; Cur = Head;
Traverse the list to the current node
(Pre=Cur; Cur=Cur->next;)
//Delete a node from a linked list
if (Pre == NULL)
//Deletion is on the first node of the list
Head = Cur -> next;
else
//Deleting a node other than the first node of the list
Pre -> next = Cur -> next;
free(Cur).
Searching a Linked List
• Notice that both the insert and delete operations on a linked list must search the list for either
the proper insertion point or to locate the node corresponding to the logical data value that is
to be deleted.
//Search the nodes in a linked list
Pre = NULL; Cur = Head;
//Search until the target value is found or the end of the list is reached
while (Cur != NULL && Cur -> data != target) {
Pre = Cur;
Cur = Cur -> next;
}
//Determine if the target is found or ran off the end of the list
if (Cur != NULL)
found = 1;
else
found = 0;
Doubly Linked List
• A doubly linked list provides a natural
implementation of the List ADT
prev next
• Nodes implement Position and store:
• element
• link to the previous node
• link to the next node element node
• Special trailer and header nodes
elements
Doubly-Linked Lists
1. Add a node.
2. Delete a node.
3. Search for a node.
4. Traverse (walk) the list. Useful for counting
operations or aggregate operations.
Adding Nodes to a Doubly-Linked List
New 39
After:
Head
Cur
Adding a Node to the Middle of a Doubly-Linked List
After: New 64
55 124
Cur
Adding a Node to the End of a Doubly-Linked List
55 74
Cur
Inserting a Node Into a Doubly-Linked List
//insert a node into a linked list
struct node *New;
New = (struct node *)malloc(sizeof(struct node));
New -> data = item;
if (Cur == NULL){ //add before first logical node or to an empty list
New -> left = Head;
New -> right = Head;
Head = New;
}
else {
if (Cur -> right == NULL) { //add at the end
New -> left = Cur;
New -> right = Cur -> right;
Cur -> right = New;
}
else { //add in the middle
New -> left = Cur;
New -> right = Cur -> right;
Cur -> right -> left = New;
Cur -> right = New;
}
}
Deleting a Node from a Doubly-Linked List
• Deleting a node requires that we logically remove the node from the
list by changing various links and then physically deleting the node
from the list (i.e., return it to the heap).
• To logically delete a node:
– First locate the node itself (Cur).
– Change the predecessor’s and successor's link fields to point each other.
– Recycle the node using the free( ) function.
Deleting the First Node from a Doubly-Linked List
Before: Code:
Cur=Head
Head Head = Cur -> right;
75 124
Cur ->right -> left = NULL;
Cur free(Cur);
After:
Head Recycled 124
Cur
Deleting a Node from a Linked List – General Case
Before:
75 23 46 12477
Cur
After:
23 Recycled 77
75 124
Cur
Deleting a Node From a Doubly-Linked List
• Notice that both the insert and delete operations on a linked list must
search the list for either the proper insertion point or to locate the node
corresponding to the logical data value that is to be deleted.
//search the nodes in a linked list
Cur = Head;
//search until the target value is found or the end of the list is reached
while (Cur != NULL && Cur -> data != target) {
Cur = Cur -> right;
}
//determine if the target is found or ran off the end of the list
if (Cur != NULL)
found = 1;
else
found = 0;
Traversing a Doubly-Linked List
• Advantages: • Disadvantages:
– Can be traversed in either – Requires more space
direction (may be essential for – List manipulations are slower
some programs) (because more links must be
– Some operations, such as changed)
deletion and inserting before a
node, become easier
Circular Linked Lists
10 20 40 55 70
Rear
Motivation
Rear
Insert Node
New Rear
Insert to head of a Circular Linked List
10 20 40 55 70
New->next = Cur;
Prev->next = New;
10 20 55 70
40
Prev Cur Rear
New
Insert to end of a Circular Linked List
10 20 40 55 70