c语言initiate,c语言initialized怎么解决

本文目录一览:

急!求野人过河问题用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/n/279664.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-20 15:05
下一篇 2024-12-20 15:05

相关推荐

  • AES加密解密算法的C语言实现

    AES(Advanced Encryption Standard)是一种对称加密算法,可用于对数据进行加密和解密。在本篇文章中,我们将介绍C语言中如何实现AES算法,并对实现过程进…

    编程 2025-04-29
  • 学习Python对学习C语言有帮助吗?

    Python和C语言是两种非常受欢迎的编程语言,在程序开发中都扮演着非常重要的角色。那么,学习Python对学习C语言有帮助吗?答案是肯定的。在本文中,我们将从多个角度探讨Pyth…

    编程 2025-04-29
  • Python被称为胶水语言

    Python作为一种跨平台的解释性高级语言,最大的特点是被称为”胶水语言”。 一、简单易学 Python的语法简单易学,更加人性化,这使得它成为了初学者的入…

    编程 2025-04-29
  • OpenJudge答案1.6的C语言实现

    本文将从多个方面详细阐述OpenJudge答案1.6在C语言中的实现方法,帮助初学者更好地学习和理解。 一、需求概述 OpenJudge答案1.6的要求是,输入两个整数a和b,输出…

    编程 2025-04-29
  • Python按位运算符和C语言

    本文将从多个方面详细阐述Python按位运算符和C语言的相关内容,并给出相应的代码示例。 一、概述 Python是一种动态的、面向对象的编程语言,其按位运算符是用于按位操作的运算符…

    编程 2025-04-29
  • Python语言由荷兰人为中心的全能编程开发工程师

    Python语言是一种高级语言,很多编程开发工程师都喜欢使用Python语言进行开发。Python语言的创始人是荷兰人Guido van Rossum,他在1989年圣诞节期间开始…

    编程 2025-04-28
  • Python语言设计基础第2版PDF

    Python语言设计基础第2版PDF是一本介绍Python编程语言的经典教材。本篇文章将从多个方面对该教材进行详细的阐述和介绍。 一、基础知识 本教材中介绍了Python编程语言的…

    编程 2025-04-28
  • Python语言实现人名最多数统计

    本文将从几个方面详细介绍Python语言实现人名最多数统计的方法和应用。 一、Python实现人名最多数统计的基础 1、首先,我们需要了解Python语言的一些基础知识,如列表、字…

    编程 2025-04-28
  • Python作为中心语言,在编程中取代C语言的优势和挑战

    Python一直以其简单易懂的语法和高效的编码环境而著名。然而,它最近的发展趋势表明Python的使用范围已经从脚本语言扩展到了从Web应用到机器学习等广泛的开发领域。与此同时,C…

    编程 2025-04-28
  • Python基础语言

    Python作为一种高级编程语言拥有简洁优雅的语法。在本文中,我们将从多个方面探究Python基础语言的特点以及使用技巧。 一、数据类型 Python基础数据类型包括整数、浮点数、…

    编程 2025-04-28

发表回复

登录后才能评论