c++ 学习笔记(21)链表化堆栈

#include "iostream"  
#include "cstdlib"
using namespace std;  
  
/*********************************************create class********************************************/  
#define LIST_INIT_SIZE 100  
#define LISTINCREMENT 10  
   
class ChainNode
{
	friend class chain;   //friend class,the class chain can use the param of ChainNode
private:
	int data;
	ChainNode *link;
};
  
class chain  
{  
private:  
	ChainNode *first;
	ChainNode *last;
public:  
	chain(){ first  = last = 0; } //相当于InitList  
	~chain();
	bool ListEmpty()const {return first == 0;}  //检查是否为empty  
	int Length()const;  //the length of the chain
    bool find(int num, int &x) const; //查看特定位置元素  
    int Search (const int &e);  //返回e 的位置   
    chain& ListInsert(int num, const int& e);  //插入数据元素e, 在num的位置  
    chain& ListDelete(int num, int &x);  //删除the place元素,and 赋值, 并返回这个chain  
	chain& Append(const int &e); //结尾添加元素
    void displayElem(ostream &out)const;  //遍历  //应该是输出到这个 ostream &out 里面。
	friend ostream & operator << (ostream & out, chain& rs); //友元函数的应用
};  
  
/******************************************************class functions**************************************************/  


chain::~chain()
{
	ChainNode *next;  //循环链表也是可以用的
	while(first)  //这样就可以清除所有节点。
	{
		next = first->link;
		delete first;
		first = next;
	}
}


int chain::Length()const
{
	int count = 0;
	ChainNode *next;
	next = first;
	while(next)
	{
		count++;
		if(next == last)
		return count;
		else 
			next = next->link;
	}
	return count;
}




bool chain::find(int num, int &x) const
{
	if(num > Length())
	{
		cout << "the chain is over";
		return false;
	}
	ChainNode *next;     //循环这里都不用改,因为查找单项已经完成
	next = first;
	for(;num > 0; num--)
	{
		x = next->data;
		next = next->link;
	}
	return true;
}


//寻找那个元素
int chain::Search (const int &e)
{
	int num = 1;
	ChainNode *next;
	next = first;    //实现循-
	last->link = first;		//环链表
	//next = first;   //单链表
	while(next->data != e)
	{
		if(next == first)     //单链表 next = last
		{
			cout << "1" << endl;
			return 0;
		}
		next = next->link;
		num++;
	}
		return num;
}


//delete elem
chain& chain::ListDelete(int num, int &x) 
{
	int len = Length();
	if(len == 0 || len < num)
	{
		cout << "the num is wrong";
		abort();
	}


	ChainNode *next = first;
	ChainNode *Third = new ChainNode;
	if(num == 1)   //如果是首个删除的情况
	{
		
		x = next->data;
		Third = next->link;
		delete next;
		first= Third;


		return *this;
	}   //end num== 1
	else if(num == len)  //end elem delete
	{
		for(int index = 1; index < num -1; index++)
		{
			next = next->link;
		}
			Third  =next->link;
			x = Third->data;
			delete Third;
			last = next;
			
			return *this;
	} //end num == len
	else
	{
		for(int index = 1; index < num -1; index++)
		{
			next = next->link;
		}
		Third = next->link;
		next->link = Third->link;
		x = Third->data;
		delete Third;


		return *this;
	}  //end num == middle
}


//insert elem
chain& chain::ListInsert(int num, const int& x)
{
	int len = Length();
	if(len == 0&& num != 1)   //如果位置不对,list 为空,且插入num不等于1
	{
		cout << "the num is wrong";
		abort();
	}
	else if(num == len+1)  //在末尾插入
	{
		Append(x);
	}


	ChainNode *next = first;   //在中间插入.
	for(int index = 1; index < num - 1; index++)  //index is the location of next
	{
		next = next->link;
	}
	ChainNode *Third = new ChainNode;
	Third->data = x;
	Third->link = next->link;
	next->link = Third;


	return *this;
}


//在末尾进行添加
chain& chain::Append(const int &e)
{
	ChainNode *next = new ChainNode;
	next->data = e;
	next->link = 0;
	if(first)  //非空
	{
		last->link = next;
		last = next;
	}
	else first = last = next;
	return *this;
}


//shoe the elem
void chain::displayElem(ostream &out)const
{
	int len = Length();
	int x;
	for(int i = 1; i <= len; i++)
	{
		find(i, x);
		out << x << " ";
	}
}


//friend function
ostream & operator << (ostream &out, chain &rs)
{
	rs.displayElem(out);
	return out;
}
 
/**************************************************************************************/
// 内存不足,内存异常检测
class NoMem {
public :
	NoMem(){}
};
// 使new引发NoMem异常而不是xalloc异常
void my_new_handler()
{
throw NoMem();
}
new_handler Old_Handler_=set_new_handler(my_new_handler);




//template <class T>
class LinkedStack:private chain 
{
public:
	bool IsEmpty()const
	{return chain::ListEmpty();}
	bool IsFull() const;
	int top()const
	{
		if(IsEmpty())
			abort();
		int x;
		find(1, x);
		return x;
	}
	LinkedStack & Add(const int &x)
	{ListInsert(1, x); return *this;}
	LinkedStack & Delete(int &x)
	{
		int len = Length();
		ListDelete(1, x); return *this;
	}
};


bool LinkedStack::IsFull()const
{
	try{ChainNode *p = new ChainNode;
	delete p;
	return false;}
	catch(NoMem){return true;}
}
  
/*******************************************************use the class****************************************************/  
int main()  
{  
	LinkedStack newch;
	int insert = 4;
	int get;
	newch.Add(insert);
	newch.Add(insert-1);
	newch.Add(insert-2);
	newch.Add(insert-3);
	cout << "the top: " << newch.top() << endl;
	
	int DeleteElem;
	newch.Delete(DeleteElem);
	newch.Delete(DeleteElem);


	cout << DeleteElem << endl;


    cin.get();  
    cin.get();  
    return 0;  
}  

1.链表式堆栈,对于多个堆栈复用的情况,每次定义一个堆栈,大小会进行申请,如果用不完岂不很浪费,因此链表化解决了这个问题:)




你可能感兴趣的:(C++,delete,search,Class,insert,iostream)