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

【数据结构】用C++实现单循环链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)

时间:2015-05-22 22:39:18      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:数据结构   c++   单循环链表   操作   类模板   

//头文件

#ifndef _LIST_H
#define _LIST_H


#include<iostream>
using namespace std;


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)
	{}
	~ListNode()
	{}


private:
	Type data;
	ListNode<Type> *next;
};


template<class Type>
class CList
{
public:
	CList()
	{
		first = last = Buynode();
		last->next = first;
	}
	~CList()
	{
		destroy();
	}
public:




	void push_back(const Type &x)            //尾插``
	{
		ListNode<Type> *s = Buynode(x);
		last->next = s;
		last = s;
		last->next = first;
		first->data++;
	}




	void push_front(const Type &x)          //头插``
	{
		ListNode<Type> *s = Buynode(x);
		s->next = first->next;
		first->next = s;
		last = s;
		for (int i = 0; i < first->data; i++)
		{
			last = last->next;
		}
		first->data++;
	}


	void pop_back()                 //尾删``
	{
		if (first->data == 0)
			return;
		ListNode<Type> *s = first;
		for (int i = 1; i < first->data; i++)
		{
			s = s->next;
		}
		delete s->next;
		s->next = first;
		last = s;
		first->data--;
	}


	void pop_front()                //头删``
	{
		if (first->data == 0)
		{
			return;
		}
		if (first->data == 1)
		{
			pop_back();
		}
		else
		{
			ListNode<Type> *s = first->next;
			first->next = s->next;
			delete s;
			first->data--;
		}
	}




	void insert_val(const Type &x)          //按值插入``
	{
		ListNode <Type> *p = Buynode(x);
		ListNode <Type> *s = first;
		if (first->data == 0)
		{
			push_back(x);
			return;
		}
		while (s->next->data<x&&s->next->next != first)
		{
			s = s->next;
		}
		if (s->next->next == first)
		{
			if (s->next->data>x)
			{
				p->next = s->next;
				s->next = p;
				first->data++;
			}
			else
				push_back(x);
		}
		else
		{
			p->next = s->next;
			s->next = p;
			first->data++;
		}


	}




	void show_list()                //显示``
	{


		ListNode<Type> *q = first->next;
		while (q!=first)
		{
			cout << q->data << "->";
			q = q->next;
		}
		cout << "NULL  first->data=" << first->data <<"  first="<<first<< " last->next=" << last->next << endl;
	}


	void sort()                     //排序``
	{
		if (first->data == 0)
		{
			return;
		}
		ListNode<Type> *p = first->next;
		ListNode<Type> *q = p->next;
		p->next = first;
		last = p;
		first->data = 1;
		ListNode<Type> *r;
		while (q != first)
		{
			insert_val(q->data);
			r = q;
			q = q->next;
			delete r;
		}
	}




	ListNode<Type>* find(const Type &x)              //查找``
	{
		ListNode<Type> *s;
		for (s = first->next; s != first; s = s->next)
		{
			if (s->data == x)
			{
				return s;
			}
		}
		return NULL;
	}


	int length()                          //求长度``
	{
		return(first->data);
	}


	void delete_val(const Type &x)       //按值删除``
	{
		if (first->data == 0)
		{
			cout << "未找到该数:" << endl;
			return;
		}
		else
		{


			ListNode<Type> *p = find(x);
			if (p == NULL)
			{
				cout << "未找到该数:" << endl;
				return;
			}
			ListNode<Type> *q = first;
			while (q->next != p)
			{
				q = q->next;
			}
			if (q->next->next == first)
			{
				pop_back();
			}
			else
			{
				q->next = p->next;
				delete p;
				first->data--;
			}
		}
	}


	void clear()                       //清除``
	{
		if (first->data == 0)
			return;
		while (first->next != first)
		{
			pop_back();
		}
	}


	void resver()                       //逆序``
	{
		if (first->data == 0)
		{
			return;
		}
		ListNode<Type> *p = first->next;
		ListNode<Type> *q = p->next;
		p->next = first;
		last = p;
		first->data = 1;
		ListNode<Type> *r;
		while (q != first)
		{
			push_front(q->data);
			r = q;
			q = q->next;
			delete r;
		}
	}


	void quit_system(int &a)               //退出``
	{
		clear();
		a = 0;
	}


	void destroy()              //摧毁``
	{
		clear();
		delete first;
	}
protected:
	ListNode<Type>* Buynode(Type x = Type())
	{
		ListNode<Type> *p = new ListNode<Type>(x);
		return p;
	}
private:
	ListNode<Type> *first;
	ListNode<Type> *last;
};




#endif

//主函数
#include"CList.h"


void main()
{
	CList<int> mylist;
	int select = 1;
	int Item;
	//int pos;
	while (select)
	{
		cout << "**************************************" << endl;
		cout << "* [1] show_list       [2] push_front *" << endl;
		cout << "* [3] push_back       [4] pop_front  *" << endl;
		cout << "* [5] pop_back        [6] insert_val *" << endl;
		cout << "* [7] find            [8] delete_val *" << endl;
		cout << "* [9]length           [10] clear     *" << endl;
		cout << "* [11] quit_system    [12] destroy   *" << endl;
		cout << "* [13] resver         [14] sort      *" << endl;
		cout << "**************************************" << endl;
		cout << "请选择:>";
		cin >> select;
		switch (select)
		{
		case 1:
			mylist.show_list();
			break;
		case 2:
			cout << "请输入要插入的值(-1结束):>";
			while (cin >> Item, Item != -1)
			{
				mylist.push_front(Item);
			}
			break;
		case 3:
			cout << "请输入要插入的值(-1结束):>";
			while (cin >> Item, Item != -1)
			{
				mylist.push_back(Item);
			}
			break;
		case 4:
			mylist.pop_front();
			;
			break;
		case 5:
			mylist.pop_back();
			break;
		case 6:
			cout << "请输入要插入的值:>";
			cin >> Item;
			mylist.insert_val(Item);
			break;
		case 7:
			cout << "请输入要查找的数:";
			cin >> Item;
			cout << "该数的地址为:";
			cout << mylist.find(Item) << endl;
			break;
		case 8:
			cout << "请输入要删除的值:>";
			cin >> Item;
			mylist.delete_val(Item);
			break;


		case 9:
			cout << "该顺序表长度为:" << mylist.length() << endl;
			break;
		case 10:
			mylist.clear();
			break;
		case 11:
			mylist.quit_system(select);
			break;
		case 12:
			mylist.destroy();
		case 13:
			mylist.resver();
			break;
		case 14:
			mylist.sort();
			break;
		default:
			break;
		}
	}
	return;
}
<img src="http://img.blog.csdn.net/20150522192337983?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<img src="http://img.blog.csdn.net/20150522192343396?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />




【数据结构】用C++实现单循环链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)

标签:数据结构   c++   单循环链表   操作   类模板   

原文地址:http://blog.csdn.net/doudouwa1234/article/details/45920573

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