[C] //假设有二个栈共同使用一块顺序存储的空间,为简单起见可设为共同使用数组 int →→→→→进入此内容的聊天室

来自 , 2020-12-11, 写在 C, 查看 153 次.
URL http://www.code666.cn/view/e21e4e58
  1. //假设有二个栈共同使用一块顺序存储的空间,为简单起见可设为共同使用数组 int
  2. //buffer[200]。它们的栈底分别设在数组的两端,而栈顶指针在进行插入操作时,向
  3. //中间方向移动。请给出进出栈的程序.
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include"test3.h"
  8. #include<string.h>
  9. #define Maxsize 200
  10. int main()
  11. {
  12.         int i,flat=1,len1,len2,n;
  13.         char str[Maxsize],e;
  14.         SqStack *A,*B;
  15.         A=CreateSqStack();//创建栈A
  16.         B=CreateSqStack();//创建栈B
  17.  
  18.         InitSqStackA(A);//初始化栈A
  19.         InitSqStackB(B);//初始化栈B
  20.  
  21.         printf("对栈A输入一组字符:\n");
  22.         gets(str);
  23.         len1=strlen(str);//len1 用于存放栈A的数据元素的个数
  24.         for(i=0;i<len1;i++)
  25.         {
  26.         flat=Judge(A,B);//判断存储空间是否已经满了,若满,则返回ERROR,否则,返回OK
  27.                 if(!flat)
  28.                 {
  29.                         printf("存储空间已满!\n");
  30.                         return 0;
  31.                 }
  32.                 PushSqStackA(A,str[i]);//入栈A
  33.         }
  34.  
  35.         printf("对栈B输入一组字符:\n");
  36.         gets(str);
  37.         len2=strlen(str);//len2 用于存放栈B的数据元素的个数
  38.         for(i=0;i<len2;i++)
  39.         {
  40.                 flat=Judge(A,B);//判断存储空间是否已经满了,若满,则返回ERROR,否则,返回OK
  41.                 if(!flat)
  42.                 {
  43.                         printf("存储空间已满!\n");
  44.                         return 0;
  45.                 }
  46.                 PushSqStackB(B,str[i]);//入栈B
  47.         }
  48.  
  49.     printf("对栈A输出几个数:\n");
  50.         scanf("%d",&n);
  51.         for(i=0;i<n;i++)
  52.         {
  53.                 PopSqStackA(A,&e);//出栈A
  54.                 printf("%c",e);
  55.         }
  56.         printf("\n");
  57.  
  58.         printf("对栈B输出几个数:\n");
  59.         scanf("%d",&n);
  60.         for(i=0;i<n;i++)
  61.         {
  62.                 PopSqStackB(B,&e);//出栈B
  63.                 printf("%c",e);
  64.         }
  65.         printf("\n");
  66.         return 0;
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. ///////////////////////////////////////
  76. /*           test.cp                */
  77. /*     filename:test.cpp            */
  78. /*     description:test.h的实现文件 */
  79. /*     designer:zhu jian            */
  80. /*     data:12-10-26                */
  81. ///////////////////////////////////////
  82.  
  83.  
  84. #include"stdio.h"
  85. #include"stdlib.h"
  86. #include"test3.h"
  87. #define Maxsize 200
  88.  
  89.  
  90. SqStack *CreateSqStack(void)//定义一个栈的指针
  91. {
  92.         SqStack *S;
  93.         S=(SqStack *)malloc(sizeof(SqStack));
  94.         if(!S)
  95.                 return NULL;
  96.  
  97.         S->top=0;
  98.         return S;
  99. }
  100.  
  101. unsigned char InitSqStackA(SqStack *S)//初始化栈A
  102. {
  103.         SqStack *p;
  104.         p=S;
  105.         if(!S)
  106.                 return ERROR;
  107.  
  108.         S->top=0;
  109.         return OK;
  110. }
  111.  
  112. unsigned char InitSqStackB(SqStack *S)//初始化栈B
  113. {
  114.         SqStack *p;
  115.         p=S;
  116.         if(!S)
  117.                 return NULL;
  118.  
  119.         p->top=200;
  120.         return OK;
  121. }
  122.  
  123. unsigned char PushSqStackA(SqStack *S,elemtype e)//将数据元素插入栈A中(进栈)
  124. {
  125.         SqStack *p;
  126.         p=S;
  127.         if(!S)
  128.                 return ERROR;
  129.  
  130.         p->data[p->top]=e;
  131.         p->top++;
  132.         return OK;
  133. }
  134.  
  135. unsigned char PushSqStackB(SqStack *S,elemtype e)//将数据元素插入栈B中(进栈)
  136. {
  137.         SqStack *p;
  138.         p=S;
  139.         if(!S)
  140.                 return ERROR;
  141.  
  142.         p->data[p->top-1]=e;
  143.         p->top--;
  144.         return OK;
  145. }
  146.  
  147. unsigned char PopSqStackA(SqStack *S,elemtype *e)//出栈A
  148. {
  149.         SqStack *p;
  150.         p=S;
  151.         if(!S)
  152.                 return ERROR;
  153.  
  154.         if(p->top == 0)
  155.         {
  156.                 printf("栈A已空!\n");
  157.                 return ERROR;
  158.         }
  159.         *e=p->data[p->top-1];
  160.         p->top--;
  161.         return OK;
  162. }
  163.  
  164. unsigned char PopSqStackB(SqStack *S,elemtype *e)//出栈B
  165. {
  166.         SqStack *p;
  167.         p=S;
  168.         if(!S)
  169.                 return ERROR;
  170.  
  171.     if(p->top == Maxsize)
  172.         {
  173.                 printf("栈B已空!\n");
  174.                 return ERROR;
  175.         }
  176.         *e=p->data[p->top];
  177.         p->top++;
  178.         return OK;
  179. }
  180.  
  181. unsigned char Judge(SqStack *A,SqStack *B)//判断栈A与栈B是否已将空间占满
  182. {
  183.         SqStack *p,*q;
  184.         p=A;
  185.         q=B;
  186.         if(!A || !B)
  187.                 return ERROR;
  188.  
  189.         if(p->top < q->top )
  190.                 return OK;
  191.         else
  192.                 return ERROR;
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200. ///////////////////////////////////////
  201. /*           test.h                 */
  202. /*     filename:test.h              */
  203. /*     description:test.h的头文件   */
  204. /*     designer:zhu jian            */
  205. /*     data:12-10-26                */
  206. ///////////////////////////////////////
  207.  
  208.  
  209. #ifndef _DATA_STRUCTURE_TEST_H_
  210. #define _DATA_STRUCTURE_TEST_H_
  211.  
  212.  
  213. #ifndef ERROR
  214. #define ERROR 0
  215. #endif
  216.  
  217. #ifndef OK
  218. #define OK 1
  219. #endif
  220.  
  221. #ifndef NULL
  222. #define NULL 0
  223. #endif
  224.  
  225.  
  226. #define Maxsize 200//定义最大数据元素量是200
  227. typedef char elemtype;//定义用elemtype取代char
  228.  
  229.  
  230. typedef struct{//定义栈的结构体
  231.         elemtype data[Maxsize];
  232.         int top;
  233. }SqStack;
  234.  
  235.  
  236. SqStack *CreateSqStack(void);//定义一个栈的指针
  237.  
  238. unsigned char InitSqStackA(SqStack *S);//初始化栈A
  239.  
  240. unsigned char InitSqStackB(SqStack *S);//初始化栈B
  241.  
  242. unsigned char PushSqStackA(SqStack *S,elemtype e);//将数据元素插入栈A中(进栈)
  243.  
  244. unsigned char PushSqStackB(SqStack *S,elemtype e);//将数据元素插入栈B中(进栈)
  245.  
  246. unsigned char PopSqStackA(SqStack *S,elemtype *e);//出栈A
  247.  
  248. unsigned char PopSqStackB(SqStack *S,elemtype *e);//出栈B
  249.  
  250. unsigned char Judge(SqStack *A,SqStack *B);//判断栈A与栈B是否已将空间占满
  251.  
  252.  
  253. #endif
  254.  
  255.  
  256.  
  257.  

回复 "//假设有二个栈共同使用一块顺序存储的空间,为简单起见可设为共同使用数组 int"

这儿你可以回复上面这条便签

captcha