码迷,mamicode.com
首页 > 编程语言 > 详细

单链表(c++带头结点,)

时间:2015-08-29 09:47:19      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:单链表   结点   可任意插入   非循环   

<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");}


版权声明:本文为博主原创文章,未经博主允许不得转载。

单链表(c++带头结点,)

标签:单链表   结点   可任意插入   非循环   

原文地址:http://blog.csdn.net/fujinlong520/article/details/48084735

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!