algorithm writing
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*temp,*head,*newnode,*prev,*head2;
void main()
{
char option;
int pos,ch,cnt,x;
do
{
printf("\n linked list operations\n1.ceation\n2.insertion\n3.search\n4.reverse\n5.deletion\n6.deletion at the end\n enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
while(1)
{
printf("\n entere the data: ");
scanf("%d",&x);
newnode=(struct node*)malloc(sizeof(node));
newnode->link=NULL;
newnode->data=x;
if(head==NULL)
{
head=newnode;
prev=newnode;
}
else
{
prev->link=newnode;
prev=newnode;
}
printf("\n do u want to add another record(y//n): ");
scanf("%s",&option);
if(option=='n')
{
break;
}
}
printf("\n the entered list is: ");
prev=head;
while(prev!=NULL)
{
printf("\t%d",prev->data);
prev=prev->link;
}
break;
case 2:
while(1)
{
printf("\n enter the data and the new position: ");
scanf("%d %d",&x,&pos);
newnode=(struct node*)malloc(sizeof(node));
newnode->link=NULL;
newnode->data=x;
if(pos==1)
{
newnode->link=head;
head=newnode;
}
else
{
cnt=1;
prev=head;
while(cnt!=pos-1)
{
prev=prev->link;
cnt++;
}
temp=prev->link;
prev->link=newnode;
newnode->link=temp;
}
printf("\n do u want to another record(y//n): ");
scanf("%s",&option);
if(option=='n')
{
break;
}
}
printf("\n the new list is: ");
temp=head;
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->link;
}
break;
case 3:
while(1)
{
printf("\n enter the data to be searched: ");
scanf("%d",&x);
temp=head;
while(temp!=NULL)
{
if(temp->data==x)
{
printf("\n the data is present ");
break;
}
else
{
temp=temp->link;
}
}
if(temp==NULL)
{
printf("\n data is not present");
}
printf("\n do you want to search another record(y//n):");
scanf("%s",&option);
if(option=='n')
{
break;
}
}
break;
case 4:
newnode=(struct node*)malloc(sizeof(node));
head2=newnode;
newnode->link=NULL;
while(head!=NULL)
{
if(head2==NULL)
{
head2=head;
//prev=head2;
head=head->link;
}
else
{
newnode=(struct node*)malloc(sizeof(node));
newnode->data=head->data;
newnode->link=head2;
head2=newnode;
head=head->link;
}
}
printf("\n the reverse of the list is: ");
temp=head2;
while(temp->link!=NULL)
{
printf("\t%d",temp->data);
temp=temp->link;
}
break;
case 5:
while(1)
{
printf("\n enter the position to be deleted: ");
scanf("%d",&pos);
if(pos==1)
{
temp=head;
head=head->link;
free(temp);
}
else
{
cnt=1;
temp=head;
while(cnt!=pos-1)
{
temp=temp->link;
cnt++;
}
prev=temp->link;
temp->link=prev->link;
free(prev);
}
printf("\n do you wish to delete another data(y//n): ");
scanf("%s",&option);
if(option=='n')
{
break;
}
}
printf("\n the new list is: ");
temp=head;
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->link;
}
break;
case 6:
while(1)
{
temp=head;
while(temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
prev->link=NULL;
free(temp);
printf("\n do u want to delete another data(y//n): ");
scanf("%s",&option);
if(option=='n')
{
break;
}
}
printf("\n the new list is:");
temp=head;
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->link;
}
break;
}
}while(ch<7);
getch();
}