c++实现数据结构2.单链表clist

头文件:clist.h

#ifndef _CLIST_H_
#define _CLIST_H_

#include<assert.h>
#include<iostream>
using namespace std;

typedef enum{ TRUE, FALSE }Status;

template<class Type>
class clist;

template<class Type>
class listnode
{
	friend class clist<Type>;
public:
	listnode() :data(Type()), next(NULL)
	{}

	listnode(Type d, listnode<Type>* n = NULL) :data(d), next(n)
	{}
private:
	Type data;
	listnode<Type> * next;
};

template<class Type>
class clist
{
public:
	clist()
	{
		listnode<Type>*s = buy_node(0);
		first = last = s;
		first->next = last;
		last->next = first;
		size = 0;
	}
	Status push_back(Type const&x)
	{
			listnode<Type>*s = buy_node(x);
			s->next = first;
			last->next = s;
			last = s;
			size++;
			return TRUE;
	}
	Status push_front(Type const&x)
	{
		listnode<Type> *s = buy_node(x);
		if (size == 0)
		{
			s->next = first;
			first->next = s;
			last = s;
			size++;
			return TRUE;
		}
		else
		{
			s->next = first->next;
			first->next = s;
				size++;
		}
		return TRUE;
	}
	void show_list()
	{
		if (size == 0)
			return;
		listnode<Type>* p = first->next;
		while (p != last->next)
		{
			cout << p->data << "-->";
			p = p->next;
	   }
		cout << "END" << endl;
	}
	Status pop_back()
	{
		if (size == 0)
			return FALSE;
		listnode<Type>* p = first->next;
		if (size == 1)
		{
			delete p;
			first->next = last;
			last->next = first;
			size--;
		}
		while (p->next != last)
			p = p->next;
		{
			p->next = last->next;
			delete last;
			last = p;
			size--;
		}
		return TRUE;
	}
	Status pop_front()
	{
		if (size == 0)
			return FALSE;
		if (size == 1)
		{
			delete (first->next);
			last->next = first;
			first->next = last;
			size--;
			return TRUE;
		}
		listnode<Type>*p = first->next;
		first->next = p->next;
		delete p;
		size--;
		return TRUE;
	}
	Status insert_val(Type const &key)
	{
	  listnode<Type>*s = buy_node(key);
	  listnode<Type>*p = first;
	  while (p->next != first && p->next->data < key)
	  {
		  p = p->next;
	  }
	  if (p->next == first)
	  {
		  s->next = first;
		  last->next = s;
		  last = s;
		  size++;
	  }
	  else
	  {
		  s->next = p->next;
		  p->next = s;
		  size++;
	  }
	  return TRUE;
	}

	int length()
	{
		return size;
	}

	listnode<Type>* find(Type const&x)
	{
		if (size == 0)
			return NULL;
		listnode<Type>* p = first;
		while (p->next != first)
		{
			if (p->next->data == x)
				return p;
			p = p->next;
		}
		return NULL;
	}
	void clear()
	{
		if (size == 0)
		{
			return;
		}
		listnode<Type> * p = first->next;
		listnode<Type> * q = p->next;
		while (q != first)
		{
			delete p;
			p = q;
			q = q->next;
		}
	}
	Status delete_val(const Type &key)
	{
		listnode<Type> * p = find(key);
		if (p == NULL)
		{
			return FALSE;
		}
		listnode<Type>* n = p->next;
		p->next = n->next;
		delete n;
		return TRUE;
	}

	void sort()
	{
		if (size == 0 || size == 1)
		{
			return;
		}
		listnode<Type>* t;
		listnode<Type>* p = first->next;
		listnode<Type>* q = p->next;
		last = p;
		p->next = first;
		while (q != first)
		{
			t = first;
			p = q;
			q = q->next;
			while (t->next != first && t->next->data < p->data)
			{
				t = t->next;
			}
			if (t->next == first)
			{
				p->next = first;
				last->next = p;
				last = p;
			}
			else
			{
				p->next = t->next;
				t->next = p;
			}
		}
	}
	void resever()
	{
		if (size == 0 || size == 1)
		{
			return;
		}
		listnode<Type> *p = first->next;
		listnode<Type> *q = p->next;
		last = p;
		last->next = first;
		while (q != first)
		{
			p = q;
			q = q->next;
			p->next = first->next;
			first->next = p;
		}
	}

	Status mesere(clist<Type> &t1, clist<Type> &t2)
	{
			t1.sort();
			t2.sort();
			listnode<Type>* p = t1.first->next;
			listnode<Type>* q = t2.first->next;
			listnode<Type>* tmp;
			while (p != t1.first && q != t2.first)
			{
				if (p->data >= q->data)
				{
					tmp = q;
					q = q->next;
					last->next = tmp;
					last = tmp;
					last->next = first;
					size++;
				}
				else
				{
					tmp = p;
					p = p->next;
					last->next = tmp;
					last = tmp;
					last->next = first;
					size++;
				}
			}
			while (p != t1.first)
			{
				tmp = p;
				p = p->next;
				last->next = tmp;
				last = tmp;
				last->next = first;
				size++;
			}
			while (q != t2.first)
			{
				tmp = q;
				q = q->next;
				last->next = tmp;
				last = tmp;
				last->next = first;
				size++; 
			}
			return TRUE;
	}

	void destroy()
	{
		clear();
		delete first;
		first = last = NULL;
	}
	listnode<Type>* proi(const Type &key)
	{
		listnode<Type> *p = find(key);
		if (p == NULL)
			return NULL;
		else
		{
			cout << p->data<<endl;
			return p;
		}
	}
	listnode<Type>* next(const Type &key)
	{
		listnode<Type> *p = find(key);
		if (p == NULL)
			return NULL;
		listnode<Type> *q = p->next;
		cout << q->next->data<<endl;
		return q->next;
	}
protected:
	listnode<Type>* buy_node(Type d)
	{
		listnode<Type> *s = new listnode<Type>(d);
		assert(s != NULL);
		return s;
	}
private:
	listnode<Type>*		first;
	listnode<Type>*		last;
	int					size;

};

#endif

实现文件:

#include"clist.h"

void main()
{
	clist<int> mylist;
	clist<int> t1;//这两个对象是用来实现功能13的,合并
	clist<int> t2;//
	int select = 1;
	int item;
	
	while (select)
	{
		cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
		cout << "&                                      &" << endl;
		cout << "& [1]  push_back   [2]  push_fornt     &" << endl;
		cout << "& [3]  show_list   [4]  pop_back       &" << endl;
		cout << "& [5]  pop_front   [6]  insert_val     &" << endl;
		cout << "& [7]  length      [8]  find           &" << endl;
		cout << "& [9]  clear       [10] delete_val     &" << endl;
		cout << "& [11] sort        [12] resever        &" << endl;
		cout << "& [13] mesere      [14] destroy        &" << endl;
		cout << "& [15] proi        [16] next           &" << endl;
		cout << "& [0]  quit_system                     &" << endl;
		cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
		cout << "please choose->";
		cin >> select;
		switch (select)
		{
		case 1:
			cout << "please cin data end with -1:" << endl;
			while (cin >> item, item != -1)
			{
				//t1.push_back(item);
				mylist.push_back(item);
			}
			break;
		case 2:
			cout << "please cin data end with -1:" << endl;
			while (cin >> item, item != -1)
			{
				//t2.push_front(item);
				mylist.push_front(item);
			}
			break;
		case 3:
			//t2.show_list();
			mylist.show_list();
			break;
		case 4:
			mylist.pop_back();
			break;
		case 5:
			mylist.pop_front();
			break;
		case 6:
			cout << "please cin the data you want insert:";
			cin >> item;
			mylist.insert_val(item);
			break;
		case 7:
			cout << mylist.length() << endl;
			break;
		case 8:
			cout << "please cin the data you want find ,it will back its address: ";
			cin >> item;
			cout << mylist.find(item) << endl;
			break;
		case 9:
			mylist.clear();
			break;
		case 10:
			cout << "please cin the data you want to dalete: ";
			cin >> item;
			mylist.delete_val(item);
			break;
		case 11:
			cout << "sort !" << endl;
			mylist.sort();
			break;
		case 12:
			mylist.resever();
			break;
		case 13:
		    mylist.mesere(t1,t2);
			break;
		case 14:
			mylist.destroy();
			break;
		case 15:
			cout << "please cin data,return its proi :";
			cin >> item;
			mylist.proi(item);
			break;
		case 16:
			cout << "please cin data,return its next :";
			cin >> item;
			mylist.next(item);
			break;
		default:
			break;
		}
	}
}


*******************************************************************************************************************************************************************************************************

代码中有些注释部分,使用时记得自己动手放开一下,因为它是用来实现某些特殊功能的,例如合并排序等等。

不可否认某些函数的效率可能不高,希望大家若有高效的实现方法,希望可以和我分享一下,大家若是有一些建议也可以提出来,大家若是觉得可以得话,可以引用一下我的

代码^_^。

*******************************************************************************************************************************************************************************************************

下面运行的截图,简单列举了一些,不一个一个列举了,我自己测试过,都能通过,大家也可以试试的:

c++实现数据结构2.单链表clist_第1张图片


你可能感兴趣的:(c++高效单链表)