Thread: implimenting a linked list?????

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Question implimenting a linked list?????

    what is the code to impliment a liked list?????
    i think i got it but i dont know how to check to see if the list is actually being created.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your code, or make a function to print your list's contents:
    Code:
    void printList( Node *n )
    {
        while( n )
        {
            printf("Node->next = %d\n", n->next ); //print address
            printf("Node->data = %d\n", n->data ); //print value
            n = n->next;
        }
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    here is my code

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


    struct book
    {
    int code;
    char title[20];
    char author[20];
    int pages;
    struct book*next;
    }*head,*work;

    FILE*library;

    main()
    {

    int code;
    int sRec;
    int pick;
    char title;
    char author;
    int pages;
    char ans;
    library=fopen("library.dat","wb");
    fread(library,sizeof(struct book),1,library);
    fclose(library);
    printf ("\t\t\tTusket Municipal Library\n\n\n");
    printf ("\tPlease make a selection...\n\n");
    printf ("\t1.) Enter a book in the inventory\n");
    printf ("\t2.) Delete a book from the inventory\n");
    printf ("\t3.) Search the inventory for a book\n");
    printf ("\t4.) Display the book inventory\n");
    printf ("\t5.) Exit the program\n\n");
    printf ("\tEnter your selection: \n");
    scanf ("%d", &pick);
    while (pick==1)
    {
    system("cls");
    head=(struct book*)malloc(sizeof(struct book));
    work=head;
    printf("Enter code:");
    scanf("%d",&code);
    printf("Enter Title:");
    scanf("%s", &title);
    printf("Enter Author:");
    scanf("%s", &author);
    printf("Enter Number of Pages:");
    scanf("%d",&pages);
    printf("Do you wnat to add another book?:");
    scanf("%s",&ans);
    while (ans!='N')
    {
    work->next=NULL;
    }
    work->next=(struct book*)malloc(sizeof(struct book));
    work=work->next;
    library=fopen("library.dat","a+b");
    if((library=fopen("library.dat","a+b"))!=NULL)
    {
    fwrite (&sRec,sizeof(struct book),(size_t)3,library);
    fclose (library);
    }
    library=fopen("library.dat","a+b");
    /* Test the write by reading it back from the file */
    library=fopen("library.dat","w+b");
    fread(&sRec,sizeof(struct book),(size_t)1,library);
    fclose(library);
    }
    while (pick==2)
    {
    }
    while (pick==3)
    {
    }
    while(pick==4)
    {
    }
    while(pick==5)
    {
    }
    return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you keep opening and closing your file? Why don't you just open it the first time as "ab+" and leave it open? I don't see the point in reopening the file every time.

    You can always use 'rewind' to go to the beginning of the file again, and also can use fseek to move about the file.

    main()
    {

    int code;
    int sRec;
    int pick;
    char title;
    char author;
    int pages;
    char ans;
    Um... no. You are using 'title' and 'author' here incorrectly. You keep reading into them, but they're only a single charcter, see:

    printf("Enter code:");
    scanf("%d",&code);
    printf("Enter Title:");
    scanf("%s", &title);
    printf("Enter Author:");
    scanf("%s", &author);
    This is a "BadThing(TM)".

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    46

    Thumbs up Re: implimenting a linked list?????

    Originally posted by mattyans
    what is the code to impliment a liked list?????
    i think i got it but i dont know how to check to see if the list is actually being created.
    You could try
    this link. It has help on many other algos as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM