CrazyEngineers
  • help in a program for linked list

    samaira

    Member

    Updated: Oct 25, 2024
    Views: 992
    can anyone give me the program for linked list with the foll conditions:
    1.first node is created
    2.we have to ask the user if he wish to add another data
    3.display 😕
    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
  • vinora

    MemberAug 26, 2009

    Re: help in a program

    java or C or C++ or python or ruby
    Are you sure? This action cannot be undone.
    Cancel
  • samaira

    MemberAug 26, 2009

    Re: help in a program

    i want the program in c
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberAug 26, 2009

    Re: help in a program

    samaira
    can anyone give me the program for linked list with the foll conditions:
    1.first node is created
    2.we have to ask the user if he wish to add another data
    3.display
    I feel you must have given this a try. Could you post your code and let us know where you are stuck.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • durga ch

    MemberAug 26, 2009

    Re: help in a program

    😁 I have nothing to say more to Pradeep - C Guru here. But yes as a kick start you must be knowing the concepts of linked lits, first try understanding pointers and how memory locations are accesesed with pointers.
    Then linked list is all about playing around with these pointers.
    Are you sure? This action cannot be undone.
    Cancel
  • itchap

    MemberAug 26, 2009

    Re: help in a program

    # include<stdio.h>
    # include<conio.h>
    #include<malloc.h>
    #include<stdlib.h>

    struct node
    {
    int data;
    struct node *link;
    };
    struct node *insert(struct node *p, int n)
    {
    struct node *temp;
    /* if the existing list is empty then insert a new node as the
    starting node */
    if(p==NULL)
    {
    p=(struct node *)malloc(sizeof(struct node)); /* creates new node
    data value passes as parameter */

    if(p==NULL)
    {
    printf("Error\n");
    exit(0);
    }
    p-> data = n;
    p-> link = p; /* makes the pointer pointing to itself because it
    is a circular list*/
    }
    else
    {
    temp = p;
    /* traverses the existing list to get the pointer to the last node of
    it */
    while (temp-> link != p)
    temp = temp-> link;
    temp-> link = (struct node *)malloc(sizeof(struct node)); /*
    creates new node using
    data value passes as
    parameter and puts its
    address in the link field
    of last node of the
    existing list*/
    if(temp -> link == NULL)
    {
    printf("Error\n");
    exit(0);
    }
    temp = temp-> link;
    temp-> data = n;
    temp-> link = p;
    }
    return (p);
    }
    void printlist ( struct node *p )
    {
    struct node *temp;
    temp = p;
    printf("The data values in the list are\n");
    if(p!= NULL)
    {
    do
    {
    printf("%d\t",temp->data);
    temp=temp->link;
    } while (temp!= p);
    }
    else
    printf("The list is empty\n");
    }

    void main()
    {
    clrscr();
    int n;
    int x;
    struct node *start = NULL ;
    printf("Enter the nodes to be created \n");
    scanf("%d",&n);
    while ( n -- > 0 )
    {
    printf( "Enter the data values to be placed in a node\n");
    scanf("%d",&x);
    start = insert ( start, x );
    }
    getch();
    printf("The created list is\n");
    printlist ( start );
    getch();
    }
    Check this program. I think this will help you.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register