本文介绍了链表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在按以下方式写一个链表。


结构列表

{

struct list * next;

char * mybuff;

};


struct list * myList = NULL;

struct list * endList = NULL;


void getline(char s [],int lim)

{

int c,i;


for(i = 0;((i< lim-1)&&((c = getchar) ())!=''\ n'')); i ++)

s [i] = c;


s [i] =''\\ \\ 0'';

} //结束方法获取线


void add_item(char * data)

{

if(!endList)

{

endList =(struct list *)malloc(sizeof(struct list));


myList = endList;

endList-> mybuff = data;

endList-> next = NULL;

} //结束如果

else

{

endList-> next =(struct list *)malloc(sizeof(struct list));

endList = endList> next;

endList-> mybuff = data;

endList-> next = NULL;

} //结束其他

}


void printList()

{

struct list * current = myList;


while(当前)

{

printf("%s \ n",current-> mybuff);

current = current-> ;下一个;

}

}


int main()

{

char buff [50];


//调用一次

getline(buff,50);

add_item(buff);


printList();

}


现在在printList方法中,什么都没有打印,但每个项目只需要一个简单的空白行。


有人可以帮我解决这个问题。

提前致谢

Hi,

I am writing a linked list in the following way.

struct list
{
struct list *next;
char *mybuff;
};

struct list *myList = NULL;
struct list *endList = NULL;

void getline(char s[], int lim)
{
int c, i;

for(i=0; ((i<lim-1) && ((c=getchar()) != ''\n'')); i++)
s[i] = c;

s[i] =''\0'';
} // end method getline

void add_item(char *data)
{
if (!endList)
{
endList = (struct list *)malloc(sizeof(struct list));

myList = endList;
endList->mybuff = data;
endList->next = NULL;
} // end if
else
{
endList->next = (struct list *)malloc(sizeof(struct list));
endList = endList>next;
endList->mybuff = data;
endList->next = NULL;
} // end else
}

void printList()
{
struct list *current = myList;

while(current)
{
printf("%s\n", current->mybuff);
current = current->next;
}
}

int main()
{
char buff[50];

// called for a n times
getline(buff, 50);
add_item(buff);

printList();
}

Now in the printList method, the nothing is being printed, but just a
simple blank line for each item.

Can someone help me solve this problem out.
Thanks in Advance

推荐答案




当然。你所拥有的不是一个字符串列表,而是一个指向buff的

指针列表,因此printList()只打印N个拷贝的任何内容

你在线输入的内容N.


DES

-

Dag-Erling Sm?rgrav -




这篇关于链表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:06