//LICA...MCA SEM-2
//All Operations of Linkllist
//Guided By: Manjoor Hussain Kapoor
#include
#include
#include
struct node
{
int data;
struct node *next;
}*start=NULL,*list;
void addbeg()
{
struct node *temp,*t;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter Data Value\n");
scanf("%d",&temp->data);
if(start==NULL)
{
temp->next=NULL;
start=temp;
}
else
{ t=(struct node *)malloc(sizeof(struct node));
t=start;
temp->next=start;
start=temp;
}
}
void addend()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter Data: ");
scanf("%d",&temp->data);
temp->next=NULL;
list=start;
if(start==NULL)
{
start=temp;
}
else
{
while(list->next!=NULL)
{
list=list->next;
}
list->next=temp;
list=temp;
}
}
void addinbet()
{
int n,count=0;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter Value: ");
scanf("%d",&temp->data);
printf("Enter Node Position You Want to Add After :");
scanf("%d",&n);
list=start;
while(list->next!=NULL)
{
count++;
if(count==n)
{
temp->next=list->next;
list->next=temp;
printf("Element Added Succesfully\n");
}
/* else
{
printf("Linklist is not yet reached this limit");
}*/
list=list->next;
}
}
void delstart()
{
start=start->next;
}
void delend()
{
list=start;
while(list->next->next!=NULL)
{
list=list->next;
}
list->next=NULL;
}
void delinbet()
{
int cout=0;
int n;
list=start;
printf("Enter postion you want to delete\n");
scanf("%d",&n);
while(list->next!=NULL)
{
cout++;
if(cout==n-1)
{
list->next=list->next->next;
printf("Node Deleted Succesfully\n");
}
list=list->next;
}
}
void linksort()
{
int temp ;
struct node *t1,*t2=(struct node *)malloc(sizeof(struct node));
for(t1=start;t1;t1=t1->next)
{
for(t2=t1;t2;t2=t2->next)
{
if(t1->data>t2->data)
{
temp=t1->data;
t1->data=t2->data;
t2->data=temp;
}
}
}
}
void display()
{
struct node * disp;
disp=(struct node *)malloc(sizeof(struct node));
disp=start;
while(disp)
{
printf("%d\t",disp->data);
disp=disp->next;
}
}
void main()
{
int ch;
char conn;
clrscr();
do
{
printf("\n1.Create @ Begning");
printf("\n2.Create @ End");
printf("\n4.Inbetween");
printf("\n5.Delete Start");
printf("\n6.Delete End");
printf("\n7.Delete Inbetween");
printf("\n8.LinkList Sort");
printf("\n3.Display");
printf("\n0.Exit\n");
scanf("\n%d",&ch);
switch(ch)
{
case 1:
addbeg();
break;
case 2:
addend();
break;
case 3:
display();
break;
case 4:
addinbet();
break;
case 5:
delstart();
break;
case 6:
delend();
break;
case 7:
delinbet();
break;
case 8:
linksort();
break;
case 0:
break;
};
printf("\nWant to continue\n");
fflush(0);
scanf("%c",&conn);
}while(conn=='y'||conn=='Y');
getch();
}
thx dude..
ReplyDelete