Linkeslist in C
Linkeslist in C
h>
#include<stdlib.h>
void create();
void insbeg();
void insend();
void inspos();
void delbeg();
void delend();
void delpos();
void display();
int count();
void search();
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *head,*temp,*newnode,*prenode,*nextnode;
void main()
{
int ch;
do
{
printf("\n1...create\n2...insertion\n3...deletion\n4...display\n5...count\
n6...search");
printf("\n enter the choice::");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:printf("\ninsertion\n1...begging\n2...ending\n3...specific
position");
printf("\n enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:insbeg();
break;
case 2:insend();
break;
case 3:inspos();
break;
default:printf("\n ivalid choice");
break;
}
break;
case 3: printf("\ndeletion\n1...begging\n2...ending\n3...specific
position");
printf("\n enter the choice:::");
scanf("%d",&ch);
switch(ch)
{
case 1:delbeg();
break;
case 2:delend();
break;
case 3:delpos();
break;
default:printf("\n ivalid choice");
break;
}
break;
case 4:display();
break;
case 5:count();
break;
case 6:search();
break;
default:printf("\n ivalid choice");
break;
}
printf("\n do you want to continue(press 1)::");
scanf("%d",&ch);
}while(ch==1);
}
void create()
{
int value;
printf("\n enter the value:::");
scanf("%d",&value);
newnode=(node *)malloc(sizeof(node));
newnode->data=value;
newnode->next=NULL;
if(head!=NULL)
{
printf("\n linked list is exist!!");
}
else
{
head=newnode;
}
}
void insbeg()
{
int value;
printf("\n enter the value:::");
scanf("%d",&value);
newnode=(node *)malloc(sizeof(node));
newnode->data=value;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
}
void insend()
{
int value;
printf("\n enter the value::");
scanf("%d",&value);
newnode=(node *)malloc(sizeof(node));
newnode->data=value;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
int count()
{
int c=0;
if(head==NULL)
{
printf("\n linked list is not exist!!");
}
else
{
temp=head;
while(temp->next!=NULL)
{
c++;
temp=temp->next;
}
c++;
printf("\n %d is count",c);
return c;
}
}
void inspos()
{
int value,pos,i;
printf("\n enter the value::");
scanf("%d",&value);
newnode=(node *)malloc(sizeof(node));
newnode->data=value;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
int c;
printf("\n enter the position:::");
scanf("%d",&pos);
c=count();
if(c<pos)
{
printf("\n invald position");
}
else
{
temp=head;
for(i=1;i<pos-1;i++)
{
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
}
}
}
void delbeg()
{
if(head==NULL)
{
printf("\n deletion is not possible!!!");
}
else
{
temp=head;
head=head->next;
printf("%d is deleted",temp->data);
free(temp);
}
}
void delend()
{
if(head==NULL)
{
printf("\n deletion is not possible!!!");
}
else
{
temp=head;
while(temp->next!=NULL)
{
prenode=temp;
temp=temp->next;
}
if(temp==head)
{
head=NULL;
printf("%d is deleted",temp->data);
free(temp);
}
else
{
prenode->next=NULL;
printf("%d is deleted",temp->data);
free(temp);
}
}
}
void delpos()
{
int i,pos;
if(head==NULL)
{
printf("\n deletion is not possible!!");
}
else
{
int c;
printf("\n enter the position::");
scanf("%d",&pos);
if(c<pos)
{
printf("\n ivalid position");
}
else
{
temp=head;
for(i=1;i<pos-1;i++)
{
temp=temp->next;
}
nextnode=temp->next;
temp->next=nextnode->next;
printf("%d is deleted",nextnode->data);
free(nextnode);
}
}
}
void display()
{
if(head==NULL)
{
printf("\n linked list is not exist!!");
}
else
{
temp=head;
while(temp->next!=NULL)
{
printf("%d--->",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}
void search()
{
int key,i=0,flag=0;
if(head==NULL)
{
printf("\n seraching is not possible!!");
}
else
{
temp=head;
printf("\n enter the key:::");
scanf("%d",&key);
while(temp->next!=NULL)
{
i=i+1;
if(temp->data==key)
{
flag=1;
printf("\nelement is available at %d place ",i);
}
temp=temp->next;
}
if(temp->data==key)
{
i=i+1;
flag=1;
printf("\nelement is available at %d place ",i);
}
if(flag==0)
{
printf("\n element is not available ");
}
}
}