数据结构2循环链表

循环链表

定义

尾结点指向头结点的单链表。解决了单链表无法查找某一结点的问题。

定义结点方式以及初始化

结点定义方式

#define MAXSIZE 20//定义数组的最大长度 
typedef struct ClinkList
{
 int data;
 struct CLinkList *next;
}Mode;

类似单链表。

循环链表的初始化:

//初始化循环链表
void ds_init(Mode **pNode)//**pNode 指向链表的第一个结点 
{
	int item;
	Mode *temp;//链表中的结点 
	Mode *target;//结点计数器
	
	printf("输入结点的值,输入0完成始化\n");	
	while(1)
	{
		scanf("%d",&item);
		fflush(stdin); //清除缓冲区 
		
		if(item == 0)
		return;

		if((*pNode) == NULL)//空表 ;head指向NULL 此时光有一个表头  下边是初始化表头 
		{
			*pNode = (Mode*)malloc(sizeof(struct ClinkList));
			if(!(*pNode))
			exit(0);
		
			(*pNode)->data = item;
			(*pNode)->next = *pNode; 
 		}
 		else
 		{
 			/*找到next指向第一个结点的结点*/
 		for
 		(
 			target = (*pNode)/*令target是指向链表的第一个结点 然后开始循环*/;
 			target ->next != (*pNode);   /*当这个结点的下一个结点指向目标第一个结点的时候停止循环;相当于遍历target = target->next);*/
 			target = target->next     /*计数器向下*/ 
 		);
		
		/*生成结点*/
		temp =  (Mode*)malloc(sizeof(struct ClinkList));
		if(!temp)
		exit(0);
		
		temp->data = item;//赋值
		item->next = *pNode;
		target->next = temp;
		}
	}
}

对循环链表的一些操作

包含了单链表无法精确定位的插入,查找,删除操作。

插入

#include<stdio.h>
#include<string.h>

typedef struct CLinkList
{
 	int data;
 	struct CLinkList *next;
}node;
//这里跟上文又有些微的不同,所以重新写了一下

void ds_insert(node **pNode,int i)
{
 	node *temp;
	 node *target;
 	node *p;
 	int item;
	 int j = 1;
 	printf("输入要插入结点的值");
	 scanf("%d",&item);
 
	 if (i == 1)
 {
	  //新插入的结点作为第一个结点
 	 temp = (node *)malloc(sizeof(struct CLinkList));
  
	  if(!temp)
 	 exit(0);
  	 temp->data = item;
  
	  //寻找到最后一个结点
	  for(target = (*pNode);target->next != (*pNode);target = target ->next)
 	 ;
  
  	temp->next = (*pNode); //插入结点指向头结点 
 	 target->next = temp;//尾结点指向插入结点 
 	 *pNode = temp; //令*pNote是新的头指针。
 }
	  else
	 {
 		 target = (&pNode);
  
	  	for( ; j<= (i-1); ++j)
		  {
	  	 	target = target->next;
		  }
		  //假如i=3   循环2次 target指向了第三个元素

 		 temp = (node *)malloc(sizeof(struct CLinkList));
  
 		 if(!temp)
		  exit(0);
  
 		 temp->data = item;
  
		  p = target ->next;
		  target->next = temp;
 		 temp->next = p;
	 }
}

删除

typedef struct ClinkList
{
	 int data;
	 struct CLinkList *next;
}node;
//这里边node成为了结构体ClinkList 

//删除结点 
void ds_delete(node **pNode,int i) //此函数需要的两个参数:指向链表的第一个结点,删除的位置 
{
 	node *target;
	 node *temp;
	 int j = 1;
 
	 if(i == 1)
	 {
		  //删除的是第一个结点
 	 	//找到最后一个结点
		  for(target = *pNode; target->next != *pNode;target = target ->next)
		   ;
  		 temp = *pNode;   
   		*pNode = (*pNode)->next;
  		 target->next = *pNode; 
  		 free(temp);
	 }
	 else
	  {
 		 target = *pNode;
  
		  for ( ; j < i-1; ++j)
 	 {
		 target = target->next;
	  }
  
 		 temp = target->next;
  		target->next = temp ->next;
 		 free (temp);
	 }
}

返回元素位置

//返回代码所在位置

#include<stdio.h>
#include<string.h>
typedef struct CLinkList
{
	 int data;
	 struct CLinkList *next;
}node;

int ds_search(node *pNode,int elem)
{
	 node *target;
	 int i = 1;
 
 	for(target = pNode;target->data != elem &&  target->next != pNode;/*搜完的话退出循环*/ ++i)
 	 {
 		  target = target->next;
	 }
 
	 if(target->next == pNode)
		  return 0;
	 else 
		  return 1;
}   	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值