循环链表的实现与操作(C语言实现)

循环链表是另一种形式的链式存贮结构。它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环。

循环链表的操作
1,循环链表的新操作
2, 获取当前游标指向的数据元素
3, 将游标重置指向链表中的第一个数据元素

4,将游标移动指向到链表中的下一个数据元素

5,直接指定删除链表中的某个数据元素


CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);
CircleListNode* CircleList_Reset(CircleList* list);
CircleListNode* CircleList_Current(CircleList* list);
CircleListNode* CircleList_Next(CircleList* list);


头文件:

#ifndef _CIRCLE_H_
#define _CIRCLE_H_
//采用数据封装的方式,防止在主函数修改其中的属性值(有点点像面向对象中的私有属性)
typedef void CircleList;
typedef struct CircleListNode   //声明指针域
{
	CircleListNode * next;
}CircleListNode;

CircleList * CircleList_Create();

void CircleList_DesTroy(CircleList * list);

void CircleList_Clear(CircleList* list);

int CircleList_Length(CircleList* list);

int CircleList_Insert(CircleList* list, CircleListNode* node, int pos);

CircleListNode* CircleList_Get(CircleList* list, int pos);

CircleListNode* CircleList_Delete(CircleList* list, int pos);

CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);

CircleListNode* CircleList_Reset(CircleList* list);

CircleListNode* CircleList_Current(CircleList* list);

CircleListNode* CircleList_Next(CircleList* list) ;

#endif 

源文件:

// 循环链表.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <malloc.h>
#include <stdlib.h>
#include "CircleList.h"

typedef struct   //定义头结点
{	   
   CircleListNode header;
   CircleListNode* sLider;  //游标
   int len;
}TCircleList;

struct Value        //定义数据结构体类型
{
	
    CircleListNode header;  //指针域
    int v;                //数据域
};




int _tmain(int argc, _TCHAR* argv[])
{	
	
     int i = 0;
    CircleList* list = CircleList_Create();
    
    struct Value v1;
    struct Value v2;
    struct Value v3;
    struct Value v4;
    struct Value v5;
    struct Value v6;
    struct Value v7;
    struct Value v8;
   
    
    v1.v = 1;
    v2.v = 2;
    v3.v = 3;
    v4.v = 4;
    v5.v = 5;
    v6.v = 6;
    v7.v = 7;
    v8.v = 8;
	
    
    CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));
    CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));
	//插入到 5 的位置,前面的顺序为 1->2->3->4  
	//                      下标: 0  1  2  3
	//                             4  5 ...
	//故而插入到 5 的位置时  打印的结果应为 :  1,5,2,3,4,后面的结果也验证了其正确性
    CircleList_Insert(list, (CircleListNode*)&v5, 5);
	
    
    for(i=0; i<CircleList_Length(list); i++)
    {
        struct Value* pv = (struct Value*)CircleList_Get(list, i);
        
        printf("%d\n", pv->v);
    }
    
    while( CircleList_Length(list) > 0 )
    {
        struct Value* pv = (struct Value*)CircleList_Delete(list, 0);
        
        printf("删除了:%d\n", pv->v);
    }
    
	
    CircleList_DesTroy(list);

	system("pause");
	return 0;
}

//创建
CircleList * CircleList_Create()
{
	TCircleList* list = (TCircleList*)malloc(sizeof(TCircleList));	
	if(NULL != list)
	{		
		list->sLider = NULL;
		list->header.next = NULL;
		list->len = 0;
		
	}
	return list;
}

//销毁
void CircleList_DesTroy(CircleList * list)
{
	free(list);
}
//清空
void CircleList_Clear(CircleList* list)
{
	TCircleList * sList = (TCircleList*)list;
	if(NULL != sList)
	{
		sList->len = 0;
		sList->header.next = NULL;
		sList->sLider = NULL;
	}
}
//获得长度
int CircleList_Length(CircleList* list)
{
	TCircleList * sList = (TCircleList*)list;
	int len = -1;
	if(NULL != sList)
	{
		len = sList->len;
	}
	return len;
}
//插入
int CircleList_Insert(CircleList* list, CircleListNode* node, int pos)
{
	TCircleList * sList = (TCircleList*)list;
	int i = 0;
	int ret = 0;
	sList->len;
	if((NULL != sList) && (pos>=0) && (NULL != node))
	{
		CircleListNode * current = (CircleListNode*)sList;
		for ( i=0; i<pos && current->next != NULL; i++)
		{
			current = current->next;
		}
		
		node->next = current->next;		
		current->next = node;
		if(sList->len == 0)
		{
			sList->sLider = node;
			node->next = node;
		}
		++(sList->len);
		ret = 1;
		
		
	}
   return ret;
}
//获得结点
CircleListNode* CircleList_Get(CircleList* list, int pos)
{
	TCircleList * sList = (TCircleList*)list;
	CircleListNode * resNode = NULL;
	int i = 0;
	if((NULL != sList) && (pos>=0))
	{
		CircleListNode * current = (CircleListNode*)sList;
		for( i=0; i<pos; i++)
		{
			//i=0时,current为头结点,current->next为真正的第一个结点
			current = current->next;
		}
		resNode = current->next;
	}
	return resNode;
}

//删除
CircleListNode* CircleList_Delete(CircleList* list, int pos)
{
	TCircleList * sList = (TCircleList*)list;
	int i = 0;
	CircleListNode * resnode = NULL;
	CircleListNode* first = sList->header.next;
	CircleListNode* last = (CircleListNode*)CircleList_Get(list,sList->len-1);
	if((NULL != sList) && (pos >= 0) && (pos < sList->len))
	{
		CircleListNode * current = (CircleListNode*)sList;
		for ( i=0; i<pos; i++)
		{
			//i=0时,current为头结点,current->next为真正的第一个结点
			current = current->next;
		}
		resnode = current->next;
		current->next = resnode->next;

		if(first == resnode)
		{
			sList->header.next = first->next;
			last->next = first->next;
		}
		if(sList->sLider == resnode)
		{
			sList->sLider = resnode->next;
		}
		if(sList->len == 0)
		{
			sList->header.next = NULL;
			sList->sLider = NULL;
		}
	}
	sList->len--;
	return resnode;
}

//根据结点来删除
CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node)
{
	TCircleList * sList = (TCircleList*)list;
	CircleListNode* resnode = NULL;
	int i = 0;
	if(NULL != sList)
	{
		CircleListNode* current = (CircleListNode*)sList;
		for ( i=0; i<sList->len; i++)
		{
			if(node == current->next)
			{
				resnode = current->next;
				break;
			}
			current = current->next;
		}
		if(NULL != resnode)
		{
			CircleList_Delete(sList,i);
		}
	}
	return resnode;
}


//将游标重置回第一个元素
CircleListNode* CircleList_Reset(CircleList* list) 
{
    TCircleList* sList = (TCircleList*)list;
    CircleListNode* ret = NULL;
    
    if( sList != NULL )
    {
		sList->sLider= sList->header.next;
        ret = sList->sLider;
    }
    
    return ret;
}

//获得当前游标下的结点
CircleListNode* CircleList_Current(CircleList* list) 
{
	
    TCircleList* sList = (TCircleList*)list;
    CircleListNode* ret = NULL;
	sList->len;
	sList->header;
	sList->sLider;
    if( sList != NULL )
    {
		ret = sList->sLider;
    }
    
    return ret;
}


//将游标移到下一个结点并获得当前移动前的结点
CircleListNode* CircleList_Next(CircleList* list) 
{
    TCircleList* sList = (TCircleList*)list;
    CircleListNode* ret = NULL;
    
	if( (sList != NULL) && (sList->sLider != NULL) )
    {
        ret = sList->sLider;
        sList->sLider = ret->next;
    }
    
    return ret;
}

运行结果:

1
5
2
3
4
删除了:1
删除了:5
删除了:2
删除了:3
删除了:4
请按任意键继续. . .

如有错误,望不吝指出。


循环链表的实现与操作(C语言实现),布布扣,bubuko.com

循环链表的实现与操作(C语言实现)

上一篇:Swift编程语言初探


下一篇:java 使用poi 结合Struts2导出execl表格