链表问题 [英] Problem with a linked list

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

问题描述




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


结构列表

{

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

推荐答案

be *********@yahoo.com (Xarky)写道:
be*********@yahoo.com (Xarky) writes:
int main()
{
char buff [50];

//呼唤一次
getline(buff,50);
add_item(buff);

printList( );
}

现在在printList方法中,没有打印任何内容,只是每个项目只有一个简单的空白行。
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.




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

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

你在线输入的内容N.


DES

-

Dag-Erling Sm?rgrav - de *@des.no


你确定这个程序是编译的,而且你不是在寻找

在其他二进制文件中?该行有一个语法错误:

endList = endList> next; (应该是endList-> next)


除此之外,你是否意识到你正在分配数据

而没有分配任何内存给结构元素mybuff?

这样你最终可能会破坏很多其他东西,但是,有时你会发现相同的字符串(mybuff)
节点。


总的来说,代码写得不好。

Are you sure this program compiled, and that you are not looking
at some other binary? There is a syntax error at the line:
endList = endList>next; (should be endList->next)

Besides this, have you realized that you are ''assigning'' data
without allocating any memory to the structure element mybuff?
This way you might end up corrupting many other things, however,
sometimes, you would find the same string (mybuff) in all the
nodes.

Overall, a poorly written code.


voke,我认为你有没有为char * mybuff分配内存

结构的成员和它将指向的是完全

实现依赖......如果相同的char *被替换为int或

普通字符它肯定会有效,只要你有正确的语法和

逻辑。

voke, what i think is u have not allocated memory fo the char * mybuff
member of the structure and what it will point is completely
implementation dependent......if the same char * is replaced with int or
plain char it will definitely work, provided u have the right syntax and
logic.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆