循环链表的基本操作

#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
using namespace std;


typedef int dataType;


typedef struct Node{
	dataType data;
	struct Node *next;
}CLListNode;
typedef CLListNode *pCLList;


//使用尾插法,创建一个循环链表
pCLList CreateLinkList_1_9_order()
{
	pCLList head=(CLListNode*)malloc(sizeof(CLListNode));
	head->next=NULL;
	CLListNode *p=NULL;
	CLListNode *r=head;
	int num=9,i=1;
	while (i<=num)
	{
		p=(CLListNode*)malloc(sizeof(CLListNode));
		p->data=i;
		r->next=p;
		r=p;
		i++;
	}
	r->next=head;
	return head;
}


//输出循环链表
void OutPut(pCLList l)
{
	CLListNode *p=l->next;
	cout<<"LinkList:";
	while(p!=l)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl<<endl;
}


//释放循环链表内存
void FreeCLinkList(pCLList *l)  //传入pCLList二级指针
{
	CLListNode *p=(*l)->next,*temp;  //定义移动指针p,初始化为头指针,指向头结点
	free(*l);
	while (p!=*l)
	{
		temp=p;
		p=p->next;
		free(temp);
	}
	*l=NULL;
}


//获取链表长度
int GetLListLength(pCLList l)
{
	CLListNode *p=l->next;
	int i=0;
	while (p!=l)
	{
		i++;
		p=p->next;
	}
	return i;
}


//连接两个循环链表
void ConnectTwoCLList(pCLList a,pCLList b)
{
	CLListNode *pa=a->next,*pb=b->next,*temp=b;
	while(pa->next!=a)
	{
		pa=pa->next;
	}
	pa->next=b->next;
	free(temp);
	while (pb->next!=b)
	{
		pb=pb->next;
	}
	pb->next=a;
}


int main()
{
	pCLList a=CreateLinkList_1_9_order();
	OutPut(a);
	pCLList b=CreateLinkList_1_9_order();
	OutPut(b);


	ConnectTwoCLList(a,b);
	OutPut(a);


	b=NULL;
	FreeCLinkList(&a);
	system("pause");
	return 1;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值