module-3 linked-list final
module-3 linked-list final
MODULE -3
Linked Lists: Definition, Representation of linked lists in Memory, Memory allocation; Garbage
Collection. Linked list operations: Traversing, Searching, Insertion, and Deletion. Doubly Linked
lists, Circular linked lists, and header linked lists. Linked Stacks and Queues. Applications of
Linked lists – Polynomials, Sparse matrix representation. Programming Examples
3.1 Linked List: “A Linked list is a linear collection of data elements called nodes, where each
node is divided into two parts: the first part called info field, contains the information of the element
and second part called link field or next pointer field, contains the address of the next node in the
list”.
Node: Info-field Link-field
Example: first
10 20 30 40 \0
Arrays–drawbacks:
(1)The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. (Static in nature)
(2) Inserting and a new element in an array of elements is expensive, because room has to be
created for the new elements and to create room existing elements have to shifted. Same holds good
for deletion also.
Garbage Collection: Whenever a node is deleted, some memory space becomes reusable. This
memory space should be available for future use. One way to do this is called „Garbage
Collection‟.
The operating system of computer may periodically collect all the deleted space into the free
space list. Any technique which does this collection is called garbage collection.
It takes place in two steps.
1. First computer runs through all lists, tagging those cells which are currently in use.
2. Then computer runs through the memory collecting the untagged space on to free storage list.
The garbage collection may take place when there is only some minimum amount of space or no
space at all left in the Free storage list or when CPU is idle and has time to do the collection.
Example: first
10 20 30 40 \0
Struct node
{
int info; //info-integer field contains information
struct node *link; //link- holds address of next node
};
typedef struct node *NODE; //giving alternative name to strcut node to NODE
NODE first; // first- self-referential structure variable
First = NULL; // Empty list by name first is created with NULL value
Create a node:
X = (data_type *) malloc (size);
Malloc(): it is used to allocate memory explicitly as and when required and exact amount of
memory space needed during execution.
X = (NODE) malloc (sizeof(struct node)); // allocate the memory space
NODE getnode()
{
NODE X;
X = (NODE) malloc (sizeof(struct node));
if ( X = = NULL) //nodes don‟t exist
{
Printf(“out of memory”);
}
return X; // allocation successful
}
Step 3: copy the address of the first node which is stored in the variable first into link field of
temp. temp -> link = first;
temp first
50 10 20 30 40 \0
Step 4: temp is inserted and then temp becomes the first node.
return temp;
first
50 10 20 30 40 \0
Step 2: copy item 50 into info field and NULL to link. temp
temp -> info = item; 50 \0
temp -> link = NULL;
Step 4: if the list is existing, then we have to insert temp at the end of list. To insert at the end,
we have to find the address of the last node.
cur = first;
while( cur -> link != NULL)
{
cur = cur ->link;
}
After executing the above code , we will get the address of last node in a list.
First cur temp
10 20 30 40 \0 50 \0
Step 5: copy the address of the temp node to cur -> link, NULL is replaced by temp address.
cur -> link = temp;
first cur temp
10 20 30 40 50 \0
Step 6: first contains the address of the first node of list. So we have to return first.
return first;
first
10 20 30 40 50 \0
10 20 30 40 50 \0
Step 3: update the pointer temp so that it contains address of the second node.
temp = temp -> link;
first temp
10 20 30 40 50 \0
Step 4: now, first points to first node and temp points to second node of the list. De-allocate the
memory of the first node.
free( first);
Step 5: once first node is deleted, and then second node temp is the first node. So, return temp as
first node. return temp;
first
20 30 40 50 \0
Step 2: if the list is existing, then we have to delete the last node of list. To delete at the raer end,
we have to find the address of the last node by using variable cur and prev.
cur = first;
prev= NULL;
while( cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}
After executing the above code , we will get the address of last node and prev get the address of
last but one node in a list.
First prev cur
10 20 30 40 50 \0
Step 4: once last node is deleted, and then prev node should be the last node. So, copy NULL to
link field of prev node.
prev -> link = NULL;
first prev
10 20 30 40 \0
10 20 30 40 \0
10 20 30 \0 40 50 \0
Step 3: now, attach address of the first node of the second list to cur node.
cur -> link = second;
First cur second
10 20 30 40 50 \0
if (first = = NULL)
return second;
if (second = = NULL)
return first;
cur = first;
while( cur ->link ! = NULL)
{
cur = cur ->link; // point cur to next node
}
Cur -> link =second;
return first;
}
10 20 30 40
Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
temp first last
40 10 20 30
40 10 20 30
Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
first last temp
10 20 30 50
Step 4: make temp as the last node by copying temp into link field of last node.
last->link = temp;
first temp
10 20 30 50
10 20 30 50
10 20 30 50
Step 4: Now remove the first node. De-allocate the memory of the first node.
free( first);
first last
10 20 30 50
10 20 30 50
10 20 30 50
Step 4: Make the last but one node as the last node and De-allocate the memory of the last node.
prev -> link = last ->link;
free( last);
first last
10 20 30 50
3.4.3 Doubly linked list: A doubly linked list is a linear collection of nodes where each node is
divided into three parts:
Info – contains information to be stored.
llink – contains the address of the left or previous node. llink info rlink
rlink - contains the address of the right or next node.
Example:
first
\0 10 20 30 \0
\0 40 10 20 30 \0
\0 40 10 20 30 \0
Step 2: To insert the node at rear end, first find the address of the last node.
cur = first;
while(cur -> rlink ! = NULL)
{
cur = cur -> rlink;
}
first cur temp
\0 10 20 30 \0 \0 40 \0
Step 3: Insert node at the end.
cur -> rlink = temp;
temp -> llink = cur;
first cur temp
\0 10 20 30 40 \0
return first;
first
\0 10 20 30 40 \0
\0 10 20 30 \0
\0 10 \0 20 30 \0
\0 10 \0 20 30 \0
Step 2:if list is existing then find the address of last node and last but one node.
cur = first, prev = NULL;
while(cur -> rlink ! = NULL)
{ prev = cur;
cur = cur -> rlink;
}
first prev cur
\0 10 20 30 40 \0
\0 10 20 30 40 \0
\0 10 20 30 \0
The info field of header node does not contain any information.
The first node starts from link field of header node i.e the node containing the info field 10 is the
first node.
head
4 10 30 40 50 \0
The info field of the header node contains the number of nodes in the list.
Step 3: Obtain the address of the first node which is stored in the head link field of head.
first = head -> link;
head first
10 20 30 40 \0
Step 4: temp node is inserted between the head and first node.
head -> link = temp;
temp temp -> link = first;
50
head first
10 20 30 40 \0
50 10 20 30 40 \0
2. Adding polynomials
Next
Down right