数据结构(C++)——单向循环链表

#include"AllHead.h"

template<class T>
class CircList;

template<class T>
class CircListNode
{
	friend class CircList<T>;
protected:
	T data;
	CircListNode *next;
public:
	CircListNode(CircListNode<T> *ptr = NULL){next = ptr;}              //只初始化指针域的构造函数
	CircListNode(T x,CircListNode<T> *ptr = NULL){data = x;next = ptr;}//初始化数据域和指针域的构造函数
};

template<class T>
class CircList
{
private:
	CircListNode<T> *first;
	CircListNode<T> *last;
	size_t size;
public:
	CircList()
	{
		first = new CircListNode<T>;
		last = first;
		last->next = first;
		size = 0;
	}
	~CircList(){makeEmpty();}
	void makeEmpty();
	Status IsEmpty(){return first == last?TRUE:FALSE;}
	Status push_back(T x);
	void show_circlist();
	Status push_front(T x);
	size_t getlenth(){return size;}
	T getHead_val(){return first!=last?first->next->data:NULL;}
	T getLast_val(){return first!=last?last->data:NULL;}
	Status pop_back();
	Status pop_front();
	CircListNode<T>* find(T key);
	Status insert_val(T x);
	Status del_val(T x);
	void sort();
	void reverse();	
//  List(ListNode<T> &L);	
//	ListNode<T>* getFirst(){return first;}
//	List& operator=(List<T> &L);
};
template<class T>
void CircList<T>::sort()///SORT//
{
	CircListNode<T> *q = first->next;
	CircListNode<T> *h;
	CircListNode<T> *p;
	CircListNode<T> *m;
	first->next = first;
	last = first;
	while(q!= first)
	{//h和q是断开后面的指针!!!
		h = q;
		q = q->next;
	 //p和m是新的表头这边的两个指针!!!
		p = first->next;
		m = first;
		while(p != first&&(p->data)<(h->data))
		{
			m = p;
			p = p->next;
		}
		if(p == first)
	    {
		  last = h;
	    }
		m->next = h;
		h->next = p;
	}
}
template<class T>
void CircList<T>::reverse()
{
	if(IsEmpty())
	{
		cout<<"空链表无法逆置"<<endl;
	}
	CircListNode<T> *h = first->next->next;
	CircListNode<T> *q = h->next;
	CircListNode<T> *m = last;
	last = first->next;
	last->next = first;

	while(q!=first)
	{
		h->next = first->next;
		first->next = h;
		h = q;
		q = q->next;
	}
	m->next = first->next;
	first->next = m;
}
template<class T>
Status CircList<T>::del_val(T x)
{
	if(IsEmpty())
	{
		cout<<"链表已空(del_val)....."<<endl;
		return ERROR;
	}
	CircListNode<T> *q = first->next;
	CircListNode<T> *h = first;
	size_t sizemem = size;
	while(q!= first)
	{
		if(q->data == x)
		{
			if(q->next == first)
	        {
				last = h;
	        }
	        h->next = q->next;
	        delete q;
	        --size;
	    	return OK;
		}
		h = q;
		q = q->next;
	}
	if(size == sizemem)
	{
		cout<<"链表中不存在该值!"<<endl;
		return ERROR;
	}
}

template<class T>
Status CircList<T>::insert_val(T x)
{
	CircListNode<T> *p = new CircListNode<T>;
	p->data = x;
	p->next = NULL;
	CircListNode<T> *q = first->next;
	CircListNode<T> *h = first;
	while(q!= first && q->data<x)
	{
		h = q;
		q = q->next;
	}	
	if(q == first)
	{
		last = p;
	}
    p->next = q;
	h->next = p;
	++size;
	return OK;
}
template<class T>
CircListNode<T>* CircList<T>::find(T key)
{
	if(IsEmpty())
	{
		cout<<"链表为空()find..."<<endl;
	}
	CircListNode<T> *q = first->next;
	while(q != first&&q->data != key)
	{
		q=q->next;
	}
	if(q == first)
	{
		return NULL;
	}
	else
	{
		return q;
	}

}
template<class T>
Status CircList<T>::pop_front()
{
	if(IsEmpty())
	{
		cout<<"空链表不能删除(pop_front)......"<<endl;
		return ERROR;
	}
	CircListNode<T> *q = first->next;
	first->next = q->next;
	delete q;
	if(size == 1)
	{
		 last = first;
	}
	--size;
	return OK;
}
template<class T>
Status CircList<T>::pop_back()
{
	if(IsEmpty())
	{
		cout<<"空链表不能删除(pop_back)......"<<endl;
		return ERROR;
	}
	CircListNode<T> *q = first;
	while(q->next!=last)
	{
		q=q->next;
	}
	q->next = first;
	delete last;
	last = q;
	--size;
	return OK;
}
template<class T>
Status CircList<T>::push_front(T x)
{
	CircListNode<T> *s = new CircListNode<T>;
	s->data = x;
	s->next = first->next;
	first->next = s;
	if(size == 0)
	{
		last = s;
	}
	++size;
	return OK;
}
template<class T>
void CircList<T>::show_circlist()
{
	CircListNode<T> *p = first;
	while(p->next != first)
	{
		p=p->next;
		cout<<p->data<<"-->";
	}
	cout<<"NULL"<<endl;
}
template<class T>
Status CircList<T>::push_back(T x)
{
	CircListNode<T> *s = new CircListNode<T>;
	s->data = x;
	last->next = s;
	last = s;
	last->next = first;
	++size;
	return OK;
}

template<class T>
void CircList<T>::makeEmpty()
{
	if(IsEmpty())
	{
		cout<<"链表已空..."<<endl;
	}
	else
	{
		CircListNode<T> *q;
		while(first->next != first)
		{
			q = first->next;
			first->next = q->next;
			delete q;
		}
		last = first;
		size = 0;
	}
}

#if 1
#include"CircList.h"
int main()
{
    CircList<int> myls;
	CircList<int> yourls;
	int select = 1;
	while(select)
	{
		cout<<"||========================================||"<<endl;
		cout<<"||==[1]push_front    [2]push_back=======||"<<endl;
		cout<<"||==[3]show_list     [4]getlenth========||"<<endl;
		cout<<"||==[5]getHead_val    [6]getLast_val=====||"<<endl;
		cout<<"||==[7]quit                          ||"<<endl;
		cout<<"||==[9]makeEmpty     [10]pop_back=======||"<<endl;
		cout<<"||==[11]pop_front    [12]insert_val=====||"<<endl;
		cout<<"||==[13]del_val      [14]sort==========||"<<endl;
		cout<<"||==[15]reverse      [16]find_val_rid==||"<<endl;  
		cout<<"||========================================||"<<endl;
		cout<<"请输入您的选择:>";
		cin>>select;
		system("cls");
		switch(select)
		{
		case 1:
			int val_pf;
			cout<<"请输入插入的值(以-1结束):>"<<endl;
			while(cin>>val_pf,val_pf!=-1)
			{
				myls.push_front(val_pf);
			}
			break;
		case 2:
			int val_pb;
			cout<<"请输入插入的值(以-1结束):>"<<endl;
			while(cin>>val_pb,val_pb!=-1)
			{
				myls.push_back(val_pb);
			}
			break;
		case 3:
     		myls.show_circlist();
			break;
		case 4:
			size_t size;
			size = myls.getlenth();
			cout<<"循环链表长度为:"<<size<<endl;
			break;
		case 5:
			int head;
			head = myls.getHead_val();
			cout<<"该链表的第一个为:"<<head<<endl;
			break;
		case 6:
			int last;
			last = myls.getLast_val();
			cout<<"该链表的最后一个值为:"<<last<<endl;
			break;
		case 7:
			exit(1);
			break;

		case 9:
			myls.makeEmpty();
			break;
		case 10:
			myls.pop_back();
			break;
		case 11:
			myls.pop_front();
			break;
		case 12:
			int val_insert;
			cout<<"请输入您要插入的值:>";
			cin>>val_insert;
			myls.insert_val(val_insert);
			break;
		case 13:
			int val_del;
			cout<<"请输入您要删除的值:>";
			cin>>val_del;
			myls.del_val(val_del);
			break;
		case 14:
			myls.sort();
			break;
		case 15:
			myls.reverse();
			break;
		case 16:
			CircListNode<int> *key;
			int key_find;
			cout<<"请输入您要查找的值;>";
			cin>>key_find;
			key = myls.find(key_find);
			if(key != NULL)
				cout<<"找到啦!-->"<<key<<endl;
			else
				cout<<"没找到..."<<endl;


			break;
		}


	}
	return 0;
	
}
#endif

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值