Wednesday, 12 February 2014

Operations On linked lists(Insertion,Deletion,Reverse list) Cprogram

Aim: To implement linked list operations(Insertion,Deletion,Reverse list) in Cprogram

Program:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void create();

void deletion();

void display();

void insert();

void reverse();

struct list

{

int data;

struct list *next;

};

typedef struct list node;

node *header;

void main()

{

int ch,item;

clrscr();

while(1)

{

printf("1.create  2.insert  3.deletion  4.display  5.reverese  6.exit\n");

printf("enter your choice");

scanf("%d",&ch);

switch(ch)

{

case 1:

create();

break;

case 3:

deletion();

break;

case 4:

display();

break;

case 2:

insert();

break;

case 5:

reverse();

break;

default :

exit(0);

}

}

}

void create()

{

header=(node*)malloc(sizeof(node));

header->data=NULL;

header->next=NULL;


}

void insert()

{

node *new,*ptr,*prev;

int item,pos,element;

printf("enter the item you want 2 insert");

scanf("%d",&item);

new=(node*)malloc(sizeof(node));

new->data=item;

printf("enter position of insertio 1.begining\t 2.end\t 3.any position");

scanf("%d",&pos);

switch(pos)

{

case 1:

new->next=header->next;

header->next=new;

break;

case 2:

ptr=header;

while(ptr->next!=NULL)

ptr=ptr->next;

new->next=NULL;

ptr->next=new;

break;

case 3:

printf("enter the element after which you want 2 insert:");

scanf("%d",&element);

ptr=header->next;

while(ptr->next!=NULL&&ptr->data!=element);

ptr=ptr->next;

if(ptr->data==element)

{

new->next=ptr->next;

ptr->next=new;

}

else

{

printf("element not found\n");

break;

}

}

}

void deletion()

{

node *ptr,*prev;

int pos,item;

printf("1.delete front \t2. delete last\t 3.delete any");

scanf("%d",&pos);

switch(pos)

{

case 1:

{

ptr=header->next;

if(ptr==NULL)

printf("list is empty\n");

else

{

header->next=ptr->next;

free(ptr);

}

break;

}

case 2:

{

ptr=header->next;

prev=header;

if(ptr==NULL)

printf("list is empty\n");

else

{

while(ptr->next!=NULL)

{

prev=ptr;

ptr=ptr->next;

}

prev->next=NULL;

free(ptr);

}

break;

}

case 3:

{

ptr=header->next;

prev=header;

printf("enter the item you want to delete");

scanf("%d",&item);

while(ptr->data!=item&&ptr->next!=NULL)

{

prev=ptr;

ptr=ptr->next;

}

if(ptr->data==item)

{

prev->next=ptr->next;

free(ptr);

}

else

printf("item not found\n");

break;

}

}

}

void display()

{

node *ptr;

ptr=header->next;

printf("header->");

while(ptr!=NULL)

{

printf("%d->",ptr->data);

ptr=ptr->next;

}

printf("NULL\n");

}

void reverse()

{

node *p1,*p2,*p3,*last;

p1=header->next;

if(p1==NULL)

printf("list is empty");

else

{

p2=p1->next;

p3=p2->next;

last=header->next;

while(last->next!=NULL)

last=last->next;

if(p3==NULL)

{

p1->next=NULL;

p2->next=p1;

header->next=p2;

}

else

{

p1->next=NULL;

while(p3!=last)

{

p2->next=p1;

p1=p2;

p2=p3;

p3=p3->next;

}

p2->next=p1;

p3->next=p2;

header->next=p3;

}

}

display();

}

output:















No comments:

Post a Comment