CrazyEngineers
  • algorithm writing

    samaira

    samaira

    @samaira-P4RsEf
    Updated: Oct 19, 2024
    Views: 991
    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<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();
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sookie

    MemberOct 4, 2009

    For the first time in my life watching code is ready but seeking for algorithm. Really funny 😐
    Are you sure? This action cannot be undone.
    Cancel
  • samaira

    MemberOct 4, 2009

    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 😔
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberOct 4, 2009

    hey do u wanna algorithm for all the operations individually??
    Are you sure? This action cannot be undone.
    Cancel
  • samaira

    MemberOct 4, 2009

    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😕
    Are you sure? This action cannot be undone.
    Cancel
  • MaRo

    MemberOct 4, 2009

    At least give hints for what the code supposed to do.
    Are you sure? This action cannot be undone.
    Cancel
  • samaira

    MemberOct 5, 2009

    the given code is about various operations on single linked list such a insertion,display,reverse and delete
    Are you sure? This action cannot be undone.
    Cancel
  • vik001ind

    MemberOct 5, 2009

    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
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register