Queue和Stack

这个礼拜写了两个模版直接放源码吧,主要为了练练模版的变成熟练度
model.h
======

#ifndef MODEL_H_
#define MODEL_H_
#include

namespace model_Stack {
#define nullptr 0
template 
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 
  Stack::Stack() {
        depth = 0;
        front = new node;
        front->next = nullptr;
  }

  template 
  Stack::Stack( const T s[],int n ): depth(n) {
        front = new node;
        front->next = nullptr;
        node *j,*k;
        k = nullptr;
        for(int i=0;inext = k;
          j->item = s[i];
          k = j;
        }
    front->next = j;
  }

  template 
  void Stack::push( const T a ) {
        node *i;
        depth++;
        i = new node;
        i->item = a;
        i->next = front->next;
        front->next = i;
  }


  template 
  void Stack::pop() {
        if(depth == 0)
          return;
        node *i = front->next;
        front->next = i->next;
        delete i;
    depth--;
  }

  template 
  T Stack::get_top() {
        node *k = front->next;
        return k->item;
  }

  template 
  int Stack::get_depth() {
        return depth;
  }

  template 
  bool Stack::is_empty(){
        if(depth == 0)return true;
        return false;
  }
  
  template 
  Stack::~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 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 
  Queue::Queue(): depth(0) {
    front = new node;
    rear = front;
    rear->next = nullptr;
  }

  template 
  Queue::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 
  Queue::~Queue() {
        node *p = front, *q;
    while(p != nullptr) {
        q = p;
        p = p->next;
        delete q;
    }
  }
  
  template 
  void Queue::pop() {
    node *p = front->next;
    front->next = p->next;
    delete p;
    depth--;
  }
  
  template 
  void Queue::push( const T a) {
    node *p = new node;
    p->item = a;
    p->next = nullptr;
    rear->next = p;
    rear = p;
    depth++;
  }

  template 
  T Queue::get_top() {
    node *p = front->next;
    return p->item;
  }

  template 
  bool Queue::is_empty() {
    if(depth == 0)return true;
    return false;
  }

  template 
  int Queue::get_depth(){
    return depth;
  }
}; 
#endif

在测试文件夹中测试模版功能

test_Stack.cpp

#include 
#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  s1(a,10);
    std::cout<

test_Queue.cpp

#include 
#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 s;
    for( i = 0; i < 5; i++) {
    cin>>a;
    s.push(a);
    }
    while(!s.is_empty()) {
    cout<>b[i];
    Queue s1(b,5);
    while(!s1.is_empty()) {
        cout<

你可能感兴趣的:(Queue和Stack)