C语言 将字符添加到链表



我正在尝试编写一个抽象数据类型来表示使用链表的整数项目集。我的程序对此工作正常。我想使用用户输入句子并将其存储在十进制值中,以便输入到链表集。当我在此句子中输入超过 10 个字符时,我的程序崩溃,我不知道为什么。下面是长代码,底部用 * 突出显示了 fgets 输入。

#include<stdio.h>
#include<stdlib.h>
#include <strings.h>
struct linkedListElement{
int data;
struct linkedListElement * next;
                    };
struct linkedListSet {

struct linkedListElement * header;
struct linkedListElement * current;
struct linkedListElement * temp;
int size;
                };
struct linkedListSet * createdSet (){
struct linkedListSet * newSet = malloc(sizeof(struct linkedListSet));
newSet->header = malloc(sizeof(struct linkedListElement));
    newSet->current = malloc(sizeof(struct linkedListElement));
    newSet->temp = malloc(sizeof(struct linkedListElement));
newSet->size = 0;
return newSet;
}
int addItem (struct linkedListSet * setPtr, int info){
    if(linkedList_lookup(setPtr, info)){return 0;}
        struct linkedListElement * newElementPtr = malloc(sizeof(struct      linkedListElement));
newElementPtr->data = info;
if(setPtr->header->data == 0){
    setPtr->header = newElementPtr;
    }//if first element set as header
if(setPtr->current->data != 0){ //if it is not first element last element points to       new element 
    setPtr->current->next = newElementPtr;
                                }
setPtr->current = newElementPtr; 
setPtr->size++;
return 1;
};
int linkedList_remove(struct linkedListSet * setPtr, int item){
if(!linkedList_lookup(setPtr, item)){return 0;};
linkedList_lookup(setPtr, item);
setPtr->temp->next = setPtr->current->next;
setPtr->size--;
return 1;
};

int linkedList_lookup(struct linkedListSet * setToBeSearched, int numToFind){
struct linkedListElement * i;
i= setToBeSearched->header;
int found = 0;
int count = 0;
setToBeSearched->temp = setToBeSearched->header;
for(count = 0, i= setToBeSearched->header; i->next !=NULL,count<setToBeSearched-    >size; i = i->next, count++){
    if(i->data == numToFind){ found = 1; setToBeSearched->current =     i;}//current points to : found matching element
    else if(found != 1){ setToBeSearched->temp = i;}// temp points to : element     before found matching element 
}
if(found){return 1;}
return 0;
};


void printMyList(struct linkedListSet * setToBePrinted){
struct linkedListElement * i;
int count = 0;
for (count = 0, i = setToBePrinted->header; count<setToBePrinted->size; i = i-  >next, count++){
    printf(" %d", i->data);
};
printf("n");
};

int main(){
//create set
struct linkedListSet * firstSet = createdSet();
struct linkedListSet * secondSet = createdSet();
struct linkedListSet * comboSet = createdSet();

addItem(firstSet, 300);
addItem(firstSet, 200);
addItem(firstSet, 100);
addItem(firstSet, 5);
addItem(firstSet, 37);
addItem(firstSet, 299);
addItem(firstSet, 201);
addItem(firstSet, 101);
addItem(firstSet, 51);
addItem(firstSet, 371);
addItem(firstSet, 301);
addItem(firstSet, 202);
addItem(firstSet, 102);
addItem(firstSet, 52);
addItem(firstSet, 377);
addItem(firstSet, 305);
addItem(firstSet, 205);
addItem(firstSet, 150);
addItem(firstSet, 55);
addItem(firstSet, 375);
addItem(firstSet, 259);
addItem(firstSet, 251);
addItem(firstSet, 151);
addItem(firstSet, 551);
addItem(firstSet, 571);
addItem(firstSet, 3501);
addItem(firstSet, 2052);
addItem(firstSet, 1052);
addItem(firstSet, 525);
addItem(firstSet, 3757);
addItem(secondSet, 340);
addItem(secondSet, 200);
addItem(secondSet, 120);
addItem(secondSet, 52);
addItem(secondSet, 3799);
addItem(secondSet, 341);
addItem(secondSet, 201);
addItem(secondSet, 121);
addItem(secondSet, 521);
addItem(secondSet, 3792);
addItem(secondSet, 342);
addItem(secondSet, 202);
addItem(secondSet, 122);
addItem(secondSet, 53);
addItem(secondSet, 3739);
addItem(secondSet, 3440);
addItem(secondSet, 2040);
addItem(secondSet, 1240);
addItem(secondSet, 524);
addItem(secondSet, 37499);
addItem(secondSet, 3441);
addItem(secondSet, 2041);
addItem(secondSet, 1241);
addItem(secondSet, 5241);
addItem(secondSet, 37492);
addItem(secondSet, 3442);
addItem(secondSet, 2042);
addItem(secondSet, 1242);
addItem(secondSet, 534);
addItem(secondSet, 37439);
printMyList(firstSet);
if(linkedList_lookup(firstSet, 100)){
    printf(" nfound number in list");
    };

printf("ncurrent data is now set to : %d  and temp to : %dn",firstSet->current->data, firstSet->temp-> data);
linkedList_remove(firstSet, 100);
printMyList(firstSet);
//**********************************************************************
struct linkedListSet * charSet = createdSet();
char strBuff1[60];
printf(" Please enter sentence: ");
fgets(strBuff1, 60, stdin);
int i = 0;
for ( int i = 0 ; strBuff1[i] != 0; i++){
    printf(" %c ", strBuff1[i]);
    addItem(charSet, strBuff1[i]);
}
//***********************************************************************
return (0);

}
i->next !=NULL,count<setToBeSearched->size

应该是

i->next !=NULL && count<setToBeSearched->size 

不应该吗?

您没有在 addItem 中设置 newElementPtr->next 指针。

还有一些其他问题

struct linkedListSet 应包含指向已创建的结构 linkedListElement 的指针。将数据从这些结构复制到结构 linkedListSet 中重复的 linkedListElement 是不可靠且不必要的。

此外,结构的 typedef 将使代码更易于阅读。

相关内容

  • 没有找到相关文章

最新更新