c - 将字符添加到链接列表中

标签 c struct linked-list

我正在尝试编写一个抽象数据类型来使用链接列表表示整数项集。 我的程序对此效果很好。我想使用用户输入的句子并将其存储为十进制值以输入到链表集。 当我在这句话中输入超过 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 指针。

还有一些其他问题

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

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

关于c - 将字符添加到链接列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19963151/

相关文章:

android - ftello() 和 fseeko() android 构建错误

使用函数更改指针包含的地址

c - 别名的语法

c++ - 如何从 C++ 中的函数返回结构?

c - 错误 : expected ‘:’ , ‘,’、 ‘;’、 ‘}’ 或 ‘__attribute__’ token 之前的 ‘=’

c# - 将结构转换为 JSON

python - 链表反转算法不起作用

c - 在链表的第n个位置添加一个包含多位数字的节点

c - 加权随机数生成

java - 相同的变量名使用了两次但程序没有抛出错误?