严蔚敏版《数据结构 (C语言版)》和《数据结构题集》(五)——栈和队列...

本文详细介绍了严蔚敏版《数据结构》中关于栈和队列的内容,包括栈的顺序表实现、链栈的建立、括号匹配检验的算法以及行编辑程序的实现。此外,还探讨了如何用两个栈模拟队列和用两个队列模拟栈的方法,以及顺序循环队列在表达式求值中的应用,但指出这种方法存在局限性。
摘要由CSDN通过智能技术生成

栈的顺序表实现

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define MAXSIZE 1024
 5 using namespace std;
 6 typedef int Elemtype;
 7 typedef struct {
 8 Elemtype data[MAXSIZE];
 9 int top;
10 }Seqstack;
11 void initSeqstack(Seqstack &s){
12 s.top=-1;
13 }
14 int stackEmpty(Seqstack &s){
15 return s.top==-1;
16 }
17 int SeqstackPush(Seqstack &s,Elemtype e){
18 if (s.top>=MAXSIZE-1)
19 return 0;
20 else {
21 s.top++;
22 s.data[s.top]=e;
23 return 1;
24 }
25 }
26 int SeqstackPop(Seqstack &s,Elemtype &e){
27 if (s.top==-1)
28 return 0;
29 else {
30 e=s.data[s.top];
31 s.top--;
32 return 1;
33 }
34 }
35 void getTop(Seqstack &s,Elemtype &e){
36 e=s.data[s.top];
37 }
38 void displaySeqstack(Seqstack &s){
39 for (int i=0;i<=s.top;i++)
40 printf("%d ",s.data[i]);
41 printf ("\n");
42 }
43 int main()
44 {
45     Seqstack s;Elemtype e;
46     initSeqstack(s);
47     for (int i=1;i<6;i++)
48     SeqstackPush(s,i);
49     displaySeqstack(s);
50     getTop(s,e);
51     printf ("%d\n",e);
52     SeqstackPop(s,e);
53     displaySeqstack(s);
54 
55 
56     return 0;
57 }

 

严蔚敏版 栈的实现

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define STACK_INIT_SIZE 100
 5 #define STACKINCREMENT 10
 6 #define OVERFLOW -2
 7 using namespace std;
 8 
 9 typedef int Elemtype;
10 typedef struct {
11 Elemtype *base;
12 Elemtype *top;
13 int stacksize;
14 }Seqstack;
15 int initStack(Seqstack &s){
16 s.base=(Elemtype *)malloc(sizeof(Elemtype)*STACK_INIT_SIZE);
17 if (!s.base ) exit(OVERFLOW);//存储分配失败
18 s.top=s.base;
19 s.stacksize=STACK_INIT_SIZE;
20 return 1;
21 }
22 int stackEmpty(Seqstack &s){
23 return s.top==s.base;
24 }
25 int stackLength(Seqstack &s){
26 return s.top-s.base;
27 }
28 int SeqstackPush(Seqstack &s,Elemtype e){
29 if (s.top-s.base >=s.stacksize){
30 s.base=(Elemtype *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(Elemtype));
31 if (!s.base) exit(OVERFLOW);
32 s.top=s.base+s.stacksize;
33 s.stacksize+=STACKINCREMENT;
34 //上溢后:
35 //1.分配存储空间给base指针 2、判断是否分配成功 3、top/stacksize重新修改正确
36 }
37 *s.top=e;
38 s.top++;//或者可以合成*s.top++=e,先赋值再++
39 return 1;
40 }
41 int SeqstackPop(Seqstack &s,Elemtype &e){
42 if(s.top==s.base){
43 printf ("null");
44 return 0;
45 }
46 s.top--;
47 e=*s.top;
48 return 1;
49 }
50 void getTop(Seqstack &s,Elemtype &e){
51 if (s.base!=s.top){
52 e=*--s.top;
53 }
54 }
55 void displaySeqstack(Seqstack &s){
56 for (int i=0;i<=s.top-s.base;i++)
57 printf("%d ",s.base[i]);
58 printf ("\n");
59 }
60 
61 int main()
62 {
63     Seqstack s;
64     initStack(s);
65     for (int i=1;i<6;i++)
66     SeqstackPush(s,i);
67     displaySeqstack(s);
68     Elemtype e;
69     getTop(s,e);
70     printf ("%d\n",e);
71     SeqstackPop(s,e);
72     displaySeqstack(s);
73 
74 
75     return 0;
76 }

 

建立链栈

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #define OVERFLOW -2
 5 using namespace std;
 6 typedef int Elemtype;
 7 typedef struct node {
 8 Elemtype data;
 9 struct node *next;
10 }node,*linkstack;
11 void initLinkstack(linkstack &top){
12 top=NULL;//无头节点的链栈
13 }
14 int linkstackEmpty(linkstack &top){
15 return top==NULL;
16 }
17 int linkstackPush(linkstack &top,Elemtype e){
18 linkstack p=(linkstack )malloc (sizeof(node));
19 if (!p) exit(OVERFLOW);
20 p->data=e;
21 p->next=top;
22 top=p;
23 return 1;
24 }
25 int linkstackPop(linkstack &top,Elemtype &e){
26 e=top->data;
27 linkstack p=top;
28 top=top->next;
29 free(p);
30 return 1;
31 }
32 void getTop(linkstack &top,Elemtype &E){
33 E=top->data;
34 }
35 void displaylinkstack(linkstack &top){
36 linkstack p=top;
37 while (p){
38 printf ("%d ",p->data);
39 p=p->next;
40 }
41 printf ("\n");
42 }
43 int main()
44 {
45     linkstack top;
46     initLinkstack(top);
47     for (int i=1;i<6;i++)
48     linkstackPush(top,i);
49     displaylinkstack(top);
50     Elemtype e;
51     getTop(top,e);
52     printf ("%d\n",e);
53     linkstackPop(top,e);
54     displaylinkstack(top);
55 
56 
57     return 0;
58 }

 

栈的应用
1.数制转换:对于输入的非负十进制整数,打印输出与之等值的八进制数。

 1 //算法3.1
 2 void
  • 0
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值