/* Linked List all operations */
#include<stdio.h>
#include<conio.h>
typedef struct ll
{
struct ll *next;
int data;
}node;
node *getnode()
{
int item;
node *temp;
temp=(node *)malloc(sizeof(node));
printf("\nenter data:");
scanf("%d",&item);
temp->data=item;
temp->next=NULL;
return temp;
}
node *create()
{
int ch;
node *trv,*start,*neww;
start=trv=NULL;
do
{
neww=getnode();
if(start==NULL)
{
start=neww;
trv=start;
}
else
{
trv->next=neww;
trv=trv->next;
}
printf("\nEnter any key to continue & 0 for stop:");
scanf("%d",&ch);
}
while(ch!=0);
return start;
}
node *insert_before_val(node *start)
{
node *neww,*trv,*prv;
int val;
if(start==NULL)
{
printf("you should atleast create link list");
}
else
{
trv=start;
printf("\nEnter value before you want to add new value:");
scanf("%d",&val);
while(trv->data!=val && trv!=NULL)
{
prv=trv;
trv=trv->next;
}
if(trv==NULL)
{
printf("make sure that %d is there",val);
}
else
{
neww=getnode();
neww->next=prv->next;
prv->next=neww;
}
}
}
int search(node *start,int ele)
{
int pos;
node *trv;
pos=0;
trv=start;
while(trv!=NULL)
{
pos++;
if(trv->data==ele)
{
break;
}
trv=trv->next;
}
return pos;
}
int count(node *start)
{
int c=0;
node *trv;
trv=start;
while(trv!=NULL)
{
c++;
trv=trv->next;
}
return c;
}
void insert_after_val(node *start)
{
node *trv,*neww;
int val;
trv=start;
if(start==NULL)
{
printf(“atleast create a node”);
}
else
{
printf(“\nEnter value after you want to add element\n”);
scanf(“%d”,&val);
while(trv->data!=val && trv!=NULL)
{
trv=trv->next;
}
if(trv==NULL)
{
printf(“\nmake sure %d is there\n”,val);
}
else
{
neww=getnode();
neww->next=trv->next;
trv->next=neww;
}
}
}
node *insert_first(node *start)
{
node *neww;
neww=getnode();
if(start==NULL)
{
neww=start;
}
else
{
neww->next=start;
start=neww;
}
return start;
}
node *insert_last(node *start)
{
node *neww,*trv;
neww=getnode();
if(start==NULL)
{
start=neww;
}
else
{
trv=start;
while(trv->next!=NULL)
{
trv=trv->next;
}
trv->next=neww;
}
return start;
}
node *del_first(node *start)
{
node *trv;
trv=start;
if(start==NULL)
{
printf(“\nAlready empty\n”);
}
printf(“\n%d is deleted\n”,start->data);
start=start->next;
free(trv);
return start;
}
void *del_last(node *start)
{
node *trv,*prv;
trv=start;
if(start==NULL)
{
printf(“\nEmpty!\n”);
}
else
{
while(trv->next!=NULL)
{
prv=trv;
trv=trv->next;
}
printf(“%d is deleted”,trv->data);
prv->next=NULL;
free(trv);
}
}
void display(node *start)
{
node *trv;
trv=start;
if(start==NULL)
{
printf(“\nNo node to display\n”);
}
printf(“\n”);
while(trv!=NULL)
{
printf(“%d “,trv->data);
trv=trv->next;
}
}
void main()
{
int ch,c,p,ele;
node *s=NULL;
clrscr();
while(1)
{
printf(“\n\n1.Create\n2.Insert front\n3.insert last\n4.Delete first\n5.Delete last\n6.display\n7.insert before value\n8.insert after val\n9.count\n10.search\n15.exit:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:s=create();
break;
case 2:s=insert_first(s);
break;
case 3:s=insert_last(s);
break;
case 4:s=del_first(s);
break;
case 5:del_last(s);
break;
case 6:display(s);
break;
case 7:insert_before_val(s);
break;
case 8:insert_after_val(s);
break;
case 9:c=count(s);
printf(“\ntotal node are %d\n”,c);
break;
case 10:
printf(“\nEnter element to search:”);
scanf(“%d”,&ele);
p=search(s,ele);
printf(“%d found at %d position”,ele,p);
break;
case 15:exit(0);
break;
default:printf(“\nEnter proper choice\n”);
}
}
}
No comments:
Post a Comment