请C语言高手帮我编写几个数据结构的小程序~(一定要用C++编写噢~)谢啦~
#include"stdioh"
#include"malloch"
#define MaxSize 10
typedef char ElemType ;
typedef struct
{
ElemType data[MaxSize]; int front,rear;
}SqQueue;
void InitQueue(SqQueue &q) //初始化队列
{
q=(SqQueue )malloc(sizeof(SqQueue));
q->front=q->rear=0;
}
void ClearQueue(SqQueue &q) //销毁队列
{ free(q); }
int QueueEmpty(SqQueue q) //判断队列是否为空
{ return(q->front==q->rear); }
int enQueue(SqQueue &q,ElemType e) //入队列
{
if((q->rear+1)%MaxSize==q->front) return 0;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e; return 1;
}
int deQueue(SqQueue &q,ElemType &e) //出队列
{
if(q->front==q->rear) return 0;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front]; return 1;
}
void numQueue(SqQueue q) //输出队列元素个数
{
if(q->rear>=q->front)
printf("这个队列的元素个数为:%d\n",q->rear-q->front);
else
printf("这个队列的元素个数为:%d\n",MaxSize-q->front+q->rear);
}
void DispQueue(SqQueue q) //输出队列
{
int i=0,f=q->front+1;
while((f+MaxSize)%MaxSize!=q->rear)
{
printf("%c\t",q->data[f]);
f++; i++;
if(i%5==0)printf("\n");
}
printf("%c\n",q->data[f]);
}
void main()
{
SqQueue q;
ElemType e;
InitQueue(q);
if(QueueEmpty(q))printf("这个队列是空的!\n");
else printf("这个队列不是空的!\n");
enQueue(q,'a'); enQueue(q,'b'); enQueue(q,'c');
if(deQueue(q,e)==1)
printf("出对元素为:%c\n此时",e);
numQueue(q);
enQueue(q,'d'); enQueue(q,'e'); enQueue(q,'f');
printf("def进队列后,");
numQueue(q);
printf("它的元素有:\n");
DispQueue(q);
ClearQueue(q);
}
这是我以前的作业,你自己组织下,应该可以解决你的问题
#include<stdioh>
#include<stdlibh>
typedef struct node NODE, PNODE;
struct node {
int data;
PNODE next;
};
PNODE newnode()
{
PNODE p = (PNODE)malloc(sizeof(NODE));
p->data = 0;
p->next = NULL;
return p;
}
void append(PNODE &p, int &val)
{
while(p->next)p = p->next;
p->data = val;
p->next = newnode();
p = p->next;
}
void display(PNODE a, PNODE b)
{
while(a->next && b->next){
printf("%d %d\n", a->data, b->data);
a = a->next;
b = b->next;
}
}
void freed(PNODE p)
{
PNODE t;
while(p) {
t = p->next;
free(p);
p = t;
}
}
void main()
{
int n, m;
PNODE q1, q2, qq1, qq2;
qq1 = q1 = newnode();
qq2 = q2 = newnode();
n = m = 0;
scanf("%d", &n);
while(n--) {
scanf("%d", &m);
if(m % 2) {
append(qq1, m);
} else {
append(qq2, m);
}
}
display(q1, q2);
freed(q1);
freed(q2);
}
楼主跟我的C语言程序设计课程要求设计的程序很像,有一点点不同,不过我的程序应该能满足你的要求,代码如下:
#include <stdioh>
#include <conioh>
#include <stringh>
#define N 10
struct student{
char name[20];
int cla;
int point1;
int point2;
int point3;
int sum;
int num;
};
void printf_student(struct student stu[]);
void in_student(struct student stu[]);
void look_student(struct student stu[]);
void main(){
student stu[N];
int choice1;
while(choice1!=5){
printf("请选择您要操作的项目:\n");
printf("1读取数据\n2录入数据\n3分析数据\n4保存数据\n5退出\n");
scanf("%d",&choice1);
switch(choice1){
case 1:
printf_student(stu);
break;
case 2:
in_student(stu);
break;
case 3:
look_student(stu);
break;
}}
}
void printf_student(struct student stu[]){
FILE fout;
int i=0;
int j=0;
fout=fopen("stu1txt","r");
if(fout==NULL){printf("找不到文件");}
else{
while(!feof(fout)){
fscanf(fout,"%s %d %d %d %d",stu[i]name,&stu[i]cla,&stu[i]point1,&stu[i]point2,&stu[i]point3);
i++;
}
printf("已经读取");
printf("\n姓名\t\t班级\t\t成绩1\t\t成绩2\t\t成绩3\n");
for(j=0;j<i;j++){
printf("%s\t\t",stu[j]name);
printf("%d\t\t",stu[j]cla);
printf("%d\t\t",stu[j]point1);
printf("%d\t\t",stu[j]point2);
printf("%d\t\t",stu[j]point3);}
}
fclose(fout);
}
void in_student(struct student stu[]){
FILE fp;
student flag;
char ch[50]={0};
fp=fopen("stu1txt","a+");
int choice=1;
if(fp==0){printf("无法打开文件\n");}
else{
while(choice){
printf("请依次输入姓名,班级,成绩1,成绩2,成绩3\n");
scanf("%s %d %d %d %d",flagname,&flagcla,&flagpoint1,&flagpoint2,&flagpoint3);
sprintf(ch,"%s\t%d\t%d\t%d\t%d",flagname,flagcla,flagpoint1,flagpoint2,flagpoint3);
/fwrite(flagname,20,1,fp);
fwrite("\t",2,sizeof("\t"),fp);
fwrite(&flagcla,sizeof(flagcla),1,fp);
fwrite("\t",sizeof("\t"),1,fp);
fwrite(&flagpoint1,sizeof(flagpoint1),1,fp);
fwrite("\t",2,sizeof("\t"),fp);
fwrite(&flagpoint2,sizeof(flagpoint2),1,fp);
fwrite("\t",2,sizeof("\t"),fp);
fwrite(&flagpoint3,sizeof(flagpoint3),1,fp);/
fwrite(ch,strlen(ch),1,fp);
printf("%s",ch);
printf("若想继续录入,请输入1,若退出,请输入0\n");
scanf("%d",&choice);
}}
fclose(fp);
}
void look_student(struct student stu[]){
FILE fp;
int i=0;
int sum=0;
student flag;
fp=fopen("stu1txt","r");
if(fp==0){printf("无法打开文件\n");}
else{
while(!feof(fp)){
fscanf(fp,"%s %d %d %d %d",stu[i]name,&stu[i]cla,&stu[i]point1,&stu[i]point2,&stu[i]point3);
i++;
}}
for(int j=0;j<i;j++){
sum=stu[j]point1+stu[j]point2+stu[j]point3;
stu[j]sum=sum;
sum=0;
}
int t=0;
for(t=1;t<i;t++){
for(j=0;j<i-t;j++){
if(stu[j]sum<stu[j+1]sum){
strcpy(flagname,stu[j+1]name);
flagcla=stu[j+1]cla;
flagpoint1=stu[j+1]point1;
flagpoint2=stu[j+1]point2;
flagpoint3=stu[j+1]point3;
flagsum=stu[j+1]sum;
strcpy(stu[j+1]name,stu[j]name);
stu[j+1]cla=stu[j]cla;
stu[j+1]point1=stu[j]point1;
stu[j+1]point2=stu[j]point2;
stu[j+1]point3=stu[j]point3;
stu[j+1]sum=stu[j]sum;
strcpy(stu[j]name,flagname);
stu[j]cla=flagcla;
stu[j]point1=flagpoint1;
stu[j]point2=flagpoint2;
stu[j]point3=flagpoint3;
stu[j]sum=flagsum;
}
}
}
for(j=0;j<i;j++){
stu[j]num=j+1;
}
fclose(fp);
FILE creat;
char ch[30]={0};
creat=fopen("stu2txt","w");
for(j=0;j<i;j++){
sprintf(ch,"%s\t%d\t%d\t%d\t%d\t%d\t%d",stu[j]name,stu[j]cla,stu[j]point1,stu[j]point2,stu[j]point3,stu[j]sum,stu[j]num);
fwrite(ch,30,1,creat);
}
printf("姓名\t班级\t成绩1\t成绩2\t成绩3\t总分\t排名\n");
for(j=0;j<i;j++){
printf("%s\t",stu[j]name);
printf("%d\t",stu[j]cla);
printf("%d\t",stu[j]point1);
printf("%d\t",stu[j]point2);
printf("%d\t",stu[j]point3);
printf("%d\t",stu[j]sum);
printf("%d\n",stu[j]num);
}
fclose(creat);
}
注意一下,我创建的文件时stu1txt
您好!很高兴为您解答问题,以下是我为您提供的解答内容:
1、如果算法描述已经很彻底了,只要补充变量定义,等语言细节就可以,把算法描述转化为各种编程语言了。如果只是泛泛而论,自己去把算法转换成伪代码描述,或者流程图之类的,然后再用C语言实现。
2、算法只是一种处理数据的一种思想(常用伪代码表示),理解这种思想之后,再用计算机语言表达出来,如果C语法不过关,是很难完成程序的编写,所以C语言本身也要认真学习才行。
《数据结构课程精讲教案合集-复旦大学(共计1061页)pdf 》百度网盘免费资源下载
abzc
#include<iostreamh>
#include<stdioh>
#include<stdlibh>
#define MAX 100
//定义二叉树链表
struct Bitree
{
char c;
struct Bitree lchild,rchild;
};
//定义队列
struct Quene
{
struct Bitree p;//指向数的节点的指针
struct Quene next;
};
//定义哈夫曼树的结构
struct Huffman
{
int weight;//权值
int tag;
int parent,lchild,rchild;
char ch;
};
struct Bitree creatBt()
{
struct Bitree btree;
char ch;
cin>>ch;
// "访问"操作为生成根结点
if(ch!='#')
{
btree=(struct Bitree )malloc(sizeof(struct Bitree )) ;
btree->c=ch;
btree->lchild=creatBt(); // 递归建(遍历)左子树
btree->rchild=creatBt(); // 递归建(遍历)右子树
}
if(ch=='#')
{
btree=NULL;
}
return btree;
}
//先序遍历递归
void preOrder1(struct Bitree btree)
{
struct Bitree p;
p=btree;
if(p!=NULL)
{
cout<<p->c<<" ";
preOrder1(p->lchild);
preOrder1(p->rchild);
}
}
//中序遍历递归
void inOrder1(struct Bitree btree)
{
struct Bitree p;
p=btree;
if(p->lchild!=NULL)
inOrder1(p->lchild);
cout<<p->c<<" ";
if(p->rchild!=NULL)
inOrder1(p->rchild);
}
//后序遍历递归
void postOrder1(struct Bitree btree)
{
struct Bitree p;
p=btree;
if(p->lchild!=NULL)
postOrder1(p->lchild);
if(p->rchild!=NULL)
postOrder1(p->rchild);
cout<<p->c<<" ";
}
//非递归先序遍历
void preOrder2(struct Bitree btree)
{
struct Bitree p, node[MAX];
int top=0;
p=btree;
do
{
while(p!=NULL)
{
cout<<p->c<<" ";
node[top]=p;
top++;
p=p->lchild;
}
if(top>0)
{
top--;
p=node[top];
p=p->rchild;
}
}
while(top>0||p!=NULL);
}
//非递归中序遍历
void inOrder2(struct Bitree btree)
{
struct Bitree p, node[MAX];
int top=0;
p=btree;
do// while(top>0||p!=NULL)
{
while(p!=NULL)
{
node[top]=p;
top++;
p=p->lchild;
}
if(top>0)
{
top--;
p=node[top];
cout<<p->c<<" ";
p=p->rchild;
}
} while(top>0||p!=NULL);
}
//非递归后序遍历
void postOrder2(struct Bitree btree)
{
struct Bitree p, node[MAX];
int top=0;
p=btree;
while(top>0||p!=NULL)
{
while(p!=NULL)
{
node[top]=p;
top++;
p=p->lchild;
}
if(top>0)
{
top--;
p=node[top];
if(p->rchild==NULL)
cout<<p->c<<" ";
p=p->rchild;
}
}
p=btree;
cout<<p->c<<" ";
}
//二叉树的高度
int btHeigh(struct Bitree btree)
{
struct Bitree p;
p=btree;
int hl=0, h2=0, max=0;
if(p!=NULL)
{
hl=btHeigh(p->lchild); //求左子树的深度
h2=btHeigh(p->rchild); //求右子树的深度
max=hl>h2hl: h2;
return(max+1);// 返回树的深度
}
else
return 0;
}
//求二叉树的叶子个数
int numLeave(struct Bitree btree)
{
struct Bitree p;
p=btree;
int static n=0;//静态变量计数
if(p!=NULL)
{
if(p->lchild==NULL&&p->rchild==NULL)
n++; //若root所指结点为叶子, 则累加
numLeave(p->lchild);
numLeave(p->rchild);
return n;
}
else
return 0;
}
//借助队列实现二叉树的层次遍历。
void btQuene(struct Bitree btree)
{
struct Quene head,rear,temp;
head=(struct Quene)malloc(sizeof(struct Quene));//给队列头指针分配器空间
head->p=btree;
head->next=NULL;
rear=head;
while(head!=NULL)
{
if(head->p->lchild!=NULL)
{
temp=(struct Quene )malloc(sizeof(struct Quene));//中间变量暂时保存数的结点
temp->p=head->p->lchild;
temp->next=NULL;
rear->next=temp;//将新结点连接在前一个结点的后面
rear=temp;
}
if(head->p->rchild!=NULL)
{
temp=(struct Quene )malloc(sizeof(struct Quene));
temp->p=head->p->rchild;
temp->next=NULL;
rear->next=temp;
rear=temp;
}
temp=head;
cout<<head->p->c;
head=head->next;
free(temp);
}
}
//建立哈夫曼树
void creatHfm()
{
struct Huffman HF;
int num;
int i,j,x1,x2,m1,m2;
cout<<"输入字符个数;";
cin>>num;
if(num<=1)
{
cout<<"不能建立哈夫曼树!"<<endl;
return ;
}
HF=new struct Huffman[2num-1];
char c;
//构造叶子结点数为num,权值为weight的哈夫曼树HF[]
for(i=0;i<num;i++)
{
cout<<"请输入第"<<i+1<<"个字符值:";
cin>>c;
HF[i]ch=c;
cout<<"请输入该字符的权值:";
cin>>HF[i]weight;
HF[i]parent=-1;
HF[i]lchild=-1;
HF[i]rchild=-1;
HF[i]tag=0;
}
//构造非叶子结点
for(i=0;i<num-1;i++)//合并n-1次
{
m1=m2=32767;//m1是最小值单元,m2为次小值
x1=x2=0;//x1,x2为下标号
for(j=0;j<num+1;j++)//找最小权值和次小权值
{
if(HF[i]weight<m1&&HF[j]tag==0)
{
m2=m1;
x2=x1;
m1=HF[j]weight;
x1=j;
}
else if(HF[j]weight<m2&&HF[j]tag==0)
{
m2=HF[j]weight;
x2=j;
}
}
//将两棵子树合并成一棵新树
HF[x1]parent=num+i;//x1,x2对应的子树分别作为n+i的左右子树
HF[x2]parent=num+i;
HF[x1]tag=1;
HF[x2]tag=1;
HF[num+i]weight=HF[x1]weight+HF[x2]weight;//新书跟的权值为左右树根的权值之和
HF[num+i]lchild=x1;//n+i的做孩子是x1
HF[num+i]rchild=x2;
}
cout<<"哈夫曼树创建成功!"<<endl;
}
//删除值为x的结点,并释放相应的空间
void btFree(struct Bitree btree,char x)
{
struct Bitree p;
p=btree;
if(p!=NULL)
{
if(p->c==x)
{
free(p);
cout<<"释放成功!"<<endl;
}
else
{
btFree(p->lchild,x);
btFree(p->rchild,x);
}
}
else
cout<<"空间释放失败!"<<endl;
}
void main()
{
int key;
int depth;
int numLea;
struct Bitree btree;
cout<<"输入数的节点,'#'表示空节点:"<<endl;
btree=creatBt();
cout<<"二叉树创建成功!"<<endl;
cout<<endl;
cout<<""<<endl;
cout<<" 选择菜单 "<<endl;
cout<<"1、递归先序遍历二叉树 "<<endl;
cout<<"2、递归中序遍历二叉树 "<<endl;
cout<<"3、递归后序遍历二叉树 "<<endl;
cout<<"4、非递归先序遍历二叉树 "<<endl;
cout<<"5、非递归中序遍历二叉树 "<<endl;
cout<<"6、非递归后序遍历二叉树 "<<endl;
cout<<"7、求二叉树的高度 "<<endl;
cout<<"8、求二叉树的叶子结点个数 "<<endl;
cout<<"9、队列实现二叉树层次遍历"<<endl;
cout<<"10、建立哈夫曼树 "<<endl;
cout<<"11、释放空间 "<<endl;
cout<<""<<endl;
for(;key!=0;)
{
cout<<"输入选项,(0表示结束操作):";
cin>>key;
switch(key)
{
case 1:
cout<<"递归先序遍历结果是:";
preOrder1(btree);
cout<<endl;
break;
case 2:
cout<<"递归中序遍历结果是:";
inOrder1(btree);
cout<<endl;
break;
case 3:
cout<<"递归后序遍历结果是:";
postOrder1(btree);
cout<<endl;
break;
case 4:
cout<<"非递归先序遍历结果是:";
preOrder2(btree);
cout<<endl;
break;
case 5:
cout<<"非递归中序遍历结果是:";
inOrder2(btree);
cout<<endl;
break;
case 6:
cout<<"非递归后序遍历结果是:";
postOrder2(btree);
cout<<endl;
break;
case 7:
depth=btHeigh(btree);
cout<<"二叉树的高度为";
cout<<depth<<endl;
break;
case 8:
numLea=numLeave(btree);
cout<<"二叉树的叶子结点数为";
cout<<numLea<<endl;
break;
case 9:
cout<<"层次遍历的结果为:";
btQuene(btree);
cout<<endl;
break;
case 10:
creatHfm();
break;
case 11:
char c;
cout<<"输入要释放的结点:";
cin>>c;
btFree(btree,c);
break;
default:
printf("\n输入选项错误!\n ");
}
}
}
1、Fibonacci数列算法:Fibonacci数列有如下特点:第1,2两个数的值为1,从第3个数开始,该数是其前面两个数之和。
2、即:F1=1(n=1),F2=1(n=2),F3=F(n-1)+F(n-2)(n>=3)。运行看看。
3、数的排列之冒泡法也叫起泡法:排序的方法有两种:一种是“升序”,从小到大,一种是“降序”,从大到小。
4、每次将相邻的两个数比较。将小的调到前头。若有6个数:9,8,5,4,2,0。第一次将最前面的8和9对调。第二次将第二和第三个数对调。。。。。。如此共进行5次,得到8-5-4-2-0-9的顺序。
5、打擂台算法的思路:打擂台算法怎么确定最后的优胜者。先找任一人上台,第二个人上去与之比武,胜者留在台上,再上去第三个人与刚才得胜的人比武,胜者留,败者下。
请C语言高手帮我编写几个数据结构的小程序~(一定要用C++编写噢~)谢啦~
本文2023-09-23 08:14:21发表“资讯”栏目。
本文链接:https://www.lezaizhuan.com/article/87335.html