Sie sind auf Seite 1von 25

Linked list

 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)

Array Linked list


1. Insertion/ deletion at a specific 1. Insertion/ deletion at a specific
print in array requires moving of print of a list is a constant-time
other elements. operation.
2. Fixed size is a limitation. It acts as 2. There is no cap on the data
cap on data storage storage
3. Allows constant-time random 3. Sequential access to the
access to the data elements.
4. There are no overhead of 4. Requirement of extra storage for
references. references.

What is a linked list ?


 A Linked list is a collection of nodes. Each node consists of two parts:
(i) Information
(ii) Pointer to next node reference

Information (address of next node or NULL)

 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;
}
}

General Algorithm to create linked list


1. Start =NULL /* initially the linked list is empty */
2. Ptr1 = create (node) /* create first node */
node.info = info
node . next = NULL
3. Start = ptr1
4. While(condition = true) /* add other node */
Ptr2 = create (node)
Node.info = info
Node.next = NULL
Ptr1.next = ptr2;
Ptr1 = ptr2;
5. Exit
main()
{
struct list * start;
- - -- ------ --
create_list(start);
- ---------- -
}

void create_list (struct list * start)


{
struct list * ptr1 , + ptr2;
char ch;
int i = 1, data ;
ptr1 = (struct list *) malloc (sizeof (struct list));
printf (“\n Enter data of node %d ”, i);
i++;
scanf(“%d”, &data);
ptr1 -> data = data;
ptr1->next =NULL;
start = ptr1;
printf(“\n Enter “n” to stop”);
ch = getch();
while (ch != ‘n’)
{
ptr2 = (struct list *) malloc (sizeof (struct list));
printf(“\n Enter the data of node %d”, i);
i++;
scanf(“%d”,&data);
ptr2-> data =data;
ptr2-> next= NULL;
ptr1-> next = ptr2;
ptr1 = ptr2;
printf(“\n Enter “n” to stop”);
ch = getch();
}
}
Traversing a linked list

1 . Current = start /*initialize current pointer*/


2. Repeat while (current!= NULL) /*Traverse whole list*/
Process node
Current = current + next /*move pointer*/
3. Exit

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;
}
}

Insertion

 Insert as a first node


 Insert as a last node
 Insert in-between the linked list
Insert as a first node

NULL

Create node (1)


(1). Create new node.
(2). New node will point to first node.
(3). Start will point to new node.
main()
{
struct list * start;
- - - - - - - -
insert_fnode (start);
}

void insert_fnode (struct list * start)


{
struct list *ptr;
int data;
ptr = (struct node *) malloc (sizeof (struct list));
printf(“\n Enter data for new node”);
scanf(“%d’, &data);
ptr-> data = data
ptr-> next =start;
start = ptr;
}

Insert as a last node

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);
. . . . . . . . . . . . .. . . . . .. . . ....
}

void insert_bnode (struct list * start , int number)


{
struct list * ptr1, *ptr2;
int i, data;
ptr2 = start;
for (i=1;i<number;i++)
ptr2 = ptr2->next;
ptr1 = (struct list *) malloc (sizeof (struct list));
printf(“\n Enter data for new node”);
scanf(“%d”, &data);
ptr1-> data = data;
ptr1-> next = ptr2-> next;
ptr2-> next = ptr1;
}
Deletion
 Delete first node
 Delete last node
 Delete in-between node

Delete first node

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);
. . . .. . .. . .. . . . . . . . . ..
}

void() delete_lnode (struct list * start)


{
struct list *ptr1, *ptr2;
if (start == NULL)
{
printf(“\n there is no node to delete . The is empty”);
return;
} start
if (start->next == NULL) NULL
{
ptr1 = start;
start = NULL; ptr1
fflush (ptr1);
else
{ start
Ptr1 =start ;
Ptr2 = ptr1->next;
While (ptr2-> next != NULL) ptr1 ptr2
{
ptr1 = ptr1->next;
ptr2 = ptr2->next;
} /*end while*/
Ptr1-> next =NULL;
Fflush (ptr2);
} /*end else*/
}

Another variation of the delete_lnode()

void Delete_lnode (struct list * start)


{
struct list *ptr;
if (start == NULL)
{
printf(“\n There is no node to delete. The is empty”);
return;
}
if (start-> next == NULL)
{
fflush (start);
start = NULL;
}
else
{
ptr = start;

while (ptr-> next->next !=NULL)


ptr = ptr-> next;
fflush (ptr -> next);
ptr-> next =NULL;
}
}
Delete in_between node

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)
. . .. . . . . . . .. . . . . . . . . . .. . . . . . . .
}

void delete_bnode (struct list * start , int number)


{
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
(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)
.. ...... .. ...............

void search_llist (struct list *start , int info)


{
int location;
struct list * curret;
location =1;
current start;
while (current !=NULL)
{
if (current -> data == info)
{
P[rintf(“\n The information %d is present at node %d”, info, location);
return;
}
}
}
main()
{
struct list *start;
delete_fnode(start);
}

void delete_fnode(struct list *start)


{
struct list *ptr;
if(start==NULL)
printf(“\n There is no node to delete.The list is empty”);
else
{
ptr=start;
start=start->next;
fflush(ptr);
return;
}
}

Delete Last node

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);
-------------------------
}

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(ptrr1);
}

else ptr1 ptr2


{
ptr1=start;
ptr2=ptr1->next;
while(ptr2->next !=NULL)
{
ptr1=ptr1->next;
ptr2=ptr2->next;
}
ptr1->next=NULL;
fflush(ptr2);
}

Another variation of the Delete_lnode()

Void delete_lnode(struct list *start)


{
struct list *ptr;
if(start==NULL)
{
printf(“\n There is no node to delete. The list is empty”);
return;
}
if (start->next==NULL)
{
fflush(start);
start=NULL;
}
else
{
ptr1=start;
while(ptr->next ->next!=NULL)
{
ptr=ptr->next;
fflush(ptr->next);
ptr->next=NULL;
}
}
}

Delete In between node

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)
--------------------------------------
}

Void delete_bnode(struct list *start,int number)

{
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

 Whether information is present in the list?


 At which location or how many locations?

1. Current=start /*initialize current pointer */


2. Set counter=0 /*initialize counter */
3. Repeat while(current!=NULL) /*find information */
if(information is found)
print location and
exit
else
counter++ /*increment counter*/
current=current->next /*increment pointer */
4. Information is not found
5. Exit

Main()
{
int info;
struct list *start;
--------------------------
search_list (start,info)
--------------------------------------
}

Void search_list(struct list *start,int info)


{
int location;
struct list *current;
location=1;
current=start;
while(current!=NULL)
{
if(current->data==info)
{
printf(“the information %d is present at node %d”,info,location);
return;
}
else
{
location++;
current=current->next;
}
} /*end while */
printf(“the information %d is not present in the linked list ”, info);

}/*end function*/

Merging two linked list

LIST 1

Start

start List2

start

Final merged list

1. Move the last node of list1 /*list2 is to be merged to the list1 */


2. Make this node to point to first node of the list1.
3. Exit.

main()

{
struct list *start1,*start2;
-----------------------------------
merge_lists(start1,start2);
-----------------------------------

Void merge_lists(struct list *start1,struct list *start2)


{
struct list *current;
if(start1==NULL) list1 list2
{ 0 0
start1=start2; 0 1
return; 1 0
} 1 1

if(start2==NULL)
return;
current=start1;
while(current>next!=NULL)
current=current->next;
current->next=start2;
}

Doubly Linked List

Left link Right Link

Information Field

Declaration
struct list
{
int data;
struct dlist *left;
struct dlist *right;
}

Struct doublelist
{
struct dlist *fnode;
struct dlist *lnode;

Create a double linked list

Main()

{
struct doublelist dblist;
------------------------------
create_dlist(dblist);
------------------------------

Void create_dlist(struct doublelist dblist)


{
struct dlist *ptr; /*creating first node*/
ptr=(struct dlist *)malloc(sizeof(struct dlist));
ptr->right=NULL; ptr
ptr->left=NULL;
dblist->lnode=ptr; lnode fnode
dblist->fnode=ptr;
while(condition is true)
{
ptr=(struct dlist*) malloc(sizeof(structdlist));

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);
------------------------------

Void traverse_dlist(struct doublelist dblist)

{
Struct dblist *ptr;

ptr=dblist->fnode;
while(ptr!=NULL)
{
process the node;
ptr=ptr->right;
}

Insert a new node in the doubly linked list

1. Insert before first node.


2. Insert after a last node trivial
3. Insert in between nodes.
Insert in between nodes
Main()

{
struct doublelist dblist;
------------------------------
traverse_dlist(dblist);
------------------------------

Void insert_bdlist(struct doublelist dblist)

{ temptr

Struct dlist *ptr,*temptr;

Ptr=dblist->fnode; (3)

Move the pointer upto node (a) (4) (1) (2)

The new node is to be inserted between


node (a) and node(b)

Temptr=(struct dlist*)malloc(sizeof(struct dlist)); (a) (b)

Temptr->right=ptr->right; (1) ptr

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

Leftmost (first) node

Fnode lnode

(Before)

(a) (b) (c)


Fnode lnode

(After)

(b) (c)

1. Make the fnode to point to node (b) of node (a)


2. Make the left pointer to NULL
3. Do garbage collection for node(a)

Rightmost (last) node

Fnode lnode

(Before)

(a) (b) (c)

Fnode lnode

(After)

(a) (b)

1. Move to the node (b)


2. Make the lnode to point to node (b)
3. Make the right pointer of node (b) to null
4. Do garbage collection for node(c)

Delete in-between node

(2)

(4) (a) (c)


(a) (b) (c)

ptr (3)
(Before) (After)

1. Move to node (a)


2. Make right pointer of node (a) to point to node (c)
3. Make left pointer of node (c) to point to node (a)
4. Do garbage collection for node (b).
Main()

{
struct doublelist dblist;
-------------------------------
delete dblist(dblist);
-----------------------------

Void delete_dblist(struct doublelist 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)

Merging two linked list (3)


fnode1 lnode1 (1) fnode2 lnode2

(a) (b) (c) (2) (d) (e) (f)

Before merging

fnode1 lnode1

(a) (b) (c) (d) (e) (f)

After Merging:Merged List

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.

Struct list Strruct head


{ {
char f_name[25]; int num_node;
char l_name[25] struct list *next;
float cgpa; }
struct list *next;
}
node of a list header of a node

Header node list nodes

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

#define NUMNODES 500


Struct nodetype

{
int info;
int next;

};

Struct node type node[NUMNODES];

------------------------------------------------

Avail=0;

For(i=0;i<NUMNODES;i++)
node[i].next=i+1;

Node[NUMNODES-1].next=-1;

-----------------------------------------

Int getnode(void) void insafter(int p,int x)

{ {
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;

} /*end getnode*/ node[p].next=q;


return;
}/*end insafter*/

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*/

Das könnte Ihnen auch gefallen