链表的相关操作

/*(1)编写一个算法判断链表中的数据项是否按从大到小排序,该链表的第一个节点由first指向。
  (2)对于给定的整数n,编写一个算法把节点插入到链表中第n个节点之后的位置,
      该链表的第一个节点由first指向。
  (3)编写一个算法来颠倒一个链表,该链表的第一个节点由first指向。
      不要复制列表元素:而是重置链表和指针,使得first指针指向原来的最后一个节点,
      且节点之间的所有链接都反向。*/
//-----------------------Linked_List.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;//链表的大小(长度)
	bool is_LangeToSmall() const;//判断数据项是否从大到小排列的函数
	void insert(const ElementType item,int pos);//插入函数
	void reverse();//链表反转函数
	void display(ostream & out) const;//输出函数
private:
	NodePointer first;//定义一个头指针
	int mySize;//链表大小(长度)
};
ostream & operator<<(ostream & out,const List & aList);//重载输出操作符
#endif
//------------------------Linked_List.cpp-------------------------------
#include<iostream>
#include<cassert>
using namespace std;

#include"Linked_List.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;
}

bool List::is_LangeToSmall() const//判断数据项是否从大到小排列的函数----------------
{
	NodePointer currPtr=first,//定义指向节点的指针
		        nextPtr;
	if(empty())//如果链表为空,进行报错提示
	{
		cerr<<"***The linked list is empty---returning garbage value***"<<endl;
	}
	else
	{
		while(currPtr!=0&&mySize>=2)//链表长度大于等于2,且当前指针非空
	    {
		    nextPtr=currPtr->next;
		    if(nextPtr!=0&&currPtr->data<nextPtr->data)//若后驱指针非0,并判断前后数据大小
		      { return false;break; }//前者小于后者,返回false并退出
		    currPtr=nextPtr;
	    }
	    return true;//链表长度为1,或者链表为降序链表时,返回true
	}	
}

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::reverse()//链表反转函数-----------------------------
{
	NodePointer predPtr=first,ptr,//定义相关指针
		        lastPtr=first;
	if(empty())//如果链表为空,进行报错提示
	{
		cerr<<"***The linked list is empty---returning garbage value***"<<endl;
        return;
	}
	else
	{
		while(lastPtr->next!=0)//循环结束时,lastPtr指向链表最后一个节点
		{
			lastPtr=lastPtr->next;
		}
		ptr=lastPtr;//ptr指向链表最后一个节点,以便first指针指向最后节点
		lastPtr=first;//lastPtr指向第一个节点
		for(int i=0;i<mySize-1;i++){
			for(int j=0;j<mySize-i-1;j++){
			    predPtr=lastPtr;//内层循环结束时,predPtr指向lastPtr指向的节点的前一个节点
		        lastPtr=lastPtr->next;//第x次内层循环结束时,lastPtr指向第(mySize-x+1)个节点
	        }
			lastPtr->next=predPtr;//lastPtr指向的节点的next存放前驱指针指向的节点的地址
			lastPtr=first;//lastPtr再次指向第一个节点,进行下一次内层循环
		}
		lastPtr->next=0;//两层循环结束后,将第一个节点的next置为0
		first=ptr;//头指针指向原链表的尾节点
	}	
}

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;
}

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

#include"Linked_List.h"

int main()
{
	
	cout<<"----------------------The program test is begin---------------------"<<endl;
	cout<<endl;
	//Routine test
	List intList;//建立链表
	cout<<"Constructing intList\n";
	
	cout<<"List is empty? "<<boolalpha<<intList.empty()<<endl;//测试判空函数
	cout<<"Display the intList's size:"<<intList.size()<<endl;//测试size函数

	cout<<"---------------------------------------------------------------------"<<endl;
	cout<<"Build ten nodes in the linked list "<<endl;//添加十个数据到链表
	for(int i=0;i<10;i++){
		intList.insert(10-i,i);
	}
	cout<<"Display the data in the linked list :";
	intList.display(cout);//显示链表数据
	cout<<"List is empty? "<<boolalpha<<intList.empty()<<endl;//再次测试判空函数
	cout<<"Display to output the size:"<<intList.size()<<endl;//再次测试size函数
	cout<<"The number in linked list is sorted from largest to smallest? "
		<<boolalpha<<intList.is_LangeToSmall()<<endl;测试is_LangeToSmall函数

	cout<<"----------------------------------------------------------------------"<<endl;
	cout<<"Insert 2017 into the linked list"<<endl;
	intList.insert(2017,7);//将2017插入链表,测试插入函数
	cout<<"Display the data in the linked linked list :"<<endl;
	intList.display(cout);//测试插入函数,是否插入到第7个节点之后的位置

	cout<<"-----------------------------------------------------------------------"<<endl;
	cout<<"The number in intList is sorted from largest to smallest? "
		<<boolalpha<<intList.is_LangeToSmall()<<endl;//再次测试is_LangeToSmall函数
	intList.reverse();//测试链表反转函数
	cout<<"Display the data in the linked list after reversing the linked list:"<<endl;
	intList.display(cout);//输出反转后的结果
	cout<<endl;

	cout<<"-----------------------------------------------------------------------"<<endl;
	//Special value detection
	/*以下一段代码主要测试一个节点条件下使用is_LangeToSmall()函数
	   reverse()函数以及insert()函数的情况*/ 
	List intList1;
	cout<<"Constructing intList1\n";

	cout<<"Insert 2017 into the linked list1"<<endl;//测试insert()函数
	intList1.insert(2017,0);
	intList1.display(cout);

	cout<<"The number in linked list1 is sorted from largest to smallest? "
		<<boolalpha<<intList1.is_LangeToSmall()<<endl;//测试is_LangeToSmall()
	
    intList1.reverse();//测试链表反转函数
	cout<<"Display the data in the linked list after reversing the linked list:"<<endl;
    intList1.display(cout);//输出反转后的结果    

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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值