数据结构(栈与队列)——栈的顺序表示和实现、队列的链式表示和实现

客官°小女子只卖身不卖艺 2022-12-24 11:50 168阅读 0赞

实验内容:

  1. 编写一个程序实现顺序栈的各种基本运算。
  2. 实现队列的链式表示和实现。
    实验步骤:
    1.初始化顺序栈
  3. 插入元素
  4. 删除栈顶元素
  5. 取栈顶元素
  6. 遍历顺序栈
  7. 置空顺序栈
  8. 初始化并建立链队列
  9. 入链队列
  10. 出链队列
  11. 遍历链队列

1、栈的顺序表示和实现

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #define TRUE 1
  5. #define FALSE 0
  6. #define OK 1
  7. #define ERROR 0
  8. #define INFEASIBLE -1
  9. #define OVERFLOW -2
  10. typedef int StackElementType;
  11. //----- 栈的顺序存储表示 -----
  12. #define Stack_Size 100
  13. #define STACKINCREMENT 10
  14. typedef struct
  15. {
  16. StackElementType elem[Stack_Size]; //用来存放栈中元素的一维数组
  17. int top; /*用来存放栈顶元素的下标,top为-1表示空栈*/
  18. }SeqStack;
  19. /*初始化顺序栈函数*/
  20. void InitStack(SeqStack *S)
  21. {
  22. /*构造一个空栈S*/
  23. S->top = -1;
  24. }
  25. /*判栈空*/
  26. int IsEmpty(SeqStack *S) /*判断栈S为空栈时返回值为真,反之为假*/
  27. {
  28. return(S->top==-1?TRUE:FALSE);
  29. }
  30. /*入栈函数*/
  31. int Push(SeqStack *S,StackElementType x)
  32. {
  33. //请完成本函数的功能
  34. if(S->top==Stack_Size-1) return (FALSE);/*栈已满*/
  35. S->top++;
  36. S->elem[S->top] = x;
  37. return (TRUE);
  38. }
  39. /*出栈函数*/
  40. int Pop(SeqStack *S,StackElementType *x)
  41. {
  42. /* 将栈S的栈顶元素弹出,放到x所指的存储空间中 */
  43. //请完成本函数的功能
  44. if(S->top==-1)
  45. return (FALSE);
  46. else
  47. {
  48. *x = S->elem[S->top];
  49. S->top--;
  50. return (TRUE);
  51. }
  52. }
  53. /*获取栈顶元素函数*/
  54. int GetTop(SeqStack *S,StackElementType *x)
  55. {
  56. /* 将栈S的栈顶元素弹出,放到x所指的存储空间中,但栈顶指针保持不变 */
  57. //请完成本函数的功能
  58. if(S->top == -1)
  59. return (FALSE);
  60. else
  61. {
  62. *x = S->elem[S->top];
  63. return (TRUE);
  64. }
  65. }
  66. int StackDisplay(SeqStack *S){
  67. //显示栈S
  68. int i = 0;
  69. if(IsEmpty(S)){
  70. printf("堆栈已空!\n");
  71. return OK;
  72. }
  73. while( i<=S->top)
  74. printf("[%d:%d]",++i,S->elem[i]);
  75. printf("\n");
  76. return OK;
  77. }//StackDisplay
  78. void main(){
  79. SeqStack St;
  80. int temp;
  81. int flag=1,ch;
  82. int e;
  83. printf("本程序实现顺序结构的堆栈的操作。\n");
  84. printf("可以进行入栈,出栈,取栈顶元素等操作。\n");
  85. InitStack(&St); //初始化堆栈St
  86. while(flag){
  87. printf("请选择:\n");
  88. printf("1.显示栈中所有元素 \n");
  89. printf("2.入栈 \n");
  90. printf("3.出栈 \n");
  91. printf("4.取栈顶元素 \n");
  92. printf("5.退出程序 \n");
  93. scanf("%d",&ch);
  94. switch(ch){
  95. case 1:
  96. StackDisplay(&St);
  97. break;
  98. case 2:
  99. printf("请输入要入栈的元素(一个整数):");
  100. scanf("%d",&e); //输入要入栈的元素
  101. temp=Push(&St,e); //入栈
  102. if(temp!=TRUE) printf("堆栈已满!入栈失败!\n");
  103. else {
  104. printf("成功入栈!\n"); //成功入栈
  105. StackDisplay(&St);
  106. }
  107. break;
  108. case 3:
  109. temp=Pop(&St,&e); //出栈
  110. if(temp==FALSE) printf("堆栈已空!\n");
  111. else {
  112. printf("成功出栈一个元素:%d\n",e); //成功出栈
  113. StackDisplay(&St);
  114. }
  115. break;
  116. case 4:
  117. temp=GetTop(&St,&e); //取得栈顶元素
  118. if(temp==FALSE) printf("堆栈已空!\n");
  119. else printf("栈顶元素是:%d\n",e); //显示栈顶元素
  120. break;
  121. default:
  122. flag=0;
  123. printf("程序结束,按任意键退出!\n");
  124. getchar();
  125. }
  126. }
  127. }

2、队列的链式表示和实现

  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define TRUE 1
  5. #define FALSE 0
  6. #define OK 1
  7. #define ERROR 0
  8. typedef int QueueElementType;
  9. typedef struct Node
  10. {
  11. QueueElementType data; /*数据域*/
  12. struct Node *next; /*指针域*/
  13. }LinkQueueNode;
  14. typedef struct
  15. {
  16. LinkQueueNode *front;
  17. LinkQueueNode *rear;
  18. }LinkQueue;
  19. /*初始化操作。*/
  20. int InitQueue(LinkQueue *Q)
  21. {
  22. /* 将Q初始化为一个空的链队列 */
  23. Q->front=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
  24. if(Q->front!=NULL)
  25. {
  26. Q->rear=Q->front;
  27. Q->front->next=NULL;
  28. return(TRUE);
  29. }
  30. else return(FALSE); /* 溢出!*/
  31. }
  32. /*入队操作。*/
  33. int EnterQueue(LinkQueue *Q,QueueElementType x)
  34. {
  35. /* 将数据元素x插入到队列Q中 */
  36. //请完成本函数的功能
  37. LinkQueueNode *NewNode;
  38. NewNode = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
  39. if(NewNode != NULL)
  40. {
  41. NewNode->data = x;
  42. NewNode->next = NULL;
  43. Q->rear->next = NewNode;
  44. Q->rear = NewNode;
  45. return(TRUE);
  46. }
  47. else return(FALSE);
  48. }
  49. /*出队操作。*/
  50. int DeleteQueue(LinkQueue *Q,QueueElementType *x)
  51. {
  52. /* 将队列Q的队头元素出队,并存放到x所指的存储空间中 */
  53. //请完成本函数的功能
  54. LinkQueueNode *p;
  55. if(Q->front == Q->rear)
  56. return(FALSE);
  57. p = Q->front->next;
  58. Q->front->next = p->next;
  59. if(Q->rear == p)
  60. Q->rear = Q->front;
  61. *x = p->data;
  62. free(p);
  63. return (TRUE);
  64. }
  65. int GetHead(LinkQueue *Q, QueueElementType *x)
  66. {
  67. /*提取队列的队头元素,用x返回其值*/
  68. //请完成本函数的功能/
  69. if(Q->rear == Q->front)
  70. return(FALSE);
  71. else
  72. {
  73. *x = Q->front->data;
  74. return(TRUE);
  75. }
  76. }
  77. int DestroyLinkQueue(LinkQueue *Q)
  78. {
  79. //销毁一个队列
  80. QueueElementType e;
  81. while(Q->front!=Q->rear)
  82. DeleteQueue(Q,&e);
  83. free(Q->front);
  84. Q->front=Q->rear=NULL;
  85. return OK;
  86. }
  87. int LinkQueueLength(LinkQueue *Q)
  88. {
  89. //队列的长度
  90. int i=0;
  91. LinkQueueNode * p=Q->front;
  92. while(p!=Q->rear){
  93. ++i;
  94. p=p->next;
  95. }
  96. return i;
  97. }
  98. int DisplayLinkQueue(LinkQueue *Q)
  99. {
  100. //显示队列中所有元素
  101. LinkQueueNode * p;
  102. int i=0;
  103. p=Q->front->next;
  104. if(p==NULL) printf("队列为空!\n");//队列为空
  105. else{
  106. while(p){
  107. //否则显示队列中所有元素
  108. printf("[%d:%d]",++i,p->data);
  109. p=p->next;
  110. }
  111. printf("\n");
  112. }
  113. return OK;
  114. }
  115. void main(){
  116. LinkQueue LQ;
  117. QueueElementType e;
  118. int flag=1,ch,len;
  119. int temp;
  120. printf("本程序实现链式结构队列的操作。\n");
  121. printf("可以进行入队列、出队列等操作。\n");
  122. InitQueue(&LQ); //初始化队列
  123. while(flag){
  124. printf("请选择:\n");
  125. printf("1.显示队列所有元素\n");
  126. printf("2.入队列\n");
  127. printf("3.出队列\n");
  128. printf("4.求队列的长度\n");
  129. printf("5.退出程序\n");
  130. scanf("%d",&ch);
  131. switch(ch){
  132. case 1:DisplayLinkQueue(&LQ); //显示队列中所有元素
  133. break;
  134. case 2:printf("请输入要人队的元素(一个整数):");
  135. scanf("%d",&e); //输入要入队列的字符
  136. EnterQueue(&LQ,e);//入队列
  137. DisplayLinkQueue(&LQ);
  138. break;
  139. case 3:temp=DeleteQueue(&LQ,&e); //出队列
  140. if(temp==TRUE){
  141. printf("出队一个元素:%d\n",e);
  142. DisplayLinkQueue(&LQ);
  143. }
  144. else printf("队列为空!\n");
  145. break;
  146. case 4:len=LinkQueueLength(&LQ);
  147. printf("队列的长度为:%d\n",len);
  148. break;
  149. default:flag=0;
  150. printf("程序运行结束,按任意键退出!\n");
  151. getchar();
  152. }
  153. }
  154. DestroyLinkQueue(&LQ);
  155. }

发表评论

表情:
评论列表 (有 0 条评论,168人围观)

还没有评论,来说两句吧...

相关阅读