Thread: Read Data-Linked list pro

  1. #1
    Supra
    Guest

    Read Data-Linked list pro

    Hi all, here is the format of the original data file an dlooks like blow:

    /* data Format*/

    artist1 cdname1
    3
    song1_1
    song1_2
    song1_3
    artist2 cdname2
    4
    song2_1
    song2_2
    song2_3
    song2_4
    artist3 cdname3
    2
    song3_1
    song3_2
    .
    .
    .

    /*and so on...*/

    And I've write structure type preparing to do some binary search tree stuff below:

    typedef struct dnode
    {
    char name[41];
    }DATA;

    typedef struct songnode
    {
    DATA song;
    char time[6];
    struct songnode *next;
    }SONGNODE, *SONGPTR;

    typedef struct cdnode
    {
    DATA cdname;
    SONGPTR songhead;
    struct cdnode *cdnext;
    }CDNODE, *CDPTR;

    typedef struct artist
    {
    DATA artistname;
    CDPTR head;
    struct artist *leftchild, *rightchild;
    }ARTIST, *ARTISTPTR;

    and My question is how can I read data file by using these structures I've defined?
    I'm still thinking about it and writing code but i'm really confused
    about linked list stuff. I hope any one can help me out!
    Thanx very very much for any help!!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    21
    Ok the correct format should be look like below:

    artist1 cdname1
    3
    1:00 song1_1
    2:00 song1_2
    3:00 song1_3
    artist2 cdname2
    4
    1:30 song2_1
    2:00 song2_2
    2:10 song2_3
    2:20 song2_4
    artist3 cdname3
    2
    3:00 song3_1
    3:03 song3_2
    .
    .
    .


    Sorry for my mistake, any thanx fo any help!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Taking typedef struct artist as an example, you want to create two functions
    1. allocates a new data element, and fills in all the relevant details.
    2. traverse the existing data structures to add the new element in the correct place.

    You basically need to do this for each of the three structures you have.

    The reading the file bit is pretty easy, so long as you can keep track of which type of record you're reading.

    A good function to write first is one which prints out the entire tree - you use this to keep testing what you're doing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    21
    Hey thank you very much, Salem, but I still confused abiut how to
    fill and traverse data. I write my code below and hope you ca help me out!!
    Thanx and Thanx again!

    void ReadData(ARTISTPTR *P)
    {
    FILE *inptr;
    int num;
    ARTISTPTR NewPtr;
    ARTIST Node;
    ARTISTPTR *PreviousPtr;

    if((inptr = fopen("data", "r")) == NULL)
    {
    printf("CANNOT OPEN THE FILE!!\n");

    }
    else
    {

    PreviousPtr=P;
    while(fscanf(inptr, "%s %s", Node.artistname.name, Node.head->cdname.name) != EOF)
    {
    fscanf(inptr, " %d",&num);
    for(int i=0;i<num;++i)
    {
    fscanf(inptr, "%s %s", Node.head->songhead->time, Node.head->songhead->song.name);
    }
    NewPtr=(ARTISTPTR)malloc(sizeof(ARTIST));

    *PreviousPtr=NewPtr;
    strcpy(NewPtr->artistname.name, Node.artistname.name);
    strcpy(NewPtr->head->cdname.name, Node.head->cdname.name);
    strcpy(NewPtr->head->songhead->time, Node.head->songhead->time);
    strcpy(NewPtr->head->songhead->song.name, Node.head->songhead->song.name);

    then call tree_traverse....?

    .
    .
    .

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    21
    Anyone help plz?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I assume that you mean

    artist1 cdname1
    3
    1:00 song1_1
    2:00 song1_2
    3:00 song1_3
    artist1 cdname2
    4
    1:30 song2_1
    2:00 song2_2
    2:10 song2_3
    2:20 song2_4
    artist2 cdname1
    2
    3:00 song3_1
    3:03 song3_2

    You get all the artist1 CD's together, followed by all artist2 CD's etc, otherwise this gets more complicated.

    > fscanf(inptr, "%s %s", Node.head->songhead->time, Node.head->songhead->song.name);
    each time you do this, you need to add the song to the end of some linked list.

    One thing you will need to implement is the detection of when artist1 becomes artist2. This is when you add a new artist to the tree.

    Here's a bit more code
    Code:
    // appends a song to the end of a song list
    // the return result is the new head of the list
    // which only really matters when the list is initially empty
    SONGPTR append_song_list ( SONGPTR head, SONGNODE data ) {
        SONGPTR newnode = malloc( sizeof(SONGNODE) );
        *newnode = data;
        newnode->next = NULL;
        if ( head == NULL ) {
            head = newnode;
        } else {
            SONGPTR temp = head;
            while ( temp->next != NULL ) {
                temp = temp->next;
            }
            temp->next = newnode;
        }
        return head;
    }
    
    // a binary tree sorted by artist name
    // follow left/right branches until a 'leaf' is found
    // then add the new node at that point
    ARTISTPTR insert_artist_tree ( ARTISTPTR tree, ARTIST data ) {
        if ( tree == NULL ) {
            ARTISTPTR newnode = malloc( sizeof(ARTIST) );
            *newnode = data;
            newnode->leftchild = NULL;
            newnode->rightchild = NULL;
            return newnode;
        } else
        if ( strcmp( data.artistname.name, tree->artistname.name ) < 0 ) {
            return insert_artist_tree( tree->leftchild, data );
        } else {
            return insert_artist_tree( tree->rightchild, data );
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    21
    Hey Salem, I really appreciate for you help and I can read data now. But I have another pro w/ print those data. I got some error when match song's number. I got segmentation fault when the number is 0. So I need somebody help me out.
    Thank you really a lot!!

    /* My code */

    /* Read data */
    void read_data(ARTISTPTR *T)
    {
    FILE *in;
    int num, i;
    ARTISTPTR New;
    ARTIST Anode;
    CDNODE Cnode;
    SONGNODE Snode;
    SONGPTR NewS;


    if((in = fopen("prog1.data", "r")) == NULL)
    {
    printf("CANNOT OPEN THE FILE!!\n");
    exit(0);
    }
    else
    {
    while(fscanf(in, " %[^;] %[^\n]", Anode.artistname.name, Cnode.cdname.name) != EOF)
    {
    New=(ARTISTPTR)malloc(sizeof(ARTIST));
    strcpy(New->artistname.name, Anode.artistname.name);
    printf("%s\n",New->artistname.name);
    New->head=(CDPTR)malloc(sizeof(CDNODE));
    strcpy(New->head->cdname.name, Cnode.cdname.name);
    New->head->cdnext=NULL;
    printf("%s\n",New->head->cdname.name);

    fscanf(in, " %d", &num);
    for(i=0; i<num; i++)
    {
    fscanf(in, "%s", Snode.time);
    fscanf(in, " %[^\n]", Snode.song.name);
    NewS=(SONGPTR)malloc(sizeof(SONGNODE));
    strcpy(NewS->time, Snode.time);
    strcpy(NewS->song.name, Snode.song.name);
    NewS->next = NULL;
    if(i)
    NewS->next = New->head->songhead;
    New->head->songhead = NewS;
    printf("%s %s\n",NewS->time, NewS->song.name);
    }
    if (*T != NULL)
    New->leftchild = *T;
    *T = New;
    }

    }
    }

    /* Print data */
    void print_data(ARTISTPTR tree)
    {
    ARTISTPTR New;
    FILE *Outptr;
    Outptr = fopen("Report.out", "w");

    New = tree;
    while (New != NULL)
    {
    fprintf(Outptr,"artist in tree - %s\n", New->artistname.name);
    printf("artist in tree - %s\n", New->artistname.name);
    fprintf(Outptr,"associated cd - %s\n", New->head->cdname.name);
    printf("associated cd - %s\n", New->head->cdname.name);
    while (New->head->songhead != NULL)
    {
    fprintf(Outptr,"song in tree - %s\n", New->head->songhead->song.name);
    printf("song in tree - %s\n", New->head->songhead->song.name);
    New->head->songhead = New->head->songhead->next;
    }
    New = New->leftchild;
    }

    fclose(Outptr);
    printf("Print data....\n");
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > New = tree;
    > New = New->leftchild;
    This is good

    > while (New->head->songhead != NULL)
    > New->head->songhead = New->head->songhead->next;
    This is bad - you're modifying the tree as you traverse it.
    Use another temp variable

    And don't forget to use the code tags to format the code
    http://www.cprogramming.com/cboard/m...bbcode#buttons
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    21
    Thank you very much Salem. And my last question is how to add a song to a cd list?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Linked List Problem
    By animeaholic in forum C Programming
    Replies: 1
    Last Post: 12-09-2002, 06:36 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM