Beruflich Dokumente
Kultur Dokumente
Array is not always good choice for data storage though all the usual operations , i.e.
deletion , insertion , updation are performed easily on arrays.
Specifically they are not suited in following conditions:
(i) Unpredictable storage requirement
(ii) Extensive manipulation of stored data (insertion / deletion)
A linked list also contains a list pointer variable called start which contains the address
of first node in the list.
Building a linked list
(1) Declare the structure that define the list entries
Struct list
{
Int data ; /* information * /
Struct list *next; /* reference */
};
A simple example of linked list
# include<stdio.h>
#include<stdlib.h>
main()
{
struct list
{
int data;
struct list * next;
};
struct list * start, *p, *r;
start = (struct list *) malloc ( sizeof (struct list)); start
start->data=15; 15
q=(struct list *) malloc (sizeof( struct list));
q->data =18; start
q->next =NULL; 15 18 NULL
start->next =q;
r = p; q
while (r!=NULL) data= 15
{ dat = 18
printf(“Data = %d\n”,r->data);
r=r->next;
}
}
Insertion
NULL
start
NULL
NULL
NULL
` NNULL
Main()
{
Struct list * start;
- - - - - -
Insert_lnode(start)
}
void insert_lnode (start list * start)
{
struct list * ptr1, *ptr2;
int data;
if ( start == NULL)
{
start = (struct list *) malloc (sizeof (struct list));
print(“\n Enter data for new node”);
scanf(“%d”, & data);
start-> data = data;
start-> next = NULL;
}
else
{
ptr2 = start;
while (ptr2-> next != NULL)
ptr2 = ptr2-> next;
ptr1 = (struct list *)malloc (sizeof(struct list *));
printf(“\n Enter data for new node”);
scanf(“%d”, &data);
ptr1-> next = NULL;
ptr2-> next = ptr1;
}
void traverse _l list (struct list * start)
{
int i = 1;
struct list * current;
current = start ;
while (current != NULL)
{
printf(“\n Node %d data = %d”, i , current-> data);
i++;
current = current-> next;
}
}
Insert in-between the linked list
start
NULL
NNULL
Main()
{
Int at_number;
Struct list * start;
... ............
Insert_bnode (start , at_number);
. . . . . . . . . . . . .. . . . . .. . . ....
}
NULL
If list is empty
Do nothing
Else
Make the start
Pointer to point to second node.
main()
{
struct list * start;
. . . . . .. . . .. . . . . .
delete_lnode (start)
.................
}
void delete_lnode (struct list * start)
{
struct list * ptr1, ptr2;
if (start == NULL)
{
printf(“\n there is no node to delete the list is empty”);
return;
}
if(start-> next == NULL)
{
ptr1 = start;
start= NULL;
fflush (ptr1);
}
}
Delede last node
start
NULL NULL
NULL
If list is empty
Do nothing
Else make the next pointer of second-last to poin to NULL
main()
{
struct list * start;
. . . . . . . . .. . .. . . .
delete_ldalete (start);
. . . .. . .. . .. . . . . . . . . ..
}
NULL NULL
(1)
(1) Let node represents the node to deleted.make the next pointer of the previous (node)
in the list.
main ()
{
int node _num;
struct list *start;
. . . . .. . . . . . . . . .
delete_bnode (stsrt , node_num)
. . .. . . . . . . .. . . . . . . . . . .. . . . . . . .
}
]
Searching a linked list
(i) Whether information is present in the list ?
(ii) At which location or how many location ?
1. Current = start /*initialize current poiner*/
2. Set counter = start /*increment poinetr*/
3. Repeat while (current !=NULL) / *find information*/
If ( information is found)
Print location and
Exit
Else
Counter ++ / * increment counter* /
Counter = current -> next /* increment pointer*/
4. Information isa not present ,
5. Exit
main()
{
int info;
struct list * start;
. . .. . . . . . . . . . . . . . . .
search_llist (start , info)
.. ...... .. ...............
if list is empty
do nothing
else make the next pointer
of second_last to point
to NULL
main()
{
struct list *start;
----------------------
delete_lnode (start);
-------------------------
}
Start
Ptr1 ptr2
Lets node represents the node to be deleted. Make the next pointer of the previous (node) to
point to next (node) in the list.
main()
{
int node_num;
struct list *start;
--------------------------
delete_bnode(start,node_num)
--------------------------------------
}
{
int i;
Struct list *ptr1,*ptr2;
ptr1=start;
ptr2=ptr1->next;
for(i=2;i<number;i++)
{
ptr1=ptr1->next;
ptr2=ptr2->next;
}
ptr1->next=ptr2->next;
fflush(ptr2);
}
Searching a linked list
Main()
{
int info;
struct list *start;
--------------------------
search_list (start,info)
--------------------------------------
}
}/*end function*/
LIST 1
Start
start List2
start
main()
{
struct list *start1,*start2;
-----------------------------------
merge_lists(start1,start2);
-----------------------------------
if(start2==NULL)
return;
current=start1;
while(current>next!=NULL)
current=current->next;
current->next=start2;
}
Information Field
Declaration
struct list
{
int data;
struct dlist *left;
struct dlist *right;
}
Struct doublelist
{
struct dlist *fnode;
struct dlist *lnode;
Main()
{
struct doublelist dblist;
------------------------------
create_dlist(dblist);
------------------------------
ptr->right=Null;
ptr->left=dblist->lnode;
ptr->left->right=ptr; lnode
or fnode
dblist->lnode->right=ptr;
dblist->lnode=ptr;
}
} ptr
Traverse a linked list
Main()
{
struct doublelist dblist;
------------------------------
traverse_dlist(dblist);
------------------------------
{
Struct dblist *ptr;
ptr=dblist->fnode;
while(ptr!=NULL)
{
process the node;
ptr=ptr->right;
}
{
struct doublelist dblist;
------------------------------
traverse_dlist(dblist);
------------------------------
{ temptr
Ptr=dblist->fnode; (3)
Ptr->right->left=temptr; (2)
Temptr->left=ptr; (3)
Ptr->right=temptr; (4)
Deletion
Delete the leftmost(first) node
Delete the rightmost (last) node
Delete a node in between the list
Fnode lnode
(Before)
(After)
(b) (c)
Fnode lnode
(Before)
Fnode lnode
(After)
(a) (b)
(2)
ptr (3)
(Before) (After)
{
struct doublelist dblist;
-------------------------------
delete dblist(dblist);
-----------------------------
{
struct dlist *ptr;
ptr=dblist->fnode;
(let node (b) is to be deleted)
move the pte upto node (a)
ptr->right=ptr->right->right; (2)
fflush(ptr->right->left); (4)
ptr->left->right=ptr; (3)
Before merging
fnode1 lnode1
Lnode1->right=fnode2; (1)
fnode2->left=lnode1; (2)
lnode1=lnode2; (3)
Header Linked List
A header node is a special node which is found in front of the list. A list which contains
this type of node is called header linked list.
Addition of polynomials
F1=5x5-3x3+2x2+x1+10x0
Struct polynomial struct head_poly
{ {
int coefficient; int num_terms;
int exponent; struct polynomial *next;
struct polynomial *next; }
}
start
5 5 5 -3 3 2 2 1 1 10 0
F2=6x6+4x4+2x2-x
start
4 6 6 4 4 2 2 -1 1
p(x)=F1(x)+F2(x)
=6x6+5x5+4x4-3x3+4x2+10x0
start
6 6 6 5 5 4 4 -3 3 4 2 10 0
{
int info;
int next;
};
------------------------------------------------
Avail=0;
For(i=0;i<NUMNODES;i++)
node[i].next=i+1;
Node[NUMNODES-1].next=-1;
-----------------------------------------
{ {
int p; int q;
if(avail==-1) if(p===-1)
{ {
printf(“\n overflow\n”); printf(\n void insertion);
exit(1); return;
} }
p=avail; q=getnode();
availnode[avail].next; node[q].info=x;
return(p); node[q].next=node[p].next;
Void freenode(int p)
{
node[p].next=avail;
avail=p;
return;
}/*end freenode*/
Void delafter(intp,int *px)
{
int q;
if(p===-1)!!(node[p].next==-1))
{
printf(“void deletion”);
return;
q=node[p].next;
*px=node[q].info;
node[p].next=node[q].next;
freenode(q);
return;
}/*end delafter*/