您的位置:首页 > 理论基础 > 数据结构算法

[数据结构]Linked_stack

2016-03-30 18:17 369 查看
//Link_Stack.h
#pragma once
enum Error_code{success,underflow,overflow};
template<class Stack_entry>
class Linked_Stack
{
public:
Linked_Stack();
bool empty()const;
Error_code pop();
Error_code top(Stack_entry &item)const;
Error_code push(const Stack_entry &item);
~Linked_Stack();
void operator=(const Linked_Stack &original);
Linked_Stack(const Stack_entry &item);
void print();
private:
struct Node
{
Node();
Node(const Stack_entry &item, Node *add_on = NULL);
Node *next;
Stack_entry entry;
};
Node *head;
};

template<class Stack_entry>
inline Linked_Stack<Stack_entry>::Node::Node()
{
next = NULL;
}

template<class Stack_entry>
inline Linked_Stack<Stack_entry>::Node::Node(const Stack_entry & item, Node * add_on)
{
next = add_on;
entry = item;
}
//Linked_stack.cpp
#include<iostream>
#include"Link_Stack.h"
using namespace std;

template<class Stack_entry>
Linked_Stack<Stack_entry>::Linked_Stack(){
head = NULL;
}

template<class Stack_entry>
bool Linked_Stack<Stack_entry>::empty() const
{
if (head)return true;
else return false;
}

template<class Stack_entry>
Error_code Linked_Stack<Stack_entry>::pop()
{
if (!head)return underflow;
Node *temp = head;
head = head->next;
delete temp;
return success;
}

template<class Stack_entry>
Error_code Linked_Stack<Stack_entry>::top(Stack_entry & item) const
{
if (head)return underflow;
item = head->entry;
return success;
}

template<class Stack_entry>
Error_code Linked_Stack<Stack_entry>::push(const Stack_entry & item)
{
head = new Node(item, head);
return success;
}

template<class Stack_entry>
Linked_Stack<Stack_entry>::~Linked_Stack()
{
while (head) {
Node *temp = head;
head = head->next;
delete temp;
}
}

template<class Stack_entry>
void Linked_Stack<Stack_entry>::operator=(const Linked_Stack & original)
{
Node *temp = original.head;
Linked_Stack <Stack_entry> middle;
if (head == original.head)return;
while (temp) {
middle.push(temp->entry);
temp = temp->next;
}
while (head) pop();
temp = middle.head;
while (temp) {
push(temp->entry);
temp = temp->next;
}
}
template<class Stack_entry>
Linked_Stack<Stack_entry>::Linked_Stack(const Stack_entry & original)
{
Node *new_head, *new_temp, *original_temp = original.head;
if (original == NULL) { new_head == NULL; return; }
new_temp = new_head = new Node(original_temp->entry);
while (original_temp) {
original_temp = original_temp->next;
new_temp->next = new Node(original_temp->entry);
new_temp = new_temp->next;
}
}

template<class Stack_entry>
void Linked_Stack<Stack_entry>::print()
{
Node *temp = head;
while (temp) {
cout << temp->entry << ' ';
temp = temp->next;
}
cout << endl;
}
//main.cpp
#include<iostream>
#include"Linked_Stack.cpp"
using namespace std;
void main() {
cout << "1.push test1." << endl;
cout << "2.push test2." << endl;
cout << "3.pop test1." << endl;
cout << "4.pop test1." << endl;
cout << "5.make test1=test2." << endl;
cout << "6.make test2=test1." << endl;
cout << "7.print test1." << endl;
cout << "8.print test2." << endl;
Linked_Stack<int> test1, test2;
while (1) {
int key=0;

cin >> key;
switch (key)
{
case 1: {
int item;
cout << "please cin item." << endl;
cin >> item;
test1.push(item);
cout << "--------------------------------------------------" << endl;
break;
}
case 2: {
int item;
cout << "please cin item." << endl;
cin >> item;
test2.push(item);
cout << "--------------------------------------------------" << endl;
break;
}
case 3:
test1.pop();
cout << "--------------------------------------------------" << endl;
break;
case 4:
test2.pop();
cout << "--------------------------------------------------" << endl;
break;
case 5:
test1 = test2;
cout << "--------------------------------------------------" << endl;
break;
case 6:
test2 = test1;
cout << "--------------------------------------------------" << endl;
break;
case 7:
test1.print();
cout << "--------------------------------------------------" << endl;
break;
case 8:
test2.print();
cout << "--------------------------------------------------" << endl;
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: