带头结点的循环链表基本操作

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
	int data;
	struct node * next;
}Node,*link;

void InitList(link * list)
{
	(*list) = (link)malloc(sizeof(Node));
	if(!(*list))
	{
		exit(-1);
	}
	(*list)->data = -9;
	(*list)->next = *list;

	//printf("+++%d+++\n",(*list)->data);
}

void CreateList(link list)
{
	link p;
	p = (link)malloc(sizeof(Node));
	
	if(p)
	{
		printf("Please input the data\n");	
		scanf("%d",&(p->data));
	}
	else
	{
		exit(-1);
	}

	p->next = list->next;
	list->next = p;

	//printf("+++%d+++\n",list->next->data);
}

void ClearList(link list)
{
	link p = list->next;
	link q;
	while(p != list)
	{
		q = p->next;
		free(p);
		p = q;
	}
	list->next = list;
}

void PrintList(link list)
{
	if(list != NULL)
	{
		if(list->next != NULL)
		{
			link p = list->next;
			printf("The list is shown below:\n");
			while(p != list)
			{
				printf("---%d---\n",p->data);
				p = p->next;
			}
		}
		else
		{
			printf("The list is empty\n");
		}
	}
	else
	{
		printf("The list does not exist\n");
	}
}

void DestroyList(link *list)
{
	link p = *list;
	while(p->next != *list)
	{
		p = p->next;
	}

	p->next = NULL;

	link q;
	while(*list)
	{
		q = (*list)->next;
		free(*list);
		*list = q;
	}
}

int ListEmpty(link list)
{
	if(list->next != NULL)
	{
		printf("The list is not empty\n");
		return 0;
	}
	else
	{
		printf("The list is empty");
		return 1;
	}
}

int ListLength(link list)
{
	int i = 0;
	link p = list->next;
	while(p != list)
	{
		i++;
		p = p->next;
	}

	printf("The length of list is %d\n",i);
	return i;
}

int GetElem(link list, int i, int *e)
{
	link p = list->next;
	int j = 1;

	while(p != list && j < i)
	{
		p = p->next;
		j++;
	}

	if(j>i)
	{
		return -1;
	}

	*e = p->data;

	//printf("+++The data is %d+++\n",*e);
	return 0;
}

int equal(int a, int b)
{
	if(a == b)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int LocateElem(link list, int e, int(*compare)(int a, int b))
{
	int i = 1;
	link p = list->next;

	while(p->next != list)
	{	
		i++;
		if(compare(e,p->data))
		{
			return i;
		}
		p = p->next;
	}
	return -1;
}

int PriorElem(link list, int cur, int *pre)
{
	link p = list->next;
	link q;

	while(p != list)
	{
		q = p->next;
		if(q->data == cur)
		{
			*pre = p->data;
			return 0;
		}
		p = q;
	}
	return -1;
}

int NextElem(link list, int cur, int *next)
{
	link p = list->next;
	while(p->next != list)
	{
		if(p->data == cur)
		{
			*next = p->next->data;
			return 0;
		}
		p = p->next;
	}
	return -1;
}

int ListInsert(link list, int i, int e)
{
	link p;
	p = list;
	link tmp;
	int j = 0;
	while(p->next != list && j < i-1)
	{
		p = p->next;
		j++;
	}

	if(!p || j > i-1)
	{
		return -1;
	}

	tmp = (link)malloc(sizeof(node));
	tmp->data = e;
	tmp->next = p->next;
	p->next = tmp;
	return 0;
}

int ListDelete(link list, int i, int *e)
{
	int j = 1;
	link p = list->next;
	while(p != list && j < i-1)
	{
		p = p->next;
		j++;
	}
	if(!p || j > i-1)
	{
		return -1;
	}

	*e = p->next->data;
	link q = p->next;
	p->next = q->next;
	free(q);
	return 0;
}

int main()
{
	link head;
	InitList(&head);
	for(int i = 0; i < 3; i++)
		CreateList(head);
	PrintList(head);

	ListEmpty(head);
	ListLength(head);

	int f = 0;
	int choice = 0;
	printf("\nplease input the choice\n");
	scanf("%d",&choice);
	if(choice <= ListLength(head) && choice >= 0)
	{
		GetElem(head,choice,&f);
		printf("The value is %d\n",f);
	}
	else
	{
		printf("the choice is out of list length\n");
	}
	
	int result = LocateElem(head,3,equal);
	printf("\n+++The index is %d\n",result-1);

	result = -2;
	PriorElem(head,1,&result);
	printf("\n@@@result is %d\n",result);

	result = -2;
	NextElem(head,4,&result);
	printf("\n###result is %d\n",result);

	printf("\n-----------\n");
	result = -2;
	ListInsert(head,1,8);
	ListInsert(head,1,9);
	PrintList(head);

	printf("\n-----------\n");
	ListDelete(head,3,&result);
	PrintList(head);

	//ClearList(head);
	//PrintList(head);

	DestroyList(&head);
	PrintList(head);

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值