單鏈表(c++帶頭結點,)

<pre name="code" class="cpp">#ifndef _SEQLIST_
#define _SEQLIST_
#include<iostream>
using namespace std;
#include<assert.h>

template<class Type> class Slist;


//節點類
template<class Type>
class Node
{
public:
	Node() :data(Type()), next(NULL){}
	Node(Type d, Node<Type> *n = NULL) :data(d), next(n){}
	void SetData(Type d)
	{
		data = d;
	}
	Type GetData()const
	{
		return data;
	}
	~Node(){}
private:
	Type data;
	Node <Type> *next;
	friend class Slist< Type>;
};
//
template<class Type>
class Slist
{
public:
	Slist( )
	{
		Node<Type> *s= _Buynode(0);
		 first = last = s;
		size = 0;
	}
	~Slist()
	{	 
	}
	Node<Type>* _Buynode(const Type &x)
	{
		 Node<Type>* s = new  Node<Type>(x);
		assert(s!=NULL);
		return s;
	}
private:
	Node<Type> *first;
	Node <Type>*last;
	int size;
	
public:
	bool push_back(const Type &x)
	{
		Node<Type>* s = _Buynode(x);
		last->next = s;
		last = s;
		size++;
		return true;
	}
	bool push_front(const Type &x)
	{
		Node<Type>* s = _Buynode(x);
		s->next = first->next;
		first->next = s;
		if (size ==0)//  0
		{
			last = s;
		}
		size++;
		return true;
	}
	bool pop_back()
	{
		if ( size==0 )//爲什麼寫成(0==size),下面調用出錯;
			return false;
		{
				Node<Type>* p = first ;
			Node<Type>* q = p->next;
				while (q->next != NULL)
				{
					q = q->next;
					p = p->next;
				}
				delete last;
				last = p;
				last->next = NULL;//程序崩潰;
				size--;
			 
		}
		return true;
	}
	bool pop_front()
	{
		if (size == 0)
			return false;
		if (size == 1)
	 
			pop_back();
		 
		else
		{
			Node<Type>* s = first->next;
			first->next = s->next;
			delete s;
			size--;
			
		}
		return true;
	}
	/*bool insert_val2(const Type &x)
	{ 
		Node<Type>* p = first;
		while (p->next->data < x && p->next != NULL)
		 
			p =p->next;
		 
		if (p->next == NULL)
		{
			push_back(x); 
		 
		}
		else
		{
	    	Node<Type>* s = _Buynode(x);
			s->next = p->next;
			p->next = s;
			size++;
          
		}
		 return true;
	}
	*/
	bool insert_val(const Type &x)
	{
		if (size == 0)
			push_back(x);
		 Node<Type> *p = first;
		while (p->next != NULL && p->next->data < x)
			p = p->next;
		if (p->next == NULL)
		{
			push_back(x);
		}
		else
		{
			 Node<Type> *s = _Buynode(x);
			s->next = p->next;
			p->next = s;
			size++;
		}
		return true;
	}

	Type length()
	{
		return size;
	}

 Node<Type>* find(const Type &key) 
	{
		if (size == 0)
			return NULL;
		 Node<Type> *p = first->next;
		while (p != NULL && p->data != key)
			p = p->next;
		return p;
	}
 bool delete_val(const Type &x)
 {
	 if (size == 0)
		 return false;
	 Node<Type>*p = find(x);
	 if (p == NULL)
		 return false;
	 if (p == last)
	 {
		 pop_back();
	 }
	 else
	 {
		 Node<Type>*q = p->next;
		 p->data = q->data;
		 p->next = q->next;
		 delete q;
		 size--;
	 }
	 return true;
 }
void sort()
 {
	 if (size == 0 || size == 1)
		 return  ;
	 Node<Type>* s= first->next;
	 Node<Type>* q =s->next;
	 last = s;
	 s->next = NULL;
	 while (q != NULL)
	 {
		 s = q;
		 q = q->next;
		 Node<Type>*p = first;
		 while ( p->next != NULL && p->next->data < s->data )
			 p = p->next;
		 if (p->next == NULL)
		 {
			 s->next = NULL;
			 last->next = s;
			 last = s;
		 }
		 else
		 {
			 s->next = p->next;
			 p->next = s;
		 }
	 
	 }

 }
void resver()
{
	if (size == 0 || size == 1)
		return;
	Node<Type>* s = first->next;
	Node<Type>* q = s->next;
	last = s;
	s->next = NULL;
	while (q != NULL)
	{
		s = q;
		q = q->next;
		push_front(s->data);
	}
}

bool next(const Type &x)
{

	Node<Type>*p=find(x);
	if (p == NULL)
	{
		cout << "未能找到" << x << endl;
		return false;
	}
	Node<Type>*q = first;

	while (q->next!= NULL&&q->data!=x)
	{
		q = q->next;
	}
	if (q->next == NULL)
	{
		cout << x << "沒有後繼" << endl;
		return false;
	}
	if (q->data = x)
	 
		cout << x << "的後繼是" << q->next->data << endl;
		return  true;
	  
}
bool prio(const Type&x)
{

	Node<Type>*p = find(x);
	if (p == NULL)
	{
		cout << "未能找到" << x << endl;
		return false;
	}
	Node<Type>*q = first;

	while ( q->data != x)
	{
		q = q->next;
	}
	if (q  == first->next)
	{
		cout << x << "沒有前驅" << endl;
		return false;
	}
	if (q->data = x)
	{
		Node<Type>*s = first;
		while (s->next != q)
		{
			s = s->next;
		}
		cout << x << "的前驅是" << s->data << endl;
	}
	return  true;

}
void  clear()
{
	Node<Type>*p = first->next;
	while (p != NULL)
	{
		first->next= p->next;
		delete p;
		p = first->next;
	}
	first->next = NULL;
	last = first;
	size = 0;
  
}
 void show_list()
	{
	 if (size == 0)
		 return;
		Node<Type> * p =  first->next;
		while (p != NULL )
		{
			cout << p->data << "-->";
				p=p->next;
		}
		cout << "ok" << endl;
	}
};

#endif

#include"SeqList.h"void main(){Slist<int> mylist;int Item;int select = 1;Node<int>* p;while (select){cout << "************************************" << endl;cout << "* [1] push_back [2] push_front *" << endl;cout << "* [3] show_list [4] pop_back *" << endl;cout << "* [5] pop_fornt [6] insert_val *" << endl;cout << "* [7] length [8] find *" << endl;cout << "* [9] merge [10] delete_val*" << endl;cout << "* [11] sort [12] resver *" << endl;cout << "* [13] next [14] prio *" << endl;cout << "* [15] clear [0] quit_system*" << endl;cout << "************************************" << endl;cout << "請選擇服務項目:>";cin >> select;switch (select){case 1:cout << "請輸入要插入的數據(-1結束):>";while (cin >> Item, Item!= -1){mylist.push_back(Item);}break;case 2:cout << "請輸入要插入的數據(-1結束):>";while (cin >> Item, Item != -1){mylist.push_front(Item);}break;case 3: mylist.show_list(); break;case 4:mylist.pop_back();break;case 5:mylist.pop_front();break;case 6:cout << "請輸入要插入的值:>";cin >> Item;mylist.insert_val(Item);break;case 7:cout << "順序表的長度爲:>" << mylist.length() << endl;break;case 8:cout << "請輸入要查找的值:>";cin >> Item; p = mylist.find(Item); if (p == NULL){cout << "要查找的數據不存在." << endl;}break;case 9:break;case 10:cout << "請輸入要刪除的值:>";cin >> Item;mylist.delete_val(Item);break;case 11:mylist.sort();break;case 12:mylist.resver();break;case 13:cout << "請輸入要查後繼的數";cin >> Item;mylist.next(Item);break;case 14:cout << "請輸入要查前驅的數";cin >> Item;mylist.prio(Item);break;case 15:mylist.clear();break;default:break;} }system("pause");}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章