这个礼拜写了两个模版直接放源码吧,主要为了练练模版的变成熟练度
model.h
======
#ifndef MODEL_H_
#define MODEL_H_
#include<iostream>
namespace model_Stack {
#define nullptr 0
template <class T>
class Stack {
private:
struct node {
T item;
node *next;
};
node *front;
int depth;
public:
Stack();
Stack( const T s[],int n );
~Stack();
void push( const T a );
void pop();
T get_top();
int get_depth();
bool is_empty();
};
template <class T>
Stack<T>::Stack() {
depth = 0;
front = new node;
front->next = nullptr;
}
template <class T>
Stack<T>::Stack( const T s[],int n ): depth(n) {
front = new node;
front->next = nullptr;
node *j,*k;
k = nullptr;
for(int i=0;i<n;i++) {
j = new node;
j->next = k;
j->item = s[i];
k = j;
}
front->next = j;
}
template <class T>
void Stack<T>::push( const T a ) {
node *i;
depth++;
i = new node;
i->item = a;
i->next = front->next;
front->next = i;
}
template <class T>
void Stack<T>::pop() {
if(depth == 0)
return;
node *i = front->next;
front->next = i->next;
delete i;
depth--;
}
template <class T>
T Stack<T>::get_top() {
node *k = front->next;
return k->item;
}
template <class T>
int Stack<T>::get_depth() {
return depth;
}
template <class T>
bool Stack<T>::is_empty(){
if(depth == 0)return true;
return false;
}
template <class T>
Stack<T>::~Stack(){
node *i = front;
node *k;
while(i!=nullptr) {
k = i;
i = i->next;
delete k;
}
depth = 0;
}
};
namespace model_Queue {
#define nullptr 0
template <class T>
class Queue {
private:
struct node{
T item;
node *next;
};
node *front;
node *rear;
int depth;
public:
Queue();
Queue( const T s[], int n);
~Queue();
void pop();
void push( const T a);
T get_top();
bool is_empty();
int get_depth();
};
template <class T>
Queue<T>::Queue(): depth(0) {
front = new node;
rear = front;
rear->next = nullptr;
}
template <class T>
Queue<T>::Queue( const T s[], int n): depth(n) {
front = new node;
node *p,*q;
front->next = nullptr;
int i;
q = front;
for( i = 0; i < n; i++) {
p = new node;
p->item = s[i];
q->next = p;
p->next = nullptr;
q = p;
}
rear = q;
}
template <class T>
Queue<T>::~Queue() {
node *p = front, *q;
while(p != nullptr) {
q = p;
p = p->next;
delete q;
}
}
template <class T>
void Queue<T>::pop() {
node *p = front->next;
front->next = p->next;
delete p;
depth--;
}
template <class T>
void Queue<T>::push( const T a) {
node *p = new node;
p->item = a;
p->next = nullptr;
rear->next = p;
rear = p;
depth++;
}
template <class T>
T Queue<T>::get_top() {
node *p = front->next;
return p->item;
}
template <class T>
bool Queue<T>::is_empty() {
if(depth == 0)return true;
return false;
}
template <class T>
int Queue<T>::get_depth(){
return depth;
}
};
#endif
在测试文件夹中测试模版功能
test_Stack.cpp
#include <iostream>
#include "../model/model.h"
using namespace model_Stack;
using std::endl;
int main() {
int i;
char a[10];
for(i=0; i<10; i++)
std::cin>>a[i];
Stack <char> s1(a,10);
std::cout<<s1.get_top()<<" "<<s1.get_depth()<<endl;
for(i=0; i<9; i++){
s1.pop();
std::cout<<s1.get_top()<<endl;
}
std::cout<<s1.get_top()<<" "<<s1.get_depth()<<endl;
if(!s1.is_empty())
s1.pop();
return 0;
}
test_Queue.cpp
#include <iostream>
#include "../model/model.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
using namespace model_Queue;
int main() {
string a;
int i,n;
Queue<string> s;
for( i = 0; i < 5; i++) {
cin>>a;
s.push(a);
}
while(!s.is_empty()) {
cout<<s.get_top()<<" "<<s.get_depth()<<endl;
s.pop();
}
int b[5];
for( i = 0; i < 5; i++)
cin>>b[i];
Queue<int> s1(b,5);
while(!s1.is_empty()) {
cout<<s1.get_top()<<" "<<s1.get_depth()<<endl;
s1.pop();
}
return 0;
}