双向链表排序,插入、删除

不考虑性能,没优化,能完成基本工作

// list.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<stdlib.h>
typedef struct tag_data
{
	int age;
}Data;
typedef struct tag_node Node;
typedef struct tag_node
{
	Node* pnext;
	Node* pprev;
	Data data;
}Node;
typedef struct tag_nodecb NodeCb;
typedef struct tag_nodecb
{
	Node* pHead;
	Node* pTail;
	int nodecount;
}NodeCb;
NodeCb* g_pNodeCb = NULL;
int InitList(NodeCb** ppNodeCb)
{
	NodeCb* pNodeCbTmp = (NodeCb*)malloc(sizeof(NodeCb));
	if (pNodeCbTmp == NULL)
	{
		printf("malloc NodeCb failed...\n");
		return -1;
	}
	pNodeCbTmp->pHead = NULL;
	pNodeCbTmp->pTail = NULL;
	pNodeCbTmp->nodecount = 0;

	*ppNodeCb = pNodeCbTmp;
	return 0;
}
Node* FindNode(NodeCb* pNodeCb, int index)
{
	//0,max  
	int i;
	Node* pNodeTmp = g_pNodeCb->pHead;
	for (i = 1; i < index; i++, pNodeTmp = pNodeTmp->pnext)
	{

	}
	return pNodeTmp;

}
int InsertNode(Node* pNodeNew, int index)
{
	if (g_pNodeCb->pTail == NULL || g_pNodeCb->pHead == NULL)
	{
		g_pNodeCb->pHead = pNodeNew;
		g_pNodeCb->pTail = pNodeNew;
		pNodeNew->pnext = NULL;
		pNodeNew->pprev = NULL;
		(g_pNodeCb->nodecount) ++;
		return 0;
	}
	if (index > g_pNodeCb->nodecount)
	{
		pNodeNew->pnext = NULL;
		pNodeNew->pprev = g_pNodeCb->pTail;
		g_pNodeCb->pTail->pnext = pNodeNew;
		g_pNodeCb->pTail = pNodeNew;
		(g_pNodeCb->nodecount)++;
		return 0;
	}
	if (index == 1 || index ==0)
	{
		g_pNodeCb->pHead->pprev = pNodeNew;
		pNodeNew->pprev = NULL;
		pNodeNew->pnext = g_pNodeCb->pHead;
		g_pNodeCb->pHead = pNodeNew;
		(g_pNodeCb->nodecount)++;
		return 0;
	}
	
	Node* pNodeTmp = FindNode(g_pNodeCb, index);
	pNodeNew->pnext = pNodeTmp;
	pNodeNew->pprev = pNodeTmp->pprev;
	pNodeTmp->pprev->pnext = pNodeNew;
	pNodeTmp->pprev = pNodeNew;
	(g_pNodeCb->nodecount)++;
	return 0;
}
int DeleteNode(NodeCb* pNodeCb, int index)
{
	if (g_pNodeCb->pTail == NULL || g_pNodeCb->pHead == NULL)
	{
		printf("empty list...\n");
		return -1;
	}
	if (index == 0 || index == 1)
	{
		g_pNodeCb->pHead = g_pNodeCb->pHead->pnext;
		g_pNodeCb->pHead->pprev = NULL;
		(g_pNodeCb->nodecount)--;
		//malloc的节点需要free
		return 0;
	}
	if (index >= g_pNodeCb->nodecount)
	{
		g_pNodeCb->pTail = g_pNodeCb->pTail->pprev;
		g_pNodeCb->pTail->pnext = NULL;
		(g_pNodeCb->nodecount)--;
		//malloc的节点需要free
		return 0;
	}
	Node* pNodeTmp = FindNode(g_pNodeCb, index);

	pNodeTmp->pnext->pprev = pNodeTmp->pprev;
	pNodeTmp->pprev->pnext = pNodeTmp->pnext;
	(g_pNodeCb->nodecount)--;
	//malloc的节点需要free
	return 0;
}
int ShowList(NodeCb* pNodeCb)
{
	Node* pNodeTmp = pNodeCb->pHead;

	for (int i = 0; i < pNodeCb->nodecount; i++, pNodeTmp = pNodeTmp->pnext)
		printf("%d\n", pNodeTmp->data.age);
	printf("--------------------------\n");
	return 0;
}
int SwapNode( Node* pNodeJ,Node* pNodeJ_1)
{
	if (pNodeJ->pprev == NULL)
	{
		pNodeJ->pnext = pNodeJ_1->pnext;
		pNodeJ_1->pnext->pprev = pNodeJ;
		pNodeJ_1->pnext = pNodeJ;
		pNodeJ->pprev = pNodeJ_1;
		g_pNodeCb->pHead = pNodeJ_1;
		pNodeJ_1->pprev = NULL;
		return 0;
	}
	if (pNodeJ_1->pnext == NULL)
	{
		pNodeJ->pprev->pnext = pNodeJ_1;
		pNodeJ_1->pprev = pNodeJ->pprev;
		pNodeJ_1->pnext = pNodeJ;
		pNodeJ->pprev = pNodeJ_1;
		pNodeJ->pnext = NULL;
		g_pNodeCb->pTail = pNodeJ;
		return 0;
	}

	pNodeJ->pprev->pnext = pNodeJ_1;
	pNodeJ_1->pprev = pNodeJ->pprev;
	
	pNodeJ->pnext = pNodeJ_1->pnext;
	pNodeJ_1->pnext->pprev = pNodeJ;
	pNodeJ_1->pnext = pNodeJ;
	pNodeJ->pprev = pNodeJ_1;
	return 0;


}
int BubbleSort(NodeCb* pNodeCb)
{
	Node* pNodeJ = NULL;
	Node* pNodeJ_1 = NULL;
	int i = 1, j = 2;

	for (i = pNodeCb->nodecount; i > 1; i--)
    for (j = 1; j < i; j++)
	//for (j = 1; j < pNodeCb->nodecount; j++)
	{
		pNodeJ   = FindNode(pNodeCb, j);
		pNodeJ_1 = FindNode(pNodeCb, j + 1);

		if (pNodeJ->data.age>pNodeJ_1->data.age)
		{
			SwapNode(pNodeJ, pNodeJ_1);
		}
	}
	return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
	Node a, b, c, d, e, f,g,h;
	a.data.age = 4;
	b.data.age = 8;
	c.data.age = 2;
	d.data.age = 5;
	e.data.age = 10;
	f.data.age = 6;
	g.data.age = 3;
	h.data.age = 1;
	InitList(&g_pNodeCb);
	InsertNode(&a, 1);
	InsertNode(&b, 10);
	InsertNode(&c, 10);
	InsertNode(&d, 2);
	InsertNode(&e, 4); 
	InsertNode(&f, 10);
	InsertNode(&g, 5);
	InsertNode(&h, 6);
	ShowList(g_pNodeCb);
	//DeleteNode(g_pNodeCb, 5);
	//ShowList(g_pNodeCb);
	BubbleSort(g_pNodeCb);
	ShowList(g_pNodeCb);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值