亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

求助,請問該如何用函數實現入棧和出棧?

求助,請問該如何用函數實現入棧和出棧?

海綿寶寶撒 2021-12-15 15:11:23
已知鏈棧Q,編寫函數判斷???,如果??談t進行入棧操作,否則出棧并輸出。(要求判斷??铡⒊鰲?、入棧用函數實現)
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

#ifndef Node_
#define Node_

template <class T> class LinkedStack;
template <class T> class LinkedQueue;

template <class T>
class Node {
friend LinkedStack<T>;
friend LinkedQueue<T>;
private:
T data;
Node<T> *link;
};

#endif
// file lstack.h
// linked stack

#include "node.h"
#include "xcept.h"

template<class T>
class LinkedStack {
public:
LinkedStack() {top = 0;}
~LinkedStack();
bool IsEmpty() const {return top == 0;}
bool IsFull() const;
T Top() const;
LinkedStack<T>& Add(const T& x);
LinkedStack<T>& Delete(T& x);
private:
Node<T> *top; // pointer to top node
};

template<class T>
LinkedStack<T>::~LinkedStack()
{// Stack destructor..
Node<T> *next;
while (top) {
next = top->link;
delete top;
top = next;
}
}

template<class T>
bool LinkedStack<T>::IsFull() const
{// Is the stack full?
try {Node<T> *p = new Node<T>;
delete p;
return false;}
catch (NoMem) {return true;}
}

template<class T>
T LinkedStack<T>::Top() const
{// Return top element.
if (IsEmpty()) throw OutOfBounds();
return top->data;
}

template<class T>
LinkedStack<T>& LinkedStack<T>::Add(const T& x)
{// Add x to stack.
Node<T> *p = new Node<T>;
p->data = x;
p->link = top;
top = p;
return *this;
}

template<class T>
LinkedStack<T>& LinkedStack<T>::Delete(T& x)
{// Delete top element and put it in x.
if (IsEmpty()) throw OutOfBounds();
x = top->data;
Node<T> *p = top;
top = top->link;
delete p;
return *this;
}



查看完整回答
反對 回復 2021-12-19
  • 1 回答
  • 0 關注
  • 242 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號