双向循环链表

使用双向循环链表实现插入、删除、排序等功能。

 头文件代码如下:

#ifndef _LINKLIST_H
#define _LINKLIST_H

#define Success 10000
#define Failure 10001

typedef int Element;

struct node
{
	Element s;
	struct node *next;
	struct node *pre;
};

typedef struct node SN;

int LinkInit(SN **head);
int LinkInsert(SN *head, int n, Element e);
int LinkCheck(SN *head, void (*p)(Element));
int LinkLength(SN *head);
int GetElement(SN *head, int p, Element *e);
int ElementDelete(SN *head, int n, Element *e);
int LinkClear(SN *head);
int LinkDestroy(SN **head);
int LinkReverse(SN *head);

#endif

功能函数代码如下:

#include <stdlib.h>
#include "LinkList.h"

int LinkInit(SN **head)
{
	*head = (SN *)malloc(sizeof(SN) * 1);
	if(NULL == *head)
	{
		return Failure;
	}

	(*head)->next = *head;
	(*head)->pre = *head;
	return Success;
}

int LinkInsert(SN *head, int n, Element e)
{
	SN *p = head;
	SN *q;
	int k = 1;
	
	if(NULL == head)
	{
		return Failure;
	}
	if(n > LinkLength(head))
	{
		return Failure;
	}
	while(k < n)
	{
		p = p->next;
		k++;
	}
	if(k > n)
	{
		return Failure;
	}

	q = (SN *)malloc(sizeof(SN) * 1);
	q->s = e;
	q->next = p->next;
	q->pre = p;
	p->next = q;
	q->next->pre = q;
	return Success;
}

int LinkCheck(SN *head, void (*p)(Element))
{
	if(head == NULL)
	{
		return Failure;
	}

	SN *q = head->next;
	while(q != head)
	{
		p(q->s);
		q = q->next;
	}

	return Success;
}

int LinkLength(SN *head)
{
	if(head == NULL)
	{
		return Failure;
	}
	int len = 0;
	SN *p = head->next;
	while(p != head)
	{
		len++;
		p = p->next;
	}
	return len;
}

int GetElement(SN *head, int p, Element *e)
{
	if(head == NULL)
	{
		return Failure;
	}
	SN *q = head->next;
	int i = 1;
	if(p > LinkLength(head) + 1)
	{
		return Failure;
	}
	while(i < p)
	{
		i++;
		q = q->next;
	}
	while(i > p)
	{
		return Failure;
	}
	*e = q->s;
	return Success;
}

int Location(SN *head, Element e, int (*p)(Element, Element))
{
	if(head == NULL)
	{
		return Failure;
	}	
	SN *q = head->next;
	int len = 1;
	while(q != head)
	{
		if(p(e, q->s))
		{
			return len;
		}
		len++;
		q = q->next;
	}
	return Failure;
}

int ElementDelete(SN *head, int n, Element *e)
{
	SN *p = head;
	int k = 1;
	if(NULL == head)
	{
		return Failure;
	}
	if(n > LinkLength(head) + 1)
	{
		return Failure;
	}
	while(k < n)
	{
		p = p->next;
		k++;
	}
	if(k > n)
	{
		return Failure;
	}

	SN *q = p->next;
	*e = q->s;
	p->next = q->next;
	q->next->pre = q;
	free(q);

	return Success;
}

int LinkClear(SN *head)
{
	if(head == NULL)
	{
		return Failure;
	}
	SN *p = head->next;
	while(p != head)
	{
		head->next = p->next;
		free(p);
		p = head->next;
	}
	head->pre = head;
	return Success;
}

int LinkDestroy(SN **head)
{
	if(head == NULL)
	{
		return Failure;
	}
	free(*head);
	(*head) = NULL;
	return Success;
}

主函数代码段如下:

#include "LinkList.h"
#include <stdio.h>

int equal(Element s1, Element s2)
{
	return (s1 == s2) ? 1 : 0;
}

void print(Element s)
{
	printf("%d ", s);
}

int main()
{
	int ret, i;
	SN *head = NULL;
	srand(time(NULL));

	ret = LinkInit(&head);
	if(ret == Success)
	{
		printf("Init Success!\n");
	}
	else
	{
		printf("Init Failure!\n");
	}
 	
	for(i = 0; i < 10; i++)
	{
		ret = LinkInsert(head, i+1, rand() % 20);
		if(ret == Failure)
		{
			printf("Insert Failure!\n");
		}
		else
		{
			printf("Insert Success!\n");
		}
	}

	ret = LinkCheck(head, print);
	if(ret == Success)
	{
		printf("\nCheck Success!\n");
	}
	else
	{
		printf("\nCheck failure!\n");
	}

	ret = LinkCheck(head, print);
	if(ret == Success)
	{
		printf("\nCheck Success!\n");
	}
	else
	{
		printf("\nCheck failure!\n");
	}

	ret = LinkLength(head);
	if(ret == Failure)
	{
		printf("Get Length Failure!\n");
	}
	else
	{
		printf("Length = %d\n", ret);
	}

	Element e;
	int p = 11;
	ret = GetElement(head, p, &e);
	if(ret == Success)
	{
		printf("%dth is Element %d\n", p, e);
	}
	else
	{
		printf("Get element failure!\n");
	}
	
	e = 3;
	ret = Location(head, e, equal);
	if(ret == Failure)
	{
		printf("%d is not exist!\n", e);
	}
	else
	{
		printf("%d is %dth!\n", e, ret);
	}
	
	p = 4;
	ret = ElementDelete(head, p, &e);
	if(ret == Failure)
	{
		printf("Delete Failure!\n");
	}
	else
	{
		printf("%d is deleted success!\n", e);
	}

	ret = LinkCheck(head, print);
   	if(ret == Success)
    {
	   printf("\nCheck Success!\n");
   	}
 	else
  	{
       printf("\nCheck failure!\n");
  	}

	ret = LinkClear(head);
	if(ret == Success)
	{
		printf("Clear Success!\n");
	}
	else
	{
		printf("Clear Failure!\n");
	}

	ret = LinkDestroy(&head);
	if(ret == Success)
	{
		printf("Destroy Success!\n");
	}
	else
	{
		printf("Destroy Failure!\n");
	}


	return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值