定义三个头文件 bitree.h seqstack.h seqqueue.h 并把该存储结构放在头文件里
二叉树bitree.h
typedef int KeyType; typedef struct Node { KeyType key; struct Node *LChild; struct Node *RChild; }BiTNode, *BiTree; void InsertBT(BiTree *bt, int key)//声明函数 { BiTree s = NULL;//建立的二叉树初始为空 if(*bt == NULL)//二叉树为空时 { s = (BiTree)malloc(sizeof(BiTNode));//给二叉树分配一个空间 s->key = key; s->LChild = NULL; s->RChild = NULL; *bt = s; } else if(key < (*bt)->key)//如果输入数据小于根节点,插入成为左子树 { InsertBT(&((*bt)->LChild), key); } else if(key > (*bt)->key)//如果输入数据大于根节点,插入成为右子树 { InsertBT(&((*bt)->RChild), key); } } void CreateBiTree(BiTree *bt,KeyType grade)//建立一个二叉树 { InsertBT(bt, grade);//插入函数 } //二叉树中序排序 void InOrder(BiTree root) { if(root != NULL) { InOrder(root->LChild);// printf("\t %d",root->key); InOrder(root->RChild); } // printf("\n"); }栈 seqstack.h
#define TRUE 1 #define FALSE 0 #define Stack_Size 50 #define StackElementType int /*顺序栈*/ typedef struct { StackElementType elem[Stack_Size]; /*用来存放栈中元素的一维数组*/ int top; /*用来存放栈顶元素的下标,top为-1表示空栈*/ }SeqStack; /*初始化*/ void InitStack(SeqStack *S) { /*构造一个空栈S*/ S->top = -1;//空栈返回值为-1 } /*判栈空*/ int IsEmptys(SeqStack *S) /*判断栈S为空栈时返回值为真,反之为假*/ { return(S->top==-1?TRUE:FALSE); } /*判栈满*/ int IsFull(SeqStack *S) /*判断栈S为满栈时返回值为真,反之为假*/ { return(S->top==Stack_Size-1?TRUE:FALSE); } /*进栈*/ int Push(SeqStack *S,StackElementType x) { if(S->top==Stack_Size-1) return(FALSE); /*栈已满*/ S->top++;//将栈顶指针+1 S->elem[S->top] = x;//在栈顶指针的位置插入元素X return(TRUE); } /*出栈*/ int Pop(SeqStack *S,StackElementType *x) { /* 将栈S的栈顶元素弹出,放到x所指的存储空间中 */ if(S->top == -1) /*栈为空*/ return(FALSE); else { *x = S->elem[S->top];//将栈顶元素赋值给X S->top--; //栈尾指针-1 return(TRUE); } } /*取栈顶元素。*/ int GetTop(SeqStack *S,StackElementType *x) { /* 将栈S的栈顶元素弹出,放到x所指的存储空间中,但栈顶指针保持不变 */ if(S->top == -1) /*栈为空*/ return(FALSE); else { *x = S->elem[S->top]; return(TRUE); } }
#define TRUE 1 #define FALSE 0 #define MAXSIZE 50 /*队列的最大长度*/ #define QueueElementType int typedef struct { QueueElementType element[MAXSIZE]; /* 队列的元素空间*/ int front; /*头指针指示器*/ int rear; /*尾指针指示器*/ }SeqQueue; /*初始化操作*/ void InitQueue(SeqQueue *Q) { /* 将*Q初始化为一个空的循环队列 */ Q->front=Q->rear=0; } /*入队操作*/ int EnterQueue(SeqQueue *Q, QueueElementType x) { /*将元素x入队*/ if((Q->rear+1)%MAXSIZE==Q->front) /*队列已经满了*/ return(FALSE); Q->element[Q->rear]=x; Q->rear=(Q->rear+1)%MAXSIZE; /* 重新设置队尾指针 */ return(TRUE); /*操作成功*/ } /*出队操作*/ int DeleteQueue(SeqQueue *Q, QueueElementType *x) { /*删除队列的队头元素,用x返回其值*/ if(Q->front==Q->rear) /*队列为空*/ return(FALSE); *x=Q->element[Q->front]; Q->front=(Q->front+1)%MAXSIZE; /*重新设置队头指针*/ return(TRUE); /*操作成功*/ } int GetHead(SeqQueue *Q, QueueElementType *x) { /*提取队列的队头元素,用x返回其值*/ if(Q->front==Q->rear) /*队列为空*/ return(FALSE); *x=Q->element[Q->front]; return(TRUE); /*操作成功*/ } int IsEmpty(SeqQueue *Q) { /*提取队列的队头元素,用x返回其值*/ if(Q->front==Q->rear) /*队列为空*/ return(TRUE); else return(FALSE); /*操作成功*/ }
#include <iostream> #include<stdio.h> #include<stdlib.h> #include"seqqueue.h" #include"seqstack.h" #include "bitree.h" //#include"struct.h" using namespace std; struct student { int stuNum; int grade; struct student *next; }; //输出学生信息 void print(struct student *head) { struct student *p = head; printf("......学号.....\t......成绩.....\n"); while(p) { printf("\t%d\t\t%d\n",p->stuNum, p->grade); p = p->next; } } //顺序表成绩录入 struct student *createlink(struct student *head,int n) { int i=1; int xuehao,chengji; struct student *s,*p; printf("请输入学生信息:\n"); printf(".....学号.....\t.....成绩.....\n"); scanf("\t%d \t\t%d",&xuehao,&chengji); s = (struct student *)malloc(sizeof(struct student));//分配头节点空间 s->stuNum = xuehao; s->grade = chengji; s->next=head; head=s; p=s; while(i < n) { scanf("%d %d", &xuehao, &chengji); s = (struct student *)malloc(sizeof(struct student));//保存新节点 s->stuNum = xuehao; s->grade = chengji; s->next = NULL; p->next = s; p = s; i++; } return head; } //降序排列学生成绩 void scoresort(struct student *head) { struct student *p, *q; int xuehao; int chengji; p = head; while(p) { q = p->next; while(q) { if(q->grade > p->grade) { xuehao = q->stuNum; q->stuNum = p->stuNum; p->stuNum = xuehao; chengji = q->grade; q->grade = p->grade; p->grade = chengji; } q = q->next; } p = p->next; } } //删除不及格学生 void delmember(struct student *head) { struct student *p; p=head; SeqStack stuNum; SeqStack sgrade; InitStack(&stuNum); InitStack(&sgrade); SeqQueue qstuNum; SeqQueue qgrade; InitQueue(&qstuNum); InitQueue(&qgrade); BiTree bt; bt=NULL; int xuehao,chengji; while(p) { if(p->grade >= 60) { EnterQueue(&qstuNum,p->stuNum); EnterQueue(&qgrade,p->grade); } else { Push(&stuNum,p->stuNum); Push(&sgrade,p->stuNum); } p=p->next; } while(!IsEmpty(&qstuNum)&&!IsEmpty(&qgrade)) { DeleteQueue(&qstuNum,&xuehao); DeleteQueue(&qgrade,&chengji); CreateBiTree(&bt,chengji); printf("\t%d\t\t", xuehao); printf("%d\n", chengji); } printf("\t及格学生成绩二叉树中序遍历结果为:\n"); InOrder(bt); } //查找学生信息 void search(struct student *head) { int stuNum; struct student *p; p = head; scanf("\t%d", &stuNum); while(p) { if(stuNum == p->stuNum) { printf("\t该学生为:\n"); printf(".......学号.....\t.......成绩.....\n"); printf("\t%d\t\t\t%d\n",p->stuNum, p->grade); } p = p->next; } // printf("输入错误,请重新输入!!!"); } //主界面函数 void menu(struct student *head) { /* int i,n; struct student *head = NULL; printf("请输入学生人数:\n"); scanf("%d", &n); head = createlink(head, n); print(head); */ int i; printf("\n.....................请选择功能................................."); printf("\n\n\t[1] 退出系统\n"); printf("\t[2] 成绩降序排列\n"); printf("\t[3] 删除不及格学生,建立二叉排序树,对该二叉排序树进行中序遍历\n"); printf("\t[4] 查询成绩\n\n"); printf("......................................................\n\n"); printf("请从键盘输入数字1-5 选择相应功能:\n"); a1:scanf("%d",&i); //a1为语句标号,配合goto使用 switch(i) { case 1 : break; case 2 : printf("\t降序输出学生信息为:\n"); scoresort(head); print(head); menu(head); case 3 : printf("\t及格学生信息为:\n"); printf("......学号.....\t......成绩.....\n"); delmember(head); menu(head); case 4 : printf("\n\t输入查找学生学号:\n"); search(head); menu(head); default: printf("\n\n 输入错误!再次输入数字1-5 :\n \n"); goto a1; } }
#include <iostream> #include<stdio.h> #include<stdlib.h> #include"seqqueue.h" #include"seqstack.h" #include "bitree.h" //#include"struct.h" using namespace std; int main() { int i,n; struct student *head = NULL; printf("--------------欢迎进入学生成绩查询系统-----------\n"); printf("请输入学生人数:\n"); scanf("%d", &n); head = createlink(head, n); print(head); menu(head); return 0; }