双链表基本操作

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

#define DNODE_SIZE sizeof(DNode)

typedef struct _tagDNode
{
	int value;
	struct _tagDNode *prov;
	struct _tagDNode *next;
}DNode;

DNode* create_double_link(DNode *node)
{
	DNode *head = NULL;
	if (node->value == 0)
	{
		return head;
	}

	head = (DNode*)malloc(DNODE_SIZE);
	head->value = node->value;
	head->prov = NULL;
	head->next = NULL;
	return head;
}

DNode* insert2tail(DNode *head,DNode *node)
{
	DNode *cur = head;
	DNode *tn = NULL;

	tn = (DNode*)malloc(DNODE_SIZE);
	tn->value = node->value;
	
	while (cur->next != NULL)
	{
		cur = cur->next;
	}

	cur->next = tn;
	tn->prov = cur;
	tn->next = NULL;
	return head;
}

DNode* insert2head(DNode *head,DNode *node)
{
	DNode *cur = head;
	DNode *tn = NULL;

	tn = (DNode*)malloc(DNODE_SIZE);
	tn->value = node->value;
	
	tn->prov = NULL;
	tn->next = cur;
	cur->prov = tn;

	return tn;
}

DNode* insert(DNode *head,DNode *node)
{
	DNode *cur = head;
	DNode *tn = NULL;

	tn = (DNode*)malloc(DNODE_SIZE);
	tn->value = node->value;

	if (cur->value >= node->value)
	{
		head = insert2head(head,node);
		return head;
	}

	while (cur->next != NULL)
	{
		if (cur->value < node->value)
		{
			cur = cur->next;
			continue;
		}
		break;
	}

	if ((cur ->next == NULL) && (cur->value <= node->value))
	{
		head = insert2tail(head,node);
		return head;
	}
	
	tn->prov = cur->prov;
	cur->prov->next = tn;
	tn->next = cur;
	cur->prov = tn;

	return head;
}

void print_link_by_head(DNode *head)
{
	DNode *tp = head;
	while (tp->next != NULL)
	{
		printf("%d ",tp->value);
		tp = tp->next;
	}
	printf("%d ",tp->value);
}

void print_link_by_tail(DNode *tail)
{
	DNode *tp = tail;
	while (tp->prov != NULL)
	{
		printf("%d ",tp->value);
		tp = tp->prov;
	}
	printf("%d ",tp->value);
}

void delete_double_link(DNode *head)
{
	DNode *cur = head;
	DNode *prov= head;
	while (cur->next != NULL)
	{
		prov = cur;
		cur = cur->next;
		free(prov);
	}
	free(cur);
	cur = NULL;
	prov = NULL;
}

DNode *gettail(DNode *head)
{
	DNode *cur = head;
	while (cur->next != NULL)
	{
		cur = cur->next;	
	}
	return cur;
}
int main(void)
{
	DNode *head = NULL;
	DNode *tail = NULL;
	DNode node = {0};
	int i = 0;
	node.value = 1;
	head = create_double_link(&node);

	node.value = 65;
	head = insert(head,&node);

	node.value = 3;
	head = insert(head,&node);

	node.value = 343;
	head = insert(head,&node);


	node.value = 33;
	head = insert(head,&node);

	node.value = 2;
	head = insert(head,&node);

	node.value = 99;
	head = insert(head,&node);

	print_link_by_head(head);
	putchar('\n');
	tail = gettail(head);
	print_link_by_tail(tail);
	putchar('\n');
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值