链表的相关操作(2)

/*   给定两个数组A1和B1:
   1)将A1和B1中的数据导入链表中,形成链表A2和B2,并打印各自链表元素;
   2)将链表A2和B2中的元素各自排序(从大到小),形成链表A3和B3,并打印各自链表元素。
   3)合并链表A3,B3,合并后的链表C的元素从大到小排列,并打印链表C。*/
//-----------------------LinkedList.h------------------------------
#include<iostream>

#ifndef LINKED_LIST
#define LINKED_LIST

typedef int ElementType;
class List//定义一个List类
{
//--------------------内部定义一个Node类-------------------------
private:
	class Node//定义一个Node类
	{
	public:
		ElementType data;//节点的数据
		Node *next;//节点的指针域

	Node(ElementType value,Node *link=0)//Node类的构造函数  
    :data(value),next(link)  
    {}
	};
	typedef Node *NodePointer;
//--------------------成员函数以及成员变量-------------------------
public:
	List();//构造函数
	~List();//析构函数
	bool empty() const;//判空函数
	int size() const;//链表的大小(长度)
	void insert(const ElementType item,int pos);//插入函数
	void import(const ElementType *array,int size);//将数组元素导入链表的函数
	void sort();//排序函数
	void merge(const List & aList);//归并链表函数
	void display(ostream & out) const;//输出函数		
private:
	NodePointer first;//定义一个头指针
	int mySize;//链表大小(长度)
};
ostream & operator<<(ostream & out,const List & aList);//重载输出操作符
#endif

//------------------------LinkedList.cpp-------------------------------
#include<iostream>
#include<cassert>
using namespace std;

#include"LinkedList.h"

List::List()//构造函数------------------------
:first(0),mySize(0)
{}

List::~List()//析构函数------------------------
{
	NodePointer currPtr=first,//定义指向节点的指针
		        nextPtr;
	while(currPtr!=0)//当前指针非0
	{
		nextPtr=currPtr->next;
		delete currPtr;  //删除当前指针
		currPtr=nextPtr;
	}
	mySize=0;//链表大小置为0
}

bool List::empty() const//判空函数-----------------------
{
	return first==0;
}

int List::size() const//链表的大小(长度)------------------------
{
	return mySize;
}

void List::insert(ElementType item,int pos)//插入到第pos个节点之后的位置----------
{
	assert(pos>=0);//保证插入位置非负
	NodePointer ptr=first,
	            newPtr=new Node(item);//定义一个带数据的节点
	if(pos==0)//表示插入到第一个节点位置
	{
		newPtr->next=first;  
        first=newPtr;
	}
	
	else if(pos<=mySize)//表示插入到第1到mySize节点之后位置
	{
		for(int i=1;i<pos;i++)
		    ptr=ptr->next;//循环结束时,ptr指向第pos个节点
        newPtr->next=ptr->next;//插入新节点操作
	    ptr->next=newPtr;//插入新节点操作
	}
	else//pos位置越界报错
	{
		delete newPtr;//将未使用的新节点返还给堆
		cerr<<"***The position out of range***\n";
		return;
	}	
    mySize++;//链表长度加一  
}  

void List::import(const ElementType *array,int size)//将数组元素导入链表的函数-----------------
{
	if(size>0)//保证数组元素个数大于0
	{
		NodePointer currPtr,newNode;
		first=new Node(*array);//头指针指向第一个节点,同时将数组元素初始化在节点的数据域
		currPtr=first;
		for(int i=1;i<size;i++)//将数组剩余数据导入链表中
	    {
		    newNode=new Node(*(array+i));//数组数据从前往后导入链表
			currPtr->next=newNode;//连接操作
			currPtr=newNode;//后移操作
	    }
	}
	else//数组元素小于等于0进行异常报错
	{
		cerr<<"The size of the array should be greater than 0"<<endl;
		return;
	}
	mySize=size;//更新链表长度
}

void List::sort()//排序函数-------------------------
{
	if(mySize<=0)//链表非空
	{
		cerr<<"***The linked list has no node***"<<endl;
		return;
	}
	ElementType tem;
	NodePointer currPtr,nextPtr;
	for(int i=0;i<mySize-1;++i) //冒泡排序 
	{
		for(int j=0;j<mySize-i-1;j++)//内层循环
		{ 
			currPtr=first;//当前指针指向第一个节点
			for(int k=0;k<j;k++)//循环结束后,当前指针指向第j+1个节点
				currPtr=currPtr->next;
			nextPtr=currPtr->next;//nextPtr指向第j+2个节点
			if(currPtr->data<nextPtr->data)//若第j+1个节点的数据小于第j+2个节点的数据,则交换数据
	        {
		       tem=currPtr->data;
		       currPtr->data=nextPtr->data;//交换语句
		       nextPtr->data=tem;
	        }
		}	  
	}	
}

void List::merge(const List & aList)//归并链表函数------------------------
{
	NodePointer lastPtr=first;//lastPtr指向第一个链表的头节点
	while(lastPtr->next!=0)//循环结束后lastptr指向第一个链表的尾节点
		lastPtr=lastPtr->next;
	lastPtr->next=aList.first;//第一个链表的尾节点与第二个链表的头节点相连
    mySize+=aList.mySize;//长度增加

	//以下部分是排序操作
	ElementType tem;
	NodePointer currPtr,nextPtr;
	for(int i=0;i<mySize-1;++i)//冒泡排序 
	{
		for(int j=0;j<mySize-i-1;j++)//内层循环
		{ 
			currPtr=first;//当前指针指向第一个节点
			for(int k=0;k<j;k++)//循环结束后,当前指针指向第j+1个节点
				currPtr=currPtr->next;
			nextPtr=currPtr->next;//nextPtr指向第j+2个节点
			if(currPtr->data<nextPtr->data)//若第j+1个节点的数据小于第j+2个节点的数据,则交换数据
	        {
		       tem=currPtr->data;
		       currPtr->data=nextPtr->data;//交换语句
		       nextPtr->data=tem;
	        }
		}	  
	}		
}

void List::display(ostream & out) const//输出函数-----------------------
{
	NodePointer ptr;
	for(ptr=first;ptr!=0;ptr=ptr->next)//从头到尾遍历并输出链表数据
		out<<ptr->data<<" ";
	out<<endl;
}

ostream & operator<<(ostream & out,const List & aList)//重载输出操作符------------------
{
	aList.display(out);//调用display()成员函数
	return out;
}

//---------------------LinkedList_main.cpp--------------------------
#include<iostream>
using namespace std;

#include"LinkedList.h"

int main()
{
	cout<<"----------------------The program test is begin---------------------"<<endl;
	cout<<endl;

	List intList;//建立链表
	cout<<"Constructing intList1 and intList2\n";
	List intList1,intList2;//定义两个链表
	cout<<"List1 is empty? "<<boolalpha<<intList1.empty()<<endl;//测试判空函数
	cout<<"Display the intList1's size:"<<intList1.size()<<endl;//测试size函数
	cout<<"List2 is empty? "<<boolalpha<<intList2.empty()<<endl;//测试判空函数
	cout<<"Display the intList2's size:"<<intList2.size()<<endl;//测试size函数
	cout<<"---------------------------------------------------------------------"<<endl;

	int a[10]={1,2,3,4,5,6,7,8,9,10};//初始化两个数组
	int b[10]={11,12,13,14,15,16,17,18,19,20};//初始化两个数组
	cout<<"There are two arrays now:";
	cout<<"a[10]={1,2,3,4,5,6,7,8,9,10};b[10]={11,12,13,14,15,16,17,18,19,20};"<<endl;
	intList1.import(a,10);//测试导入函数
	intList2.import(b,10);//测试导入函数
	cout<<"Import the data of the array into the linked list"<<endl;
	cout<<"List1 is empty? "<<boolalpha<<intList1.empty()<<endl;//测试判空函数
	cout<<"Display the intList1's size:"<<intList1.size()<<endl;//测试size函数
	cout<<"List2 is empty? "<<boolalpha<<intList2.empty()<<endl;//测试判空函数
	cout<<"Display the intList2's size:"<<intList2.size()<<endl;//测试size函数
	cout<<"---------------------------------------------------------------------"<<endl;

	cout<<"Display the data in the linked list:"<<endl;
	cout<<"intList1 :"<<endl; intList1.display(cout);//显示导入数组数据后的结果
	cout<<"intList2 :"<<endl; intList2.display(cout);//显示导入数组数据后的结果

	cout<<"---------------------------------------------------------------------"<<endl;
	cout<<"Sort the data in the list from large to small"<<endl;
	cout<<"intList1 :"<<endl; intList1.sort(); intList1.display(cout);//测试排序函数
	cout<<"intList2 :"<<endl; intList2.sort(); intList2.display(cout);//测试排序函数

	cout<<"---------------------------------------------------------------------"<<endl;
	cout<<"Merge two linked lists"<<endl;
	intList1.merge(intList2);//测试归并链表函数
	cout<<"intList1 :"<<endl; intList1.display(cout);//输出归并链表后的结果

	cout<<"Insert a node into the linked list"<<endl;
	intList1.insert(2017,20);intList1.display(cout);

	cout<<"----------------------The program test is end--------------------------"<<endl;
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值