静态数据成员static的问题

之前写了一个简易的栈:
简单实现一个栈
在这里面想要让头指针head为整个类共有,但问题就出现在这里,之后无论我在哪里再次重新定义一个栈,头指针head都指向了一个值,导致栈中的值都是一样的,那样一个程序中我就只能定义一个栈

测试代码(测试在不同位置定义一个int类型的栈,这些栈的值是否是同一个):

void f(void) {                                  //在其它函数中定义一个栈stack2
	Stack<int> stack2;
	cout << endl << "f-stack2:" << endl;        //输出stack2的值
	for (int i = 0; i < 10; ++i) {
		cout << stack2.top() << ends;
		stack2.pop();
	}
}

int main() {
	Stack<int> stack;                           //在main函数中定义一个栈stack
	for (int i = 1; i < 11; ++i) {stack.push(i);}//将数据压入stack栈
	cout << "main-stack:" << endl;                   //输出stack栈中的值
	for (int i = 0; i < 10; ++i) {
		cout << stack.top() << ends;
		stack.pop();
	}

	for (int i = 1; i < 11; ++i) { stack.push(i); }//将数据压入stack栈
	Stack<int> stack1;                               //在main函数中定义一个栈stack1
	cout << endl << "main-stack1:" << endl;           //输出stack1栈的值
	for (int i = 0; i < 10; ++i) {
		cout << stack1.top() << ends;
		stack1.pop();
	}

	for (int i = 1; i < 11; ++i) { stack.push(i); }//将数据压入stack栈
	f();
	return 0;
}

由输出可知,这三个栈确实是同一个值
输出结果

解决办法(将数据储存功能移出Stack类,去掉静态数据成员head,改用指向储存数据的结构体的指针head代替):

template<class Type>
struct List {             //结构体链表
	Type val;
	List* next;

	List(Type date) : val(date), next(0) {};
	~List() { delete next; }
};

template<class Type>         //定义模板类
class Stack
{
private:
	List<Type>* head;     //指向表头的指针

public:
	Stack() :head(NULL) {}
	~Stack() {
		delete head;
	}

	void push(Type);
	Type top();
	Type pop();
	bool empty();
};

template<class Type>
void Stack<Type>::push(Type date) {
	List<Type>* temp = new List<Type>(date);   //新建节点
	temp->next = head;         //将新建节点连接到表头
	head = temp;               //更新表头指针
}

template<class Type>
Type Stack<Type>::top() {
	if (!head) { throw - 1; }//表头指针为空则结束程序

	return head->val;
}

template<class Type>
Type Stack<Type>::pop() {
	if (!head) { throw - 1; }//表头指针为空时结束程序

	Type re = head->val;
	head = head->next;
	return re;
}

template<class Type>
bool Stack<Type>::empty(){
	return head;
}

测试空栈时进行一些非法操作

struct temp
{
	int t;
	temp(int a) :t(a) {}
	temp(void):t(0) {}
};

int main() {
	Stack<temp> stack;
	cout << stack.top().t << endl;//测试head为空时
	return 0;
}

空栈输出栈顶元素结束程序
测试自定义类型的出栈入栈

struct temp
{
	int t;
	temp(int a) :t(a) {}
	temp(void):t(0) {}
};

int main() {
	Stack<temp> stack;
	//cout << stack.top().t << endl;//测试head为空时
	for (int i = 1; i < 5; ++i) {
		temp* test = new temp(i);
		stack.push(*test);       //测试入栈
		cout << stack.top().t << endl;//测试栈顶元素
	}
	for (int i = 1; i < 5; ++i) {
		cout << stack.top().t << endl;//测试栈顶元素
		stack.pop();             //测试出栈
	}
	return 0;
}

自定义类型出栈入栈正常

测试一个程序中定义多个同一类型的栈

void f(void) {                                  //在其它函数中定义一个栈stack2
	Stack<int> stack2;
	cout << endl << "f-stack2:" << endl;        //输出stack2的值
	for (int i = 0; i < 10; ++i) {
		cout << stack2.top() << ends;
		stack2.pop();
	}
}

int main() {
	Stack<int> stack;                           //在main函数中定义一个栈stack
	for (int i = 1; i < 11; ++i) {stack.push(i);}//将数据压入stack栈
	cout << "main-stack:" << endl;                   //输出stack栈中的值
	for (int i = 0; i < 10; ++i) {
		cout << stack.top() << ends;
		stack.pop();
	}

	for (int i = 1; i < 11; ++i) { stack.push(i); }//将数据压入stack栈
	Stack<int> stack1;                               //在main函数中定义一个栈stack1
	cout << endl << "main-stack1:" << endl;           //输出stack1栈的值
	for (int i = 0; i < 10; ++i) {
		cout << stack1.top() << ends;
		stack1.pop();
	}

	for (int i = 1; i < 11; ++i) { stack.push(i); }//将数据压入stack栈
	f();
	return 0;
}

这里因为将数据压入stack栈,所以stack1栈为空,输出空栈stack1的栈顶元素导致异常

将数据分别压入stack1,stack2栈

void f(void) {                                  //在其它函数中定义一个栈stack2
	Stack<int> stack2;
	for (int i = 100; i < 121; ++i) { stack2.push(i); }//将数据压入stack2栈
	cout << endl << "f-stack2:" << endl;        //输出stack2的值
	for (int i = 0; i < 10; ++i) {
		cout << stack2.top() << ends;
		stack2.pop();
	}
}

int main() {
	Stack<int> stack;                           //在main函数中定义一个栈stack
	for (int i = 1; i < 11; ++i) {stack.push(i);}//将数据压入stack栈
	cout << "main-stack:" << endl;                   //输出stack栈中的值
	for (int i = 0; i < 10; ++i) {
		cout << stack.top() << ends;
		stack.pop();
	}

	Stack<int> stack1;                               //在main函数中定义一个栈stack1
	for (int i = 11; i<20; ++i) { stack1.push(i); }    //将数据压入stack1栈
	cout << endl << "main-stack1:" << endl;           //输出stack1栈的值
	for (int i = 0; i < 10; ++i) {
		cout << stack1.top() << ends;
		stack1.pop();
	}
	
	f();
	return 0;
}

正常输出
输出正常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值