第五周项目1-建立顺序栈算法库

1.	问题描述及代码:  
[cpp] view plain copy
1.	/*    
2.	*烟台大学计控学院     
3.	*作    者:朱建豪    
4.	*完成日期:2016年9月29日 
5.	*问题描述:定义顺序栈存储结构,实现其基本运算,并完成测试。  
6.	  要求:  
7.	  1、头文件sqstack.h中定义数据结构并声明用于完成基本运算的函数。对应基本运算的函数包括: 
8.	 
9.	void InitStack(SqStack *&s);    //初始化栈 
10.	void DestroyStack(SqStack *&s);  //销毁栈 
11.	bool StackEmpty(SqStack *s);     //栈是否为空 
12.	int StackLength(SqStack *s);  //返回栈中元素个数——栈长度 
13.	bool Push(SqStack *&s,ElemType e); //入栈 
14.	bool Pop(SqStack *&s,ElemType &e); //出栈 
15.	bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素 
16.	void DispStack(SqStack *s);  //输出栈1 
17.	2、在sqstack.cpp中实现这些函数  
18.	3、在main函数中完成测试,包括如下内容:  
19.	(1)初始化栈s  
20.	(2)判断s栈是否为空  
21.	(3)依次进栈元素a,b,c,d,e  
22.	(4)判断s栈是否为空  
23.	(5)输出栈长度  
24.	(6)输出从栈顶到栈底元素  
25.	(7)出栈,并输出出栈序列  
26.	(8)判断s栈是否为空  
27.	(9)释放栈  
28.	*/  

(1)sqstack.h
[cpp] view plain copy
1.	#ifndef SQSTACK_H_INCLUDED  
2.	#define SQSTACK_H_INCLUDED  
3.	  
4.	#define MaxSize 100  
5.	typedef char ElemType;  
6.	typedef struct  
7.	{  
8.	    ElemType data[MaxSize];  
9.	    int top;                //栈指针  
10.	} SqStack;                  //顺序栈类型定义  
11.	  
12.	void InitStack(SqStack *&s);    //初始化栈  
13.	void DestroyStack(SqStack *&s);  //销毁栈  
14.	bool StackEmpty(SqStack *s);     //栈是否为空  
15.	int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
16.	bool Push(SqStack *&s,ElemType e); //入栈  
17.	bool Pop(SqStack *&s,ElemType &e); //出栈  
18.	bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
19.	void DispStack(SqStack *s);  //输出栈  
20.	  
21.	  
22.	  
23.	#endif // SQSTACK_H_INCLUDED  

(2)sqstack.cpp
[cpp] view plain copy
1.	#include"sqstack.h"  
2.	#include<stdio.h>  
3.	#include<malloc.h>  
4.	void InitStack(SqStack *&s)  
5.	{  
6.	    s=(SqStack *)malloc(sizeof(SqStack));  
7.	    s->top=-1;  
8.	}  
9.	void DestroyStack(SqStack *&s)  
10.	{  
11.	    free(s);  
12.	}  
13.	int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
14.	{  
15.	    return(s->top+1);  
16.	}  
17.	bool StackEmpty(SqStack *s)  
18.	{  
19.	    return(s->top==-1);  
20.	}  
21.	bool Push(SqStack *&s,ElemType e)  
22.	{  
23.	    if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
24.	        return false;  
25.	    s->top++;  
26.	    s->data[s->top]=e;  
27.	    return true;  
28.	}  
29.	bool Pop(SqStack *&s,ElemType &e)  
30.	{  
31.	    if (s->top==-1)     //栈为空的情况,即栈下溢出  
32.	        return false;  
33.	    e=s->data[s->top];  
34.	    s->top--;  
35.	    return true;  
36.	}  
37.	bool GetTop(SqStack *s,ElemType &e)  
38.	{  
39.	    if (s->top==-1)         //栈为空的情况,即栈下溢出  
40.	        return false;  
41.	    e=s->data[s->top];  
42.	    return true;  
43.	}  
44.	  
45.	void DispStack(SqStack *s)  //输出栈  
46.	{  
47.	    int i;  
48.	    for (i=s->top;i>=0;i--)  
49.	        printf("%c ",s->data[i]);  
50.	    printf("\n");  
51.	}  

(3)main.cpp
[cpp] view plain copy
1.	#include <stdio.h>  
2.	#include "sqstack.h"  
3.	  
4.	int main()  
5.	{  
6.	    ElemType e;  
7.	    SqStack *s;  
8.	    printf("(1)初始化栈s\n");  
9.	    InitStack(s);  
10.	    printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
11.	    printf("(3)依次进栈元素a,b,c,d,e\n");  
12.	    Push(s,'a');  
13.	    Push(s,'b');  
14.	    Push(s,'c');  
15.	    Push(s,'d');  
16.	    Push(s,'e');  
17.	    printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
18.	    printf("(5)栈长度:%d\n",StackLength(s));  
19.	    printf("(6)从栈顶到栈底元素:");DispStack(s);  
20.	    printf("(7)出栈序列:");  
21.	    while (!StackEmpty(s))  
22.	    {  
23.	        Pop(s,e);  
24.	        printf("%c ",e);  
25.	    }  
26.	    printf("\n");  
27.	    printf("(8)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
28.	    printf("(9)释放栈\n");  
29.	    DestroyStack(s);  
30.	    return 0;  
31.	  
32.	}  
运行结果:
<img src="https://img-blog.csdn.net/20160929105704420?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
知识点及总结:
熟悉栈的基本运算
学习心得:
终于会用coodblocks编程序了 在处理编译器的问题的同时也一遍一遍加深了对算法的认识

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值