#include<stdio.h>
#include<stdlib.h>
typedef struct NODE
{
int data;
NODE* next;
NODE* prior;
}NODE,*PNODE;
PNODE Create()
{
int i = 0;
PNODE head, p1, p2;
head = (PNODE)malloc(sizeof(PNODE));
p1= (PNODE)malloc(sizeof(PNODE));
head->next = p1;
p1->prior = head;
for (; i <= 9; i++)
{
p2= (PNODE)malloc(sizeof(PNODE));
p1->data = i;
p1->next = p2;
p2->prior = p1;
p1 = p2;
}
return head;
}
void Print(PNODE head)
{
int i = 0;
for(; i <= 9;i++)
{
printf("%d\n", head->data);
head = head->next;
}
for (i=0; i <= 9; i++)
{
printf("%d\n", head->data);
head = head->prior;
}
}int main()
{
int ins;
PNODE pHead;
pHead = Create();
Print(pHead); return 0;
}
輸出是沒有輸出。不知道為啥。debug了一會兒,結果如圖,說是堆記憶體破壞了。
uj5u.com熱心網友回復:
啊啊。這里寫的是錯的。主函式里是分配了pHead空間的。但是也是沒有任何輸出
uj5u.com熱心網友回復:
你寫的Create函式回傳的head指向的節點中沒存放資料,而你在Print函式中直接先列印head->next這是錯誤的uj5u.com熱心網友回復:
啊。謝謝您!
我說我咋找不出來。可是為什么我寫單向鏈表的時候同樣是這樣卻能夠實作呢?
uj5u.com熱心網友回復:
#include<stdio.h>
#include<stdlib.h>
typedef struct NODE
{
int data;
struct NODE *next;
}NODE,*PNODE;
PNODE creat()
{
int i=0;
PNODE pHead,pNode1,pNode2;
pHead=(PNODE)malloc(sizeof(PNODE));
pNode1=(PNODE)malloc(sizeof(PNODE));
pHead->next=pNode1;
pNode1->next=NULL;
for(;i<=10;i++)
{
pNode2=(PNODE)malloc(sizeof(PNODE));
pNode1->data=https://bbs.csdn.net/topics/i;
pNode1->next=pNode2;
pNode1=pNode2;
}
return pHead;
}
void print(PNODE pHead)
{
int i=0;
for(;i<=10;i++)
{
printf("%d\n",pHead->data);
pHead=pHead->next;
}
}
int main()
{
PNODE pHead;
pHead=creat();
print(pHead);
return 0;
}
uj5u.com熱心網友回復:
你寫的Create函式回傳的head指向的節點中沒存放資料,而你在Print函式中直接先列印head->next這是錯誤的
啊。謝謝您!我說我咋找不出來。可是為什么我寫單向鏈表的時候同樣是這樣卻能夠實作呢?
#include<stdio.h>
#include<stdlib.h>
typedef struct NODE
{
int data;
struct NODE *next;
}NODE,*PNODE;
PNODE creat()
{
int i=0;
PNODE pHead,pNode1,pNode2;
pHead=(PNODE)malloc(sizeof(PNODE));
pNode1=(PNODE)malloc(sizeof(PNODE));
pHead->next=pNode1;
pNode1->next=NULL;
for(;i<=10;i++)
{
pNode2=(PNODE)malloc(sizeof(PNODE));
pNode1->data=https://bbs.csdn.net/topics/i;
pNode1->next=pNode2;
pNode1=pNode2;
}
return pHead;
}
void print(PNODE pHead)
{
int i=0;
for(;i<=10;i++)
{
printf("%d\n",pHead->data);
pHead=pHead->next;
}
}
int main()
{
PNODE pHead;
pHead=creat();
print(pHead);
return 0;
}
同樣的問題,Create函式回傳的head指向的節點中沒存放資料,而你在Print函式中直接先列印head->data這是錯誤的,,應該先執行head=head->next; 陳述句。
uj5u.com熱心網友回復:
你寫的Create函式回傳的head指向的節點中沒存放資料,而你在Print函式中直接先列印head->next這是錯誤的
啊。謝謝您!我說我咋找不出來。可是為什么我寫單向鏈表的時候同樣是這樣卻能夠實作呢?
#include<stdio.h>
#include<stdlib.h>
typedef struct NODE
{
int data;
struct NODE *next;
}NODE,*PNODE;
PNODE creat()
{
int i=0;
PNODE pHead,pNode1,pNode2;
pHead=(PNODE)malloc(sizeof(PNODE));
pNode1=(PNODE)malloc(sizeof(PNODE));
pHead->next=pNode1;
pNode1->next=NULL;
for(;i<=10;i++)
{
pNode2=(PNODE)malloc(sizeof(PNODE));
pNode1->data=https://bbs.csdn.net/topics/i;
pNode1->next=pNode2;
pNode1=pNode2;
}
return pHead;
}
void print(PNODE pHead)
{
int i=0;
for(;i<=10;i++)
{
printf("%d\n",pHead->data);
pHead=pHead->next;
}
}
int main()
{
PNODE pHead;
pHead=creat();
print(pHead);
return 0;
}
同樣的問題,Create函式回傳的head指向的節點中沒存放資料,而你在Print函式中直接先列印head->data這是錯誤的,,應該先執行head=head->next; 陳述句。
不。我的意思是。這個程式是可以運行的。上面那個程式是不可以運行的。但是給頭結點賦值后卻變得可以運行了。而之前那個可以運行的程式本身沒有賦值給頭結點也能夠運行。我想知道為什么?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/87370.html
標籤:新手樂園
