Code For Alls..!

get the code!!

Wednesday, 19 July 2017

Single link list

#include<stdio.h>
#include<conio.h> #include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL,*tail=NULL,*temp,*prev,*newnode;
void createnode()
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("Memory is not allocated.....\n");
exit(0);
}
else
{
printf("Enter the element to store...\t");
scanf_s("%d",&newnode->data);
newnode->next=NULL;
}
}
void createlist()
{
int ch=1;
while(ch)
{
createnode();
if(head==NULL)
head=tail=newnode;
else
{
tail->next=newnode;
tail=newnode;
}
printf("Do you want to insert element again prees 1...\t");
scanf_s("%d",&ch);
}
}
void display()
{
if(head==NULL)
printf("list is empty...\n");
else
{
temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
}
void insertfirst()
{
createnode();
if(head==NULL)
head=tail=newnode;
else
{
newnode->next=head;
head=newnode;
}
}
void insertlast()
{
createnode();
if(head==NULL)
head=tail=newnode;
else
{
tail->next=newnode;
tail=newnode;
}
}
void insertinter()
{
int pos,i=1;
printf("Enter the position to insert.....\t");
scanf_s("%d",&pos);
createnode();
if(head==NULL)
head=tail=newnode;
else
{
temp=head;
while(temp!=NULL && i<pos-1)
{
i=i+1;
temp=temp->next;
}
if(temp==NULL)
{
printf("There is no position in the list....\n");
free(newnode);
}
else
{
newnode->next=temp->next;
temp->next=newnode;
}
}
}
void delefrst()
{
if(head==NULL)
printf("List is empty..........\n");
else
{
temp=head;
head=head->next;
free(temp);
}
}
void delelast()
{
if(head==NULL)
printf("List is empty.....\n");
else
{
temp=head->next;
if(temp==NULL)
{
head=NULL;
free(temp);
}
else
{
prev=head;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
tail=prev;
free(temp);
}
}
}
void deleinter()
{
int pos,i=1;
if(head==NULL)
printf("List is empty.....\n");
else
{
printf("Enter the position to delete..\t");
scanf_s("%d",&pos);
temp=head;
if(pos==1)
{
head=head->next;
free(temp);
}
else
{
while(temp!=NULL&&i<pos-1)
{
temp=temp->next;
i=i+1;
}
if(temp==NULL)
printf("There is no positon in the list...\n");
else
{
prev=temp;
temp=temp->next;
prev->next=temp->next;
free(temp);
}
}
}
}
void search()
{
int k;
if(head==NULL)
printf("list is empty....\n");
else
{
printf("Enter the element....\t");
scanf("%d",&k);
temp=head;
while(temp!=NULL)
{
if(k==temp->data)
{
printf("Given element is found...\n");
break;
}
temp=temp->next;
}
if(temp==NULL)
printf("Given element is not found...\n");
}
}
void main()
{
int ch=1;
while(ch)
{
printf("1.create list\t2.display\t3.insertfirst\n4.insertlast\t5.insertinter\t6.deletefirst\t7.delelast\n8.deleinter\t9.searching\n");
printf("Enter your choioce...\t");
scanf_s("%d",&ch);
switch(ch)
{
case 1:
createlist();
break;
case 2:
display();
break;
case 3:
insertfirst();
break;
case 4:
insertlast();
break;
case 5:
insertinter();
break;
case 6:
delefrst();
break;
case 7:
delelast();
break;
case 8:
deleinter();
break;
case 9:
search();
break;
default:
printf("List is empty...\n");
}
}
}