C++实现单链表的几个基本功能

闲来无事,自己动手实现了单链表的一些基本操作(增、删、改、查)以及一些附属的功能。


链表的头文件如下:

#ifndef CLINK_H_
#define CLINK_H_

typedef int elemType;//元素项

struct Item //节点项
{
   elemType data;
   Item *p_next;
};

class CLink
{
public:
	CLink();//构造函数
	CLink(const CLink & link); //拷贝构造函数
	CLink & operator = (const CLink & otherLink); //赋值函数

	void rearAdd(elemType data); //在末尾添加元素;
	void headAdd(elemType data); //在头部添加元素;
	void print() const; //遍历链表并打印;
	void destroy();//销毁链表,将链表成为空链表;
	int size() const; //得到单链表的长度;
        bool isEmpty();// 判断链表是否为空;
	elemType getElement(int pos) const; //获得链表中第pos个节点的元素;
	int find(elemType x); //查找x第一次出现的位置,若没有,返回-1;
        void modifyElement(int pos, elemType x); //修改pos位置的元素值为x;
	void insertElement(int pos, elemType data);//将元素插入在链表的第pos位置上;
	void deleteElement(int pos); //删除链表的第pos个节点;

	void sort(); //对链表元素进行升序排序;

	~CLink();
private:
	Item *pHead; //头节点
	int length; //链表长度
};

#endif

基本功能实现如下:

1.末尾追加元素

void CLink::rearAdd(elemType data)
{
	
	if(!pHead) //若添加时为空链表
	{
		pHead = new Item;
		pHead->data = data;
		pHead->p_next = NULL;
	}
	else
	{
	       Item *temp = pHead;
		while(temp->p_next)
		{
			temp = temp->p_next;
		}
		temp->p_next = new Item;
		temp = temp->p_next;

		temp->data = data;
		temp->p_next = NULL;
	}
	length++;
}

2.首部添加元素

void CLink::headAdd(elemType data)
{
	Item * addElement = new Item;
	addElement->data = data;
	addElement->p_next = NULL;

	if(!pHead)//添加时为空链表
	{
		pHead = addElement;
	}
	else
	{
		addElement->p_next = pHead;
		pHead = addElement;
	}
}

3.遍历链表并打印元素

void CLink::print() const
{
	Item *temp = pHead;
	int count = 0;
	while(temp)
	{
       cout << temp->data << "\t"; 
	   count++;

	   if(count % 5 == 0)
	   {
		   cout << endl;
	   }

	   temp = temp->p_next;
	}
}

4.获得链表长度

int CLink::size() const
{
	return length;
}

5.判断链表是否为空

bool CLink::isEmpty()
{
   return pHead == NULL;
}

6.获得第pos个节点的元素

elemType CLink::getElement(int pos) const
{
	if(pos < 1 || pos > length)
	{
		throw "获得元素位置非法!";
	}
	int index = 0;
	Item *temp = pHead;
	while(temp && ++index < pos)
	{
	   temp = temp->p_next;
	}

	return temp->data;
}

7.查找x第一次出现的位置,若没有,返回-1

int CLink::find(elemType x)
{
	Item *temp = pHead;
	int index = 0;

	while(temp && ++index <= length)
	{
       if(temp->data == x)
	   {
		   return index;
	   }
	   temp = temp->p_next;
	}

	return -1;
}

8.修改第pos个节点的值

void CLink::modifyElement(int pos, elemType x)
{
	if(pos < 1 || pos > length)
	{
		throw "修改节点位置pos非法!";
	}

	Item *temp = pHead;
	int index = 0;
	while(temp && ++index <= length)
	{
		if(index == pos)
		{
           temp->data = x;
		   return ;
		}
		temp = temp->p_next;
	}
}

9.将元素插入在链表的第pos位置上

void CLink::insertElement(int pos, elemType data)
{
	if(pos < 1 || pos > length + 1)
	{
		throw "插入pos位置不合法!";
	}

	Item * insertElement = new Item;
	insertElement->data = data;
	insertElement->p_next = NULL;
	
    
	if(pos == 1)//pos在链表的头位
	{
       insertElement->p_next = pHead;
	   pHead = insertElement;
	   length++;
	}
	else
	{
	   	Item *temp = pHead;
        int index = 0;
       while(temp && ++index <= length)
	   {
		   if(index + 1 == pos)
		   {
			   insertElement->p_next = temp->p_next;
			   temp->p_next = insertElement;

			   length++;

			   return ;
		   }
		   temp = temp->p_next;
	   }
	}
}


10.删除链表的第pos个节点

void CLink::deleteElement(int pos)
{
	if(pos < 1 || pos > length)
	{
		throw "删除位置pos非法!";
	}
    
	Item * deleteElement;
	if(pos == 1)
	{
       deleteElement = pHead;
	   pHead = pHead->p_next;
	   delete deleteElement;
	   length--;
	}
	else
	{
		Item *temp = pHead;
		int index = 0;
		while(temp && ++index < length)
		{
			if(index + 1 == pos)
			{
				deleteElement = temp->p_next;
				temp->p_next = temp->p_next->p_next;
				delete deleteElement;

				length--;
				return ;
			}
			temp = temp->p_next;
		}
	}
}

11.对节点进行升序排序

void CLink::sort()
{
	bool sortflags;
	elemType a, b;
   for(int i = 1; i < length ; i++)
   {
	   sortflags = true;
	   for(int j = 1; j <= length - i; j++)
	   {
           if( ( a = getElement(j) ) > (b = getElement(j+1) ) )
		   {
			   modifyElement(j, b);
			   modifyElement(j+1, a);

			   sortflags = false;
		   }
	   }
	   if(sortflags)
		   return ;
   }
}


12.销毁链表并置为空链表

void CLink::destroy()
{
	Item *temp;
	while(pHead)
	{
		temp = pHead;
		pHead = pHead->p_next;
		delete temp;
	}
	length = 0;
}


对有指针成员的类处理(利用上面实现的功能进行处理):

1.构造函数

CLink::CLink():pHead(NULL),length(0){};


2.拷贝构造函数

CLink::CLink(const CLink & otherLink):pHead(NULL),length(0)
{

	for(int i = 1; i <= otherLink.length; i++)
	{
		rearAdd(otherLink.getElement(i));
	}
}

3. =运算符重载

CLink & CLink::operator =(const CLink & otherLink)
{
	destroy();
	for(int i = 1; i <= otherLink.length; i++)
	{
		rearAdd(otherLink.getElement(i));
	}
	return *this;
}

4. 析构函数

CLink::~CLink()
{
	Item *temp;
	while(pHead)
	{
		temp = pHead;
		pHead = pHead->p_next;
		delete temp;
	}
}





你可能感兴趣的:(C++实现单链表的几个基本功能)