【发布时间】:2015-02-01 15:09:35
【问题描述】:
我只是想创建一个字符的链接列表。代码如下:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
char data;
struct node *next;
};
void push(struct node** head,char ndata)
{
struct node* temp=(struct node*)malloc(sizeof(node));
temp->data=ndata;
temp->next=(*head);
*head=temp;
}
void display(struct node* head)
{
struct node* temp=head;
while(temp)
{
printf("%c ",temp->data);
temp=temp->next;
}
}
int main()
{
struct node* head;
int a=1;
push(&head,'a');
push(&head,'b');
push(&head,'c');
push(&head,'b');
push(&head,'a');
display(head);
getch();
return 0;
}
我正在使用 push() 函数,它在头部插入值。然后使用 display() 方法显示列表中的值。当我执行程序时, 它说“program10.exe 已停止工作”。 我不明白问题是什么。有人可以帮忙吗?
【问题讨论】:
-
sizeof(node)?没有typedef?怎么样?? -
我正在使用 Dev-CPP 编译器。它会自动“附加” typedef。
标签: c linked-list segmentation-fault