Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12839154
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类:

2012-12-25 09:25:51

一、工程源码


点击(此处)折叠或打开

  1. // d-linklist.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <malloc.h>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>

  8. #define OK 0
  9. #define FAIL -1

  10. typedef struct DLINKLIST
  11. {
  12.     DLINKLIST *pPrevNode;
  13.     DLINKLIST* pNextNode;
  14.     int data;
  15. }List;

  16. int g_data = 0;

  17. List *Create_List()
  18. {
  19.     List *pHeadNode = NULL;
  20.     pHeadNode = (List *) malloc(sizeof(List));
  21.     if( pHeadNode == NULL )
  22.         return NULL;

  23.     pHeadNode->data = g_data++;
  24.     pHeadNode->pPrevNode = NULL;
  25.     pHeadNode->pNextNode = NULL;

  26.     return pHeadNode;
  27. }

  28. //****************************************************************************************
  29. // 头插法添加链表节点
  30. //****************************************************************************************
  31. int Insert_Node_From_Head(List **ppHeadNode)
  32. {
  33.     List *pNewNode = NULL;
  34.     List *p = (*ppHeadNode);

  35.     int i = 0;
  36.     do
  37.     {
  38.         pNewNode = (List *)malloc(sizeof(List));
  39.         if(pNewNode == NULL)
  40.             return FAIL;

  41.         pNewNode->data = g_data--;
  42.         p->pPrevNode = pNewNode;
  43.         pNewNode->pNextNode = p;
  44.         pNewNode->pPrevNode = NULL;
  45.         p = pNewNode;
  46.         i ++;
  47.     }while(i < 3);

  48.     (*ppHeadNode) = p;

  49.     return OK;
  50. }

  51. //****************************************************************************************
  52. // 尾插法添加链表节点
  53. //****************************************************************************************
  54. int Insert_Node_From_Tail(List **ppHeadNode)
  55. {
  56.     List *p = (*ppHeadNode);
  57.     List *pNewNode = NULL;
  58.     bool isCreateSuccess = false;

  59.     while(p->pNextNode != NULL )
  60.     {
  61.         p = p->pNextNode;
  62.     }

  63.     for(int i = 0; i<3; i++)
  64.     {
  65.         pNewNode = (List *)malloc(sizeof(List));
  66.         if( pNewNode == NULL)
  67.         {
  68.             isCreateSuccess = false;
  69.             continue;
  70.         }
  71.         else
  72.         {
  73.             isCreateSuccess = true;
  74.             pNewNode->data = g_data++;
  75.         }

  76.         p->pNextNode = pNewNode;
  77.         pNewNode->pPrevNode = p;
  78.         pNewNode->pNextNode = NULL;
  79.         p = pNewNode;
  80.     }

  81.     if(isCreateSuccess)
  82.         return OK;
  83.     else
  84.         return FAIL;
  85. }

  86. int Delete_Node(List **ppHeadNode,int data)
  87. {
  88.     List *p = *ppHeadNode; // 取出双链表的首地址
  89.     while(p->pNextNode != NULL)
  90.     {
  91.         if(p->data == data)
  92.         {
  93.             /* 如果是头结点 */
  94.             if(p->pPrevNode == NULL)
  95.             {
  96.                 p->pNextNode->pPrevNode = NULL;
  97.                 *ppHeadNode = p->pNextNode;
  98.                 free(p);
  99.             }
  100.             else
  101.             {
  102.                 p->pNextNode->pPrevNode = p->pPrevNode;
  103.                 p->pPrevNode->pNextNode = p->pNextNode;
  104.                 free(p);
  105.             }
  106.             break;
  107.         }
  108.         else
  109.         {
  110.             p = p->pNextNode;
  111.         }
  112.     }
  113.     return OK;
  114. }

  115. void Show_List_Data(List *pHeadNode)
  116. {
  117.     List *p = pHeadNode;
  118.     printf("Current data in d-list is :\n");

  119.     while(p->pNextNode != NULL)
  120.     {
  121.         printf("%5d,",p->data);
  122.         p = p->pNextNode;
  123.     }

  124.     printf("\n\n");
  125. }

  126. int _tmain(int argc, _TCHAR* argv[])
  127. {
  128.     printf("===================D-LINK LIST OPERATION ================\n");
  129.     List *L = NULL;
  130.     int option = 0;
  131.     int del_data = 0;
  132.     bool isStopLoop = false;

  133.     if( (L=Create_List()) == NULL)
  134.     {
  135.         printf("Create d link list fail \n");
  136.         return FAIL;
  137.     }

  138.     while(true)
  139.     {
  140.         printf(" 0, insert d-node from head \n 1, insert d-node from tail \n 2,delete a d-node \n 3, show d-list 4, clear screen\n 5,exit\n");
  141.         scanf("%d",&option);
  142.         switch(option)
  143.         {
  144.         case 0:
  145.             if(Insert_Node_From_Tail(&L) == FAIL)
  146.                 printf("insert node fail");
  147.             Show_List_Data(L);
  148.             break;
  149.         case 1:
  150.             if(Insert_Node_From_Head(&L) == FAIL)
  151.                 printf("insert a node fail");
  152.             Show_List_Data(L);
  153.             break;
  154.         case 2:
  155.             printf("input data in node\n");
  156.             scanf("%d",&del_data);
  157.             if(Delete_Node(&L,del_data) == FAIL)
  158.                 printf("data isn't in d-list!\n");
  159.             Show_List_Data(L);
  160.             break;
  161.         case 3:
  162.             Show_List_Data(L);
  163.             break;
  164.         case 4:
  165.             /* 系统清屏 */
  166.             system("cls");
  167.             break;

  168.         case 5:
  169.             isStopLoop = true;
  170.             break;

  171.         default:
  172.             break;
  173.         }

  174.         if(isStopLoop)
  175.             break;
  176.     }

  177.     return 0;
  178. }

 

二、效果图

image

图 头插、尾插法添加节点

 

image

图 删除节点

阅读(3064) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~