将char添加到链表

时间:2013-11-13 19:58:33

标签: 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);


}

1 个答案:

答案 0 :(得分:1)

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

应该是

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

不应该吗?

您没有在addItem中设置newElementPtr-&gt;下一个指针。

还有一些其他问题

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

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