Reputation: 87
I am trying to learn linked lists and I am just making this program for practice. It contains several linked lists made from two structs. Everything from the struct letterLink works fine (typedef ss) That linked list simply takes a string of user inputted characters, prints them backwards, then sorts them alphabetically.
That all works fine.
It’s the other linked list called struct wordLink (typedef sw) that is giving me trouble. I’m attempting to parse the string of letters already entered into a linked list of words base on spaces in the letter string.
Which is created by calling the functions createWordList and appendWord.
Then printed with printWord.
For now don’t worry about the claim of sorting the words alphabetically, in the main function, as I haven't gotten to that function yet.
I’m also aware of a possible bug that might occur in the createWordList function when the user enters more than one space between words, since ptr->letter would be 32 (ascii space) after leaving the inner while loop thius not satisfy the condition of entering back into that loop. I’ll deal with that later.
For now, I’m trying to create the linked list of words, but this will not even print the first word.
Any hints or help would be greatly appreciated, as I just learning this, and can’t figure out why it won’t work.
Thanks. I’ll post in full, for compiling purposes, but It’s only the last three functions before freeWord in this program that Im grappling with. (createWordList which uses appendWord printed by printWord)
Thanks.
#include <stdio.h>
#include <stdlib.h>
typedef struct linkletter{
char letter;
struct linkletter* next;
} ss;
typedef struct linkword{
char word[20];
struct linkword* next;
} sw;
ss * createLetter(char);
sw * createWordList(ss* strt);
ss * readLetter(void);
ss * append(ss* end, ss * newLetter);
sw* appendWord(sw* endWord, sw* newWord);
void printLetter(ss*);
void printWord(sw*);
void freeLetter(ss*);
void freeWord(sw *wordStart);
ss * searchLetter(ss* start, char letter);
ss * insertAtFront(ss* start, ss* newptr);
ss * reverseLetter(ss* start);
ss * sortedCopy(ss* start);
ss * insertIntoSorted(ss* start, ss* newLet);
int main (void){
ss *start, *backwards, *sorted;
sw *wordStart;
printf("enter as many words as you wish separated by spaces:\n (Max 20 letters per word)\n");
start=readLetter();
printf("\nyou typed:");
printLetter(start);
printf("\nBackwards: ");
backwards= reverseLetter(start);
printLetter(backwards);
printf("\nSorted by Letter: ");
sorted=sortedCopy(start);
printLetter(sorted);
printf("\nYour words in alphetical order:\n");
wordStart=createWordList(start);
printWord(wordStart);
freeLetter(start);
freeLetter(backwards);
freeLetter(sorted);
freeWord(wordStart);
return 0;
}
ss *createLetter(char let){
ss *ptr;
ptr=(ss*)malloc(sizeof(ss));
ptr->letter=let;
ptr->next=NULL;
return ptr;
}
ss * readLetter(void){
char c;
ss *end, *start, *newLetter;
start=NULL;
scanf("%c",&c);
while(c!='\n'){
newLetter=createLetter(c);
if (start==NULL){
start = newLetter;
end=start;
} else {
end=append(end,newLetter);
}
scanf("%c",&c);
}
return start;
}
ss *append (ss *end, ss *newLetter){
end->next=newLetter;
return end->next;
}
void printLetter(ss *start){
ss* ptr =start;
while(ptr!=NULL){
printf("%c",ptr->letter);
ptr=ptr->next;
}
}
void freeLetter(ss *start){
ss* ptr=start;
ss *tmp;
while(ptr!=NULL){
tmp=ptr->next;
free(ptr);
ptr=tmp;
}
}
ss * searchLetter(ss* start, int number){
ss* ptr = start;
while((ptr!=NULL) && (ptr->letter!=number)){
ptr=ptr->next;
}
return(ptr);
}
ss* insertAtFront(ss* start, ss* newptr){
newptr->next=start;
return(newptr);
}
ss * reverseLetter(ss* start){
ss *ptr= start;
ss *bstart = start;
ss* newLetter;
if (start!=NULL){
bstart=createLetter(start->letter);
ptr=ptr->next;
}
while(ptr != NULL) {
newLetter=createLetter(ptr->letter);
bstart=insertAtFront(bstart, newLetter);
ptr=ptr->next;
}
return(bstart);
}
ss* insertIntoSorted(ss* start, ss* newLet){
ss* ptr = start;
ss* prev = NULL;
while((ptr!=NULL) && (ptr->letter<newLet->letter)){
prev=ptr;
ptr=ptr->next;
}
if(prev == NULL) {
start = insertAtFront(start,newLet);
} else {
prev->next = newLet;
newLet->next = ptr;
}
return(start);
}
ss* sortedCopy (ss* start){
ss* ptr = start;
ss * sortedStart= NULL;
ss* newLetter;
if(start!=NULL) {
sortedStart = createLetter(start->letter);
ptr=ptr->next;
}
while(ptr!=NULL){
newLetter = createLetter(ptr->letter);
sortedStart = insertIntoSorted(sortedStart, newLetter);
ptr = ptr->next;
}
return(sortedStart);
}
sw* createWordList(ss* start){
ss *ptr=start;
sw *ptrWord=NULL;
sw *endWord;
sw *wordStart=ptrWord;
int i=0;
while(ptr->letter!='\n'){
ptrWord=(sw*)malloc(sizeof(sw));
while(ptr->letter!=32) {
ptrWord->word[i]=ptr->letter;
ptr=ptr->next;
i++;
}
ptrWord->next=NULL;
if(wordStart==NULL){
wordStart=ptrWord;
endWord=wordStart;
} else {
appendWord(endWord,wordStart);
}
}
return wordStart;
}
sw* appendWord(sw* endWord, sw* newWord){
endWord->next=newWord;
return endWord->next;
}
void printWord(sw *wordStart){
sw* ptrWord =wordStart;
while(ptrWord!=NULL){
printf("%s",ptrWord->word);
ptrWord=ptrWord->next;
}
}
void freeWord(sw *wordStart){
ss* ptr=wordStart;
ss *tmp;
while(ptr!=NULL){
tmp=ptr->next;
free(ptr);
ptr=tmp;
}
}
Upvotes: 0
Views: 55
Reputation: 41170
In createWordList
you have
while(ptr->letter!='\n'){
ptrWord=(sw*)malloc(sizeof(sw));
while(ptr->letter!=32) {
ptrWord->word[i]=ptr->letter;
ptr=ptr->next;
i++;
}
Then
ptrWord->next=NULL;
if(wordStart==NULL){
wordStart=ptrWord;
endWord=wordStart;
} else {
appendWord(endWord,wordStart);
}
You are not using the return value of appendWord
and you append wordStart
instead of ptrWord
-- maybe you meant
endWord = appendWord(endWord, ptrWord);
Once you hit a space character you stop updating ptr
since that update is inside
while (ptr->letter!=32) {
EDITED
Added problem with not updating ptr
after space characters.
"Every word starts with '\n' because you don't skip past it." was a mistake on my part, I removed it.
Upvotes: 1