1 #include<stdio.h> 2 #include<stdlib.h> 3 #define MAXSIZE 10 4 5 typedef int ElemType; 6 //队列结构体 7 typedef struct{ 8 ElemType data[MAXSIZE]; 9 int front,rear;//队头与队尾指针 (实际就是两个变量) 10 }SqQueue; 11 //初始化顺序队列 12 int InitQueue(SqQueue *S){ 13 S->front=S->rear=0; 14 //指针赋0,就是不指向任何对象,相当于NULL。 15 } 16 //销毁队列 17 int DestroyQueue(SqQueue *S){ 18 19 } 20 //判断队列为空 21 int IsEmpty(SqQueue *S){ 22 if(S->front==S->rear){ 23 return 1; 24 } 25 return 0; 26 } 27 //判断队列以满 28 int IsFull(SqQueue *S){ 29 if(S->rear==MAXSIZE){ 30 return 1; 31 } 32 return 0; 33 } 34 //进队运算算法 35 int EnQueue(SqQueue *S){ 36 if(IsFull(S)){ 37 return printf("队满\n"); 38 } 39 int n; 40 printf("请输入你想入队的值\n"); 41 scanf("%d",&n); 42 S->data[S->rear]=n; 43 S->rear=S->rear+1; 44 // S->front=S->front+1; 45 return printf("进队成功\n"); 46 } 47 //输出队列元素 48 int DisElem(SqQueue *S){ 49 if(IsEmpty(S)){ 50 return printf("队内是空无法输出\n"); 51 } 52 int i=S->front;//i=0 53 printf("队内元素有:\n"); 54 for(int n=0;i<S->rear;n++){ 55 printf("%3d",S->data[i]); 56 i++; 57 } 58 printf("\n"); 59 return printf("输出成功"); 60 } 61 //获取队首元素 62 int GetHead(SqQueue *S){ 63 if(IsEmpty(S)){ 64 return printf("队内是空\n"); 65 } 66 return printf("队首元素为\n%d",S->data[S->front]); 67 } 68 int GetLength(SqQueue *S){ 69 if(IsEmpty(S)){ 70 return printf("队内为空\n"); 71 } 72 return printf("队的长度为%d",S->rear); 73 74 } 75 //出队 76 int DeleteQueue(SqQueue *S){ 77 if(IsEmpty(S)){ 78 return printf("队内是空无法输出\n"); 79 } 80 int x; 81 // SqQueue *temp=S->front; 82 x=S->data[S->front]; 83 S->front=S->front+1; 84 return printf("出队的元素为%d",x); 85 } 86 int main(){ 87 SqQueue S; 88 int n; 89 InitQueue(&S); 90 while(true){ 91 printf("|||||||||||||||||||||||||||||||||||||||||||||||\n"); 92 printf("|| 1:入队 ||\n"); 93 printf("|| 2:查看队伍元素 ||\n"); 94 printf("|| 3:查看队首元素 ||\n"); 95 printf("|| 4:查看队列长度 ||\n"); 96 printf("|| 2:出队 ||\n"); 97 printf("|| (输入-1退出系统) ||\n"); 98 printf("|||||||||||||||||||||||||||||||||||||||||||||||\n"); 99 100 printf("请输入你的操作序号\n"); 101 scanf("%d",&n); 102 switch(n){ 103 case 1: 104 EnQueue(&S); 105 break; 106 case 2: 107 DisElem(&S); 108 break; 109 case 3: 110 GetHead(&S); 111 break; 112 case 4: 113 GetLength(&S); 114 break; 115 case 5: 116 DeleteQueue(&S); 117 break; 118 case -1: 119 return 0; 120 121 } 122 123 124 125 } 126 }
原文地址:https://www.cnblogs.com/longlonglonglong/p/10999767.html
时间: 2024-10-19 05:49:29