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

来源:互联网 发布:sql中给表起别名 编辑:程序博客网 时间:2024/06/03 04:41

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

// 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的节点需要freereturn 0;}if (index >= g_pNodeCb->nodecount){g_pNodeCb->pTail = g_pNodeCb->pTail->pprev;g_pNodeCb->pTail->pnext = NULL;(g_pNodeCb->nodecount)--;//malloc的节点需要freereturn 0;}Node* pNodeTmp = FindNode(g_pNodeCb, index);pNodeTmp->pnext->pprev = pNodeTmp->pprev;pNodeTmp->pprev->pnext = pNodeTmp->pnext;(g_pNodeCb->nodecount)--;//malloc的节点需要freereturn 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;}


0 0