【殷人昆数据结构】第二章3.2 链式栈LinkedStack代码的调试

LinkedStack链式栈

链式栈无栈满问题,空间可扩充
插入与删除仅在栈顶处执行
链式栈的栈顶在链头
适合于多栈操作

主函数

#include "LinkedStack.h"
#include <fstream>
#include <cassert>
using namespace std;
int main(){
	LinkedStack<int> sta;
	ifstream fin("data.txt");
	assert(fin);
	int data;
	while (!fin.eof()){
		assert(fin >> data);
		sta.Push(data);		
	}
	cout << "The initial Stack in the file is:\n" << sta;
	cout << "The current size of the Stack is: " << sta.getSize() << endl;
	sta.getTop(data);
	cout << "The current Top of the Stack is : " << data << endl;
	sta.Pop(data);
	cout << "\nDo a Pop operation, then the stack is:\n" << sta << endl;
	cout << "The data popped is: " << data << endl;
	sta.getTop(data);
	cout << "The current Top of the Stack is : " << data << endl;

	cout << "\nTest the state of the stack:\n";
	if (sta.IsEmpty())	cout << "The stack is empty now!\n";
	else if (sta.IsFull())	cout << "The stack is full now!\n";
		else	cout << "The stack is not empty and not full now!\n";
	sta.makeEmpty();
	cout << "Now make the stack empty, then the state of the stack is:\n";
	if (sta.IsEmpty())	cout << "The stack is empty now!\n";
	else if (sta.IsFull())	cout << "The stack is full now!\n";
		else	cout << "The stack is not empty and not full now!\n";
	return 0;
}

StackNode结点类定义

template <typename T>struct StackNode{
	T data;
	StackNode<T> *link;
	StackNode(T d = 0, StackNode<T> *next = NULL):link(next),data(d){}
};

LinkedStack类定义

template <typename T>class LinkedStack{
private:
	StackNode<T> *top;
public:
	LinkedStack():top(NULL){};
	~LinkedStack(){
		makeEmpty();
	}
	void Push(const T &x);
	bool Pop(T &x);
	bool getTop(T &x)const;	
	int getSize()const;
	bool IsEmpty()const{
		return top == NULL;
	}
	bool IsFull()const{
		return false;   //我们等会儿看看这个sb函数有没有被调用
	}
	void makeEmpty();
	friend ostream& operator << (ostream &os, LinkedStack<T> &s){
		os << "Stack Size: " << s.getSize() << endl;
		StackNode<T> *p = s.top;
		int i = 0;
		while (p){
			os << ++i << ": " << p->data << endl;
			p = p->link;
		}
		return os;
	}
};

makeEmpty函数定义

从栈链头开始删除

template <typename T>void LinkedStack<T>::makeEmpty(){
	StackNode<T> *p;
	while (top){//最后top为NULL
		p = top;
		top = top->link;
		delete p;
	}
}

Pop和Push函数

template <typename T>void LinkedStack<T>::Push(const T &x){
	top = new StackNode<T>(x, top);    //先执行赋值语句右边这个内存分配语句
	//然后移动top指针到新到结点
	assert(top);
}

template <typename T>bool LinkedStack<T>::Pop(T &x){
	if (IsEmpty()){   
		return false;   
	}
	StackNode<T> *p = top;   
	top = top->link;   
	x = p->data;   
	delete p;   
	return true;	 
}

getTop和getSize函数

template <typename T>bool LinkedStack<T>::getTop(T &x)const{
	if (IsEmpty())	return false;   
	x = top->data;   
	return true;    
}

template <typename T>int LinkedStack<T>::getSize()const{
	StackNode<T> *p = top;
	int k = 0;
	while (p){
		p = p->link;
		k++;
	}
	return k;
}

整一个头文件

#ifndef LINKEDSTACK_H
#define LINKEDSTACK_H
#include <iostream>
#include <cassert>
using namespace std;

template <typename T>struct StackNode{
	T data;
	StackNode<T> *link;
	StackNode(T d = 0, StackNode<T> *next = NULL):link(next),data(d){}
};

template <typename T>class LinkedStack{
private:
	StackNode<T> *top;
public:
	LinkedStack():top(NULL){}//ÎÞÍ·½áµã
	~LinkedStack(){
		makeEmpty();
	}
	void Push(const T &x);
	bool Pop(T &x);
	bool getTop(T &x)const;	
	int getSize()const;
	bool IsEmpty()const{
		return top == NULL;
	}
	bool IsFull()const{
		return false;
	}
	void makeEmpty();
	friend ostream& operator << (ostream &os, LinkedStack<T> &s)	{
		os << "Stack Size: " << s.getSize() << endl;
		StackNode<T> *p = s.top;
		int i = 0;
		while (p){
			os << ++i << ": " << p->data << endl;
			p = p->link;
		}
		return os;
	}
};

template <typename T>void LinkedStack<T>::makeEmpty(){
	StackNode<T> *p;
	while (top){//×îºótopΪNULL
		p = top;
		top = top->link;
		delete p;
	}
}

template <typename T>void LinkedStack<T>::Push(const T &x){
	top = new StackNode<T>(x, top);
	assert(top);
}

template <typename T>bool LinkedStack<T>::Pop(T &x){
	if (IsEmpty()){
		return false;
	}
	StackNode<T> *p = top;
	top = top->link;
	x = p->data;
	delete p;
	return true;	
}

template <typename T>bool LinkedStack<T>::getTop(T &x)const{	
	if (IsEmpty())	return false;
	x = top->data; 
	return true;
}

template <typename T>int LinkedStack<T>::getSize()const{
	StackNode<T> *p = top;
	int k = 0;
	while (p){
		p = p->link;
		k++;
	}
	return k;
}
#endif
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值