Thread: Help Me!!

  1. #1
    Rec-E
    Guest

    Unhappy Help Me!!

    Ok the goal of this program is to extract lines from a text file, store each word in a binary search tree. Each node in the search tree has pointer to a structure similar to a queue. the queue has a rear and front member , a pointer to a linked list and a size field that tells how many nodes there are in the linked list. Its pretty long and i will post each file in a seperate reply to a post. Right now im getting a segmentation error in my InsertBST function at the point (strcmp(d->word, word) line towards the end of the function. Im assumming when i called the FindBST function that &d was not the right parameter to put there but Im not sure. Also any help with my program would be great. If you see any other mistakes please dont hesistate to let me know. And if you have the patience to actually go through the whole program then thank you. I apprecuate any advice. Thanks.

  2. #2
    Rec-E
    Guest

    BST implementation file

    /*BST Implementation File */

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include "error.h"
    #include "qlist.h"
    #include "bst.h"

    struct BSTNode
    {
    char *word;
    QList NewQList;
    BSTPointer LChild;
    BSTPointer RChild;
    };

    struct BSTInfo
    {
    BSTPointer root;
    };

    /*NewBST creates a new Binary Search Tree*/
    BST NewBST()
    {
    BST t=malloc(sizeof(struct BSTInfo));
    if(t==NULL)
    FatalError("Out of Storage");
    t->root=NULL;
    return t;
    }


    /*FindBST finds a certain word within a tree*/
    BSTPointer FindBST(char word[], BST t, BSTPointer *pp)
    {
    BSTPointer p=t->root;
    *pp=p;
    while(p!=NULL)
    if(strcmp(p->word, word)==0)
    return p;
    else if(strcmp(p->word, word)>0)
    {
    *pp=p;
    p=p->LChild;
    }
    else
    {
    *pp=p;
    p=p->RChild;
    }
    return NULL;
    }

    /*InsertBST inserts a word into a tree*/
    void InsertBST(char word[], int lineNum, BST t)
    {
    BSTPointer b,c,d;
    if(t==NULL)
    {
    b=malloc(sizeof(struct BSTNode));
    if(b==NULL)
    FatalError("Out of Storage");
    b->word=malloc(sizeof(word));
    strcpy(b->word,word);
    b->NewQList=NewQList(lineNum);
    b->LChild=NULL;
    b->RChild=NULL;
    t->root=b;
    }
    else
    {
    c=FindBST(word, t, &d);
    if(c!=NULL)
    InsertQList(lineNum, c->NewQList);
    else
    {
    b=malloc(sizeof(struct BSTNode));
    if(b==NULL)
    FatalError("Out of Storage");
    b->word=malloc(sizeof(word));
    strcpy(b->word, word);
    b->NewQList=NewQList(lineNum);
    b->LChild=NULL;
    b->RChild=NULL;
    if(strcmp(d->word, word)<0)
    d->RChild=b;
    else
    d->LChild=b;
    }
    }
    }

    /*Print BST prints out all words along with how many times tehy appear in
    the text file*/
    void PrintBST(BSTPointer t)
    {
    int n=0;
    if(t)
    {
    PrintBST(t->LChild);
    while(t->word[n]!=NULL)
    {
    putchar(t->word[n]);
    n++;
    }
    printf("%10d\n", sizeQList(t->NewQList));
    PrintBST(t->RChild);
    }
    }

    /*PrintSelectBST searches for a specific word and prints what lines it
    appears on*/
    void PrintSelect(BST t, char word[])
    {
    BSTPointer p, q;
    p=FindBST(word, t, &q);
    if(p==NULL)
    printf("Word is not in text file.\n");
    else
    {
    puts(p->word);
    PrintQList(p->NewQList);
    }
    }

  3. #3
    Rec-E
    Guest

    Qlist implementation file

    /*QList Implementation file */

    #include<stdio.h>
    #include<stdlib.h>
    #include "error.h"
    #include "qlist.h"

    typedef struct LineList *PtrToNode;
    struct LineList
    {
    int lineNum;
    PtrToNode next;
    };

    typedef struct Queue *QList;
    struct Queue
    {
    PtrToNode NewList;
    PtrToNode rear;
    PtrToNode front;
    int size;
    };


    /*NewQlist makes a new qlist with size of 1*/
    QList NewQList(int line)
    {
    PtrToNode p;
    QList q=malloc(sizeof(struct Queue));
    if(q==NULL)
    FatalError("Out of Storage");
    q->size=1;
    p=malloc(sizeof(struct LineList));
    if(p==NULL)
    FatalError("Out of Storage");
    p->lineNum=line;
    p->next=NULL;
    q->NewList=q->rear=q->front=p;
    return q;
    }

    /*InsertQList inserts a new line number into a queue list*/
    void InsertQList(int line, QList q)
    {
    PtrToNode p=malloc(sizeof(struct LineList));
    if(p==NULL)
    FatalError("Out of Storage");
    p->lineNum=line;
    p->next=NULL;
    q->rear->next=p;
    q->rear=p;
    (q->size)++;
    }

    /*sizeQList returns the size of the qlist*/
    int sizeQList(QList q)
    {
    return q->size;
    }

    /*PrintQList prints out the line numbers stored in the qlist*/
    void PrintQList(QList q)
    {
    PtrToNode p=q->front;
    while(p!=NULL)
    {
    printf("%5d", p->lineNum);
    p=p->next;
    }
    }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    pROGRAM

    /**/

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include "qlist.h"
    #include "bst.h"
    #include "error.h"
    #define MAX_LINE_SIZE 130
    #define MAX_WORD_SIZE 30
    #define MAX_FILE_SIZE 100

    void ExtractWord(char line[],int LineNum, BST t);

    int main()
    {
    BST tree;
    int lineNumber;
    char filename[MAX_FILE_SIZE];
    char line[MAX_LINE_SIZE];
    FILE *fp;
    printf("Enter a filename: ");
    gets(filename);
    fp=fopen(filename, "r");
    if(fp==NULL)
    FatalError("File did not open.");
    tree=NewBST();
    lineNumber=1;
    while(fgets(line, MAX_LINE_SIZE, fp)!=NULL)
    {
    ExtractWord(line,lineNumber, tree);
    lineNumber++;
    }
    fclose(fp);

    return 0;
    }

    void ExtractWord(char line[], int LineNum, BST t)
    {
    char ch;
    char *wp;
    char word[MAX_WORD_SIZE];
    int n,j;
    n=j=0;

    while(line[n]!=NULL)
    {

    line[n]=tolower(line[n]);
    n++;
    }
    n=0;
    ch=line[n];
    while(ch!=NULL)
    {
    while(isalpha(ch))
    {
    word[j]=ch;
    n++;
    j++;
    ch=line[n];
    }
    word[j]='\0';

    wp=malloc(j+1);
    if(wp==NULL)
    FatalError("Out of Storage");
    strcpy(wp, word);

    InsertBST(wp, LineNum, t);
    n++;
    ch=line[n];
    j=0;
    }
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    I accidentally posted the qlist.c file three times i think. Sorry.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    Here goes someof the code . Ive changed very little of it. let me know what u think. its pretty long.

Popular pages Recent additions subscribe to a feed