Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

补全此代码#include <iostream> using namespace std; #define maxSize 50 typedef int T;…

  1. User

    补全此代码#include <iostream> using namespace std; #define maxSize 50 typedef int T; //顺序栈 class SeqStack { private: T *Sstack;//栈使用内存首地址 int top;//栈顶指针 int RSize;//栈分配元素个数 void overflow(); public: SeqStack(); ~SeqStack(); void push(T x); bool pop(T& x); bool getTop(T& x); bool isEmpty(); bool isFull(); int getSize(); void makeEmpty(); void print(); }; //构造 SeqStack::SeqStack() { data = new T[MaxSize]; top = -1; } //析构 SeqStack::~SeqStack() { delete[] data; } //栈溢出时扩大栈容量 void SeqStack::overflow() { if (Sstack != NULL) { T *newArray = new T[RSize + 20]; ... } } //进栈 void SeqStack::push(T x) { if (top == maxSize - 1) return false; top++; data[top] = e; return ture; } //出栈 bool SeqStack::pop(T& x) { if (empty()) return false; e = data[top]; top--; return true; } //获取栈顶元素 bool SeqStack::getTop(T& x) { if (empty()) return false; e = data[top]; return true; } //栈是否空 bool SeqStack::isEmpty() { return top == -1; } //栈是否满 bool SeqStack::isFull() { ... } //栈实际元素个数 int SeqStack::getSize() { ... } //置栈空 void SeqStack::makeEmpty() { ... } //由栈顶到栈底依次输出栈元素 void SeqStack::print() { ... } //菜单 void menu() { cout << "1.进栈" << endl; cout << "2.出栈" << endl; cout << "3.获取栈顶元素" << endl; cout << "4.栈置空" << endl; cout << "5.由栈顶到栈底依次输出栈元素" << endl; cout << "6.退出" << endl; } //执行菜单功能 void function(int num, SeqStack *ss) { switch (num) { int x; case 1: ... break; case 2: ... break; case 3: ... break; case 4: ... break; case 5: ... break; default: exit(0); } }

    int main(int argc, char** argv) { SeqStack *ss = new SeqStack; int num; while (true) { menu(); cin >> num; function(num, ss); } delete ss; return 0; }

  2. Assistant

    #include using namespace std; #define MaxSize 50 typedef int T; //顺序栈 class SeqStack { private: T *data;//栈使用内存首地址 int top;//栈顶指针 int RSize;//栈分配元素个数 void overflow(); public: SeqStack(); ~SeqStack(); void push(T x); bool pop(T& e); bool getTop(T& e); bool isEmpty(); bool isFull(); int getSize(); void makeEmpty(); void print(); }; //构造 SeqStack::SeqStack() { data = new T[MaxSize]; top = -1; } //析构 SeqStack::~SeqStack() { delete[] data; } //栈溢出时扩大栈容量 void SeqStack::overflow() { if (data != NULL) { T *newArray = new T[RSize + 20]; for(int i = 0; i <=top; i++) { newArray[i] = data[i]; } delete[] data; data = newArray; RSize += 20; } } //进栈 void SeqStack::push(T x) { if (top == MaxSize - 1) { overflow(); } top++; data[top] = x; } //出栈 bool SeqStack::pop(T& x) { if (isEmpty()) { return false; } x = data[top]; top--; return true; } //获取栈顶元素 bool SeqStack::getTop(T& x) { if (isEmpty()) { return false; } x = data[top]; return true; } //栈是否空 bool SeqStack::isEmpty() { return top == -1; } //栈是否满 bool SeqStack::isFull() { return top == MaxSize - 1; } //栈实际元素个数 int SeqStack::getSize() { return top + 1; } //置栈空 void SeqStack::makeEmpty() { top = -1; } //由栈顶到栈底依次输出栈元素 void SeqStack::print() { for (int i = top; i >= 0; i--) { cout << data[i] << " "; } cout << endl; } //菜单 void menu() { cout << "1.进栈" << endl; cout << "2.出栈" << endl; cout << "3.获取栈顶元素" << endl; cout << "4.栈置空" << endl; cout << "5.由栈顶到栈底依次输出栈元素" << endl; cout << "6.退出" << endl; } //执行菜单功能 void function(int num, SeqStack *ss) { switch (num) { int x; case 1: cout << "请输入要进栈的元素:"; cin >> x; ss->push(x); cout << "进栈成功!" << endl; break; case 2: if (ss->pop(x)) { cout << "出栈成功,出栈的元素为:" << x << endl; } else { cout << "出栈失败,栈为空!" << endl; } break; case 3: if (ss->getTop(x)) { cout << "栈顶元素为:" << x << endl; } else { cout << "栈为空!" << endl; } break; case 4: ss->makeEmpty(); cout << "栈已置空!" << endl; break; case 5: ss->print(); break; default: exit(0); } }

    int main(int argc, char** argv) { SeqStack *ss = new SeqStack; int num; while (true) { menu(); cin >> num; function(num, ss); } delete ss; return 0; }

  3. User

    编写C++程序实现栈的链式存储(链栈LinkStack),使其实现与代码一中一样的基本功能

  4. Assistant

    #include using namespace std;

    typedef int T;

    //链栈节点 struct Node { T data; Node* next; };

    //链栈 class LinkStack { private: Node* top; //栈顶指针 public: LinkStack(); ~LinkStack(); void push(T x); bool pop(T& x); bool getTop(T& x); bool isEmpty(); void makeEmpty(); void print(); };

    //构造函数 LinkStack::LinkStack() { top = nullptr; }

    //析构函数 LinkStack::~LinkStack() { makeEmpty(); }

    //进栈 void LinkStack::push(T x) { Node* newNode = new Node; newNode->data = x; newNode->next = top; top = newNode; }

    //出栈 bool LinkStack::pop(T& x) { if (isEmpty()) { return false; } Node* temp = top; x = top->data; top = top->next; delete temp; return true; }

    //获取栈顶元素 bool LinkStack::getTop(T& x) { if (isEmpty()) { return false; } x = top->data; return true; }

    //栈是否空 bool LinkStack::isEmpty() { return top == nullptr; }

    //置栈空 void LinkStack::makeEmpty() { while (!isEmpty()) { Node* temp = top; top = top->next; delete temp; } }

    //由栈顶到栈底依次输出栈元素 void LinkStack::print() { Node* curr = top; while (curr != nullptr) { cout << curr->data << " "; curr = curr->next; } cout << endl; }

    //菜单 void menu() { cout << "1.进栈" << endl; cout << "2.出栈" << endl; cout << "3.获取栈顶元素" << endl; cout << "4.栈置空" << endl; cout << "5.由栈顶到栈底依次输出栈元素" << endl; cout << "6.退出" << endl; }

    //执行菜单功能 void function(int num, LinkStack* ls) { switch (num) { int x; case 1: cout << "请输入要进栈的元素:"; cin >> x; ls->push(x); cout << "进栈成功!" << endl; break; case 2: if (ls->pop(x)) { cout << "出栈成功,出栈的元素为:" << x << endl; } else { cout << "出栈失败,栈为空!" << endl; } break; case 3: if (ls->getTop(x)) { cout << "栈顶元素为:" << x << endl; } else { cout << "栈为空!" << endl; } break; case 4: ls->makeEmpty(); cout << "栈已置空!" << endl; break; case 5: ls->print(); break; default: exit(0); } }

    int main(int argc, char** argv) { LinkStack* ls = new LinkStack; int num; while (true) { menu(); cin >> num; function(num, ls); } delete ls; return 0; }

  5. User

    将此代码整理排版

  6. Assistant

    #include using namespace std;

    typedef int T;

    //链栈节点 struct Node { T data; Node* next; };

    //链栈 class LinkStack { private: Node* top; //栈顶指针 public: LinkStack(); ~LinkStack(); void push(T x); bool pop(T& x); bool getTop(T& x); bool isEmpty(); void makeEmpty(); void print(); };

    //构造函数 LinkStack::LinkStack() { top = nullptr; }

    //析构函数 LinkStack::~LinkStack() { makeEmpty(); }

    //进栈 void LinkStack::push(T x) { Node* newNode = new Node; newNode->data = x; newNode->next = top; top = newNode; }

    //出栈 bool LinkStack::pop(T& x) { if (isEmpty()) { return false; } Node* temp = top; x = top->data; top = top->next; delete temp; return true; }

    //获取栈顶元素 bool LinkStack::getTop(T& x) { if (isEmpty()) { return false; } x = top->data; return true; }

    //栈是否空 bool LinkStack::isEmpty() { return top == nullptr; }

    //置栈空 void LinkStack::makeEmpty() { while (!isEmpty()) { Node* temp = top; top = top->next; delete temp; } }

    //由栈顶到栈底依次输出栈元素 void LinkStack::print() { Node* curr = top; while (curr != nullptr) { cout << curr->data << " "; curr = curr->next; } cout << endl; }

    //菜单 void menu() { cout << "1.进栈" << endl; cout << "2.出栈" << endl; cout << "3.获取栈顶元素" << endl; cout << "4.栈置空" << endl; cout << "5.由栈顶到栈底依次输出栈元素" << endl; cout << "6.退出" << endl; }

    //执行菜单功能 void function(int num, LinkStack* ls) { switch (num) { int x; case 1: cout << "请输入要进栈的元素:"; cin >> x; ls->push(x); cout << "进栈成功!" << endl; break; case 2: if (ls->pop(x)) { cout << "出栈成功,出栈的元素为:" << x << endl; } else { cout << "出栈失败,栈为空!" << endl; } break; case 3: if (ls->getTop(x)) { cout << "栈顶元素为:" << x << endl; } else { cout << "栈为空!" << endl; } break; case 4: ls->makeEmpty(); cout << "栈已置空!" << endl; break; case 5: ls->print(); break; default: exit(0); } }

    int main(int argc, char** argv) { LinkStack* ls = new LinkStack; int num; while (true) { menu(); cin >> num; function(num, ls); } delete ls; return 0; }