链表


SNode* LinkInsert(SNode *head,SNode *newNode);
void LinkPrint(SNode *head);
SNode* LinkDelete(SNode *head,ElemType e);
SNode* LinkDestroy(SNode *head);


void DataStructure()
{
	int i;
	//队列
	//SQueue queue;
	//QueueInit(&queue);
	//_beginthread(WriteToQueue,0,&queue);
	//WriteToQueue(&queue);
	//free(queue.addr);

	//链表
	//SNode* head = (SNode*)malloc(sizeof(SNode));
	SNode* head =NULL;
	//head->next = head;
	//head->pre = head;
	for(i=0; i<5; i++)
	{
		//SNode* node = (SNode*)malloc(sizeof(SNode));
		SNode node;
		node.element = i+1;
		head = LinkInsert(head,&node);
	}
	for(i=3; i<7;i++)
	{
		head = LinkDelete(head,i);
		LinkPrint(head);
	}

	LinkPrint(head);
	head = LinkDestroy(head);
	LinkPrint(head);

}


/************************************************************************/
/*                                                                      */
/************************************************************************/

static SNode* LinkInsert(SNode *head,SNode *newNode)
{
	SNode* node,*ptr = head,*pt;
	assert(NULL != (node = (SNode*)malloc(sizeof(SNode))));
	node->element = newNode->element;
	if(head == NULL)
	{
		head = node;
		node->next = NULL;
		node->pre = NULL;
	}
	else
	{
		while(ptr != NULL)
		{
			if(ptr->element < node->element)
			{
				pt = ptr;
				ptr = ptr->next;
			}
			else
			{
				//ptrb->next = node;
				ptr->pre->next=node;
				node->pre = ptr->pre;
				node->next = ptr;
				ptr->pre = node;
			}
		}
		if(ptr == NULL)
		{
			pt->next = node;
			node->pre = pt;
			node->next = NULL;
		}
	}
	return head;
}


static void LinkPrint(SNode *head)
{
	while(head!=NULL)
	{
		printf("%d ",head->element);
		head = head->next;
	}
	printf("-\n");
}


static SNode* LinkDelete(SNode *head,ElemType e)
{
	SNode *ptr = head;
	while(ptr != NULL)
	{
		if(ptr->element != e)
		{
			ptr = ptr->next;
		}
		else if(ptr == head)
		{
			head = ptr->next;
			free(ptr);
			break;
		}
		else if(ptr->next == NULL)
		{
			//ptrb->next = NULL;
			ptr->pre->next = NULL;
			free(ptr);
			break;
		}
		else
		{
			ptr->pre->next = ptr->next;
			ptr->next->pre = ptr->pre;
			free(ptr);
			break;
		}
	}
	return head;
}


static SNode* LinkDestroy(SNode *head)
{
	SNode *ptr;
	while(head!=NULL)
	{
		ptr = head;
		//printf("%d ",head->element);
		head = head->next;
		free(ptr);
	}
	//printf("-\n");
	return head;
}


/*

void DataStructure()
{
	int i;
	//队列
	//SQueue queue;
	//QueueInit(&queue);
	//_beginthread(WriteToQueue,0,&queue);
	//WriteToQueue(&queue);
	//free(queue.addr);

	//链表
	SNode* head = (SNode*)malloc(sizeof(SNode));
	head->next = head;
	head->pre = head;
	for(i=0; i<5; i++)
	{
		SNode* node = (SNode*)malloc(sizeof(SNode));
		node->element = i+1;
		LinkInsert(head,node);
	}
	for(i=3; i<7;i++)
	{
		LinkDelete(head,i);
		LinkPrint(head);
	}

	LinkPrint(head);
	head = LinkDestroy(head);
	LinkPrint(head);

}

*/
SNode* LinkInsert(SNode *head,SNode *node)
{
	//SNode* node;
	head->next->pre = node;
	node->next = head->next;
	node->pre = head;
	head->next = node;

	return head;
}


void LinkPrint(SNode *head)
{
	SNode *nil = head;
	head = head->next;
	while(head != nil)
	{
		printf("%d ",head->element);
		head = head->next;
	}
	printf("-\n");
}


SNode* LinkDelete(SNode *head,ElemType e)
{
	SNode *ptr = head->next;
	while(ptr != head)
	{
		if(ptr->element != e)
		{
			ptr = ptr->next;
		}
		else
		{
			ptr->pre->next = ptr->next;
			ptr->next->pre = ptr->pre;
			free(ptr);
			break;
		}

	}
	return head;
}


SNode* LinkDestroy(SNode *head)
{
	SNode *ptr,*nil = head;
	head = head->next;
	while(head != nil)
	{
		ptr = head;
		//printf("%d ",head->element);
		head = head->next;
		free(head->pre);
	}
	//printf("-\n");
	head->next = head;
	head->pre = head;
	return head;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZenZenZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值