循环链表及其接口函数的C++实现

转载请注明出处:http://blog.csdn.net/hongkangwl/article/details/22284249

循环链表及其接口函数的C++实现循环链表及其接口函数的C++实现


首先建立节点的结构体

struct node
{
	int date;
	node* ptrnext;
};

然后在循环链表的类中声明需要的成员和方法

class circlelinklist
{
private:
	 int  length;
//	int capacity;
public:
	node* root;
//	node* nodeptr;
	circlelinklist()
	{
		length = 0;
//		capacity = 0;
		root = new node;
		root->ptrnext = NULL;
//		nodeptr = NULL;
	}
	int circlelinklist_insertnode(circlelinklist* ptr, int position, int member);
	int circlelinklist_delectnode(circlelinklist* ptr, int position);
	int circlelinklist_getnode(circlelinklist* ptr, int position);
	bool circlelinklist_empty(circlelinklist* ptr);
    int circlelinklist_getlength(circlelinklist* ptr);
	void circlelinklist_display(circlelinklist* ptr, int num);
	


};

又有插入操作,囧~~每次插入总是最麻烦的

int circlelinklist::circlelinklist_insertnode(circlelinklist *ptr, int position, int member)
{
	node* nodeinsert = new node;
	nodeinsert->date = member;
	node* current = root->ptrnext;
//	node* back;
	if (root->ptrnext == NULL) //链表是否为空
	{
		root->ptrnext = nodeinsert;
		nodeinsert->ptrnext = nodeinsert;
		ptr->length++;
		return 0;
	} 
	else
	{
		if (position == 0)//0位置比较特殊,因为头结点指向0位,而别的位置和头结点没有直接关系
		{
			while ((current->ptrnext) != (root->ptrnext))
			{
				current = current->ptrnext;
			}
			nodeinsert->ptrnext = root->ptrnext;
			root->ptrnext = nodeinsert;
			current->ptrnext = nodeinsert;
			/*for (int i = 0; i < ptr->circlelinklist_getlength(ptr) ; i++ )
			{
				current = current ->ptrnext;
			}
			current->ptrnext = nodeinsert;
			nodeinsert->ptrnext = root->ptrnext;
			root->ptrnext = nodeinsert;*/
			ptr->length++;
			return 0;
		} 
		else//其他位置正常插入
		{
			for (int i = 0; i < position - 1 ; i++)
			{
				current =current->ptrnext;
			}
			nodeinsert->ptrnext = current->ptrnext;
			current->ptrnext = nodeinsert;
			ptr->length++;
			return 0;
		}
		

		
	}
	
}

删除操作时同样要注意0节点

int circlelinklist::circlelinklist_delectnode(circlelinklist* ptr, int position)
{
	if (ptr->circlelinklist_getlength(ptr) == 0)
	{
		cout<<"链表已经为空oop!!"<<endl;
		return 0;
	} 
	else if (ptr->circlelinklist_getlength( ptr) == 1)//只有一个节点时要特殊考虑
	{
		root->ptrnext =NULL;
		ptr->length--;
	}
	else
	{
		if (position == 0)//0节点特殊考虑
		{
			node* current = root->ptrnext;
			while (current->ptrnext != root->ptrnext)
			{
				current = current->ptrnext;
			}
			root->ptrnext = root->ptrnext->ptrnext;
			current->ptrnext = root->ptrnext;
			ptr->length--;
		} 
		else
		{
			node* current = root->ptrnext;
			for (int i = 0; i < position - 1; i++)
			{
				current =current ->ptrnext;
			}
			current->ptrnext = current->ptrnext->ptrnext;
			ptr->length--;
		}
	}
}

下面是完整实现和测试程序:

#include <iostream>
#include <cstdlib>
using namespace std;
struct node
{
	int date;
	node* ptrnext;
};
class circlelinklist
{
private:
	 int  length;
//	int capacity;
public:
	node* root;
//	node* nodeptr;
	circlelinklist()
	{
		length = 0;
//		capacity = 0;
		root = new node;
		root->ptrnext = NULL;
//		nodeptr = NULL;
	}
	int circlelinklist_insertnode(circlelinklist* ptr, int position, int member);
	int circlelinklist_delectnode(circlelinklist* ptr, int position);
	int circlelinklist_getnode(circlelinklist* ptr, int position);
	bool circlelinklist_empty(circlelinklist* ptr);
    int circlelinklist_getlength(circlelinklist* ptr);
	void circlelinklist_display(circlelinklist* ptr, int num);
	


};

int circlelinklist::circlelinklist_delectnode(circlelinklist* ptr, int position)
{
	if (ptr->circlelinklist_getlength(ptr) == 0)
	{
		cout<<"链表已经为空oop!!"<<endl;
		return 0;
	} 
	else if (ptr->circlelinklist_getlength( ptr) == 1)//只有一个节点时要特殊考虑
	{
		root->ptrnext =NULL;
		ptr->length--;
	}
	else
	{
		if (position == 0)//0节点特殊考虑
		{
			node* current = root->ptrnext;
			while (current->ptrnext != root->ptrnext)
			{
				current = current->ptrnext;
			}
			root->ptrnext = root->ptrnext->ptrnext;
			current->ptrnext = root->ptrnext;
			ptr->length--;
		} 
		else
		{
			node* current = root->ptrnext;
			for (int i = 0; i < position - 1; i++)
			{
				current =current ->ptrnext;
			}
			current->ptrnext = current->ptrnext->ptrnext;
			ptr->length--;
		}
	}
}
void circlelinklist::circlelinklist_display(circlelinklist *ptr, int num)
{
	node* current = root->ptrnext;
	for (int i  = 0; i < num; i++)
	{
		cout<<current->date<<" ";
		current = current->ptrnext;
	}
	cout<<endl;
}
int circlelinklist::circlelinklist_getlength(circlelinklist* ptr)
{
	return ptr->length;
}
int circlelinklist::circlelinklist_insertnode(circlelinklist *ptr, int position, int member)
{
	node* nodeinsert = new node;
	nodeinsert->date = member;
	node* current = root->ptrnext;
//	node* back;
	if (root->ptrnext == NULL) //链表是否为空
	{
		root->ptrnext = nodeinsert;
		nodeinsert->ptrnext = nodeinsert;
		ptr->length++;
		return 0;
	} 
	else
	{
		if (position == 0)//0位置比较特殊,因为头结点指向0位,而别的位置和头结点没有直接关系
		{
			while ((current->ptrnext) != (root->ptrnext))
			{
				current = current->ptrnext;
			}
			nodeinsert->ptrnext = root->ptrnext;
			root->ptrnext = nodeinsert;
			current->ptrnext = nodeinsert;
			/*for (int i = 0; i < ptr->circlelinklist_getlength(ptr) ; i++ )
			{
				current = current ->ptrnext;
			}
			current->ptrnext = nodeinsert;
			nodeinsert->ptrnext = root->ptrnext;
			root->ptrnext = nodeinsert;*/
			ptr->length++;
			return 0;
		} 
		else//其他位置正常插入
		{
			for (int i = 0; i < position - 1 ; i++)
			{
				current =current->ptrnext;
			}
			nodeinsert->ptrnext = current->ptrnext;
			current->ptrnext = nodeinsert;
			ptr->length++;
			return 0;
		}
		

		
	}
	
}

int main()
{
	circlelinklist* CircleListObject = new circlelinklist;
	for (int i = 0; i < 8; i++)
	{
		CircleListObject->circlelinklist_insertnode(CircleListObject,i,i);
	}
	CircleListObject->circlelinklist_display(CircleListObject,20);
	CircleListObject->circlelinklist_delectnode(CircleListObject,2);
	CircleListObject->circlelinklist_display(CircleListObject,20);
	CircleListObject->circlelinklist_delectnode(CircleListObject,0);
	CircleListObject->circlelinklist_display(CircleListObject,20);
	
	return 0;
}


循环链表及其接口函数的C++实现


测试正确~~~

循环链表及其接口函数的C++实现,布布扣,bubuko.com

循环链表及其接口函数的C++实现

上一篇:java笔记之封装、继承、重写、多态以及类型转换


下一篇:java引发的思考——网络编程初解