使用循环增加char值到链表[C] [英] Adding Char Value to a Linked List using Looping [C]
问题描述
我有一个即时通讯目前正试图将值添加到它的链接列表。但是,我必须设置不正确我的指针或者有一些与内存分配回事。
我要令牌添加到列表中,但每次有一个新的循环中的数据重叠。例如:
第1次:
REPL>一
一个
第2次:
REPL>乙
b
乙
注意如何公正消失了,我想保持previous值,而在新的价值增加。
下面是我的code迄今:
结构节点{
INT VAL;
结构节点*接下来的;
};结构节点*头= NULL;
结构节点* CUR = NULL;结构节点* create_list(int值)
{
结构节点* PTR =(结构节点*)malloc的(的sizeof(结构节点));
如果(NULL == PTR)返回NULL;
ptr-> VAL =价值;
ptr->接着= NULL; ptr->接下来=头;
头= PTR; 返回PTR;
};结构节点*插入(int值)
{
如果(NULL ==头)
回报(create_list(值)); 结构节点* PTR =(结构节点*)malloc的(的sizeof(结构节点));
ptr-> VAL =价值;
ptr->接着= NULL; ptr->接下来=头;
头= PTR; 返回PTR;};无效print_list(无效)
{
结构节点* PTR =头; 而(PTR!= NULL){
的printf(%S \\ n,ptr-> VAL);
PTR = ptr->接下来,
} 返回;
}结构EXP {
int型的;
工会{
INT NUM;
字符名称;
双小数;
焦炭strq;
}值;
};
INT主(INT ARGC,CHAR *的argv [])
{
而(1){
输出(REPL>);
字符*存储[30];
字符* TOK;
焦克;
炭缓冲液[20];
INT POS = 0,I;
与fgets(缓冲区,缓冲区尺寸,标准输入); TOK = strtok的(缓冲,); 而(TOK){
POS ++;
存储[POS] = TOK;
create_list(存储[POS]);
TOK =的strtok(NULL,);
} print_list();
}
}
我看到你的code以下问题:
- 在
print_list
,您可能需要更改的printf(%S \\ n,ptr-> VAL);
到的printf(%C \\ N,ptr-> VAL);
如果你想在节点作为字符打印值 - 我不知道你为什么使用它之前递增
POS
。你可能意味着该行create_list(存储[POS])后,增加它;
- 参数类型为
create_list
是INT
。你是通过一个的char *
它。也许你的意思是通过存储[POS] [0]
。 - 您可能也意味着
TOK = strtok的(TOK,);
。否则,,而
循环没有做任何好处。
在我做我的电脑这些更改到code,程序的表现就像你所期望它O操作。
I have a linked list which Im currently trying to add values to it. But I must have set my pointers incorrectly or there is something going on with the memory allocation.
I want to add tokens to the list but everytime there is a new loop the data overlaps. For example:
1st time:
repl> a a
2nd time:
repl> b b
b
Notice how the a just disappears, I want to keep the previous values while adding in new values.
Here's my code so far:
struct node {
int val;
struct node *next;
};
struct node *head = NULL;
struct node *cur = NULL;
struct node* create_list (int value)
{
struct node *ptr = (struct node*) malloc(sizeof (struct node));
if (NULL == ptr) return NULL;
ptr->val = value;
ptr->next = NULL;
ptr->next = head;
head = ptr;
return ptr;
};
struct node* insertion (int value)
{
if (NULL == head)
return (create_list(value));
struct node *ptr = (struct node*)malloc(sizeof(struct node));
ptr->val = value;
ptr->next = NULL;
ptr->next = head;
head = ptr;
return ptr;
};
void print_list(void)
{
struct node *ptr = head;
while(ptr != NULL) {
printf(" %s\n",ptr->val);
ptr = ptr->next;
}
return;
}
struct exp {
int type;
union {
int num;
char name;
double decimal;
char strq;
} value;
};
int main(int argc, char *argv[])
{
while(1) {
printf("repl>");
char *storage [30];
char* tok;
char g;
char buffer[20];
int pos = 0, i;
fgets(buffer,sizeof(buffer),stdin);
tok = strtok(buffer," ");
while(tok) {
pos++;
storage[pos] = tok;
create_list(storage[pos]);
tok = strtok(NULL," ");
}
print_list();
}
}
I see the following problems in your code:
- In
print_list
, you may want to changeprintf(" %s\n",ptr->val);
toprintf(" %c\n",ptr->val);
if you want to print the value at the node as a character. - I don't know why you are incrementing
pos
before using it. You probably meant to increment it after the linecreate_list(storage[pos]);
. - The argument type to
create_list
is anint
. You are passing achar *
to it. Perhaps you meant to passstorage[pos][0]
. - You probably also meant
tok = strtok(tok, " ");
. Otherwise, thewhile
loop is not doing you any good.
After I made those changes to your code in my computer, the program behaved like you expected it o.
这篇关于使用循环增加char值到链表[C]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!