i understand all that but what i dont get it how to put the words into the linked list
i know this is a bit cubersome but here it is.it runs and compiles but there is nothing in my start list.i dont know where i am going wrong
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
FILE*in;
typedef struct node{
char word[20];
int count;
struct node *next;
}Node,*Nodeptr;
//FUNCTIONS=======================================================================================================
Nodeptr addinplace(Nodeptr ,char[] );
Nodeptr makenode(char[] );
void printlist(Nodeptr );
int getword(FILE*,char[]);
Nodeptr search(Nodeptr,char[] );
//================================================================================================================
int main(){
in=fopen("shims.txt","r");
if(in==NULL){
printf("Filenot found!! \n");-
system("pause");
exit(1);
}
char wod[20],string[20];
int cmp,x=1;
Nodeptr start=NULL,srch;
while(getword(in,wod)!=0){
addinplace(start,wod);
printf("%s ",wod);
}
printf("!\n");
printlist(start);
printf("5");
printf("\n");
fclose(in);
system("pause");
return 0;
}
//making a node to hold the word
Nodeptr makenode(char string[]){
Nodeptr str=(Nodeptr)malloc(sizeof(Node));
strcpy(str->word,string);
str->next=NULL;
return str;
}
//adds words inorder into th3 list
Nodeptr addinplace(Nodeptr top,char string[]){
Nodeptr str,curr,prev,makenode(char[]);
str=makenode(string);
prev=NULL;
curr=top;
while(curr!=NULL && strcmp(curr->word,string)<0){
prev=curr;
curr=curr->next;
}
if(prev==NULL){
str->next=top;
return str;
}
str->next=curr;
prev->next=str;
return top;
}
//prints linked structures
void printlist(Nodeptr top){
while(top!=NULL){
printf("%s\n",top->word);
top=top->next;
}
}
//stores the next word if any,in wrd;word is converted to lowercase
//return 1 if found 0 otherwise.
int getword(FILE*in,char string[]){
char ch;
int n=0;
//read over blank space
while(!isalpha(ch=getc(in)) && ch!='\0')
if(ch==EOF) return 0;
string[n++]=tolower(ch);
while(isalpha(ch=getc(in)) && ch!='\0')
if(n<20)string[n++]=tolower(ch);
string[n]='\0';
return 1;
}
//searches through the list
Nodeptr search(Nodeptr top,char string[]){
while(top!=NULL && (strcmp(top->word,string)!=0))
top=top->next;
return top;
}