Pages - Menu

Tuesday, 22 July 2014

Implementation of linked list in C.

Here is the code..

#include<stdio.h>


#define m 10
int del[m],i=0;
struct node
{
int info;
struct node *next;
} *start= NULL,*d = NULL;
void traverse();
void insert_beg();
void insert_end();
void insert_pos();
void delete_beg();
void delete_end();
void delete_pos();
void main()
{
int i,ch=1;

printf(" 1:traversing\t2.insertion at beggining\n");
printf("3:insertion at end\t 4:insertion at position\n");
printf(" 5:delettion from begining\t 6:deletion at end\n");
printf("7:deletion from position\n");
while(ch==1)
{
printf("Enter your choice\n");
scanf("%d",&i);
switch(i)
{
case 1:traverse();
break;
case 2:insert_beg();
break;
case 3:insert_end();
break;
case 4:insert_pos();
break;
case 5:delete_beg();
break;
case 6:delete_end();
break;
case 7:delete_pos();
break;
default:printf("TRY AGAIN\n");
}
printf("Do you want any more opration 1.yes\t2.No.\n");
scanf("%d",&ch);
}

}
void traverse()
{
struct node *ptr;
int j=0,k=0,item;
printf("Enter the item you want to search\n");
scanf("%d",&item);
if(start==NULL)
printf("List is empty\n");
else
{
ptr=start;
while(ptr!=NULL)
{
printf("%d\t",ptr->info);
if(ptr->info==item)
k=2;
ptr=ptr->next;
j++;
}
}
printf("Count is %d\n",j);
if(k==2)
printf("Element is present\n");
else
printf("Not present\n");
}
void insert_beg()
{
struct node *ptr;
int item;
ptr= (struct node *)malloc(sizeof(struct node));
if(ptr==NULL)
printf("No space avilable\n");
else
{
printf("Enter the value you want to insert\n");
scanf("%d",&item);
ptr->info=item;
ptr->next=start;
start=ptr;
}
}
void insert_end()
{
struct node *ptr,*ptr2;
int item;
if(start==NULL)
insert_beg();
else
{
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr==NULL)
printf("No space avilable\n");
else
{
ptr2=start;
while(ptr2!=NULL)
ptr2=ptr2->next;
printf("Enter the value you want to insert\n");
scanf("%d",&item);
ptr->info=item;
ptr2->next=ptr;
ptr->next=NULL;
}
}
}
void insert_pos()
{
struct node *ptr,*ptr2;
int d,n,item;
printf("Enter the location\n");
scanf("%d",&n);
ptr=start;
for(d=0;d<n-1;d++)
{
ptr=ptr->next;
if(ptr==NULL)
{if(d==n)
insert_end();
else
{printf("\nThere are less than %d nodes in list\n",n);
return;}
}}
if(start==NULL && n==1)
insert_beg();
else if(start==NULL)
printf("Not a valid position\n");
else
{ ptr2=(struct node *)malloc(sizeof(struct node));
if(ptr2==NULL)
printf("Overflow can not insert\n");
else
{printf("Enter the value\n");
scanf("%d",&item);
ptr2->next=ptr->next;
ptr->next=ptr;
}
}
}
void delete_beg()
{
struct node *ptr;
ptr=start;
del[i]=ptr->info;
i++;
start=ptr->next;
free(ptr);
}
void delete_end()
{
struct node *ptr;
ptr=start;
if(ptr->next==NULL)
delete_beg();
else
{while(ptr->next->next!=NULL)
ptr=ptr->next;
del[i]=ptr->next->info;
i++;
ptr->next=NULL;
free(ptr);
}
}
void delete_pos()
{struct node *ptr,*ptr2;
int j,n;
printf("Enter the location\n");
scanf("%d",&n);
if(start==NULL)
{printf("cannot delete undeflow\n");
return;}
else
{ ptr=start;
while(j<n-1)
{ptr=ptr->next;
j++;
}
if(ptr==NULL)
printf("Not a valid position \n");
else
{del[i]=ptr->info;
i++;
ptr2=start;
while(j<n)
{ptr2=ptr->next;
ptr->next=ptr2;
}
}
}
free(ptr);
}
For Turbo C users.
#include<conio.h>
     #include<stdio.h>
#include<alloc.h>
#define m 10
int del[m],i=0;
struct node
{
int info;
struct node *next;
} *start= NULL,*d = NULL;
void traverse();
void insert_beg();
void insert_end();
void insert_pos();
void delete_beg();
void delete_end();
void delete_pos();
void main()
{
int i,ch=1;
clrscr();
printf(" 1:traversing\t2.insertion at beggining\n");
printf("3:insertion at end\t 4:insertion at position\n");
printf(" 5:delettion from begining\t 6:deletion at end\n");
printf("7:deletion from position\n");
while(ch==1)
{
printf("Enter your choice\n");
scanf("%d",&i);
switch(i)
{
case 1:traverse();
break;
case 2:insert_beg();
break;
case 3:insert_end();
break;
case 4:insert_pos();
break;
case 5:delete_beg();
break;
case 6:delete_end();
break;
case 7:delete_pos();
break;
default:printf("TRY AGAIN\n");
}
printf("Do you want any more opration 1.yes\t2.No.\n");
scanf("%d",&ch);
}
getch();
}
void traverse()
{
struct node *ptr;
int j=0,k=0,item;
printf("Enter the item you want to search\n");
scanf("%d",&item);
if(start==NULL)
printf("List is empty\n");
else
{
ptr=start;
while(ptr!=NULL)
{
printf("%d\t",ptr->info);
if(ptr->info==item)
k=2;
ptr=ptr->next;
j++;
}
}
printf("Count is %d\n",j);
if(k==2)
printf("Element is present\n");
else
printf("Not present\n");
}
void insert_beg()
{
struct node *ptr;
int item;
ptr= (struct node *)malloc(sizeof(struct node));
if(ptr==NULL)
printf("No space avilable\n");
else
{
printf("Enter the value you want to insert\n");
scanf("%d",&item);
ptr->info=item;
ptr->next=start;
start=ptr;
}
}
void insert_end()
{
struct node *ptr,*ptr2;
int item;
if(start==NULL)
insert_beg();
else
{
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr==NULL)
printf("No space avilable\n");
else
{
ptr2=start;
while(ptr2!=NULL)
ptr2=ptr2->next;
printf("Enter the value you want to insert\n");
scanf("%d",&item);
ptr->info=item;
ptr2->next=ptr;
ptr->next=NULL;
}
}
}
void insert_pos()
{
struct node *ptr,*ptr2;
int d,n,item;
printf("Enter the location\n");
scanf("%d",&n);
ptr=start;
for(d=0;d<n-1;d++)
{
ptr=ptr->next;
if(ptr==NULL)
{if(d==n)
insert_end();
else
{printf("\nThere are less than %d nodes in list\n",n);
return;}
}}
if(start==NULL && n==1)
insert_beg();
else if(start==NULL)
printf("Not a valid position\n");
else
{ ptr2=(struct node *)malloc(sizeof(struct node));
if(ptr2==NULL)
printf("Overflow can not insert\n");
else
{printf("Enter the value\n");
scanf("%d",&item);
ptr2->next=ptr->next;
ptr->next=ptr;
}
}
}
void delete_beg()
{
struct node *ptr;
ptr=start;
del[i]=ptr->info;
i++;
start=ptr->next;
free(ptr);
}
void delete_end()
{
struct node *ptr;
ptr=start;
if(ptr->next==NULL)
delete_beg();
else
{while(ptr->next->next!=NULL)
ptr=ptr->next;
del[i]=ptr->next->info;
i++;
ptr->next=NULL;
free(ptr);
}

}
void delete_pos()
{struct node *ptr,*ptr2;
int j,n;
printf("Enter the location\n");
scanf("%d",&n);
if(start==NULL)
{printf("cannot delete undeflow\n");
return;}
else
{ ptr=start;
while(j<n-1)
{ptr=ptr->next;
j++;
}
if(ptr==NULL)
printf("Not a valid position \n");
else
{del[i]=ptr->info;
i++;
ptr2=start;
while(j<n)
{ptr2=ptr->next;
ptr->next=ptr2;
}
}
}
free(ptr);

}

No comments:

Post a Comment