#pragma once
#ifndef _CLIST_H_
#define _CLIST_H_

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

template<class Type> class List;

typedef enum { FALSE, TRUE }Status;

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

template<class Type>
class List
{
public:
	List()
	{
		ListNode<Type> *s = new ListNode<Type>(0);
		assert(s != NULL);
		last = first = s;
		size = 0;
	}
	~List()
	{}
public:
	Status push_back(const Type &x)
	{
		ListNode<Type> *s = new ListNode<Type>(x);
		last->next = s;
		last = s;
		size++;
		return TRUE;
	}
	Status 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++;
		return TRUE;
	}
	Status pop_back()
	{
		if (size == 0)
		{
			return FALSE;
		}
		ListNode<Type> *p = first;
		while (p->next != last)
		{
			p = p->next;
		}
		delete last;
		last = p;
		last->next = NULL;
		size--;
		return TRUE;
	}
	Status pop_front()
	{
		if (size == 0)
		{
			return FALSE;
		}
		ListNode<Type> *p = first->next;
		first->next = p->next;
		delete p;
		if (size == 1)
		{
			last = first;
		}
		size--;
		return TRUE;
	}
	void show_List()
	{
		ListNode<Type> *p = first->next;
		while (p != NULL)
		{
			cout << p->data << "->";
			p = p->next;
		}
		cout << "NULL" << endl;
	}
	Status insert_val(const Type &x)
	{
		ListNode<Type> *p = first;
		while (p->next != NULL && p->next->data < x)
		{
			p = p->next;
		}
		if (p->next == NULL)
		{
			push_back(x);
		}
		else
		{
			ListNode<Type> *s = new ListNode<Type>(x);
			s->next = p->next;
			p->next = s;
			size++;
		}
		/*
		ListNode<Type> *s = new ListNode<Type>(x);
		ListNode<Type> *q = p->next;
		if(last->data > x)
		{
		while(q->data < x )
		{
		p = p->next;
		q = q->next;
		}
		p->next = s;
		s->next = q;
		size++;
		}
		else
		{
		push_back();
		}*/

		return TRUE;
	}
	ListNode<Type>* find(const Type &key) const
	{
		if (size == 0)
		{
			return NULL;
		}
		ListNode<Type> *p = first->next;
		while(p != NULL && p->data != key)
		{
		p = p->next;
		}
		return p;
// 		while (p != NULL)
// 		{
// 			if (p->data == key)
// 			{
// 				return p;
// 			}
// 			p = p->next;
// 		}
// 		return NULL;
	}
	Status delete_val(const Type &x)
	{
		if (size == 0)
		{
			return FALSE;
		}
		ListNode<Type> *p = find(x);
		if (p == NULL)
		{
			return FALSE;
		}
		if (p == last)
		{
			pop_back();
		}
		else
		{
			ListNode<Type> *q = p->next;
			p->data = q->data;
			p->next = q->next;
			delete q;
			size--;
			return TRUE;
		}
		return FALSE;
		/*	ListNode<Type> *p = first;
		ListNode<Type> *q = p->next;
		if(last->data == x)
		{
		pop_back();
		}
		while(q->data != x)
		{
		p = p->next;
		q = q->next;
		}
		p->next = q->next;
		return TRUE;*/
	}
	Status modify_val(const Type &x, const Type &y)
	{
		ListNode<Type> *p = find(x);
		if (p == NULL)
		{
			return FALSE;
		}
		p->data = y;
		return TRUE;
	}
	int length()
	{
		return size;
	}
	void clear()
	{
		ListNode<Type> *p = first->next;
		while (p != NULL)
		{
			first->next = p->next;
			delete p;
			p = first->next;
		}
		last = first;
		size = 0;
	}
	void destroy()
	{
		clear();
		delete first;
		first = last = NULL;
	}
	void sort()
	{
		if (size==0 || size==1)
		{
			return;
		}
		ListNode<Type> *p = first->next;
		ListNode<Type> *q = p->next;
		last = p;
		last->next = NULL;
		while (q != NULL)
		{
			p = q;
			q = q->next;

			ListNode<Type> *s = first;
			while (p->data > s->next->data && s->next != NULL)
			{
				s = s->next;
			}
			if (s->next == NULL)
			{
				p->next = NULL;
				last->next = p;
				last = p;
			}
			else
			{
				p->next = s->next;
				s->next = p;
			}
		}
	}

	void resver()
	{
		if (size==0 || size==1)
		{
			return;
		}
		ListNode<Type> *p = first->next;
		ListNode<Type> *q = p->next;
		last = p;
		last->next = NULL;
		while (q != NULL)
		{
			p = q;
			q = q->next;
			p->next = first->next;
			first->next = p;
		}
	}
	Status merge(List<Type> <1, List<Type> <2)
	{
		ListNode<Type> *p = lt1.first->next;
		ListNode<Type> *q = lt2.first->next;
		ListNode<Type> *s = first;
		while (p != NULL && q != NULL)
		{
			if (p->data < q->data)
			{
				s->next = p;
				p = p->next;
				s = s->next;
			}
			else
			{
				s->next = q;
				q = q->next;
				s = s->next;
			}
		}
		while (p != NULL)
		{
			s->next = p;
			p = p->next;
			s = s->next;
		}
		while (q != NULL)
		{
			s->next = q;
			q = q->next;
			s = s->next;
		}
		size = lt1.size + lt2.size;
		return TRUE;
	}
	ListNode<Type> *prio(ListNode<Type> *p) const
	{
		if (p == NULL || p == first->next)
		{
			return NULL;
		}
		ListNode<Type> *pr = first;
		while (pr->next != p)
		{
			pr = pr->next;
		}
		return pr;
	}
	ListNode<Type> *next(ListNode<Type> *p) const
	{
		if (p == NULL || p->next == NULL)
		{
			return NULL;
		}
		ListNode<Type> *q = first;
		while (q->next != p)
		{
			q = q->next;
		}
		return q;
	}
	ListNode<Type> *Next(const Type &x) const
	{
		if (size==0 || size==1)
		{
			return NULL;
		}
		ListNode<Type> *q = find(x);
		if (q == NULL)
		{
			return NULL;
		}
		return q->next;
	}
	ListNode<Type> *Prio(const Type &x) const
	{
		if(size==0 || size==1)
		{ 
			return NULL;
		}
		ListNode<Type> *p = first;
		ListNode<Type> *q = p->next;
		while (q != NULL)
		{
			if (q->data == x)
			{
				return p;
			}
			p = p->next;
			q = q->next;
		}
		return NULL;
	}
private:
	ListNode<Type> *first;
	ListNode<Type> *last;
	size_t         size;
};

#endif  



#include "CList.h"

void main()
{
	List<int> mylist;
	List<int> youlist;
	List<int> mergelist;

	int select = 1;
	int pos;
	int item;
	system("Color 0d");
	while (select)
	{
		cout << "************************************" << endl;
		cout << "* [0]  quit_system [1] push_back   *" << endl;
		cout << "* [2]  push_front  [3] show_list   *" << endl;
		cout << "* [4]  pop_back    [5] pop_front   *" << endl;
		cout << "* [6]  insert_val  [7] delete_val  *" << endl;
		cout << "* [8]  merge       [9] next        *" << endl;
		cout << "* [10] find       [11] sort        *" << endl;
		cout << "* [12] resver     [13] length      *" << endl;
		cout << "* [14] clear      [15] destroy     *" << endl;
		cout << "* [16] modify_val [17] prio        *" << 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:
			system("cls");
			mylist.show_List();
			system("pause");
			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 << "请输入要删除的值:>";
			cin >> item;
			mylist.delete_val(item);
			break;
		case 8:
			for (int i = 1; i < 10; i+=2)
			{
				mylist.push_back(i);
			}
			for (int i = 2; i < 10; i += 2)
			{
				youlist.push_back(i);
			}
			mergelist.merge(mylist, youlist);
			mergelist.show_List();
			break;
		case 9:
			cout << "请输入要查找的值:>";
			cin >> item;
			cout << "所在查找值的后继为:" << mylist.Next(item) << endl;
			break;
		case 10:
			cout << "请输入要查找的值:>";
			cin >> item;
			cout << "该值指针为:" << mylist.find(item) << endl;
			break;
		case 11:
			mylist.sort();
			break;
		case 12:
			mylist.resver();
			break;
		case 13:
			cout << "线性表的长度为:" << mylist.length() << endl;
			break;
		case 14:
			mylist.clear();
			break;
		case 15:
			mylist.destroy();
			break;
		case 16:
			cout << "请输入要改动的值:>";
			cin >> item;
			cout << "请输入改动后的值:>";
			cin >> pos;
			mylist.modify_val(item, pos);
			break;
		case 17:
			cout << "请输入要查找的值:>";
			cin >> item;
			cout << "所在查找值的前驱为:" << mylist.Prio(item) << endl;
			break;
		default:
			break;
		}
	}
}


执行界面例如以下:

【C++/数据结构】单链表的基本操作_ios