#include <stdio.h>
#include <stdlib.h>
typedef struct node_struct {
int data;
struct node_struct *next;
} node;
typedef node *list;
int main()
{
int temp;
list head , tempList;
char cont = 'Y';
head = NULL;
do {
printf("Enter Data\n");
scanf("%d",&temp); fflush(stdin);
tempList = (list)malloc(sizeof(node));
tempList->data = temp;
tempList->next = head->next; // This line has error
head->next = tempList;
printf("Do you wish to continue (Y/N)\n");
scanf("%c", &cont); fflush(stdin);
} while (cont == 'Y');
return 0;
}
程序接收到的信號SIGSEGV,分段錯誤。 0x08048516 in mainself()listself.c:24c程序列表訪問值分段錯誤
我想將指針指向最新的輸入值。但head-> next給我分段錯誤。
我的問題是我如何實現這個邏輯?
user input 1 : 5
user input 2 : 6
user input 3 : 3
和列表的內部結構像
頭 - > 3 - > 6 - > 5
此外while循環取入值CONT之前剛剛退出。可以用它來代替「\ n」。任何解決方案?
'head-> next'是no的地址,因爲head本身爲null,然後將它分配給templist-> next。 – 0decimal0 2014-09-27 13:44:28
請讓我知道,如何在上面的代碼中初始化head-> NULL旁邊? – yantrakaar 2014-09-27 14:04:41