数据结构与算法-实验2-自定义栈,并实现push、pop、改栈顶元素、取栈顶元素
来源:互联网 发布:河北seo按效果付费 编辑:程序博客网 时间:2024/06/10 23:56
#include <iostream>
using namespace std;
const int maxnum = 3;
typedef struct Datastack //定义栈中层数据类型
{
int data; //节点中数据类型
struct Datastack * pre, *next; //双向栈
}datastack;
datastack *Create_stack(); //创建栈,返回计数节点指针
datastack *push_stack(datastack *p,int data); //进栈
datastack *pop_stack(datastack *p); //出栈
datastack *changeTop_stack(datastack *p,int data); //改栈顶元素
int top_stack(datastack *p); //取栈顶元素
bool Isempty_stack(datastack *p); //栈空判断
bool Isfull_stack(datastack *p); //栈满判断
int main()
{
datastack *head = NULL; //定义头指针
cout << "Create_stack() 正在创建一个栈区域... "; //创建栈存储区
head = Create_stack();
if (head != NULL)
{
cout << "创建栈存储区成功!\n";
}
else
{
cout << "创建栈存储区失败!\n";
return 0;
}
cout << "Isempty() return "; //判空
if (Isempty_stack(head))
{
cout << "现在栈为空!\n";
}
else
{
cout << "现栈中存在元素!\n";
}
cout << "push(2)" << endl; //进栈第一个元素
head = push_stack(head, 2);
cout << "top() return " << top_stack(head) << endl;
cout << "push(5)" << endl; //进栈第二个元素
head = push_stack(head, 5);
cout << "top() return " << top_stack(head) << endl;
cout << "push(11)" << endl; //进栈第三个元素
head = push_stack(head, 11);
cout << "top() return " << top_stack(head) << endl;
cout << "changeTop_stack()=6;" << endl; //改变栈顶元素
head = changeTop_stack(head, 6);
cout << "changeTop_stack() return " << top_stack(head) << endl;
cout << "Isfull(): return "; //判满
if (Isfull_stack(head))
{
cout << "栈已满!\n";
}
else
{
cout << "栈中还可以存储元素!\n";
}
cout << "pop(6)" << endl; //出栈第一个元素
head = pop_stack(head);
cout << "top() return " << top_stack(head) << endl;
cout << "pop(5)" << endl; //出栈第二个元素
head = pop_stack(head);
cout << "top() return " << top_stack(head) << endl;
cout << "pop(2) return " << endl; //出栈第三个元素
head = pop_stack(head);
cout << "Isempty() return "; //判空
if (Isempty_stack(head))
{
cout << "现在栈为空!\n";
}
else
{
cout << "现栈中存在元素!\n";
}
free(head);
return 0;
}
datastack *Create_stack() //创建一个栈计数节点,最后返回栈底指针
{
datastack *p = NULL;
datastack *s = NULL;
s = (datastack *)malloc(sizeof(datastack)); //只创建计数节点
s->pre = NULL;
s->data = 0;
s->next = NULL;
p = s;
return p;
}
//进栈
datastack *push_stack(datastack *p,int data)
{
datastack *s = NULL;
if (Isfull_stack(p)) //判满
{
cout << "栈已满,入栈操作失败!\n";
return p;
}
if (p->data == 0)
{
s = (datastack *)malloc(sizeof(datastack)); //节点空间
s->pre = p;
s->data = data;
s->next = NULL;
p->next = s;
p->data++;
}
else
{
s = (datastack *)malloc(sizeof(datastack)); //新节点空间
s->pre = p->next;
s->data = data;
s->next = NULL;
(p->next)->next = s;
p->next = s;
p->data++;
}
return p;
}
//出栈
datastack *pop_stack(datastack *p)
{
datastack *pfree = NULL;
if (Isempty_stack(p)) //判空
{
cout << "栈已空,出栈操作失败!\n";
return p;
}
pfree = p->next;
p->next = pfree->pre;
(p->next)->next = NULL;
pfree->pre = NULL;
free(pfree);
p->data--;
return p;
}
//取栈顶元素
int top_stack(datastack *p)
{
if (Isempty_stack(p)) //判空
{
cout << "栈已空,栈顶无元素!\n";
return 0;
}
return (p->next)->data;
}
//改栈顶元素
datastack *changeTop_stack(datastack *p,int data)
{
if (Isempty_stack(p)) //判空
{
cout << "栈已空,改栈顶元素操作失败!\n";
return p;
}
(p->next)->data = data;
return p;
}
//判栈满
bool Isfull_stack(datastack *p)
{
if (p->data == maxnum)
{
return true;
}
return false;
}
//判栈空
bool Isempty_stack(datastack *p)
{
if (p->data == 0)
{
return true;
}
return false;
}
- 数据结构与算法-实验2-自定义栈,并实现push、pop、改栈顶元素、取栈顶元素
- 数据结构与算法-实验3-自定义栈,并实现走迷宫问题
- 【数据结构】堆的实现(包括:默认成员函数,插元素push,删元素pop,访问根节点top,判空,大小)
- 常用数据结构2——栈,实现PUSH、POP和取最小值操作算法时间复杂度为o(1)
- 数据结构之栈的push与pop操作(顺序存储结构的c实现)
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。队列中的元素为int类
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 使用Hyper-V PowerShell
- Java基础:面向对象
- HDU 5974 A Simple Math Problem (数学)
- Android开发-DesignDemo-AndroidStudio(十一)FloatingActionButton(3)
- android studio使用教学提高篇 重点难点
- 数据结构与算法-实验2-自定义栈,并实现push、pop、改栈顶元素、取栈顶元素
- LeetCode 33. Search in Rotated Sorted Array
- apache-shiro杂记(二) 关于多realm认证的策略
- BSTR ASCII Unicode _T 宏
- java 签名
- 自定义View笔记
- 正则表达式
- Django(Session,Cookie)
- bootstrap学习总结-css组件(三)