Linked Lists Notes
Linked Lists Notes
Introduction
We have seen representation of linear
sequential allocation method of storage, as
unacceptable in cases like:
Pointers are used for the dynamic memory allocation. These pointers are
always of same length regardless of which data element it is pointing to(
int, float, struct etc,). This enables the manipulation of pointers to be
performed in a uniform manner using simple techniques. These make us capable
of representing a much more complex relationship between the elements of a
data structure than a linear order method.
The use of pointers or links to refer to elements of a data structure
implies that elements, which are logically adjacent, need not be physically
adjacent in the memory. Just like family members dispersed, but still bound
together.
Fig 1.
Link
NODE
Structure of a Node
- 1 -
Consider an example where the marks obtained by the students are stored
in a linked list as shown in the figure :
|data |Next|
--->
70
65
---> 45
---> 62
NULL
|<-NODE ->|
fig 2. Singly Linked List
In figure 2, the arrows represent the links. The data part of each node
consists of the marks obtained by a student and the next part is a pointer to
the next node. The NULL in the last node indicates that this node is the last
node in the list and has no successors at present. In the above the example
the data part has a single element marks but you can have as many elements as
you require, like his name, class etc.
There are several operations that we can perform on linked lists. We can
see some of them now. To begin with we must define a structure for the node
containing a data part and a link part. We will write a program to show how
to build a linked list by adding new nodes in the beginning, at the end or in
the middle of the linked list. A function display() is used to display the
contents of the nodes present in the linked list and a function delete(),
which can delete any node in the linked list .
typedef struct node
{
int data;
struct node *link;
}NODE;
# include < stdio.h >
# include < alloc.h >
main()
{
NODE *p;
P = NULL;
- 2 -
disply(p);
printf(\n No of elements in the linked list = %d,
}
To begin with the variable p has been declared as
This pointer is a pointer to the first node in the list.
nodes get added to the list, p will always be the first
list. When no node exists in the linked list , p will
indicate that the linked list is empty. Now we will write
these functions.
pointer to a node.
No matter how many
node in the linked
be set to NULL to
and discuss each of
- 3 -
r = malloc(sizeof(NODE));
r->data = num;
r->link = NULL;
temp->link = r;
}
}
--->
---> 3
--->
NULL
- 4 -
temp
--->
---> 3
--->
NULL
node
being
added.
Each
point to
condition
the space
time through the loop the statement temp= temp->link makes temp
the next node in the list. When temp reaches the last node the
temp->link != NULL would fail. Once outside the loop we allocate
for the new node through the statement
r = malloc(sizeof(NODE));
Once the space has been allocated for the new node its data part is
filled with num and the link part with NULL. Note that this node is now going
to be the last node in the list.
All that now remains is connecting the previous last node to this new
last node. The previous node is being pointed to by temp and the new last
node is by r. they are connected through the statement
temp->link = r;
There is often a confusion amongst
temp=temp->link makes temp point to the
understand this with the help of an
containing 4 nodes temp is pointing to
figure below:
temp
150
1 400
2 700
3 910
4 NULL
150
400
700
910
- 5 -
--->
---> 3
---> 4
--->17 NULL
Before Addition
temp p
999 ---> 1
---> 2
---> 3
---> 4
--->17 NULL
After Addition
Fig 5. Addition of a node in the beginning of a SLL
For adding a new node at the beginning, firstly space is allocated for
this node and data is stored in it through the statement
temp->data = num;
Now we need to make the link part of this node point to the existing
first node. This has been achieved through the statement
temp->link = *q;
Lastly this new node must be made the first node in the list. This has
been attained through the statement
*q = temp;
- 6 -
temp
--->
---> 3
---> 4
--->17 NULL
99
Before Insertion
P
temp
--->
---> 3
--->17 NULL
r
99
After Insertion
Fig 6. Insertion of a node in the specified position
- 7 -
and
straightforward.
So
no
further
- 8 -
old
--->
---> 3
temp
---> 4
--->17 NULL
node to be deleted = 4
Before deletion
old
--->
--->
--->
17 NULL
- 9 -
temp
After deletion
Fig 7. Deletion of a node from SLL
Though the above linked list depicts a list of integers, a linked list
can be used for storing any similar data. For example, we can have a linked
list of floats, character array, structure etc.
<--------
INFO
PREV
--------->
NEXT
NODE
Fig 8. Node structure of a DLL
NULL
20
15
70
60 NULL
- 10 -
main()
{
NODE *p;
p = NULL;
d_append(&p,11);
d_append(&p,21);
clrscr();
display(p);
printf(\n No of elements in the doubly linked list =
%d, count(p));
d_add_beg(&p,33);
d_add_beg(&p,55);
disply(p);
printf(\n No of elements in the doubly linked list =
%d, count(p));
d_add_after(p,1,4000);
d_add_after(p,2,9000);
disply(p);
printf(\n No of elements in the linked list = %d,
count(p));
d_delete(&p,51);
d_delete(&p,21);
disply(p);
printf(\n No of elements in the linked list = %d,
}
- 11 -
/* assign data
q->prev =
q->data =
q->next =
and pointers*/
NULL;
num;
*s;
}
else
{
while(q->next != NULL ) /* goto the end of */
q = q->next;
/* list */
r = malloc(sizeof(NODE));
r->data = num;
r->next = NULL;
r->prev = q;
q->next = r;
}
}
/* adds a new node after the specified number of nodes */
d_add_after(NODE *q, int loc, int num)
{
NODE *temp;
int i;
/* skip to the desired position*/
for( i=0 ; i<loc; i++)
{
q = q->next;
if(q == NULL)
{
- 12 -
- 13 -
/* after deletion */
}
q = q->next ; /* goto next node if not found */
}
printf(\n Element %d not found,num);
}
As you must have realized by now any operation on linked list involves
adjustments of links. Since we have explained in detail about all the
- 14 -
NULL
NULL
New node
After Addition
99
Before Appending
P
99
- 15 -
After Appending
Addition of new node at the beginning.
Related Function : d_add_beg()
p
33
Before Addition
33
After Addition
in the DLL
- 16 -
temp
66
New Node
Before Insertion
N 1
temp
66
After Insertion
Fig 11. Insertion of
Deletion Of a Node
Case 1: Deletion of first node
Related function : d_delete()
p
55
Node to be deleted : 55
Before Deletion
- 17 -
After Deletion
88
Node to be deleted : 88
Before Deletion
After Deletion
77
Node to be deleted : 77
Before Deletion
- 18 -
After Deletion
Fig 11.Deletion of
in the DLL
- 19 -
return;
}
/* traverse both linked lists till the end. If end of any one linked
list is encountered then the loop is terminated */
while( p != NULL && q != NULL)
{
/* if node being added in the first list */
if ( *s == NULL)
{
*s = malloc(sizeof(NODE));
z = *s;
}
else
{
z->link = malloc(sizeof(NODE));
z = z->link;
}
if( p->data < q->data)
{
z->data = p->data;
p = p->link;
}
else
{
if( p->data > q->data)
{
z->data = q->data;
q = q->link;
}
else
{
if( p->data == q->data)
{
z->data = q->data;
q = q->link;
p = p->link;
}
}
}
}
/* if end of first list has not been reached */
while( p != NULL)
{
z->link = malloc(sizeof(NODE));
z = z->link;
z->data = p->data;
p = p->link;
- 20 -
}
/* if end of second list has not been reached */
while( q != NULL)
{
z->link = malloc(sizeof(NODE));
z = z->link;
z->data = q->data;
q = q->link;
}
z->link = NULL;
}
In this program, assume that structure NODE with data and link is
available. Also using add() used for singly linked list earlier we have two
linked lists. Three pointers point to three linked lists. The merge function
can be called to merge the two linked lists. This merged list is pointed to
by the pointer third. While merging two lists it is assumed that the lists
themselves are in ascending order.
main()
{
PNODE (first, *second, *total;
int i = 0;
first = second = total = NULL;/* empty linked lists */
- 21 -
p_append(&first,1,4,5);
p_append(&first,1,5,4);
p_append(&first,1,7,2);
p_append(&first,1,8,1);
p_append(&first,1,9,0);
clrscr();
display_p(first);
p_append(&second,1,5,6);
p_append(&second,2,5,5);
p_append(&second,-3,5,4);
p_append(&second,4,5,3);
p_append(&second,6,5,1);
display_p(second);
p_addition(first,second, &total)
display_p(total);
};
The function to append the polynomial p_append() and display_p() are
similar to our functions for singly linked list. So they are expected to be
written by the user. The function to add two polynomials is given below.
- 22 -
{
z->coeff = y->coeff;
z->exp = y->exp;
y = y->link;
/* goto the next node */
}
else
{
if( x->exp > y->exp)
{
z->coeff = x->coeff;
x->exp = x->exp;
x = x->link;
/* goto the next node */
}
else
{
if( x->exp == y->exp)
{
z->coeff = x->coeff + y->coeff;
x->exp = x->exp;
x = x->link;
/* goto the next node */
y = y->link;
/* goto the next node */
}
}
}
}
/*assign remaining elements of the first polynomial to the
result */
while( x != NULL)
{
if( *s == NULL)
{
*s = malloc(sizeof(PNODE));
z = *s;
}
else
{
z->link = malloc(sizeof(PNODE));
z = z->link;
}
z->coef = x->coef;
z->exp = x->exp;
x = x->link;
}
/*assign remaining elements of the second polynomial to the
result */
while( y != NULL)
{
if( *s == NULL)
{
- 23 -
*s = malloc(sizeof(PNODE));
z = *s;
}
else
{
z->link = malloc(sizeof(PNODE));
z = z->link;
}
z->coef = y->coef;
z->exp = y->exp;
y = y->link;
}
z->link = NULL; /* at the end of list append NULL*/
}
In this program two polynomials are built and pointed by the pointers
first and second. Next the function p_addition() is called to carry out the
addition of these two polynomials. In this function the linked lists
representing the two polynomials are traversed till the end of one of them is
reached. While doing this traversal the polynomials are compared on term-byterm basis. If the exponents of the two terms being compared are equal then
their coefficients re added and the result is stored in the third polynomial.
If the exponents are not equal then the bigger exponent is added to the third
polynomial. During the traversal if the end of one list is reached the
control breaks out of the while loop. Now the remaining terms of that
polynomial are simply appended to the resulting polynomial. Lastly the result
is displayed.
Exercises:
1. WAP for adding and deleting nodes from an ascending order linked list.
2. WAP to reverse a singly linked list by adjusting the links.
3. Write programs for reversing a doubly linked list (though it does not
serve any purpose it will give practice to manipulate the pointers.
4. WAP to delete a node after the specified node and before a specified
node using both singly and doubly linked lists.
5. WAP to break a linked list into two linked lists using both SLL and
DLL.
6. Write a program to add two polynomials using a DLL.
7. WAP to multiply two polynomials for both SLL and DLL.
8. WAP to add two long integers. Each integer may contain 15 to 20 digits,
which can be stored in nodes, a digit each or more depending on the
users choice. Add these long integers (from least significant digit
backwards) and display the resultant list.
- 24 -