奇葩Stack之实现。

#include <iostream>
using namespace std;

template <class T>
class Data
{
public:
	T value;
	Data* prev;
	Data* next;
	int tag;
};

template <class T>
class Stack
{
public:
	Stack()
	{
		index = 0;
		stack = new Data<T>();
		stack->next = nullptr;
		stack->prev = nullptr;
		stack->tag = 0;
	}

	~Stack()
	{
		//while (stack != nullptr)
		//{
		//	stack = stack->next;
		//	delete stack->prev;
		//}
	}

	void push(T value);
	T pop();
	void selfShow();
private:
	Data<T>* stack;
	int index;
};

template <class T>
void Stack<T>::push(T value)
{
	if (index == 0)
	{
		stack = new Data<T>();
		stack->value = value;
		stack->next = nullptr;
		stack->prev = 0;
		stack->tag = index;
		++index;
	}
	else
	{
		Data<T>* newData = new Data<T>();
		newData->value = value;
		newData->tag = index;
		newData->next = nullptr;
		if(stack->next == nullptr)
		{
			newData->prev = stack;
			stack->next = newData;
		}
		else
		{
			Data<T>* temp_f = stack;
			Data<T>* temp_l = stack;
			while (temp_f != nullptr)
			{
				temp_l = temp_f;
				temp_f = temp_f->next;
			}
			temp_l->next = newData;
			newData->prev = temp_l;
		}
		
		++index;
	}
}

template <class T>
T Stack<T>::pop()
{
	while (stack != nullptr && stack->next)
	{

	}
}

template <class T>
void Stack<T>::selfShow()
{
	Data<T>* current = stack;
	while (current->next)
	{
		current = current->next;
	}
	while (current->prev)
	{
		cout << current->value << endl;
		current = current->prev;
	}
	cout << current->value << endl;
}

int main()
{
	Stack<int> intStack;
	intStack.push(1);
	intStack.push(2);
	intStack.push(3);
	intStack.push(4);
	intStack.push(5);
	intStack.selfShow();
	system("pause");
}

奇葩Stack之实现。》有5个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注