#include<stdio.h>
#include<stdlib.h>
typedef int Elemtype;
typedef struct link
{
int elem;
struct link* next;
}link;
link * initLink() {
link* p = (link*)malloc(sizeof(link));//創建一個頭結點
link* temp = p;//宣告一個指標指向頭結點,用于遍歷鏈表
//生成鏈表
for (int i = 1; i < 5; i++) {
link* a = (link*)malloc(sizeof(link));
a->elem = i;
a->next = NULL;
temp->next = a;
temp = temp->next;
}
return p;
}
void bianli(link* p)
{
link* q = p;
while (q->next)
{
q = q->next;
printf("%d",q->elem);
}
printf("\n");
}
int main()
{
link* p = initLink();
bianli(&p);
return 0;
}
為什么地址會出錯啊,本人新手一枚檢查不出來,求大佬解答
uj5u.com熱心網友回復:
111求解啊uj5u.com熱心網友回復:
供參考:#include<stdio.h>
#include<stdlib.h>
typedef int Elemtype;
typedef struct link
{
int elem;
struct link* next;
}link;
link * initLink()
{
link* p = (link*)malloc(sizeof(link));//創建一個頭結點
link* temp = p;//宣告一個指標指向頭結點,用于遍歷鏈表
//生成鏈表
for (int i = 1; i < 5; i++) {
link* a = (link*)malloc(sizeof(link));
a->elem = i;
a->next = NULL;
temp->next = a;
temp = a; // temp = temp->next;
}
return p;
}
void bianli(link* p)
{
link* q = p;
while (q->next)
{
q = q->next;
printf("%d ",q->elem);
}
printf("\n");
}
int main()
{
link* p = initLink();
bianli(p); //bianli(&p);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283242.html
標籤:C語言
