单循环链表

list.h
#ifndef _LIST_H
#define _LIST_H

#include"utili.h"

typedef int ElemType;

template<class Type>
class List;

template<class Type>
class ListNode
{
friend class List<Type>;
public:
	ListNode():data(Type()),next(NULL)
	{}
	ListNode(const Type d,ListNode<Type> *n = NULL):data(d),next(n)
	{}
	~ListNode()
	{}
private:
	Type data;
	ListNode<Type> *next;
};

template<class Type>
class List
{
public:
	List()
	{
		first = last = new ListNode<Type>;
		last->next = first;
		size = 0;
	}
	~List()
	{
		
	}
public:
	void push_back(const Type &x)			//后插函数
	{
		ListNode<Type> *s = new ListNode<Type>(x);
		last->next = s;
		s->next = first;

		last = s;
		++size;
	}
	void push_front(const Type &x)			//前插函数
	{
		ListNode<Type> *s = new ListNode<Type>(x);
		s->next = first->next;
		first->next = s;
		if(size == 0)
			last = s;
		++size;
	}
	void show_list()						//打印函数
	{
		ListNode<Type> *s = first->next;
		while(s != first)
		{
			cout<<s->data<<"-->";
			s = s->next;
		}
		cout<<"Over!"<<endl;
	}
	bool Is_Empty()							//判空
	{
		if(first->next == first)
			return true;
		else
			return false;
	}
	
	bool pop_bacK()							//尾删函数
	{
		if(Is_Empty() == true)
			return false;
		ListNode<Type> *p = first;
		while(p->next != last)
			p = p->next;
		p->next = first;
		delete last;
		last = p;
		--size;
		return true;
	}
	bool pop_front()						//头删函数
	{
		if(Is_Empty() == true)
			return false;
		ListNode<Type> *p = first->next;
		first->next = p->next;
		delete p;
		if(p == last)
			last = first;
		--size;
		return true;
	}
	bool insert(const Type &x)				//按值插入
	{
		ListNode<Type> *p = first;
		while(p->next != first && p->next->data <x)
			p = p->next;
		ListNode<Type> *s = new ListNode<Type>(x);
		if(s == NULL)
			return false;
		if(p->next == first)
			last = s;
		s->next = p->next;
		p->next = s;
		++size;
		return true;
	}
	ListNode<Type>* find(const Type &key)	//查找函数
	{
		ListNode<Type> *p = first->next;
		while(p != first && p->data != key)
			p = p->next;
		if(p == first)
			return NULL;
		else
			return p;
	}
	bool erase(const Type &key)				//删除值函数
	{
		if(Is_Empty())
			return false;
		ListNode<Type> *p = first;
		while(p->next != first && p->next->data != key)
			p = p->next;
		ListNode<Type> *q = p->next;
		p->next = p->next->next;
		delete q;
		q = NULL;
		--size;
		return true;
	}
	bool insert(ListNode<Type> *s)			//按结点插入
	{
		ListNode<Type> *p = first;
		while(p->next != first && p->next->data < s->data)
			p = p->next;
		if(p->next == first)
			last = s;
		s->next = p->next;
		p->next = s;
		return true;
	}
	/*
	bool insert(const Type &x)				//按值插入
	{
		ListNode<Type> *p = first;
		while(p->next != first && p->next->data <x)
			p = p->next;
		ListNode<Type> *s = new ListNode<Type>(x);
		if(p->next == first)
			last = s;
		s->next = p->next;
		p->next = s;
		++size;
		return true;
	}
	*/
	void sort()							//排序函数
	{
		if(Is_Empty())
			return;
		ListNode<Type> *p = first->next,*t = p->next;
		last = p;
		last->next = first;

		while(t != first)
		{
			p = t;
			t = p->next;
			insert(p);
		}
	}
	void clear()							//清空函数
	{
		if(size == 0)
			return;
		ListNode<Type> *p = first->next;
		while(p != first)
		{
			first->next = p->next;
			delete p;
			p = first->next;
		}
		first->next = first;
		last = first;
		size = 0;
	}
	void reverse()						//转置函数
	{
		if(Is_Empty())
			return;
		ListNode<Type> *p = first->next,*t = p->next;
		last = p;
		last->next = first;

		while(t != first)
		{
			p = t;
			t = p->next;
			push_front(p->data);
			delete p;
		}
	}
private:
	ListNode<Type> *first;
	ListNode<Type> *last;
	size_t			size;
};
#endif


utili.h
#ifndef _UTILI_H
#define _UTILI_H

#include<iostream>
using namespace std;

#endif

main.cpp
#include"list.h"

int main()
{
	List<int> mylist;
	int select = 1;
	ElemType Item;
	int postion;
	while(select)
	{
		cout<<"******************************************"<<endl;
		cout<<"[1]push_back       [2]push_front         *"<<endl;
		cout<<"[3]show_list       [0]quit_system        *"<<endl;
		cout<<"[4]pop_back        [5]pop_front          *"<<endl;
		cout<<"[6]insert_val      [7]delete_val         *"<<endl;
		cout<<"[8]find            [9]clear              *"<<endl;
		cout<<"[10]sort           [11]reverse           *"<<endl;
		cout<<"[12]ListEmpty      [13]ListLength        *"<<endl;
		cout<<"[14]GetElem        [15]LocateElem        *"<<endl;
		cout<<"******************************************"<<endl;
		cout<<"input select:->";
		cin>>select;
		switch(select)
		{
		
		case 1:
			cout<<"input data(-1 Over):>";
			while(cin>>Item,Item != -1)
				mylist.push_back(Item);
			break;
		case 2:
			cout<<"input data(-1 Over):>";
			while(cin>>Item,Item != -1)
				mylist.push_front(Item);
			break;
		case 3:
			mylist.show_list();
			break;
		/*
		case 4:
			if(mylist.pop_back() == false)
				cout<<"删除失败!"<<endl;
			else
				cout<<"删除成功!"<<endl;
			break;
		*/
		case 5:
			if(mylist.pop_front() == false)
				cout<<"删除失败"<<endl;
			else
				cout<<"删除成功"<<endl;
			break;
		case 6:
			cout<<"请输入要插入的值:>";
			cin>>Item;
			mylist.insert(Item);
			break;
		case 7:
			cout<<"请输入要删除的值:"<<endl;
			cin>>Item;
			if(mylist.erase(Item) == false)
				cout<<"删除失败"<<endl;
			else
				cout<<"删除成功"<<endl;
			break;
		case 8:
			cout<<"请输入要查找的值:>";
			cin>>Item;
			if(NULL == mylist.find(Item))
				cout<<"查无此值!"<<endl;
			else
				cout<<"地址为:"<<mylist.find(Item)<<endl;
			break;
		case 9:
			mylist.clear();
			break;
		case 10:
			mylist.sort();
			break;
		case 11:
			mylist.reverse();
			break;
		/*
		case 12:
			if(mylist.ListEmpty() == true)
				cout<<"线性表为空"<<endl;
			else
				cout<<"线性表不为空"<<endl;
			break;
		case 13:
			cout<<"线性表的长度为:"<<mylist.ListLength()<<endl;
		case 14:
			cout<<"请输入要查找的位置:>";
			cin>>postion;
			if(mylist.GetElem(postion,Item) == false)
				cout<<"查无此值"<<endl;
			else
				cout<<"查到了"<<Item<<endl;
			break;
		case 15:
			cout<<"输入要查找的数:";
			cin>>Item;
		if(mylist.LocateElem(Item,compare) == 0)
				cout<<"没有找到满足的位置"<<endl;
			else 
				cout<<"找到了位置"<<endl;
		*/
		default:
			break;
		}
	#define _TIME_
	#define _DATE_

	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值