Re: How to fix this structure?
From: Al Bowers (xabowers_at_rapidsys.com)
Date: 05/18/04
- Next message: Joe Wright: "Re: rjh?"
- Previous message: Andrey Tarasevich: "Re: C performance"
- In reply to: Chris R.: "How to fix this structure?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 17 May 2004 19:13:51 -0400
Chris R. wrote:
> Hi everyone. I am trying to finish my homework, but I seem not to
> figure out how to fix this structure that seems to make wrong output.
> What this program should do is use structure to save the word and line
> number on which it was in array pointer using malloc and print them
> out alphabetized (no duplicates).
>
> Most of it works, except the words seem to be corrupted. Here is a
> code, if anyone can help me it would help so much since I've been
> working on this for a really long time. I tried even changing '\0' to
> NULL, but then program errored out.
>
>
.... code snipped ....
There are many errors, so I want take the time to try to fix your
code. Along with the errors you have a design problem with the
date type and with all the operations being done in one function, main.
I would make the data type List which contains a pointer to an
array of data type WORD as one member and a counter to keep a count
on the number of words in the list as the other member. In the
data type WORD you would store the word in on member and the other
member would be the line number.
Example:
typedef struct WORD
{
unsigned linenum;
char *name;
} WORD;
typedef struct LIST
{
WORD *word;
unsigned count;
} LIST;
Now you should write fucntions that do simple operations
on the LIST datatype. For example AddWordtoList() to add
a word, PrintList() to print the list, FreeList() to free
the allocations once you are finished with the list.
You could use Standard C functions bsearch to search the
list for dupes and function qsort to sort the list as you
build it.
An example of a possible definiion of AddWordtoList().
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int cmp(const void *v1, const void *v2)
{
const WORD *w1 = v1;
const WORD *w2 = v2;
return strcmp(w1->name,w2->name);
}
int AddWordtoList(LIST *p, const char *word, unsigned linenr)
{
WORD *tmp,key;
char *s, *s1;
if((s = malloc(strlen(word)+1)) == NULL) return 0;
strcpy(s,word);
for(s1 = s; (*s1 = tolower((unsigned char)*s1));s1++) ;
key.name = s;
if(p->word != NULL)
if(bsearch(&key,p->word,p->count,sizeof *p->word,cmp))
{ /* dupe */
free(s);
return 0;
}
tmp = realloc(p->word,(p->count+1)*(sizeof *tmp));
if(tmp == NULL)
{
free(s);
return 0;
}
p->word = tmp;
p->word[p->count].name = s;
p->word[p->count++].linenum = linenr;
qsort(p->word,p->count,sizeof *p->word,cmp);
return 1;
}
-- Al Bowers Tampa, Fl USA mailto: xabowers@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/
- Next message: Joe Wright: "Re: rjh?"
- Previous message: Andrey Tarasevich: "Re: C performance"
- In reply to: Chris R.: "How to fix this structure?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|