用c++实现 c++单链表的实现(采用模板类)


函数实现数据的插入(头插&&尾插)、删除(头删&&尾删)、查找、按值插入、按值删除、求长、单链表清除、单链表摧毁、数据的逆置以及数据排序

main函数

#include"List.h"//单链表

void main()
{
	List mylist;
	int select = 1;
	int Item;
	while(select)
	{
		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<<"请输入要删除的值:>";
			cin>>Item;
			mylist.delete_val(Item);
			break;
		case 8:
			cout<<"请输入要查找的值:>";
			cin>>Item;
			 mylist.find(Item);
			break;
        case 9:
            mylist.length();
            break;
        case 10:
            mylist.clear();
            break;
        case 11:
            mylist.destroy();
            break;
        case 12:
            mylist.reserv();
            break;
        case 13:
            mylist.sort();
		default:
			break;
		}
	}
}


List.h函数

#ifndef _LIST_H
#define _LIST_H

#include
using namespace std;

template
class List;

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

template
class List
{
public:
	List()
	{
		first = last = Buynode();
	}
	~List()
	{
		//destroy();
	}
public:
	void push_back(const Type &x);
	void push_front(const Type &x);
	void show_list();
	void pop_back();
	void pop_front();
	void insert_val(const Type &x);
	void delete_val(const Type &x);
	int  length();
	void clear();
	void destroy();
	void reserv();
	void sort();
	void find(const Type &x);
protected:
	ListNode* Buynode(Type x = Type())
	{
		ListNode *p = new ListNode(x);
		return p;
	}
private:
	ListNode *first;
	ListNode *last;
};

template
void List::push_back(const Type &x)//尾插
{
	ListNode *s = Buynode(x);
	last->next = s;
	last = s;
	first->data++;
}
template
void List::push_front(const Type &x)//头插
{	
	ListNode *s = Buynode(x);
	s->next = first->next;
	first->next = s;
	first->data++;
}

template
void List::show_list()//显示
{
	ListNode *p = first->next;
	while(p != NULL)
		{
			cout<data<<" ";
			p=p->next;
		}
	cout<
void List::pop_back()//尾删
{
	while(first->data==0)
		return;
	ListNode *p = first;
	while(p->next != last)
	{
		p = p->next;
	}
	delete p->next;
	p->next = NULL;
	last=p;
	first->data--;
}
template
void List::pop_front()//头删
{
	ListNode *p = first->next;
	ListNode *tmp = NULL;
	if(first->data>0)
	{
		tmp = p->next;
		first->next = tmp;
		delete p ;
		p = NULL;
		first->data--;
	}
}

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

template
void List::delete_val(const Type &x)//按值删除
{
	ListNode *p = first->next;
	ListNode *tmp = NULL;
	if(x == p->data)
	{
		pop_front();
		return;
	}
	else if(last->data==x)
	{
		pop_back();
		return;
	}
	else
	{
	while(p->next!=NULL)
	{
		if(p->next->data == x)
		{
			tmp = p->next;
			p->next = tmp->next;
			delete tmp;
			first->data--;
			return;
		}
		p = p->next;
	}
	}
}

template
int List::length()//求长
{
	cout<data<data;
}

template
void List::clear()//清除
{
	while(first->data>0)
		pop_front();
}

template
void List::destroy()//摧毁链表
{
	clear();
	delete first;
	first=last = NULL;
}


template
void List::reserv()//逆序
{
	ListNode *p = first->next;
    ListNode *curr = p->next;
    ListNode *tmp = NULL;

    first->next->next = NULL;
    while (curr)
    {
        tmp = curr->next;
        curr->next = p;
        p = curr;
        curr = tmp;
    }
    first->next = p;
}

template
void List::sort()//排序
{
	ListNode *q = first->next;
    while (q->next)
    {
        ListNode *p = q;
        while(p->next)
        {
             if(q->data > p->next->data)
                {
                    Type tmp =q->data;
                    q->data = p->next->data;
                    p->next->data = tmp;
                }
                p = p->next;
        }
        q = q->next;
    }
}

template
void List::find(const Type &x)//查找
{
		ListNode *q = first->next;
		int count = 0;
			while(q !=NULL)
			{
				if(q->data == x)
				{
					count++;
				}	
				q=q->next;
			}
			if(count)
				cout<<"存在该值"<

用c++实现 c++单链表的实现(采用模板类)_第1张图片

用c++实现 c++单链表的实现(采用模板类)_第2张图片

 

用c++实现 c++单链表的实现(采用模板类)_第3张图片

用c++实现 c++单链表的实现(采用模板类)_第4张图片

用c++实现 c++单链表的实现(采用模板类)_第5张图片

用c++实现 c++单链表的实现(采用模板类)_第6张图片

用c++实现 c++单链表的实现(采用模板类)_第7张图片

用c++实现 c++单链表的实现(采用模板类)_第8张图片

 

用c++实现 c++单链表的实现(采用模板类)_第9张图片

用c++实现 c++单链表的实现(采用模板类)_第10张图片

用c++实现 c++单链表的实现(采用模板类)_第11张图片

你可能感兴趣的:(c++)