#include<iostream>
using namespace std;
const int increase=20;
template<class T>
class seqstack{
private:
T*elements;
int top;
int max;
void overflow();
public:
seqstack(int m){
top=-1;
max=m;
T*elements=new T[max];
}
void push(T x){
if(top==max-1) overflow();
elements[++top]=x;
}
void input(int n){
T a;
while(n){
cin>>a;
(*this).push(a);
n--;
top++;
}
}
bool pop(T&x){
if(top==-1) return false;
else{
x=elements[top--];
return true;
}
}
friend ostream& operator<<(ostream& out,seqstack<T>& b);
};
template<class T>
ostream& operator<<(ostream& out,seqstack<T>& b){
out<<"top="<<b.top<<endl;
int i;
for(i=0;i<=b.top;i++)
out<<b.elements[i]<<" ";
return out;
}
template<class T>
void seqstack<T>::overflow(){
T*d=new T[max+increase];
int i;
for(i=0;i<=top;i++){
d[i]=elements[i];
}
delete[]elements;
elements=d;
}
int main(){
int n;
cin>>n;
seqstack<int> s(50);
s.input(n);
int a;
s.pop(a);
int m;
s.push(m);
cout<<s;
return 0;
}
uj5u.com熱心網友回復:
什么問題?編譯錯誤還是運行錯誤uj5u.com熱心網友回復:
編譯錯誤了。uj5u.com熱心網友回復:
編譯錯誤了。
uj5u.com熱心網友回復:
template<typename T>friend ostream& operator<<(ostream& out,seqstack<T>& b);
uj5u.com熱心網友回復:
還是不可以哎,還是編譯有問題。
uj5u.com熱心網友回復:
#include <iostream>
#include<iostream>
using namespace std;
const int increase = 20;
template<class T>
class seqstack {
private:
T* elements;
int top;
int max;
void overflow();
public:
seqstack(int m) {
top = -1;
max = m;
T* elements = new T[max];
}
void push(T x) {
if (top == max - 1) overflow();
elements[++top] = x;
}
void input(int n) {
T a;
while (n) {
cin >> a;
(*this).push(a);
n--;
top++;
}
}
bool pop(T& x) {
if (top == -1) return false;
else {
x = elements[top--];
return true;
}
}
template<typename T>
friend ostream& operator<<(ostream& out, seqstack<T>& b);
};
template<class T>
ostream& operator<<(ostream& out, seqstack<T>& b) {
out << "top=" << b.top << endl;
int i;
for (i = 0; i <= b.top; i++)
out << b.elements[i] << " ";
return out;
}
template<class T>
void seqstack<T>::overflow() {
T* d = new T[max + increase];
int i;
for (i = 0; i <= top; i++) {
d[i] = elements[i];
}
delete[]elements;
elements = d;
}
int main() {
int n;
cin >> n;
seqstack<int> s(50);
s.input(n);
int a;
s.pop(a);
int m = 10;
s.push(m);
cout << s;
return 0;
}
自己找不同吧
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/66130.html
標籤:C++ 語言
上一篇:求助大佬 萬分感謝
