本文目錄一覽:
- 1、急!求野人過河問題用c語言實現!!
- 2、C語言單鏈表運行問題
- 3、數據結構-使用C語言
- 4、按先序次序建立以下二叉樹,然後按先序的順序輸出結點的值、層次、左右孩子結點;用C語言編寫,初學數據結
- 5、c語言地址突然變為0
- 6、C語言 數據結構 二叉樹層次遍歷
急!求野人過河問題用c語言實現!!
本來沒打算髮出來。送你算了。
下面的程序稱得上perfect,程序在dev-c++下編譯通過.
#include stdio.h
#include stdlib.h
#include ctype.h
#define maxloop 100 /* 最大層數,對於不同的擴展方法自動調整取值 */
#define pristnum 3 /*初始化時設定有3個野人3個傳教士,實際可以改動*/
#define slavenum 3
struct SPQ{ int sr,pr; /* 船運行一個來回後河右岸的野人、傳教士的人數 */
int sl,pl; /* 船運行一個來回後河左岸的野人、傳教士的人數 */
int ssr,spr; /* 回來(由左向右時)船上的人數 */
int sst,spt; /* 去時(由右向左時)船上的人數 */
int loop; /* 本結點所在的層數 */
struct SPQ *upnode ,*nextnode;/* 本結點的父結點和同層的下一個結點的地址 */
}spq;
int loopnum;/* 記錄總的擴展次數 */
int openednum;/* 記錄已擴展節點個數 */
int unopenednum;/* 記錄待擴展節點個數 */
int resultnum;
struct SPQ *opened;
struct SPQ *oend;
struct SPQ *unopened;
struct SPQ *uend;
struct SPQ *result;
void initiate();
void releasemem();
void showresult();
void addtoopened(struct SPQ *ntx);
int search();
void goon();
int stretch(struct SPQ* ntx);
void recorder();
int main()
{
int flag; /* 標記擴展是否成功 */
for( ; ; )
{
initiate();
flag = search ();
if(flag == 1)
{
recorder();
releasemem();
showresult();
goon();
}
else
{
printf(“無法找到符合條件的解”);
releasemem();
goon();
}
}
system(“pause”);
return 0;
}
void initiate()
{
int x;
char choice;
uend = unopened = (struct SPQ*)malloc(sizeof(spq));
if(uend==NULL)
{
printf(“\n內存不夠!\n”);
exit(0);
}
unopenednum=1;
openednum=0;
unopened – upnode = unopened; /* 保存父結點的地址以成鏈表 */
unopened – nextnode = unopened;
unopened – sr = slavenum;
unopened – pr = pristnum;
unopened – sl = 0;
unopened – pl = 0;
unopened – sst = 0;
unopened – spt = 0;
unopened – ssr = 0;
unopened – spr = 0;
unopened – loop = 0;
printf(“題目:設有n個傳教士和m個野人來到河邊,打算乘一隻船從右岸到左岸去。\n”);
printf(“該船的負載能力為兩人。在任何時候,如果野人人數超過傳教士人數,野人\n”);
printf(“就會把傳教士吃掉。他們怎樣才能用這條船安全的把所有人都渡過河去?\n”);
printf(“\n默認的n、m值皆為3\n”);
for(;;)
{
printf(“\n是否修改?(Y/N)”);
scanf(“%s”,choice);
choice=toupper(choice);
if(choice==’Y’)
{
printf(“\n請輸入傳教士人數”);
for(;;)
{
scanf(“%d”,x);
if(x0)
{
unopened – pr = x;
break;
}
else printf(“\n輸入值應大於0!\n請重新輸入”);
}
printf(“\n請輸入野人人數”);
for(;;)
{
scanf(“%d”,x);
if(x0)
{
unopened – sr = x;
break;
}
else printf(“\n輸入值應大於0!\n請重新輸入”);
}
break;
}
if(choice==’N’)break;
}
}
int search()
{
int flag;
struct SPQ *ntx; /* 提供將要擴展的結點的指針 */
for( ; ; )
{
ntx = unopened; /* 從待擴展鏈表中提取最前面的一個 */
if(ntx-loop == maxloop)
return 0;
addtoopened(ntx); /* 將ntx加入已擴展鏈表,並將這個節點從待擴展鏈表中去掉 */
flag = stretch(ntx); /* 對ntx進行擴展,返回-1,0,1 */
if(flag == 1)
return 1;
}
}
int stretch(struct SPQ *ntx)
{
int fsr , fpr ; /* 在右岸上的人數 */
int fsl , fpl ; /* 在左岸上的人數 */
int sst , spt ; /* 出發時在船上的人數 */
int ssr , spr ; /* 返回時船上的人數 */
struct SPQ *newnode;
for (sst = 0 ; sst = 2 ; sst++) /* 討論不同的可能性並判斷是否符合條件 */
{
fsr = ntx – sr;
fpr = ntx – pr;
fsl = ntx – sl;
fpl = ntx – pl;
if ((sst = fsr) (( 2 – sst) = fpr))/* 滿足人數限制 */
{
spt = 2 – sst;
fsr = fsr – sst;
fpr = fpr – spt;
if((fpr == 0) (fsr == 0))/* 搜索成功 */
{
newnode = (struct SPQ*) malloc (sizeof(spq));
if(newnode==NULL)
{
printf(“\n內存不夠!\n”);
exit(0);
}
newnode – upnode = ntx; /* 保存父結點的地址以成鏈表 */
newnode – nextnode = NULL;
newnode – sr = 0;
newnode – pr = 0;
newnode – sl = opened – sr;
newnode – pl = opened – pr;
newnode – sst = sst;
newnode – spt = spt;
newnode – ssr = 0;
newnode – spr = 0;
newnode – loop = ntx – loop + 1;
oend – nextnode = newnode;
oend = newnode;
openednum++;
return 1;
}
else if ((fpr – fsr) * fpr = 0) /* 判斷是否滿足傳教士人數必須大於或等於野人人數 */
{
fsl = fsl + sst;
fpl = fpl + spt;
for (ssr = 0 ; ssr = 1 ; ssr++) /* 返回 */
{
int ffsl , ffpl;
if ((ssr = fsl) ((1 – ssr) = fpl))
{
spr = 1 – ssr;
ffsl = fsl – ssr;
ffpl = fpl – spr;
if ((ffpl – ffsl) * ffpl = 0)
{ /* 若符合條件則分配內存並付值 */
int ffsr , ffpr;
ffsr = fsr + ssr;
ffpr = fpr + spr;
newnode = (struct SPQ*) malloc (sizeof(spq));
if(newnode==NULL)
{
printf(“\n內存不夠!\n”);
exit(0);
}
newnode – upnode = ntx; /* 保存父結點的地址以成鏈表 */
newnode – sr = ffsr;
newnode – pr = ffpr;
newnode – sl = ffsl;
newnode – pl = ffpl;
newnode – sst = sst;
newnode – spt = spt;
newnode – ssr = ssr;
newnode – spr = spr;
newnode – loop = ntx – loop + 1;
uend – nextnode = newnode;
uend = newnode;
unopenednum++;
}
}
}
}
}
}
return 0;
}
void addtoopened(struct SPQ *ntx)
{
unopened = unopened – nextnode;
unopenednum–;
if (openednum == 0 )
oend = opened = ntx;
oend – nextnode = ntx;
oend = ntx;
openednum++;
}
void recorder()
{
int i , loop;
struct SPQ *newnode;
struct SPQ *ntx;
loop = oend – loop;
ntx = oend;
resultnum = 0;
for( i = 0 ; i = loop ; i++ )
{
newnode = (struct SPQ*) malloc (sizeof(spq));
if(newnode==NULL)
{
printf(“\n內存不夠!\n”);
exit(0);
}
newnode – sr = ntx – sr;
newnode – pr = ntx – pr;
newnode – sl = ntx – sl;
newnode – pl = ntx – pl;
newnode – sst = ntx – sst;
newnode – spt = ntx – spt;
newnode – ssr = ntx – ssr;
newnode – spr = ntx – spr;
newnode – nextnode = NULL;
ntx = ntx – upnode;
if(i == 0)
result = newnode;
newnode – nextnode = result;
result = newnode;
resultnum++;
}
}
void releasemem()
{
int i;
struct SPQ* nodefree;
for ( i = 1 ; i openednum ; i++ )
{
nodefree = opened;
opened = opened – nextnode;
free(nodefree);
}
for ( i = 0 ; i unopenednum ; i++ )
{
nodefree = unopened;
unopened = unopened – nextnode;
free(nodefree);
}
}
void showresult()
{
int i;
int fsr , fpr ; /* 在右岸上的人數 */
int fsl , fpl ; /* 在左岸上的人數 */
struct SPQ* nodefree;
printf(“%d個傳教士”,result – pr);
printf(“%d個野人”,result – sr);
printf(“%d個傳教士”,result – pl);
printf(“%d個野人”,result – sl);
for ( i = 1 ; i resultnum ; i++ )
{
nodefree = result;
result = result – nextnode;
free(nodefree);
printf(“\n\n\t左岸人數 船上人數及方向 右岸人數\n”);
printf(“第%d輪\n”,i);
fpl = result – pl – result – spt + result – spr;
fpr = result – pr – result – spr;
fsl = result – sl – result – sst + result – ssr;
fsr = result – sr – result – ssr;
printf(“傳教士%8d%8d\t-\t%8d\n”,fpl,result – spt,fpr);
printf(“野 人%8d%8d\t-\t%8d\n”,fsl,result – sst,fsr);
printf(“傳教士%8d%8d\t-\t%8d\n”,result – pl,result – spr,result – pr – result – spr);
printf(“野 人%8d%8d\t-\t%8d\n”,result – sl,result – ssr,result – sr – result – ssr);
}
printf(“\n全體傳教士和野人全部到達對岸”);
free(result);
}
void goon()
{
char choice;
for(;;)
{
printf(“是否繼續?(Y/N)\n”);
scanf (“%s” , choice);
choice=toupper(choice);
if(choice==’Y’)break;
if(choice==’N’)exit(0);
}
}
C語言單鏈表運行問題
問題應該出在你的那個initiate函數和那個append函數上。沒怎麼用過你那種函數形式,在測試的時候發現,貌似是你的l和s的空間開闢的時候出錯了。那就是你的函數有問題。我給你改成了返回指針的函數形式。另外。你的for循環也有問題。結果不是你所要的結果,至於為什麼會那樣。你自己先去分析一下。再對比一下我的for,你應該就會明白的。。下面就是改了之後的程序。純手打。望採納
#includestdio.h
#includestdlib.h
typedef struct Data
{
int num;
struct Data* next;
}D;
D *initiate(void)
{
D *L;
L=(D *)malloc(sizeof(D));
L-next=NULL;
return L;
}
/*void append(D *s)
{
s=(D *)malloc(sizeof(D));
}*/
void main()
{
D *l,*s,*p;
int i,t=3;
l=initiate();
printf(“你的for循環結果:\n”);
for(i=4;i0;(int)i/=2)
{
// append(s);
s=initiate();
s-num=i;
s-next=l-next;
l-next=s;
}
p=l-next;
while(t–)
{
printf(“%d\n”,p-num);
p=p-next;
}
printf(“我的for循環結果:\n”);
p=l;
for(i=4;i0;(int)i/=2)
{
// append(s);
s=initiate();
s-num=i;
p-next=s;
p=s;
}
p=l-next;
t=3;
while(t–)
{
printf(“%d\n”,p-num);
p=p-next;
}
system(“pause”);
}
數據結構-使用C語言
加*表示是指針,即函數用的是傳地址,這樣參數在函數裏面被修改後是可以保存的。要初始化當然需要吧這樣了
而求當前數據元素個數的時候,只是告訴函數有這樣一個表,而對錶本身不需要做任何修改,就不需要傳地址,就是不加*,傳值,這也保證了表的安全性,以免被修改
按先序次序建立以下二叉樹,然後按先序的順序輸出結點的值、層次、左右孩子結點;用C語言編寫,初學數據結
#includestdio.h
#includestdlib.h
#includeconio.h
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char elemtype;
typedef struct BiTNode{
elemtype data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//構造二叉樹
Status CreateBiTree(BiTree T){
elemtype ch;
ch=getchar();
if(ch==’ ‘){T=NULL;}
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
return FALSE;
T-data=ch;
CreateBiTree(T-lchild);
CreateBiTree(T-rchild);
}
return OK;
}
// 先序遍歷
void PreOrderTraverse(BiTree T){
if(T!=NULL){
printf(“%c “,T-data);
PreOrderTraverse(T-lchild);
PreOrderTraverse(T-rchild);
}
}
//葉子節點的個數
Status Leafnumber(BiTree T){
int num1=0,num2=0;
if(T==NULL)
return 0;
else if (T-lchild==NULLT-rchild==NULL) return 1;
else
{
num1=Leafnumber(T-lchild);
num2=Leafnumber(T-rchild);
return(num1+num2);
}
}
//樹的深度
Status DepthTree(BiTree T){
int llength=0,rlength=0;
if(T==NULL) return 0;
else{
llength=DepthTree(T-lchild);
rlength=DepthTree(T-rchild);
return(llengthrlength)?(llength+1):(rlength+1);
}
}
void main()
{
BiTree s;
printf(“輸入字符串,使用空格代表空\n”);
CreateBiTree(s);
printf(“先序輸出:\n”);
PreOrderTraverse(s);
printf(“\n樹的深度:%d\n”,DepthTree(s));
getch();
}
c語言地址突然變為0
*m變0,因為你有m=a, 循環結束後a==NULL, 所以*m==a==NULL;
其它還有一些地方需要改的:
void insert(node *h,int i,int x) //依次輸入指向頭結點的指針,要插入的位置,插入的值。
{
node *p1,*p2,*t;
int j=0;
p1=p2=h;
t=(node*)malloc(sizeof(node));
t-data=x;
printf(“%d”,p1-next );
printf(“\nout\n”);
while(p1-nextj=i-1)
{
p1=p1-next;
j++;
}
if(j==i)
{
if(p1-next ==NULL)
{
p1-next=t;
t-next =NULL;
}
else
{
p2=p1-next;
t-next=p2;
p1-next =t;
}
}
else
printf(“error!”);
}
int main()
{
node *m;
node *a;
initiate(a);
m=a;
printf(” \nthe address of *m:%d\n”,m);
for(a=(m)-next;a;a=a-next)
{
printf(“%d “,a-data);
printf(” \nthe address of *m:%d\n”,m); // 運行可以發現,*m突然變成了0,求解
}
printf(” \nthe address of *m:%d\n”,m);
insert(m,2,77);
for(a=(m)-next;a;a=a-next)
{
printf(“%d “,a-data);
}
return 0;
}
C語言 數據結構 二叉樹層次遍歷
#include “stdio.h”
#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)//訪問節點
{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);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/279664.html