自定义栈数据结构,并且实现取出栈中最小元素

Stack.h

#include<iostream>
using namespace std;

const int success = 1;
const int overflow = 2;
const int underflow = 3;

typedef int nodeEntry;
struct stackGetMin
{
	Stack temp;
	Stack minStack;
	void push(int item)
	{
		temp.push(item);
		if(minStack.empty() || item<=minStack.top())
			minStack.push(item);
	}
	bool getMin(int &min)
	{
		if(minStack.empty())
			return false;
		else
		{
			min = minStack.top();
			return true;
		}
	}
};
struct node
{
	nodeEntry data;
	node *next;
};
class Stack
{
public:
	Stack::Stack();
	Stack::~Stack();
	bool Stack::empty();
	int Stack::size();
	int Stack::pop();
	int Stack::Min();
	nodeEntry Stack::top();
	int Stack::push(const nodeEntry &item);
	void Stack::operator = (const Stack &original);
	Stack::Stack(const Stack &original);
protected:
	node *top_node;
	int count;
	int min;
};

bool Stack::empty()
{
	return count == 0;
}
int Stack::pop()
{
	if(top_node !=NULL)
	{
		node *old_node = top_node;
		top_node = top_node->next;
		delete old_node;
		count--;
		return success;
	}
	return underflow;
}
int Stack::push(const nodeEntry &item)
{
	node *new_top = new node;
	if(new_top == NULL)
		return overflow;
	if(count == 0)
		min = item;
	else
		min = (min>item) ? item : min;
	new_top->data = item;
	new_top->next = top_node;
	count++;
	top_node = new_top;
	return success;
}
nodeEntry Stack::top()
{
	if(top_node == NULL)
		return underflow;
	return top_node->data;
}
Stack::Stack()
{
	top_node = NULL;
	count = 0;
}
Stack::~Stack()
{
	while(count--)
		pop();
}
int Stack::size()
{
	return count;
}
int Stack::Min()
{
	return min;
}
void Stack::operator =(const Stack &original)
{
	node *new_top,*new_temp;
	node *original_node = original.top_node;
	if(original_node == NULL)
		new_top == NULL;
	else
	{
		new_top = new_temp = new node;
		new_top->data = new_temp->data = original_node->data;
		new_top->next = new_temp->next = NULL;
		while(original_node->next !=NULL)
		{
			original_node = original_node->next;
			new_temp->next = new node;
			new_temp = new_temp->next;
			new_temp->data = original_node->data;
			new_temp->next = NULL;
		}
	}
	while(!empty())
		pop();
	top_node = new_top;
}
Stack::Stack(const Stack &original)
{
	node *new_copy,*new_copy_head;
	node *original_node = original.top_node;
	if(original_node == NULL)
		new_copy_head = NULL;
	else
	{
		new_copy = new_copy_head = new node;
		new_copy->data = new_copy_head->data = original_node->data;
		new_copy->next = new_copy_head->next = NULL;
		while(original_node->next != NULL)
		{
			original_node = original_node->next;
			new_copy->next = new node;
			new_copy = new_copy->next;
			new_copy->data = original_node->data;
			new_copy->next = NULL;
		}
	}
	while(!empty())
		pop();
	top_node = new_copy_head;
}

<test.cpp>

#include<iostream>
#include<stdlib.h>
#include"Stack.h"
using namespace std;
int main()
{
	stackGetMin stackMin;
	stackMin.push(2);
	stackMin.push(2);
	stackMin.push(4);
	stackMin.push(3);
	stackMin.push(10);
	int min;
	stackMin.getMin(min);
	cout<<min<<endl;
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值