数据结构实践(一)—— 栈
来源:互联网 发布:纪元1404 类似 知乎 编辑:程序博客网 时间:2024/04/30 17:00
数据结构实践(一)—— 栈
使用语言C/C++
关于栈的各种基本操作写了下,包括严蔚敏的《数据结构》中没有详细写出的。
C++用的不是很熟。
Stack.h
/**************************************// Claim of Function in Sequence Stack***************************************/#ifndef __STACK_H__ #define __STACK_H__typedef int SElemType; // Change the definition of SElemType for various data typestypedef class SqStack{ private: SElemType* top; SElemType* base; int stacksize; public: SqStack(); bool InitStack(); bool DestroyStack(); bool ClearStack(); bool IsEmpty(); bool Push(SElemType p); bool Pop(SElemType &e); bool show(); int StackLen(); SElemType* Base(); SElemType* Top(); protected: void Resetbase(SElemType* sbase); void Resettop(SElemType* stop);}SqStack;#endif
Stack.cpp
/********************************************// Definition of Function in Sequence Stack********************************************/#include <stdio.h>#include <stdlib.h>#include "Stack.h"#define STACK_SIZE_INIT 100#define STACK_SIZE_EXTRA 10// Initiate a stackbool SqStack::InitStack(){ if (base) DestroyStack(); base = (SElemType *)malloc(STACK_SIZE_INIT * sizeof(SElemType)); if (!base) return false; top = base; stacksize = STACK_SIZE_INIT; return true;}// Destroy an existed stackbool SqStack::DestroyStack(){ if (!base) return false; free(base); top = NULL; base = NULL; stacksize = 0; return true;}// Clear the content of the stackbool SqStack::ClearStack(){ if (!base) return false; top = base; stacksize = 0; return true;}// Judge if the stack is emptybool SqStack::IsEmpty(){ if (top == base) return true; return false;}// Add an element to the stackbool SqStack::Push(SElemType p){ if (!base) return false; while (top - base >= stacksize) base = (SElemType *)realloc(base, (stacksize += STACK_SIZE_EXTRA) * sizeof(SElemType)); if (!base) return false; *top++ = p; if (*top != p) return false; return true;}// Pop out an elementbool SqStack::Pop(SElemType &e){ if (!base) return false; if (top == base) return false; e = *--top; return true;}// Reset the base point of the stack (Advanced Function) void SqStack::Resetbase(SElemType* sbase){ base = sbase;}// Reset the top point of the stack (Advanced Function)void SqStack::Resettop(SElemType* stop){ top = stop;}// Get the base pointSElemType* SqStack::Base(){ return base;}// Get the top pointSElemType* SqStack::Top(){ return top;}// Get the length of the stackint SqStack::StackLen(){ return stacksize;}// Show all the element of the stackbool SqStack::show(){ if (!base) return false; if (top == base){ printf("Empty!"); return true; } SElemType* p; printf("The Stack contain element include:\n"); for (p = base; p < top; p++){ printf("%d\n",*p); } return true;}// Initiate the parameter using initiating list SqStack::SqStack():stacksize(0){ base = (SElemType *)malloc(STACK_SIZE_INIT * sizeof(SElemType)); top = base; stacksize = STACK_SIZE_INIT;}
main.cpp(测试用)
#include <stdio.h>#include "Stack.h"// Change the definition of SElemType for various data types#define SElemType int void main(){ SqStack S; for (int i = 1; i <= 10; i++){ S.Push(i); } S.show();}
阅读全文
0 0
- 数据结构实践(一)—— 栈
- 数据结构实践项目——查找(一)
- 数据结构实践项目——查找(一)
- 数据结构—栈(一)
- 第四周项目三数据结构实践(一)——单链表:逆置
- 数据结构实践——数制转换(栈)
- 数据结构实践项目——栈
- 数据结构实践——后缀表达式(栈)
- 数据结构实践——单链表逆置
- 数据结构实践——括号的匹配(栈)
- 数据结构实践——建立顺序栈算法库
- 数据结构实践——停车场模拟(栈和队列综合)
- CoreData实践(二)——设计数据结构
- 数据结构实践——顺序表应用(1)
- 数据结构实践——顺序表应用(2)
- 数据结构实践——猴子选大王(数组版)
- 数据结构实践项目——查找(二)
- ESLQ基础知识实践——实践一
- Hadoop--HDFS
- C++ explicit关键字详解
- 输出所有的最长公共子序列
- Kotlin学习记录(四)—— 常用集合的使用
- 奇虎360前端面试题
- 数据结构实践(一)—— 栈
- 欢迎使用CSDN-markdown编辑器
- React+Redux开发实战项目【美团App】,没你想的那么难
- 快速排序原理及Java实现
- JDBC更新数据库信息
- Dubbo的使用方法
- java swing
- 电脑显卡4种接口类型:VGA、DVI、HDMI、DP
- LC Divide & Conquer Summary