数据结构(C++)——单链表

#include"AllHead.h"
template<class T>
class List;

template<class T>
class ListNode
{
	template<class T>
	friend class List;
private:
	T data;
	ListNode<T> *next;
public:
	ListNode(ListNode<T> *ptr = NULL){next = ptr;}              //只初始化指针域的构造函数
	ListNode(T x,ListNode<T> *ptr = NULL){data = x;next = ptr;}//初始化数据域和指针域的构造函数
};
template<class T>
class List
{
private:
	ListNode<T> *first;
	ListNode<T> *last;
	size_t   size;
public:
	List()
	{
		first = new ListNode<T>;
		last = first;
		size = 0;
	}
	~List(){makeEmpty();}
	List(ListNode<T> &L);
	Status push_back(T x);
	Status push_front(T x);
	Status IsEmpty(){return first == last?TRUE:FALSE;}
	void show_list();
	size_t getlenth(){return size;}
	T getHead_val(){return first!=last?first->next->data:NULL;}
	T getLast_val(){return first!=last?last->data:NULL;}
	ListNode<T>* getFirst(){return first;}
	void makeEmpty();
	Status pop_back();
	Status pop_front();
	void sort();
	void reverse();
	Status insert_val(T x);
	Status del_val(T x);
	ListNode<T>* find(T key);
	List& operator=(List<T> &L);
};

template<class T>
List<T>& List<T>::operator=(List<T>& L)
{
	T value;
	ListNode<T> *oldptr = L.getFirst();//获得旧的链表的头结点
	ListNode<T> *newptr = new ListNode<T>;//新开一个节点作为头结点
	first = newptr;
	while(oldptr->next != NULL)
	{
		value = oldptr->next->data;
		newptr->next = new ListNode<T>(value);
		newptr = newptr->next;
		oldptr = oldptr->next;
		newptr->next = NULL;
	}
	return *this;

}

template<class T>
Status List<T>::del_val(T x)
{
	if(IsEmpty())
	{
		cout<<"链表已空(del_val)....."<<endl;
		return ERROR;
	}
	ListNode<T> *q = first;
	ListNode<T> *h;
	size_t sizemem = size;
	while(q!= NULL)
	{
		if(q->data == x)
		{
			if(q->next == NULL)//(q == last)
	        {
				last = h;
	        }
	        h->next = q->next;
	        delete q;
	        --size;
	    	return OK;
		}
		h = q;
		q = q->next;
	}
	if(size == sizemem)
	{
		cout<<"链表中不存在该值!"<<endl;
		return ERROR;
	}
}

template<class T>
void List<T>::reverse()
{
	if(IsEmpty())
	{
		cout<<"空链表无法逆置"<<endl;
	}
	ListNode<T> *h = first->next->next;
	ListNode<T> *q = h->next;
	ListNode<T> *m = last;
	last = first->next;
	first->next->next = NULL;

	while(q!=NULL)
	{
		h->next = first->next;
		first->next = h;
		h = q;
		q = q->next;
	}
	m->next = first->next;
	first->next = m;
}
template<class T>
ListNode<T>* List<T>::find(T key)
{
	if(IsEmpty())
	{
		cout<<"链表为空。。。"<<endl;
	}
	ListNode<T> *q = first->next;
	while(q != NULL&&q->data != key)
	{
		q=q->next;
	}
	return q;
}
template<class T>
Status List<T>::insert_val(T x)/INSERT_VAL/
{
	ListNode<T> *p = new ListNode<T>;
	p->data = x;
	p->next = NULL;
	ListNode<T> *q = first->next;
	ListNode<T> *h = first;
	while(q!= NULL&&q->data<x)
	{
		h = q;
		q = q->next;
	}	
	if(q == NULL)
	{
		h->next = p;
		last = p;
	}
	else
	{
		p->next = q;
		h->next = p;
	}
	++size;
	return OK;
}

template<class T>
void List<T>::sort()///SORT//
{
	ListNode<T> *q = first->next;
	ListNode<T> *h;
	first->next = NULL;
	last = first;
	while(q!= NULL)
	{//h和q是断开后面的指针!!!
		h = q;
		q = q->next;
	 //p和m是新的表头这边的两个指针!!!
		ListNode<T> *p = first->next;
		ListNode<T> *m = first;
		while(p!= NULL&&(p->data)<(h->data))
		{
			m = p;
			p = p->next;
		}
		if(p == NULL)
	    {
		  m->next = h;
		  h->next = NULL;
		  last = h;
	    }
		else
	    {
			h->next = p;
		    m->next = h;
	     }
	}
}
template<class T>
Status List<T>::pop_front()
{
	if(IsEmpty())
	{
		cout<<"空链表不能删除(pop_front)......"<<endl;
		return ERROR;
	}
	ListNode<T> *q = first->next;
	first->next = q->next;
	delete q;
	if(size == 1)
	{
		 last = first;
	}
	--size;
	return OK;


}
template<class T>
Status List<T>::pop_back()
{
	if(IsEmpty())
	{
		cout<<"空链表不能删除(pop_back)......"<<endl;
		return ERROR;
	}//--------以下代码有待改进----------(用一个指针)
	ListNode<T> *q = first;
	ListNode<T> *h;
	while(q->next!=NULL)
	{
		h = q;
		q=q->next;
	}
	last = h;
	h->next = NULL;
	delete q;
	--size;
	return OK;
}
template<class T>
void List<T>::makeEmpty()//清空完后要处理下first和last,注意维护好list这个类。
{
	ListNode<T> *q = first->next;
	while(q != NULL)
	{
		first->next = q->next;
		delete q;
		q = first->next;
	}
	last = first;
	size = 0;
}
template<class T>
List<T>::List(ListNode<T> &L)
{
	T value;
	ListNode<T> *oldptr = L.getFirst();//获得旧的链表的头结点
	ListNode<T> *newptr = first = new ListNode<T>;//新开一个节点作为头结点
	while(oldptr->next != NULL)
	{
		value = oldptr->next->data;
		newptr->next = ListNode<T>(value);
		newptr = newptr->next;
		oldptr = oldptr->next;
		newptr->next = NULL;
	}
}

template<class T>
Status List<T>::push_back(T x)
{
	ListNode<T> *s = new ListNode<T>;
	s->data = x;
	last->next = s;
	s->next = NULL;
	last = s;
	++size;
	return OK;

}
template<class T>
Status List<T>::push_front(T x)
{
	ListNode<T> *s = new ListNode<T>;
	s->data = x;
	s->next = first->next;
	first->next = s;
	if(size == 0)
	{
		last = s;
	}
	++size;
	return OK;
}
template<class T>
void List<T>::show_list()
{
	ListNode<T> *p = first->next;
	while(p!=NULL)
	{
		cout<<p->data<<"-->";
		p=p->next;
	}
	cout<<"NULL"<<endl;
}

#if 1
#include"list.h"
int main()
{
	List<int> myls;
	List<int> yourls;
	int select = 1;
	while(select)
	{
		cout<<"||======================================||"<<endl;
		cout<<"||==[1]push_front    [2]push_back=======||"<<endl;
		cout<<"||==[3]show_list     [4]getlenth========||"<<endl;
		cout<<"||==[5]getHead_val   [6]getLast_val=====||"<<endl;
		cout<<"||==[7]quit          [8]测试赋值 =======||"<<endl;
		cout<<"||==[9]makeEmpty     [10]pop_back=======||"<<endl;
		cout<<"||==[11]pop_front    [12]insert_val=====||"<<endl;
		cout<<"||==[13]del_val      [14]sort===========||"<<endl;
		cout<<"||==[15]reverse      [16]find_val_rid===||"<<endl;  
		cout<<"||======================================||"<<endl;
		cout<<"请输入您的选择:>";
		cin>>select;
		system("cls");
		switch(select)
		{
		case 1:
			int item_pf;
			cout<<"请输入插入的值(以-1结束):>"<<endl;
			while(cin>>item_pf,item_pf!=-1)
			{
				myls.push_front(item_pf);
			}
			break;
		case 2:
			int item_pb;
			cout<<"请输入插入的值(以-1结束):>"<<endl;
			while(cin>>item_pb,item_pb!=-1)
			{
				myls.push_back(item_pb);
			}
			break;
		case 3:
			myls.show_list();
			break;
		case 4:
			size_t size;
			size = myls.getlenth();
			cout<<"链表长度为:"<<size<<endl;
			break;
		case 5:
			int head;
			head = myls.getHead_val();
			cout<<"该链表的第一个为:"<<head<<endl;
			break;
		case 6:
			int last;
			last = myls.getLast_val();
			cout<<"该链表的最后一个值为:"<<last<<endl;
			break;
		case 7:
			exit(1);
			break;
		case 8:
			cout<<"即将为您拷贝..."<<endl;
			yourls = myls;
			cout<<"为您打印拷贝过来的内容:";
	    	yourls.show_list();
			break;
		case 9:
			myls.makeEmpty();
			break;
		case 10:
			myls.pop_back();
			break;
		case 11:
			myls.pop_front();
			break;
		case 12:
			int val_insert;
			cout<<"请输入您要插入的值:>";
			cin>>val_insert;
			myls.insert_val(val_insert);
			break;
		case 13:
			int val_del;
			cout<<"请输入您要删除的值:>";
			cin>>val_del;
			myls.del_val(val_del);
			break;
		case 14:
			myls.sort();
			break;
		case 15:
			myls.reverse();
			break;
		case 16:
			ListNode<int> *key;
			int key_find;
			cout<<"请输入您要查找的值;>";
			cin>>key_find;
			key = myls.find(key_find);
			if(key != NULL)
				cout<<"找到啦-->"<<key<<endl;
			else
				cout<<"没找到..."<<endl;
			break;
		}

	}
	return 0;

}
#endif




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值