#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
void insbeg();
void insend();
void inskpos();
void display();
struct node * search();
void sort();
struct node
{
int info;
struct node *link;
}*start=NULL;
void main()
{
int ch;
char ans;
struct node *l;
clrscr();
do
{
printf("Select any one");
printf("\n1. Insertion at beginning");
printf("\n2. Insertion at end");
printf("\n3. Insertion at 'Kth' position");
printf("\n4. Search location of an element in the list");
printf("\n5. Sort list");
printf("\n6. Display list");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: insbeg();
break;
case 2: insend();
break;
case 3: inskpos();
break;
case 4: l=search();
printf("\nAddress of element is %d",l);
break;
case 5: sort();
break;
case 6: display();
break;
default:printf("\nInvalid input");
}
printf("\nDo you wish to continue?");
flushall();
scanf("%c",&ans);
}while(ans=='Y'|| ans=='y');
getch();
}
//------------------------------------------------------------------------------------------------------------
void insbeg()
{
struct node *temp;
temp=(struct node *)malloc (sizeof(struct node));
printf("\nEnter info");
scanf("%d",&temp->info);
temp->link=start;
start=temp;
}
//------------------------------------------------------------------------------------------------------------
void insend()
{
struct node *ptr;
ptr=start;
if(start==NULL)
{
start=(struct node *)malloc(sizeof(struct node));
printf("\nEnter info");
scanf("%d",&start->info);
start->link=NULL;
}
else
{
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=(struct node *)malloc(sizeof(struct node));
ptr=ptr->link;
printf("\nEnter info");
scanf("%d",&ptr->info);
ptr->link=NULL;
}
}
//-----------------------------------------------------------------------------------------------------------
void inskpos()
{
int k,i;
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
ptr=start;
printf("\nEnter position");
scanf("%d",&k);
if(k==1)
{
printf("\nEnter info");
scanf("%d",&temp->info);
temp->link=start;
start=temp;
}
else
{
for(i=1;i<=k-2;i++)
{
if(ptr==NULL)
{
printf("\nList is short,can't insert");
return;
}
ptr=ptr->link;
}
printf("\nEnter info");
scanf("%d",&temp->info);
temp->link=ptr->link;
ptr->link=temp;
}
}
//------------------------------------------------------------------------------------------------------------
void display()
{
struct node *ptr=start;
while(ptr!=NULL)
{
printf("\n%d",ptr->info);
ptr=ptr->link;
}
}
//------------------------------------------------------------------------------------------------------------
struct node * search()
{
int item;
struct node *ptr=start,*loc;
printf("\nEnter item");
scanf("%d",&item);
while(ptr!=NULL && ptr->info!=item)
ptr=ptr->link;
if(ptr->info==item)
loc=ptr;
else
loc=NULL;
return loc;
}
//------------------------------------------------------------------------------------------------------------
void sort()
{
int temp;
struct node *ptr=start,*ptr1;
while(ptr->link!=NULL)
{
ptr1=ptr->link;
while(ptr1!=NULL)
{
if(ptr->info>ptr1->info)
{
temp=ptr->info;
ptr->info=ptr1->info;
ptr1->info=temp;
}
ptr1=ptr1->link;
}
ptr=ptr->link;
}
}
No comments:
Post a Comment