can't insert linked list in binary tree

Discussion in 'C' started by davidelias16, Oct 11, 2009.

  1. davidelias16

    davidelias16 New Member

    Joined:
    Oct 10, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi everyone, I am really really stuck on this one if any one can help it would be so very good.

    My problem is that when I insert into the linked list for single node. The result in the end when I have finished adding to the tree, the linked list in every node are all the same but they dont contain information related only to the node in question

    Can anyone please help me inplement a linked list for the song number so that whenever a word is entered into the BST it creates or appends to the linked list which is stored as headItem in the word struct

    parameters are ./wordsearch lyrics

    *****************SEARCH.H FILE************************

    Code:
    /****************************************************************************
    *COSC1285/2123 - Algorithms and Analysis
    * Semester 2 2009 Assignment #2
    * Full Name        : David Elias
    * Student Number   : 3197587
    * Yallara Username : s3197587
    * Course Code      : COSC1285/2123
    * Program Code     : BP094
    ****************************************************************************/
    
    #include "search.h"
    
    /*gcc -ansi -Wall -pedantic -o wordsearch search.c*/
     
    int main(int argc, char **argv){
    
     char *word[30];
     int i;
     int j;
     int k;
     int t;
     FILE *songs;
     char str[3000];
    /*  BSTWort *root = new BSTWord;
     */
    /*  open file */
     songs = fopen(argv[1], "r");
    
     printf("\nPlease be patient while list is populated...\n");
    
      i=0;
     /*  while each line is not empty... */
      while(fgets(str, 3000, songs)!=NULL)
       {
       /* dynamically allocate pointer in matrix[i] with the size of int */
        t=0;
    
        for(k=0; k<strlen(str);k++)
        {
         if (str[k-1] == ' ')
         /*prevent double spacing related errors*/
         if(str[k] != ' ')
         t++;
        }
        
         for(j=0;j<(t+1);j++)/*just for each row*/
          {
           if(j==0) /* if first word */
              {
               strcpy(word, strtok( str, delims ));
               /*  string tokenise line between spaces*/
               insertWord(word, i+1, j+1);
              }
          else
              {
               strcpy(word, strtok( NULL, delims ));
               insertWord(word, i+1, j+1);
              }
           }
         
        i++;
       }
       
        printf("\n%i unique words successfully inserted into tree!\n\n", count); 
       
        wordsearch();
       
     return EXIT_SUCCESS;
     }
     
     
    /* BST()
    {
    root = NULL;
    }
      */
    int insertWord(char *inword, int songnum, int wordnum)
       {   
        int success = 0;
        
    /*     printf("%s-%i-%i, ", inword, songnum, wordnum);
     */
    if( (newNode=malloc(sizeof(WordType))) == NULL)
        {
            fprintf(stderr, "\nMemory Allocation for ListInsert failed!\n");
            fprintf(stderr, "\nAborting data entry!\n");
            exit(1);
        } 
        
        strcpy(newNode->word, inword);
        newNode->songnumber = songnum;
        newNode->wordnumber = wordnum;    
        newNode->left = NULL;
        newNode->right = NULL;
        
       if (root == NULL)
        {
            root = newNode;
            count++;
            success = 1;        
            printf("   root word: %s\n",root->word);
            
    
        }
        else if (root->word == inword)
        {
            return 0;
        }
        
        /* Insert into the BST sub branch */
        else
        {
        int pass=0;
        
        current=root;
        
        while(pass!=1)
        {
          /* less than current root  */
         if (strcmp(newNode->word, current->word)<0)
         {      
          if(current->left != NULL)
          {
           current=current->left;
           pass=0;
          }
          else
          {
    
          
          
          current->left = newNode;      
          count++;
          pass=1;
          }
         }
         /* greater than current root */
         else if(strcmp(newNode->word, current->word)>0)
         {
          if(current->right != NULL)
          {
           current=current->right;
           pass=0;
          }
          else
          {
          
    
          
          current->right = newNode;
          count++;
          pass=1;
          }
         }
         else
         {      
         
    
         
          count++; 
          pass=1;
         }
        }
       
       }
    
        
        return success;
    }
    
    
    
    void wordsearch()
    {
       int pass=0;
       int i=0;
    
       while(pass==0)
      {
        char qword[70];
        int pass2=0;
        
        current=root;
        
        printf("Enter a query: \n");
        fgets(qword, 80, stdin);
    
           for(i=0;i<strlen(qword);i++)
                 {
                   if ( qword[i] == '\n' )
                     {
                       qword[i] = '\0';
                       break;
                     }
                 }
        
         if(*qword=='0')
         {
         printf("Exiting...\n\n");
         pass=1;
         }
         else
         {
         
    
        while(pass2==0)
        {
    /*       printf("%s-%s", qword, current->word);  */
         if (strcmp(qword, current->word)<0)
         {
           current=current->left;
         }
         else if(strcmp(qword, current->word)>0)
         {
           current=current->right;
         }
         else if(strcmp(qword, current->word)==0)
         {
           pass2=1;
         }
         
         if(current==NULL)
         pass2=2;
        }
        
        if(pass2==1)
        {
        printf("Song  Positions\n");
        printf("%i:",current->wordnumber);
            current5=current->headItem;
            while(current5)
            {
            printf("%i,", current5->songnumber);
            current5 = current5->nextItem;
            }
        
        printf("found\n\n");
        }
        else if(pass2==2)
        {
        printf("not found\n\n");
        }
        
        }
       }
      }
      
      
    
    

    *****************SEARCH.C FILE************************


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define delims " "
    
    /* each string can contain this may characters */
    #define MAX_STRING_SIZE      30
    
    
    
    /* typedef struct BSTWord *WordTypePtr;
    WordTypePtr root;  */ 
     
    /* BSTWord *newNode = new BSTWord;
    BSTWord *root = new BSTWord;
    BSTWord *parent = new BSTWord;
    BSTWord *current = new BSTWord; */
    /* 
    BSTWord *parent = new BSTWord;
    BSTWord *current = new BSTWord;
     
     */
     
    int count;
    
    typedef struct word* WordPtr;
    typedef struct item* ItemTypePtr;
    /* typedef struct song* SongPtr;
     */
    WordPtr root, current, head, newNode, previous, next;
    ItemTypePtr new1, current1, head1, previous1;
    ItemTypePtr new2, current2, head2, previous2;
    ItemTypePtr new3, current3, head3, previous3;
    ItemTypePtr new4, current4, head4, previous4;
    ItemTypePtr new5, current5, head5, previous5;
    /* SongPtr new2, current2, head2, previous2;
     */
    int insertWord(char *inword, int songnum, int wordnum);
    
    /* typedef struct song
    {
       int wordnumber;
       SongPtr nextSong;
    } SongType;
     */
    typedef struct item
    {
       int songnumber;
       int count;
    /*    SongPtr nextItem; */
       ItemTypePtr nextItem;
     } ItemType;
    
    
    typedef struct word
    {
       char word[MAX_STRING_SIZE + 1];
       int songnumber;
       int wordnumber;
       int count;
       ItemTypePtr headItem;
       WordPtr left; /*points to the left child*/
       WordPtr right; /*points to the right child*/
    } WordType;
    
    typedef struct BSTWord BSTWord;
    
    /*  int InsertIntoSubTree(WordType parent, WordType current);
     */
    
    /* BSTWord * root; 
    BSTWord * current; 
    BSTWord * head;  */
    
    /* int findword(char *word, LinkedList &list); */
    void wordsearch();
    
    
    
    
    

    *****************SONGS FILE************************

    Code:
    twenty thousand seconds twenty thousand seconds since youve left and im still counting and twenty thousand reasons to get up get some thing done but im still waiting is someone kind enough to pick me up and give me food assure me that the world is good but you should be here you should be here how colors can change and even the texture of rain and whats that ugly little stain on the bathroom floor id rather not be with that right now id rather be floating in space somewhere or worry about the ozone layer
    twenty thirty two housewives shock in blue what in the world is it coming to what in the world twenty thirty three cannabis in tea what in the world acid is free what in the world if daddy could see today hed be turning in his grave if mummy could see the way the boys and girls and the manner in which they talk to their parents twenty thirty four women fight the wars men are too bored theyre scrubbing floors men are too bored theyre staying at home doing the chores what in the world do you remember
    
    
     
  2. davidelias16

    davidelias16 New Member

    Joined:
    Oct 10, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    sorry the first code is the .c file and the second is the .h sorry for the mistake
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice