這是我的計劃是怎樣,但它不打印的完整列表從而進入last.I不明白什麼是連接或沒有問題的唯一節點的數據:的單清單打印錯誤
基本結構節點
struct node
{
int data;
struct node *link;
};
定義標題作爲鏈接列表的開始:
用於插入struct node *header;
功能和打印:
void insertFront_sl();
void print_sl();
主要功能:列表的
void main()
{
clrscr();
header=(struct node *)malloc(sizeof(struct node));
header->link=NULL;
header->data=NULL;
insertFront_sl();
insertFront_sl();
insertFront_sl();
insertFront_sl();
print_sl();
getch();
}
void insertFront_sl(){
struct node *temp;
int x;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nMeM0rY Insufficient ..");
}
else
{
printf("\nGot New Node \nNow Insert Data Into Node : ");
scanf("%d",&x);
temp->data=x;
header->link=temp;
}
}
void print_sl(){
struct node *ptr;
ptr=header;
while(ptr->link !=NULL)
{
ptr=ptr->link;
printf("%d\t",ptr->data);
}
}
你能顯示它打印的輸出嗎? –
你從來沒有設置'temp-> link' –
'insertNode_sl()'是有問題的,它不需要更新'header'(可能只有當header-> link爲NULL時,只有在你第一次插入節點時)。但請縮進您的代碼,以便閱讀。代碼中還有其他設計問題,但這不在此範圍之內。 –