C/C++基本数据结构:顺序表/链表堆栈
2014-04-17 10:50
429 查看
堆栈
一、基础数据结构
1.顺序表:数组,存储在连续的空间中,随机访问方便,空间要求高,插入删除效率低。
2.链表:数据存储在不连续的节点中,各节点彼此关联,空间利用率高,插入删除效率高,随机访问不方便。
二、堆栈
1.基本特征:后进先出。
2.基本操作:压入(push),弹出(pop)
3.实现要点:初始空间,栈顶指针,判空判满。
代码实现:
// 顺序表堆栈
#include <iostream>
using namespace std;
// 堆栈
class Stack {
public:
// 构造函数
Stack (size_t size = 10) : m_data (new int[size]), m_top (0), m_size (size) {}
// 析构函数
~Stack (void) {
if (m_data) {
delete m_data;
m_data = NULL;
}
}
// 压入
void push (int data) {
if (m_top >= m_size)
throw OverFlow ();
m_data[m_top++] = data;
}
// 弹出
int pop (void) {
if (! m_top)
throw UnderFlow ();
return m_data[--m_top];
}
// 空否
bool empty (void) {
return ! m_top;
}
private:
// 上溢异常
class OverFlow : public exception {
public:
const char* what (void) const throw () {
return "堆栈上溢!";
}
};
// 下溢异常
class UnderFlow : public exception {
public:
const char* what (void) const throw () {
return "堆栈下溢!";
}
};
int* m_data; // 数组
size_t m_top; // 栈顶
size_t m_size; // 容量
};
int main (void) {
try {
Stack stack;
for (size_t i = 0; i < 10; i++)
stack.push (i);
//stack.push (10);
while (! stack.empty ())
cout << stack.pop () << endl;
//stack.pop ();
}
catch (exception& ex) {
cout << ex.what () << endl;
return -1;
}
return 0;
}
// 链表堆栈
#include <iostream>
using namespace std;
// 堆栈
class Stack {
public:
// 构造函数
Stack (void) : m_top (NULL) {}
// 析构函数
~Stack (void) {
for (Node* next; m_top; m_top = next) {
next = m_top -> m_next;
delete m_top;
}
}
// 压入
void push (int data) {
m_top = new Node (data, m_top);
}
// 弹出
int pop (void) {
if (! m_top)
throw UnderFlow ();
int data = m_top -> m_data;
Node* next = m_top -> m_next;
delete m_top;
m_top = next;
return data;
}
// 空否
bool empty (void) {
return ! m_top;
}
private:
// 下溢异常
class UnderFlow : public exception {
public:
const char* what (void) const throw () {
return "堆栈下溢!";
}
};
// 节点
class Node {
public:
Node (int data = 0, Node* next = NULL) : m_data (data), m_next (next) {}
int m_data;
Node* m_next;
};
Node* m_top; // 栈顶
};
int main (void) {
try {
Stack stack;
for (size_t i = 0; i < 10; i++)
stack.push (i);
//stack.push (10);
while (! stack.empty ())
cout << stack.pop () << endl;
//stack.pop ();
}
catch (exception& ex) {
cout << ex.what () << endl;
return -1;
}
return 0;
}
一、基础数据结构
1.顺序表:数组,存储在连续的空间中,随机访问方便,空间要求高,插入删除效率低。
2.链表:数据存储在不连续的节点中,各节点彼此关联,空间利用率高,插入删除效率高,随机访问不方便。
二、堆栈
1.基本特征:后进先出。
2.基本操作:压入(push),弹出(pop)
3.实现要点:初始空间,栈顶指针,判空判满。
代码实现:
// 顺序表堆栈
#include <iostream>
using namespace std;
// 堆栈
class Stack {
public:
// 构造函数
Stack (size_t size = 10) : m_data (new int[size]), m_top (0), m_size (size) {}
// 析构函数
~Stack (void) {
if (m_data) {
delete m_data;
m_data = NULL;
}
}
// 压入
void push (int data) {
if (m_top >= m_size)
throw OverFlow ();
m_data[m_top++] = data;
}
// 弹出
int pop (void) {
if (! m_top)
throw UnderFlow ();
return m_data[--m_top];
}
// 空否
bool empty (void) {
return ! m_top;
}
private:
// 上溢异常
class OverFlow : public exception {
public:
const char* what (void) const throw () {
return "堆栈上溢!";
}
};
// 下溢异常
class UnderFlow : public exception {
public:
const char* what (void) const throw () {
return "堆栈下溢!";
}
};
int* m_data; // 数组
size_t m_top; // 栈顶
size_t m_size; // 容量
};
int main (void) {
try {
Stack stack;
for (size_t i = 0; i < 10; i++)
stack.push (i);
//stack.push (10);
while (! stack.empty ())
cout << stack.pop () << endl;
//stack.pop ();
}
catch (exception& ex) {
cout << ex.what () << endl;
return -1;
}
return 0;
}
// 链表堆栈
#include <iostream>
using namespace std;
// 堆栈
class Stack {
public:
// 构造函数
Stack (void) : m_top (NULL) {}
// 析构函数
~Stack (void) {
for (Node* next; m_top; m_top = next) {
next = m_top -> m_next;
delete m_top;
}
}
// 压入
void push (int data) {
m_top = new Node (data, m_top);
}
// 弹出
int pop (void) {
if (! m_top)
throw UnderFlow ();
int data = m_top -> m_data;
Node* next = m_top -> m_next;
delete m_top;
m_top = next;
return data;
}
// 空否
bool empty (void) {
return ! m_top;
}
private:
// 下溢异常
class UnderFlow : public exception {
public:
const char* what (void) const throw () {
return "堆栈下溢!";
}
};
// 节点
class Node {
public:
Node (int data = 0, Node* next = NULL) : m_data (data), m_next (next) {}
int m_data;
Node* m_next;
};
Node* m_top; // 栈顶
};
int main (void) {
try {
Stack stack;
for (size_t i = 0; i < 10; i++)
stack.push (i);
//stack.push (10);
while (! stack.empty ())
cout << stack.pop () << endl;
//stack.pop ();
}
catch (exception& ex) {
cout << ex.what () << endl;
return -1;
}
return 0;
}
相关文章推荐
- C/C++基本数据结构:顺序表/链表 队列
- 【C++/数据结构】循环链表的基本操作
- C++ 单链表基本操作分析与实现 链表 链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结
- 【C++/数据结构】双向链表的基本操作
- 数据结构 顺序表的基本操作 (C++ 版本)
- C++数据结构 链表的基本操作
- c++基本数据结构的类的用法--栈,队列,链表
- 数据结构-线性表(顺序表与链表的基本知识 以及ArrayList 源码分析)
- 数据结构(C++实现)--链表(1) 基本实现
- C/C++基本数据结构:链表的基本使用和实现
- 程序:找出两个链表的第一个公共节点及c++中堆栈、队列的基本操作
- 基本数据结构:链表(list) - C小加 - C++博客
- 【C++/数据结构】顺序表的基本操作
- c/c++ 数据结构-线性表(单链表基本操作的实现)
- 《摘》面向对象c++数据结构--基本数据结构之链表-双向链表(结束)(凌风)
- [C++]数据结构:有序链表SortedChain的基本实现与操作
- (摘)面向对象c++数据结构之基本数据结构-链表-使用链表管理器(连载中)
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- 面向对象c++数据结构--基本数据结构之链表(连载中……)(凌风)
- SqListFunc顺序链表的12个基本操作的C语言代码(数据结构)