Wednesday 22 August 2012

operation on linked list | Reverse | insert | add | delete | search

Data structure Program | operation on linked list | Reverse | insert | add | delete | search
#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct node
{
int info;
struct node *link;
}*start;

void create(int data)
{
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}

void display()
{
struct node *temp;
temp=start;
if(start==NULL)
printf("\nList Is Empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}

void insbeg(int data)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=start;
start=temp;
}

void insmid(int data,int pos)
{
int i;
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
q=start;
for(i=1;i<pos-1;i++)
q=q->link;
temp->link=q->link;
q->link=temp;
}

void insend(int data)
{
struct node *temp,*q;
q=start;
temp=malloc(sizeof(struct node));
temp->info=data;
while(q->link!=NULL)
q=q->link;
q->link=temp;
temp->link=NULL;
}

void delfir()
{
struct node *temp;
temp=start;
start=start->link;
free(temp);
}

void delmid(int data)
{
struct node *temp,*q;
q=start;
while(q->link->link!=NULL)
{
if(q->link->info==data)
{
temp=q->link;
q->link=temp->link;
free(temp);
}
q=q->link;
}
}

void delend()
{
struct node *q,*temp;
q=start;
while(q->link->link!=NULL)
q=q->link;
temp=q->link;
q->link=NULL;
free(temp);
}

void rev()
{
struct node *p1,*p2,*p3;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}

void delpos(int pos)
{
int i;
struct node *q,*temp;
q=start;
for(i=1;i<pos-1;i++)
q=q->link;
temp=q->link;
q->link=temp->link;
free(temp);
}

void search(int data)
{
int i=0,flag=1;
struct node *temp;
temp=start;
while(temp!=NULL)
{
i++;
if(temp->info==data)
{
printf("\n%d is found at %d position",data,i);
flag=0;
break;
}
temp=temp->link;
}
if(flag==1)
printf("%d is not found",data);
}

void searchpos(int pos,int n)
{
int i;
struct node *temp;
temp=start;
if(pos>n)
printf("\nyou enter wrong position");
else
{
for(i=1;i<pos;i++)
temp=temp->link;
printf("\n%d is found at %d position",temp->info,i);
}
}

void menu()
{
int ch,n,num,i,pos;
puts("\n\n1:for create list");
puts("2:for display");
puts("3:for exit");
puts("4:for insertion at begining");
puts("5:for insertion at between");
puts("6:for insertion at end");
puts("7:for delete first element");
puts("8:for deletion at between");
puts("9:for delete last element");
puts("10:for reverse the list");
puts("11:for delete element by position");
puts("12:for searching the element");
puts("13:for searching the element by position");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nhow many node you want to enter:- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the %d element:- ",i+1);
scanf("%d",&num);
create(num);
}
menu();
break;
case 2:
display();
menu();
break;
case 3:
exit(0);
case 4:
printf("\nenter the element you want to insert:- ");
scanf("%d",&num);
insbeg(num);
menu();
break;
case 5:
printf("\nenter the element you want to insert:- ");
scanf("%d",&num);
printf("\nenter the position you want to insert:- ");
scanf("%d",&pos);
insmid(num,pos);
menu();
break;
case 6:
printf("\nenter the element you want to insert:- ");
scanf("%d",&num);
insend(num);
menu();
break;
case 7:
delfir();
menu();
break;
case 8:
printf("\nenter the element you want to delete:- ");
scanf("%d",&num);
delmid(num);
menu();
break;
case 9:
delend();
menu();
break;
case 10:
rev();
menu();
break;
case 11:
printf("\nenter the position:- ");
scanf("%d",&pos);
delpos(pos);
menu();
break;
case 12:
printf("\nenter the element you want to search:- ");
scanf("%d",&num);
search(num);
menu();
break;
case 13:
printf("\nenter the position for searching the element:- ");
scanf("%d",&pos);
searchpos(pos,n);
menu();
break;
default:
printf("\nYou enter wrong choice");
menu();
}
}

void main()
{
clrscr();
start=NULL;
menu();
getch();
}
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: