#include?"stdio.h"

創(chuàng)新互聯(lián)專注于玉山企業(yè)網站建設,自適應網站建設,電子商務商城網站建設。玉山網站建設公司,為玉山等地區(qū)提供建站服務。全流程按需設計,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
#include?"stdlib.h"
typedef?struct?btnode//二叉鏈表類型定義
{char?data;
struct?btnode?*lchild,*rchild;
}bintree,*Bintree;
typedef?struct?LinkQueueNode//鏈隊列類型定義
{bintree?*data;
struct?LinkQueueNode?*next;
}LKQueNode;
typedef?struct?LKQueue
{LKQueNode?*front,*rear;
}LKQue;
void?InitQueue(LKQue?*LQ)//初始化隊列
{LKQueNode?*p;
p=(LKQueNode*)malloc(sizeof(LKQueNode));
LQ-front=p;
LQ-rear=p;
(LQ-front)-next=NULL;
}
int?EmptyQueue(LKQue?*LQ)//判斷隊列是否為空
{if(LQ-front==LQ-rear)
return?1;
else?return?0;
}
void?EnQueue(LKQue?*LQ,Bintree?x)//入隊列
{LKQueNode?*p;
p=(LKQueNode*)malloc(sizeof(LKQueNode));
p-data=x;
p-next=NULL;
(LQ-rear)-next=p;
LQ-rear=p;
}
int?OutQueue(LKQue?*LQ)//出隊列
{LKQueNode?*s;
if?(?EmptyQueue(LQ))
{exit(0);return?0;}
else
{s=(LQ-front)-next;
(LQ-front)-next=s-next;
if(s-next==NULL)
LQ-rear=LQ-front;
free(s);
return?1;}
}
Bintree?GetHead(LKQue?*LQ)//取隊列首元素
{LKQueNode?*p;bintree?*q;//q-data=-1;?錯誤在這里沒有分配空間就賦值
if(EmptyQueue(LQ))
return?q;
else?{p=LQ-front-next;
return?p-data;
}
}?
Bintree?initiate()//建二叉樹?
{char?ch;Bintree?t;
ch=getchar();????
if(ch=='#')?t=NULL;
else
{t=(Bintree)malloc(sizeof(bintree));
t-data=ch;
t-lchild=initiate();
t-rchild=initiate();
}
return?t;
}
void?Visit(Bintree?p)//訪問節(jié)點
{printf("%c",p-data);?//輸出是char
}
int?height(Bintree?t)
{int?ld,rd;
if(t==NULL)?return?0;
else?
{ld=height(t-lchild);
rd=height(t-rchild);
return?1+(ldrd?ld:rd);
}
}
void?levelorder(Bintree?bt)//層次遍歷
{LKQue?Q;Bintree?p;
InitQueue(Q);
if(bt!=NULL)
{EnQueue(Q,bt);
while(!EmptyQueue(Q))
{p=GetHead(Q);
OutQueue(Q);
Visit(p);
if(p-lchild!=NULL)??EnQueue(Q,p-lchild);
if(p-rchild!=NULL)??EnQueue(Q,p-rchild);
}
}
}
void?main()
{Bintree?T;
T=initiate();
printf("%d",height(T));
levelorder(T);
}
return "head"; 為什么函數(shù)返回是字符串,你的返回類型是結構體指針的
cannot convert `const char*' to `a*' in return
不能反轉char * 到 a* 的返回值
#include stdio.h
#include malloc.h
#define Len sizeof(a)
#define null 0
#include conio.h
struct a
{
int k;
struct a *next;
};
struct a* gethead()//建立
{
struct a *head, *p2;
printf("please enter data.");
head=p2=(struct a*)malloc(Len);
scanf("%d",p2-k); //輸入要地址
while(p2-k!=0)
{
p2-next=(struct a*)malloc(Len);
p2=p2-next;
scanf("%d",p2-k); //輸入要地址
}
p2-next=null;
return head; //返回指針
}
main()
{
struct a * head=gethead(); //變量名都沒有
}
有兩個地方錯誤
1:IsEmpty函數(shù)中是空返回真,非空返回假,你寫反了
int?IsEmpty(LinkQueue?*Q)
{
if(Q-front==Q-rear)
return(TRUE);////////////
else??return(FALSE);/////////////
}
2:GetHead函數(shù)中*x=Q-front-next-data;因為Q-front只是頭指針,不保存數(shù)據
int?GetHead(LinkQueue?*Q,int?*x)
{
if(!IsEmpty(Q))
{
*x=Q-front-next-data;///////////////
return(TRUE);
}
else?return(FALSE);
}
就幾個明顯的錯誤提一下
1. 函數(shù) enterqueue中
s-queue[s-tear];
這句沒有實際操作,估計應該是 s-queue[s-tear] = x; 用于在隊列最后添加一個新的元素
2. 主函數(shù)中,大約21行左右
for(j=0;jk;j++)
{
deletequeue(l,e);
if(gethead(l,f)==0) // 這里多了一個分號,導致判斷后沒有任何動作可之心,刪之
*f=0;
*e=*e+*f;
enterqueue(l,*e);
}
3. 主函數(shù)大約30行左右,打印輸出
for(j=0;jl-tear;j++,p++) // 這里不可用i作循環(huán)變量,否則干擾外循環(huán)i
printf("%d ",*p);
4. deletequeue(l,e);
if(gethead(l,f)==0) // 多了分號
*f=0;
*e=*e+*f;
從語句目的看,是希望從deletequeue和gethead函數(shù)中,得到e或者f的地址,但是 1)由于傳遞的是指針的值,所以即使函數(shù)中修改了,主函數(shù)中無法得到新的地址;2)這兩個函數(shù)體中,并沒有設置地址的代碼,總之,e 和 f 是沒有可操作地址的
5. 最重要一點,沒有看懂解題思路是什么。
即使按照刪一個節(jié)點,再添兩個節(jié)點的想法,也應該從隊列的后面開始,這樣間隔相加,并放在后移一個位置上,不至于影響后續(xù)計算。而這段程序從前開始修改隊列,那新的節(jié)點產生,不就干擾后面的計算了?
所以,建議先說明一下解題思路,這樣便于解決程序中的各種問題。
分享標題:c語言gethead函數(shù) C語言head
URL鏈接:http://www.yijiale78.com/article4/hhsdoe.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊、網站內鏈、網站維護、電子商務、營銷型網站建設、企業(yè)建站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)