algorithm writing

hey can any one give me any idea on how to write algorithm for the following c code which is the code for various linked list operations such as insertion ,display,reverse,& deletion.😕
#include
#include
#include
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();
}

Replies

  • sookie
    sookie
    For the first time in my life watching code is ready but seeking for algorithm. Really funny 😐
  • samaira
    samaira
    well actually it is funny but i never wrote an algorithm before program.Usually only understanding the concepts is more than enough for me to write a code but now i have to submit a journal with algorithms and flowchart and i m not able to understand how to write.SO plz 😔
  • Manish Goyal
    Manish Goyal
    hey do u wanna algorithm for all the operations individually??
  • samaira
    samaira
    yes and main thing that bothers me is that how to write structure in an algorithm and even the while(1) loop i have used is troubling me😕
  • MaRo
    MaRo
    At least give hints for what the code supposed to do.
  • samaira
    samaira
    the given code is about various operations on single linked list such a insertion,display,reverse and delete
  • vik001ind
    vik001ind
    You should try to study some simple algorithms, that how its written, then you can just convert the C program in to algorithm, its quite easy.
    You can use while in a algorithm or use repeat for a while of C.

    Eg.
    i=0;
    while(i<3)
    {// some code
    i++;
    }


    i=0;
    repeat
    { some code
    increment i
    }till i < 3

You are reading an archived discussion.

Related Posts

sigh! When I insert a DVD/CD in my inbuilt drive, it just spilts it out! 😔. The same Disk works awesome in my roomie's computer. I tried to download drivers...
hey the following c code executes properly except case 4.This is the code for polynomial operations using linked list.So can anyone tell me my mistake😔 #include #include #include #include int...
click to view Computer Ads but Old age by Mahesh Dahale
study in abroad Forms section where Student [FONT="]Study[/FONT] at a Foreign Institution give some information about their university programs and where in this section[FONT="][/FONT] will help CEans become familiar with...
How to simulate an Pick and Place Robot in pro-e..... is there an option to simulate by giving the value of degress of freedom.