我试图写一个抽象的数据类型来表示使用链表的整数项集。
我的程序对此很好用。我想使用用户输入语句并将其存储为十进制值,以输入到链接列表集。
当我在此句子中输入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 : %d\n",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指针。

还有其他一些问题

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

同样,结构的typedef会使代码更易于阅读。

10-07 17:12