当它尝试打印句子时,获取空格或 NULL 节点时出现问题。
输入文件是 txt 文件,内容为“这是我们大放异彩的时刻”,不带引号
还存在两个空的输出文件,用于以后的与当前问题无关的输出。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 200
typedef struct data{
char *word;
struct data *next;
}node;
node *head=NULL;
node *copy=NULL;
node *prev=NULL;
int _log=-1;
void read_content(char *filename);//prototype
node* create_word(char *string);//prototype
void add_word(node *word);//prototype
void delete_word(char *string);//prototype
void print_words();//prototype
void log_it(node *curr, node *next, int op);//prototype
void undo();//prototype
int main(int argc, char *argv[]){
if(argc!=4){
printf("\nInvalid parameters");
return 0;
}//end if
head=malloc(sizeof(node));
copy=malloc(sizeof(node));
prev=malloc(sizeof(node));
read_content(argv[1]);
int option=4;
int stringLength;
char *del_word=malloc(sizeof(char) * MAX);
char *word=malloc(sizeof(char) * MAX);
node *addit;
while(option!=0){
printf("\nSentence: ");
print_words();
printf("\nMenu: \n(1) Add new word\n(2) Delete word\n(3) Undo last action\n(0) Exit\nOption: ");
scanf("%d", &option);
if(option==1){//add new word
printf("\nPlease enter new word: ");
scanf("%s", word);
addit=create_word(word);
printf("\n%s", word);
add_word(addit);
}//end if
else if(option==2){//delete option
printf("\nPlease enter word to delete: ");
scanf("%s", del_word);
delete_word(del_word);
}//end if
else if(option==3){
undo();
}//end if
else if(option==0){
printf("\nTerminating program");
return 0;
}//end if
else{
printf("\nUnrecognized option");
}//end else
}//end while
return 0;
}//end main
//this is course cs2050
void read_content(char *filename){
FILE *input=fopen(filename, "r");
if(input==NULL){
head=NULL;
printf("\nEmpty list started");
}
char s[MAX];
char* token;
while(fgets(s, sizeof(s), input) != NULL)
{
int length = strlen(s)-1;
if(s[length] == '\n')
s[length] = '\0';
}//end while
token = strtok(s, " ");
//head = NULL;
node *newnode;
while (token)
{
char *s = malloc(sizeof(char) * strlen(token));
strcpy(s, token);
newnode=create_word(s);//create new node
printf("\n%s", newnode->word);//error checking
add_word(newnode);//add to linked list
token = strtok(NULL, " ");
}//end while
fclose(input);
}//end read content
//creates node from char string
node* create_word(char *string){
node *newNode=malloc(sizeof(node));
newNode->word=string;
newNode->next=NULL;
return newNode;
}//end create word
//adds node to link list
void add_word(node *word){
node *current=head;
if(current==NULL){
current=word;
}//end if
else{
while(current->next!=NULL){
current=current->next;
}//end while;
current->next=word;
}//end else
//printf("\n%s", current->next->word);
_log=1;
}//end add word
void delete_word(char *string){
node *current=head;
node *previous=NULL;
while(string!=current->word){
previous=current;
current=current->next;
}//end while
if(string==current->word){
previous->next=current->next;
copy->word=current->word;
copy->next=current->next;
prev=current;
}//end while
_log=-1;
}//end delete word
//function to print linked list as sentence
void print_words(){
node *printhead=head;
//printf("\n");
//printhead=printhead->next;
while(printhead!=NULL){
printf("%s ", printhead->word);
printhead=printhead->next;
}//end while
}//end print words
void log_it(node *curr, node *next, int op){
}//end log it
void undo(){
node *current=head;
node *prev=malloc(sizeof(node));
if(_log==-1)//last action was delete word
{
}//end if
else if(_log==1)//last action was insert word
{
}//end else if
else{
}//end else
}//end undo
下面的输出:
print_words() 打印:(null) 这是我们大放异彩的时候了
似乎一开始就有一个空节点起作用,但似乎找不到原因。