Linked Lists
Linked Lists
Linked Lists
4.1. Introduction
Info Link
NODE
Fig 1. Structure of a Node
|data |Next|
main()
{
NODE *p;
clrscr();
display(p);
display(p);
disply(p);
disply(p);
printf(“\n No of elements in the linked list = %d”,
}
temp = *q;
r = malloc(sizeof(NODE));
r->data = num; /* add node at the */
r->link = NULL; /* end of the list */
temp->link = r;
}
Data Structures with ‘c’ 55
}
if( *q == NULL )
temp->data = num;
temp->link = NULL;
In the other case, when the linked list is not empty, the
condition :
if( *q == NULL)
temp = *q;
Then using temp we have traversed through the entire linked list
using the statements:
while(temp->link != NULL)
temp=temp->link;
p temp
p temp r
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.
temp->link = r;
temp
temp p
Before Addition
temp p
After Addition
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;
temp = q;
for( i=0 ; i<loc; i++)
{
temp = temp->link;
if(temp == NULL)
{
printf(“ There are less than %d elements in
the list”,loc);
return;
}
}
r = malloc(sizeof(NODE));
r->data = num;
r->link = temp->link;
temp->link = r;
}
99
Before Insertion
P temp
99
After Insertion
r->link = temp->link;
temp->link = r;
count(NODE *q)
{
int c = 0;
return (c);
}
display(NODE *q)
{
printf(“\n”);
temp = *q;
P old temp
node to be deleted = 4
Before deletion
temp
After deletion
PREV NEXT
NODE
The left pointer of the leftmost node and the right pointer
of the rightmost node are NULL indicating the end in each
direction.
main()
{
NODE *p;
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);
d_delete(&p,51);
d_delete(&p,21);
disply(p);
printf(“\n No of elements in the linked list = %d”,
}
}
else
{
r = malloc(sizeof(NODE));
r->data = num; /* add node at the */
r->next = NULL; /* end of the list */
r->prev = q;
q->next = r;
}
}
q = q->prev;
temp = malloc(sizeof(NODE));
temp->data = num;
temp->prev = q;
temp->next = q->next;
temp->next->prev = temp;
q->next = temp;
}
displayLR(NODE *q)
{
printf(“\n”);
displayRL(NODE *q)
{
printf(“\n”);
while( q != NULL)
{
printf(“%d”,q->data);
q=q->prev;
}
}
while( q != NULL)
{
if(q->data == num)
{
if(q == *s) /*if it is the first node */
{
*s = (*s)->next;
(*s)-> prev = NULL;
}
else
{
/* if the node is last node */
if( q->next == NULL)
q->prev->next = NULL;
else
/* node is intermediate */
{
q->prev->next = q->next;
q->next->prev = q->prev;
}
free(q);
}
return; /* after deletion */
}
q = q->next ; /* goto next node if not found */
}
printf(“\n Element %d not found”,num);
}
p = *s = NULL
Before Addition
NULL 1 NULL
New node
After Addition
P q
N 1 2 3 4 N
99 N
Before Appending
P q
N 1 2 3 4
After Appending
N 1 2 3 4 N
N 33
Before Addition
q p
N 33
1 2 3 4 N
p q
N 1 2 3 4 N
temp
N 66
New Node
Before Insertion
P q temp
2 3 66 4 N
N 1
After Insertion
p q
N 55 1 2 3 N
Node to be deleted : 55
Before Deletion
p q
N 1 2 3 N
After Deletion
p q
2 3 88 N
N 1
Node to be deleted : 88
Before Deletion
p q
2 3 N
Data Structures with ‘c’ 72
N 1
After Deletion
p q
N 1 2 77 3 4 N
Node to be deleted : 77
Before Deletion
N 1 2 3 N 4 N
After Deletion
if ( *s == NULL)
{
*s = malloc(sizeof(NODE));
z = *s;
}
else
while( p != NULL)
{
z->link = malloc(sizeof(NODE));
z = z->link;
z->data = p->data;
p = p->link;
}
while( q != NULL)
{
z->link = malloc(sizeof(NODE));
z = z->link;
z->data = q->data;
q = q->link;
}
main()
{
PNODE (first, *second, *total;
int i = 0;
p_append(&first,1,4,5);
p_append(&first,1,5,4);
p_append(&first,1,7,2);
Data Structures with ‘c’ 76
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);
};
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;
}
while( y != NULL)
{
if( *s == NULL)
{
*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;
}
Exercises: