欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++使用模板類實現(xiàn)鏈式棧

 更新時間:2020年04月27日 14:39:23   作者:ChanJose  
這篇文章主要為大家詳細介紹了C++使用模板類實現(xiàn)鏈式棧,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++使用模板類實現(xiàn)鏈式棧的具體代碼,供大家參考,具體內(nèi)容如下

一、實現(xiàn)程序:

1.Stack.h

#ifndef Stack_h
#define Stack_h
 
template <class T>
class Stack {
public:
  Stack(){}; // 構(gòu)造函數(shù)
  void Push(const T x); // 新元素進棧
  bool Pop(); // 棧頂元素出棧
  virtual bool getTop(T &x) const = 0; // 讀取棧頂元素,由x返回
  virtual bool isEmpty() const = 0; // 判斷??辗?
  // virtual bool isFull() const = 0; // 判斷棧滿否,因為鏈式棧不存在不滿的情況
  virtual int getSize() const = 0; // 計算棧中元素個數(shù)
};
 
 
#endif /* Stack_h */

2.LinkedStack.h

#ifndef LinkedStack_h
#define LinkedStack_h
#include <iostream>
#include "Stack.h"
using namespace std;
 
template <class T>
struct LinkNode {
  T data;
  LinkNode<T> *link;
};
 
//類的前置聲明
template <class T>
class LinkedStack;
 
//友元函數(shù)的聲明
template <class T>
ostream& operator<<(ostream& out, LinkedStack<T>& s);
template <class T>
class LinkedStack: public Stack<T> {
public:
  LinkedStack(); // 構(gòu)造函數(shù)
  ~LinkedStack();// 析構(gòu)函數(shù)
  void Push(const T x); // 進棧
  bool Pop(); // 出棧
  bool getTop(T &x) const; // 讀取棧頂元素
  bool isEmpty()const; // 判斷棧是否為空
  int getSize()const; // 求棧的元素個數(shù)
  void makeEmpty(); // 清空棧的內(nèi)容
  friend ostream& operator << <T>(ostream& out, LinkedStack<T>& s); // 重載輸出函數(shù)
private:
  LinkNode<T> *top; // 棧頂指針,即鏈頭指針
};
template <class T>
LinkedStack<T>::LinkedStack() {
  // 構(gòu)造函數(shù),置空棧
  top = new LinkNode<T>(); // 引入頭指針:不存放數(shù)據(jù)
  top->link = NULL;
}
template <class T>
LinkedStack<T>::~LinkedStack() {
  // 析構(gòu)函數(shù),釋放內(nèi)存空間
  makeEmpty();
}
template <class T>
void LinkedStack<T>::Push(const T x) {
  // 進棧:將元素值x插入到鏈式棧的棧頂,即鏈頭
  LinkNode<T> *newNode = new LinkNode<T>(); // 創(chuàng)建包含x的新結(jié)點
  if(newNode == NULL) {
    cerr << "內(nèi)存空間分配失?。? << endl;
    exit(1);
  }
  newNode->data = x;
  newNode->link = top->link; // 指向頭指針的下一個結(jié)點:即棧中第一個存放有效數(shù)據(jù)的結(jié)點
  top->link = newNode; // 頭指針往前移
}
template <class T>
bool LinkedStack<T>::Pop() {
  // 出棧:刪除棧頂結(jié)點
  if(isEmpty())
    return false; // ???,不出棧
  LinkNode<T> *p = top->link; // 暫存棧頂元素
  top->link = p->link; // 棧頂指針退到新的棧頂位置
  delete p;
  p = NULL;
  return true;
}
 
template <class T>
bool LinkedStack<T>::getTop(T &x) const {
  // 讀取棧頂元素
  if(isEmpty())
    return false;
  x = top->link->data; // 棧不空,返回棧頂元素的值。這里top為頭指針,所以棧頂元素為:top->link
  return true;
}
 
template <class T>
bool LinkedStack<T>::isEmpty()const {
  // 判斷棧是否為空
  if(top->link == NULL) // 棧為空
    return true;
  return false;
}
 
template <class T>
int LinkedStack<T>::getSize()const {
  // 求棧的元素個數(shù)
  int len = 0;
  
  LinkNode<T> *current = top->link;
  while(current != NULL) {
    len++;
    current = current->link;
  }
  return len;
}
 
template <class T>
void LinkedStack<T>::makeEmpty() {
  // 清空棧的內(nèi)容
  LinkNode<T> *current = top->link;
  while(current != NULL) {
    top->link = current->link; // 保存鏈式棧準備要刪除的結(jié)點的下一個結(jié)點,防止丟失
    delete current; // 釋放
    current = NULL; // 先指向空
    current = top->link; // 再指向剩下鏈表的首結(jié)點
  }
}
 
template <class T>
ostream& operator<<(ostream& out, LinkedStack<T>& s) {
  // 重載輸出函數(shù)
  LinkNode<T> *current = s.top->link;
  while(current != NULL) {
    out << current->data << " ";
    current = current->link;
  }
  return out;
}
#endif /* LinkedStack_h */

3.main.cpp

#include "LinkedStack.h"
using namespace std;
 
int main(int argc, const char * argv[]) {
  int n, x, choice, len; // val存儲值,choose存儲用戶的選擇
  bool finished = false;
  LinkedStack<int> L; // 對象
  
  while(!finished) {
    cout << "1:建棧:" << endl;
    cout << "2:進棧" << endl;
    cout << "3:出棧:" << endl;
    cout << "4:讀取棧頂元素:" << endl;
    cout << "5:棧是否為空:" << endl;
    cout << "6:棧中的元素個數(shù):" << endl;
    cout << "7:清空棧的內(nèi)容:" << endl;
    cout << "8:輸出棧中元素的值:" << endl;
    cout << "9:退出" << endl;
    cout << "請輸入你的選擇[1-9]:" << endl;
    cin >> choice;
    switch(choice) {
      case 1:
        cout << "請輸入要進棧的數(shù)的個數(shù):";
        cin >> n;
        cout << "請輸入要進棧的數(shù)(以空格隔開):" << endl;
        for(int i=0; i < n; i++) {
          cin >> x;
          L.Push(x);
        }
        break;
      case 2:
        cout << "請輸入要進棧的數(shù):";
        cin >> x;
        L.Push(x);
        break;
      case 3:
        if(L.Pop())
          cout << "出棧成功!" << endl;
        else
          cout << "棧為空!" << endl;
        break;
      case 4:
        if(L.getTop(x))
          cout << "棧頂元素的值為:" << x << endl;
        else
          cout << "棧為空!" << endl;
        break;
      case 5:
        if(L.isEmpty())
          cout << "棧為空!" << endl;
        else
          cout << "棧不為空!" << endl;
        break;
      case 6:
        len = L.getSize();
        cout << "棧中的元素個數(shù)為:" << len << endl;
        break;
      case 7:
        L.makeEmpty(); // 清空棧
        break;
      case 8:
        if(L.isEmpty())
          cout << "棧為空!" << endl;
        else
          cout << L << endl;
        break;
      case 9:
        finished = true;
        break;
      default:
        cout << "輸入錯誤,請重新輸入!" << endl;
    } // switch
  } // while
  return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論