整个结构:
在这里我就直接贴代码了 写的很挫 欢迎各位大v提出宝贵的意见
function.h
#ifndef FUNCTION_H_INCLUDED
#define FUNCTION_H_INCLUDED
#define OK 1
#define ERROR 0
//队列数据类型的定义
typedef int QElemType;
typedef int Status;
//链队结点定义
typedef struct QNode
{
QElemType data;
struct QNode *next;
} QNode, *QueueNode;
//链队定义
typedef struct LQueue
{
QueueNode front;
QueueNode rear;
} LQueue, *LinkQueue;
//构造一个空队列
Status Init_Queue(LinkQueue &link_queue);
//销毁一个队列
Status DestroyQueue(LinkQueue &link_queue);
//插入元素,往队尾插
Status EnQueue(LinkQueue &link_queue, QElemType ElemType);
//若队列不空,则删除Q的队头元素,并用e返回其值,并返回OK,否则返回ERROR
Status DeQueue(LinkQueue &link_queue, QElemType &ElemType);
//取队头元素
Status GetHeadQueue(LinkQueue &link_queue, QElemType &ElemType);
//初始化杨辉三角
Status Init_YangHui(LinkQueue &link_queue);
//打印杨辉三角
Status Print_YangHui(LinkQueue link_queue,int n);
#endif // FUNCTION_H_INCLUDED
function.cpp
#include <stdio.h>
#include <stdlib.h>
#include "function.h"
//构造一个空队列
Status Init_Queue(LinkQueue &link_queue)
{
//初始化
link_queue = (LinkQueue)malloc(sizeof(LQueue));
link_queue->front = link_queue->rear = (QueueNode)malloc(sizeof(QNode));
if(!link_queue->front)
{
printf("构造空队列失败,系统内存不足!\n");
return ERROR;
}
link_queue->front->next = NULL;
link_queue->rear->next = NULL;
return OK;
}
//销毁一个队列
Status DestroyQueue(LinkQueue &link_queue)
{
while(link_queue->front)
{
link_queue->rear = link_queue->front->next;
free(link_queue->front);
link_queue->front=link_queue->rear;
}
//删除最后一个结点
free(link_queue);
printf("成功销毁该队列!\n");
return OK;
}
//插入元素,往队尾插
Status EnQueue(LinkQueue &link_queue, QElemType ElemType)
{
QueueNode p_Node = (QueueNode)malloc(sizeof(QNode));
if(!p_Node)
{
printf("插入元素失败,系统内存不足!\n");
return ERROR;
}
//元素赋值
p_Node->data = ElemType;
p_Node->next = NULL;
//往队尾插
link_queue->rear->next = p_Node;
link_queue->rear = p_Node;
return OK;
}
//取队头元素
Status GetHeadQueue(LinkQueue &link_queue, QElemType &ElemType)
{
if(link_queue->front != link_queue->rear)
{
ElemType = link_queue->front->next->data;
return OK;
}
}
//若队列不空,则删除Q的队头元素,并用e返回其值,并返回OK,否则返回ERROR
Status DeQueue(LinkQueue &link_queue, QElemType &ElemType)
{
QueueNode Queue_Temp;
if(link_queue->front == link_queue->rear)
{
printf("删除元素失败,队列已空!\n");
return ERROR;
}
Queue_Temp = link_queue->front->next;
ElemType = Queue_Temp->data;
//指向下一个元素
link_queue->front->next = Queue_Temp->next;
//结束的标识,但头尾相接就是结束标识
if(link_queue->rear == Queue_Temp)
{
link_queue->rear = link_queue->front;
}
free(Queue_Temp);
return OK;
}
//初始化杨辉三角
Status Init_YangHui(LinkQueue &link_queue)
{
EnQueue(link_queue,0);
EnQueue(link_queue,1);
EnQueue(link_queue,0);
return OK;
}
//打印杨辉三角
Status Print_YangHui(LinkQueue link_queue, int n)
{
QElemType ElemType;
//单独打印第一行 排版
for(int i=1; i<=n-1; i++)
{
printf("\t");
}
//第二个元素的数据域
printf("%d\n",link_queue->front->next->next->data);
//从第二行开始打印
for(int i=2; i<=n; i++)
{
//对显示的数据进行排版
for(int j=1; j<=n-i; j++)
{
printf("\t");
}
do
{
DeQueue(link_queue, ElemType);
ElemType = ElemType + link_queue->front->next->data;
EnQueue(link_queue, ElemType);
printf("%d\t\t",ElemType);
}while(link_queue->front->next->data != 0);//当第二次取队头元素为0时,结束循环
printf("\n"); //每次打印一行过后,换行
EnQueue(link_queue, 0); //每次循环后,在队尾增加一个0,以保证下次循环的进行
}
return OK;
}
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "function.h"
void initView_Function()
{
printf("\n\n\n\n");
system("color 84");
printf("\t================================================\n");
printf("\t| |\n");
printf("\t| 《队列的基本操作以及杨辉三角形》 |\n");
printf("\t| |\n");
printf("\t================================================\n");
printf("\t| |\n");
printf("\t| 1.初始化QUEUE 2.入队QUEUE |\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t| 3.取队头元素 4.杨辉三角形 |\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t| 5退出 |\n");
printf("\t| |\n");
printf("\t================================================\n");
}
Status selectFunction()
{
//选择功能
int selectCount;
printf("请选择功能......\n");
scanf("%d", &selectCount);
return selectCount;
}
int main()
{
//选择功能
int sel;
LinkQueue link_queue;;
//退出while循环的参数
int flag = 1;
while(flag)
{
initView_Function();
sel = selectFunction();
switch(sel)
{
case 1:
if(Init_Queue(link_queue))
{
printf("\t初始化成功:\n");
}
else
{
printf("\t初始化失败:\n");
}
system("pause");
system("cls");
break;
case 2:
int make;
printf("请输入元素:\n");
scanf("%d", &make);
while(make != 0)
{
EnQueue(link_queue, make);
scanf("%d", &make);
}
system("pause");
system("cls");
break;
case 3:
QElemType ElemType;
GetHeadQueue(link_queue, ElemType);
printf("%d\n", ElemType);
system("pause");
system("cls");
break;
case 4:
int level;
LinkQueue link_queue_1;
link_queue_1 = (LinkQueue)malloc(sizeof(LQueue));
Init_Queue(link_queue_1);
Init_YangHui(link_queue_1);
printf("请输入杨辉三角的层数n:\n");
scanf("%d",&level);
Print_YangHui(link_queue_1, level);
DestroyQueue(link_queue_1);
system("pause");
system("cls");
break;
case 5:
flag = 0;
system("pause");
system("cls");
break;
}
}
}
最后在这里贴上一图 花了一个早上 哈哈
最后在贴出执行的结果