链表

链表是c++数据结构中重要的一节。下面通过一个程序来说明如何定义链表以及如何操控链表。

程序如下:


#ifndef LISTNODE_H
#define LISTNODE_H

template<class NODETYPE> class List;
template<class NODETYPE> 

class ListNode
{
	friend class List<NODETYPE > ;

	public:
		ListNode(const NODETYPE &);
		NODETYPE getData() const;

	private:
		NODETYPE data;
		ListNode<NODETYPE> *nextPtr;


};
template<class NODETYPE>
ListNode<NODETYPE>::ListNode(const NODETYPE &info):data(info),nextPtr(0)
{

}

template<class NODETYPE>

NODETYPE ListNode<NODETYPE>::getData() const
{

	return data;
}

#endif


以上是ListNode.h的定义

#ifndef LIST_H
#define LIST_H

#include<iostream>
using std::cout;
#include<new>
#include "ListNode.h"

template<class NODETYPE>
class List
{

	public:
		List();
		~List();
		void insertAtFront(const NODETYPE & );
		void insertAtBack(const NODETYPE &);
		bool  removeFromFront( NODETYPE &);
		bool removeFromBack( NODETYPE &);
		bool isEmpty() const;
		void print() const;

	private:
		ListNode<NODETYPE> *firstPtr;
		ListNode<NODETYPE> *lastPtr;

		ListNode<NODETYPE> *getNewNode(const NODETYPE &);




};

template<class NODETYPE>
List<NODETYPE>::List():firstPtr(0),lastPtr(0)
{


}

template<class NODETYPE>
List<NODETYPE>::~List()
{

	if(!isEmpty())
	{
	
		cout<<"Destroying nodes ...\n";
			ListNode<NODETYPE> *currentPtr = firstPtr;
				ListNode<NODETYPE> *tempPtr;
				while(currentPtr != 0)
				{
				
					tempPtr = currentPtr;
					cout<<tempPtr->data<<"\n";
					currentPtr = currentPtr->nextPtr;
					delete tempPtr;


				
				}





	}

	cout<<"All nodes destroyed\n";

}

template<class NODETYPE>
void List<NODETYPE>::insertAtFront(const NODETYPE &value )
{

	ListNode<NODETYPE> *newPtr = getNewNode(value);
	if(isEmpty())
		firstPtr = lastPtr = newPtr;

	else
	{
	
		newPtr->nextPtr = firstPtr;
		firstPtr = newPtr;
	}

}

template<class NODETYPE>
void List<NODETYPE>::insertAtBack(const NODETYPE &value )
{

	ListNode<NODETYPE> *newPtr = getNewNode(value);
	if(isEmpty())
		firstPtr = lastPtr = newPtr;

	else
	{
	
	lastPtr->nextPtr = newPtr;
		lastPtr = newPtr;
	}

}

template<class NODETYPE>
bool List<NODETYPE>::removeFromFront( NODETYPE &value )
{


	if(isEmpty())
	return false;

	else
	{
		ListNode< NODETYPE> *tempPtr = firstPtr;
		if(firstPtr == lastPtr)
			firstPtr = lastPtr = 0;
			else
		firstPtr = firstPtr->nextPtr ;
		value = tempPtr->data;
		delete tempPtr;
		return true;
	
	
	}

}


template<class NODETYPE>
bool  List<NODETYPE>::removeFromBack( NODETYPE &value )
{


	if(isEmpty())
	return false;

	else
	{
		ListNode< NODETYPE> *tempPtr = lastPtr;
		if(firstPtr == lastPtr)
			firstPtr = lastPtr = 0;
			else
		{
		
				ListNode< NODETYPE> *currentPtr = firstPtr;
				while(currentPtr->nextPtr != lastPtr)
					currentPtr = currentPtr->nextPtr;
				lastPtr = currentPtr;
				currentPtr->nextPtr = 0;




		}
		value = tempPtr->data;
		delete tempPtr;

		return true;


	
	
	}

}

template<class NODETYPE>
bool List<NODETYPE>::isEmpty() const 
{

	return firstPtr == 0;
}

template<class NODETYPE>

ListNode<NODETYPE> *List<NODETYPE>::getNewNode(const NODETYPE &value)
{
	return new ListNode<NODETYPE>(value);



}

template<class NODETYPE>
void List<NODETYPE>::print() const
{

	if(isEmpty())
	{
	
		cout<<"The list is empty\n\n";
		return ;
	}
ListNode<NODETYPE> *currentPtr = firstPtr;
cout<<"The list is ";

while(currentPtr != 0)
{
	cout<<currentPtr -> data<<" ";
	currentPtr = currentPtr -> nextPtr;


}

cout<<"\n\n";


}

#endif




以上是list.h的定义


#include<iostream>
using std::cin;
using std::endl;
#include<string>

using std::string;

#include "list.h"
template<class T>
void testList(List<T> &listObject,const string &typeName)
{
	cout<<"Testing a list of "<<typeName<<"value\n";

	instructions();
	int chioce;
	T value;
	do
	{
		cout<<"?";
		cin>>chioce;
		switch(chioce)
		{
		
		case 1:
			cout<<"Enter "<<typeName<<" :";
			cin>>value;
			listObject.insertAtFront(value);
			listObject.print();
			break;

		case 2:
			cout<<"Enter "<<typeName<<" :";
			cin>>value;
			listObject.insertAtBack(value);
			listObject.print();
			break;
		case 3:
			if(	listObject.removeFromFront(value))
				cout<<value<<"removed from list\n";

				listObject.print();
				break;

		case 4:
				if(	listObject.removeFromBack(value))
				cout<<value<<"removed from list\n";

				listObject.print();
				break;

	
		
		}
	

	
	
	
	}while(chioce != 5);


	cout<<"End list test\n\n";


}

void instructions()
{

	cout<<"Enter one of the following :\n"
		<<"1 to insert at begging of list\n"
		<<"2 to insert at the end of list\n"
		<<"3 to delete from beginning of list\n"
		<<"4 to delete from end of list\n"
		<<"5 to end list processing\n";

}
int main()
{
	List<int> integerList;
	testList(integerList,"integer");

	List<double> doubleList;
	testList(doubleList,"double");




	return 0;
}

以上是主函数的定义。


程序运行的结果是:


程序用List类模板来操控一个整数列表和一个浮点数列表。主程序定义了5个选项:1、在表头插入一个元素2、在表尾插入一个元素3、删除表头的元素4、删除表尾的元素。5、终止列表处理

该程序使用了两个类模板,在每个List对象中封了一个NodeListd对象的链表。

注意:

新节点连接成员赋空值,指针在使用前初始化。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值