Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1441372
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2014-02-28 15:16:52


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>

  4. /***********************************     链表相关操作    ***********************/
  5. typedef struct NODE
  6. {
  7.     int data;
  8.     struct NODE* next;
  9. }NODE;

  10. NODE* Create()
  11. {
  12.     NODE* head;
  13.     head = (NODE*)malloc(sizeof(NODE));
  14.     if (NULL == head)
  15.         return NULL;
  16.     head->next = NULL;

  17.     return head;
  18. }

  19. void Insert(NODE**head, int dat)
  20. {
  21.     if (NULL == head)
  22.         return;

  23.     NODE* HeadList = *head;
  24.     NODE* Next = HeadList->next;
  25.     while (NULL != Next)
  26.     {
  27.         HeadList = Next;
  28.         Next = HeadList->next;
  29.     }
  30.     
  31.     NODE* InsertNode;
  32.     InsertNode = (NODE*)malloc(sizeof(NODE));
  33.     if (NULL == InsertNode)
  34.         return;
  35.     
  36.     InsertNode->data = dat;
  37.     InsertNode->next = Next;
  38.     HeadList->next = InsertNode;

  39.     return;
  40. }

  41. void Visit(NODE*head)
  42. {
  43.     if (NULL == head)
  44.         return;

  45.     NODE* Head = head->next;
  46.     while (Head != NULL)
  47.     {
  48.         printf("%d\n",Head->data);
  49.         Head = Head->next;
  50.     }
  51.     return;
  52. }

  53. void Delete(NODE**head, int dat)
  54. {
  55.     if (NULL == head)
  56.         return;
  57.     NODE* Head = *head;
  58.     NODE* Next = Head->next;

  59.     NODE* tmp;
  60.     while (Next != NULL)
  61.     {
  62.         if (Next->data == dat)
  63.         {
  64.             tmp = Next->next;
  65.             Head->next = tmp;
  66.             free(Next);
  67.             Next = tmp;
  68.         }
  69.         else
  70.         {
  71.             Head = Next;
  72.             Next = Head->next;
  73.         }
  74.     }
  75.     return;
  76. }

  77. /************************************** 树相关操作 ********************/
  78. typedef struct TREE
  79. {
  80.     char data;
  81.     struct TREE *left;
  82.     struct TREE *right;
  83. }TREE;

  84. static int index=0;
  85. char str[100] = "ABDH#K###E##CFI###G#J##";

  86. TREE* CreateHead()
  87. {
  88.     TREE* topHead;
  89.     topHead = (TREE*)malloc(sizeof(TREE));
  90.     if (NULL == topHead)
  91.         return NULL;
  92.     topHead->data = str[index++];
  93.     topHead->left = NULL;
  94.     topHead->right = NULL;

  95.     return topHead;
  96. }

  97. void CreateTree(TREE** tree)
  98. {
  99.     char tmp = str[index++];
  100.     if ('\0' == tmp)
  101.         return ;
  102.     if ('#' == tmp)
  103.         *tree = NULL;
  104.     else
  105.     {
  106.         *tree = (TREE*)malloc(sizeof(TREE));
  107.         if (NULL == *tree)
  108.         {
  109.             return;
  110.         }
  111.         (*tree)->data = tmp;

  112.         CreateTree(&(*tree)->left);
  113.         CreateTree(&(*tree)->right);
  114.     }
  115. }

  116. void PreVisit(TREE* tree)
  117. {
  118.     if (NULL != tree)
  119.     {
  120.         printf("%c\n",tree->data);
  121.         PreVisit(tree->left);
  122.         PreVisit(tree->right);
  123.     }
  124. }
  125. void MidVisit(TREE* tree)
  126. {
  127.     if (NULL != tree)
  128.     {
  129.         PreVisit(tree->left);
  130.         printf("%c\n",tree->data);
  131.         PreVisit(tree->right);        
  132.     }
  133. }
  134. void EndVisit(TREE* tree)
  135. {
  136.     if (NULL != tree)
  137.     {
  138.         PreVisit(tree->left);
  139.         PreVisit(tree->right);    
  140.         printf("%c\n",tree->data);
  141.     }
  142. }

  143. int Depth(TREE* tree)
  144. {
  145.     if (NULL == tree)
  146.         return 0;

  147.     int DepLeft=0,DepRight=0;
  148.     if (tree->left)
  149.         DepLeft = Depth(tree->left);
  150.     else
  151.         DepLeft = 0;

  152.     if (tree->right)
  153.         DepRight = Depth(tree->right);
  154.     else
  155.         DepRight = 0;

  156.     return DepLeft>DepRight? DepLeft+1:DepRight+1;
  157. }

  158. /**************************************** 图相关操作 **********************/
  159. /*
  160. #define N 10
  161. typedef struct Graph
  162. {
  163.     int Mutex[N][N];
  164.     int points;
  165.     int edges;
  166. }Graph;

  167. void CreateGraph(Graph* grap)
  168. {
  169.     printf("pls putin the points and edges!\n");
  170.     scanf("%d %d",&grap->points,&grap->edges);

  171.     int i,j;
  172.     for (i=0;i<grap->points;i++)
  173.     {
  174.         for (j=0;j<grap->points;j++)
  175.         {
  176.             grap->Mutex[i][j]=0;
  177.         }
  178.     }
  179.     int row = 0,col = 0;
  180.     for(i=0;i<grap->edges;i++)
  181.     {
  182.         printf("pls putin the %d edges : start and end !\n",i);
  183.         scanf("%d %d",&row,&col);
  184.         printf("edge weight :\n");
  185.         scanf("%d",&grap->Mutex[row][col]);
  186.         grap->Mutex[col][row] = grap->Mutex[row][col];
  187.     }
  188. }

  189. void VisitGrap(Graph grap)
  190. {
  191.     int begin=0,end=0;
  192.     printf("putin the point to check:begin end!\n");
  193.     scanf("%d %d",&begin,&end);
  194.     printf("Mutex[%d][%d]=%d\n",begin,end,grap.Mutex[begin][end]);
  195. }
  196. */

  197. //邻接链表方法
  198. #define MAX 100
  199. typedef enum
  200. {
  201.     NoVisit=0,
  202.     HasVisit=1
  203. }VisitStat;

  204. typedef enum
  205. {
  206.     NoExisit = 0,
  207.     IsExisit = 1
  208. }ExistStat;

  209. typedef enum
  210. {
  211.     NoEmpty = 0,
  212.     IsEmpty = 1
  213. }EmptyStat;

  214. typedef struct ListNode
  215. {
  216.     int pointNo;
  217.     int wight;
  218.     struct ListNode* next;
  219. }ListNode;

  220. typedef struct GNODE
  221. {
  222.     int pointNo;
  223.     ListNode* listHead;
  224. }GNODE,GPoint[MAX];

  225. typedef struct Graph
  226. {
  227.     GPoint GListNode;
  228.     int points;
  229.     int edges;
  230. }GRAPH;

  231. void CreateG(GRAPH* grap)
  232. {
  233.     scanf("%d %d",&grap->points,&grap->edges);

  234.     int i;
  235.     ListNode* tmpHead;

  236.     for (i=0; i<grap->points;i++)
  237.     {
  238.         grap->GListNode[i].pointNo = i;
  239.         tmpHead = (ListNode*)malloc(sizeof(ListNode));
  240.         if (NULL != tmpHead)
  241.         {
  242.             tmpHead->next = NULL;
  243.             grap->GListNode[i].listHead = tmpHead;
  244.         }
  245.     }
  246.     
  247.     int start=0,end=0,weight=0;
  248.     ListNode* InsertNode;
  249.     ListNode* Head;
  250.     ListNode* Next;
  251.     for (i=0;i<grap->edges;i++)
  252.     {
  253.         scanf("%d %d %d",&start,&end,&weight);
  254.         
  255.         InsertNode = (ListNode*)malloc(sizeof(ListNode));
  256.         if (NULL == InsertNode)
  257.             return ;
  258.         InsertNode->pointNo = end;
  259.         InsertNode->wight = weight;
  260.         Head = grap->GListNode[start].listHead;
  261.         Next = Head->next;
  262.         while (Next != NULL)
  263.         {
  264.             Head = Next;
  265.             Next = Head->next;
  266.         }
  267.         InsertNode->next = Next;
  268.         Head->next = InsertNode;

  269.         
  270.         InsertNode = (ListNode*)malloc(sizeof(ListNode));
  271.         if (NULL == InsertNode)
  272.             return ;
  273.         InsertNode->pointNo = start;
  274.         InsertNode->wight = weight;
  275.         Head = grap->GListNode[end].listHead;
  276.         Next = Head->next;
  277.         while (Next != NULL)
  278.         {
  279.             Head = Next;
  280.             Next = Head->next;
  281.         }
  282.         InsertNode->next = Next;
  283.         Head->next = InsertNode;
  284.     }

  285. }
  286. typedef struct QueueNode
  287. {
  288.     int PonintNo;
  289.     struct QueueNode* next;
  290. }QueueNode;

  291. QueueNode* CreateQueue()
  292. {
  293.     QueueNode* head = (QueueNode*)malloc(sizeof(QueueNode));
  294.     if (head == NULL)
  295.     {
  296.         return NULL;
  297.     }
  298.     head->next = NULL;
  299. }

  300. EmptyStat EmptyQueue(QueueNode* queueHead)
  301. {
  302.     if (NULL == queueHead)
  303.         return IsEmpty;
  304.     QueueNode* Head = queueHead;
  305.     QueueNode* Next = Head->next;
  306.     if (NULL == Next)
  307.         return IsEmpty;
  308.     else
  309.         return NoEmpty;
  310. }

  311. void EnterQueue(QueueNode** queue,int pointNo)
  312. {
  313.     if (queue == NULL)
  314.         return;

  315.     QueueNode* Head = *queue;
  316.     QueueNode* Next = Head->next;
  317.     while(Next != NULL)
  318.     {
  319.         if (Next->PonintNo == pointNo)
  320.         {
  321.             return;
  322.         }
  323.         Head = Next;
  324.         Next = Head->next;
  325.     }
  326.     QueueNode* InsertNode = (QueueNode*)malloc(sizeof(QueueNode));
  327.     if (NULL == InsertNode)
  328.     {
  329.         return;
  330.     }
  331.     InsertNode->PonintNo = pointNo;
  332.     InsertNode->next = Next;
  333.     Head->next = InsertNode;
  334.     return;
  335. }
  336. QueueNode* OutQueue(QueueNode** queue)
  337. {
  338.     if (NULL == queue)
  339.         return NULL;
  340.     
  341.     QueueNode* Head = *queue;
  342.     QueueNode* Next = Head->next;
  343.     QueueNode* tmp;
  344.     if (NULL == Next)
  345.         return NULL;
  346.     tmp = Next;
  347.     Head->next = Next->next;
  348.     return tmp;
  349. }

  350. void BFS(GRAPH* grap)
  351. {
  352.     int VisitLog[MAX];
  353.     int i;
  354.     for (i=0; i<grap->points; i++)
  355.     {
  356.         VisitLog[i] = NoVisit;
  357.     }

  358.     QueueNode* OutNode;
  359.     QueueNode* QueueHead = CreateQueue();
  360.     EnterQueue(&QueueHead,grap->GListNode[0].pointNo);
  361.     while(EmptyQueue(QueueHead) == NoEmpty)
  362.     {
  363.         OutNode = OutQueue(&QueueHead);
  364.         if (OutNode == NULL)
  365.         {
  366.             return;
  367.         }

  368.         VisitLog[OutNode->PonintNo] = HasVisit;
  369.         ListNode* ListHead = grap->GListNode[OutNode->PonintNo].listHead;
  370.         ListNode* ListNext = ListHead->next;
  371.         while (ListNext != NULL)
  372.         {
  373.             if (VisitLog[ListNext->pointNo] == NoVisit)
  374.             {
  375.                 printf("(%d,%d) = %d\n",OutNode->PonintNo,ListNext->pointNo,ListNext->wight);
  376.                 EnterQueue(&QueueHead,ListNext->pointNo);
  377.             }
  378.                         
  379.             ListHead = ListNext;
  380.             ListNext = ListHead->next;
  381.         }
  382.         free(OutNode);
  383.         OutNode = NULL;
  384.     }

  385. }

  386. void DFS_Trav(GRAPH* grap,int i,int* Visit)
  387. {
  388.     Visit[i] = HasVisit;
  389.     ListNode* ListNext = grap->GListNode[i].listHead->next;
  390.     while (NULL != ListNext)
  391.     {
  392.         if (Visit[ListNext->pointNo] == NoVisit)
  393.         {
  394.             printf("(%d,%d)=%d\n",i,ListNext->pointNo,ListNext->pointNo);
  395.             DFS_Trav(grap,ListNext->pointNo,Visit);
  396.         }
  397.         ListNext = ListNext->next;
  398.     }
  399. }
  400. void DFS(GRAPH* grap)
  401. {
  402.     int VisitLog[MAX];
  403.     int i;
  404.     for (i=0; i<grap->points; i++)
  405.     {
  406.         VisitLog[i] = NoVisit;
  407.     }

  408.     for (i=0; i<grap->points; i++)
  409.     {
  410.         if (VisitLog[i] == NoVisit)
  411.         {
  412.             DFS_Trav(grap,i,VisitLog);
  413.         }
  414.     }
  415. }

  416. void main()
  417. {
  418.     printf("------------- 链表相关操作 -------------\n");
  419.     NODE* List;
  420.     List = Create();

  421.     int i;
  422.     for (i=1; i<10; i++)
  423.     {
  424.         Insert(&List, i);
  425.     }
  426.     Visit(List);
  427.     printf("----------\n");
  428.     Delete(&List,5);
  429.     Visit(List);

  430.     printf("-------------- 树相关操作 --------------\n");

  431.     TREE* tree = NULL;
  432. //    tree = CreateHead();
  433.     CreateTree(&tree);
  434.     PreVisit(tree);
  435.     printf("-------\n");
  436.     MidVisit(tree);
  437.     printf("-------\n");
  438.     EndVisit(tree);

  439.     printf("Depth = %d\n",Depth(tree));

  440.     printf("-------------------    图相关操作    --------------------------\n");
  441.     GRAPH myGrap;
  442.     CreateG(&myGrap);

  443.     BFS(&myGrap);

  444.     printf("-------DFS-----------\n");
  445.     DFS(&myGrap);
  446. }

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