亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3547 | 回復(fù): 5
打印 上一主題 下一主題

[C] 單向鏈表數(shù)據(jù)插入銷毀問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2014-09-29 16:12 |只看該作者 |倒序?yàn)g覽
各位大牛們好,請(qǐng)問在一個(gè)已創(chuàng)建的單向鏈表中插入一個(gè)新數(shù)據(jù)后再執(zhí)行銷毀鏈表操作沒有將這個(gè)新插入的結(jié)點(diǎn)刪除,是否有什么好辦法?
代碼如下:
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #define stLen        sizeof(stNode)

  4. typedef struct student
  5. {
  6.         char        sname[10];
  7.         int                iage;
  8.         struct student        *next;
  9. }stNode;

  10. //創(chuàng)建一個(gè)定長的單向鏈表
  11. stNode *create(int num)
  12. {
  13.         stNode *p,*q,*head;
  14.         int icount = 0;

  15.         p = q = (stNode *)malloc(stLen);
  16.        
  17.         head = NULL;

  18.         while(num--)
  19.         {
  20.                 icount++;

  21.                 p = (stNode *)malloc(stLen);
  22.                 scanf("%s%d",&p->sname,&p->iage);

  23.                 if(icount == 1)        head = p;
  24.                 else q->next = p;
  25.                 p->next = NULL;
  26.                 q = p;
  27.         }

  28.         q->next = NULL;

  29.         return head;
  30. }

  31. //鏈表數(shù)據(jù)打印輸出
  32. void myprint(stNode *node)
  33. {
  34.         if(node == NULL)
  35.         {
  36.                 printf("輸入的鏈表為空,鏈表不能為空!\n");
  37.                 exit (-1);
  38.         }

  39.         while(node)
  40.         {
  41.                 printf("name->[%s],age->[%d]\n",node->sname,node->iage);
  42.                 node = node->next;
  43.         }
  44. }

  45. //計(jì)算鏈表長度
  46. int Node_Length(stNode *node)
  47. {
  48.         int iLength = 0;
  49.         while(node){
  50.                 node = node->next;
  51.                 iLength++;
  52.         }
  53.         return iLength;
  54. }

  55. //刪除指定位置記錄
  56. stNode *Node_Delete(stNode *node,int inum)
  57. {
  58.         stNode *p1,*p2;
  59.         int node_length = 0,iRun = 0;

  60.         if(node == NULL)
  61.         {
  62.                 printf("輸入的鏈表為空,鏈表不能為空!\n");
  63.                 exit (-1);
  64.         }

  65.         p1 =  node;
  66.         node_length = Node_Length(node);

  67.         if(node_length < inum)
  68.         {
  69.                 printf("輸入的鏈表長度[%d]不足[%d]\n",node_length,inum);
  70.                 exit (-1);
  71.         }

  72.         while(iRun++ < (inum-1))
  73.         {
  74.                 p2 = p1;
  75.                 p1 = p1->next;
  76.         }

  77.         if(p1 == node)
  78.         {
  79.                 if(node_length == 1)
  80.                 {
  81.                         free(node);
  82.                         node = NULL;
  83.                 }
  84.                 else node = p1->next;
  85.                 free(p1);
  86.         }

  87.         else p2->next = p1->next;

  88.         return node;
  89. }

  90. //插入新數(shù)據(jù)到鏈表指定位置
  91. stNode *Node_Insert(stNode *node,stNode *innode,int inum)
  92. {
  93.         stNode *p1,*p2;
  94.         int node_length = 0,iRun = 0;

  95.         node_length = Node_Length(node);

  96.         if(node == NULL || inum > node_length || innode == NULL)
  97.         {
  98.                 printf("鏈表為空或需插入的位置[%d]超過鏈表長度[%d]!\n",inum,node_length);
  99.                 exit(-1);
  100.         }

  101.         p1 = node;

  102.         while(iRun++ < inum)
  103.         {
  104.                 p2 = p1;
  105.                 p1 = p1->next;
  106.         }

  107.         p2->next = innode;
  108.         p2->next->next = p1;

  109.         return node;
  110. }

  111. //更新鏈表數(shù)據(jù)
  112. stNode *Node_Update(stNode *node,stNode *upnode,int inum)
  113. {
  114.         stNode *p1;
  115.         int node_length = 0,iRun = 0;

  116.         node_length = Node_Length(node);

  117.         if(node == NULL || inum > node_length || upnode == NULL)
  118.         {
  119.                 printf("鏈表為空或需插入的位置[%d]超過鏈表長度[%d]!\n",inum,node_length);
  120.                 exit(-1);
  121.         }

  122.         p1 = node;

  123.         while(iRun++ < inum-1)
  124.         {
  125.                 p1 = p1->next;
  126.         }

  127.         strcpy(p1->sname,upnode->sname);
  128.         p1->iage = upnode->iage;

  129.         return node;
  130. }

  131. //鏈表逆序
  132. stNode *Node_Reverse(stNode *node)
  133. {
  134.         stNode *next,*prev;

  135.         prev = NULL;

  136.         while(node)
  137.         {
  138.                 next = node->next;
  139.                 node->next = prev;
  140.                 prev = node;
  141.                 node = next;
  142.         }

  143.         return prev;
  144. }

  145. //遞歸算法逆序鏈表
  146. stNode *Node_Reverse2(stNode *node)
  147. {
  148.         stNode        *NewHead;

  149.         if(node == NULL || node->next == NULL)
  150.                 return node;

  151.         NewHead = Node_Reverse2(node->next);

  152.         node->next->next = node;
  153.         node->next = NULL;

  154.         return NewHead;
  155. }

  156. //銷毀鏈表
  157. stNode *Node_Destroy(stNode *node)
  158. {
  159.         stNode *pnode;

  160.         while(node)
  161.         {
  162.                 pnode = node->next;
  163.                 free(node);
  164.                 node = pnode;
  165.         }

  166.         return node;
  167. }

  168. int main()
  169. {
  170.         stNode *p,*q,*pn1,*pn2;
  171.         int iLength = 0;
  172.         int        iDelete = 0;
  173.         int        iInsert = 0;
  174.         int        iUpdate = 0;
  175.        
  176.         printf("開始創(chuàng)建鏈表\n");
  177.         printf("輸入需要?jiǎng)?chuàng)建鏈表的大小:");
  178.         scanf("%d",&iLength);

  179.         p = create(iLength);
  180.         myprint(p);

  181.         printf("輸入需要?jiǎng)h除的鏈表位置:");
  182.         scanf("%d",&iDelete);

  183.         Node_Delete(p,iDelete);
  184.         myprint(p);

  185.         q = (stNode *)malloc(stLen);
  186.         printf("輸入需要插入的位置和姓名年齡:\n");
  187.         scanf("%d%s%d",&iInsert,&q->sname,&q->iage);

  188.         Node_Insert(p,q,iInsert);
  189.         myprint(p);

  190.         pn1 = (stNode *)malloc(stLen);
  191.         printf("輸入需要修訂的鏈表位置和姓名年齡:\n");
  192.         scanf("%d%s%d",&iUpdate,&pn1->sname,&pn1->iage);

  193.         Node_Update(p,pn1,iUpdate);
  194.         myprint(p);

  195.         printf("現(xiàn)在開始逆序輸出鏈表:\n");
  196.         //pn2 = Node_Reverse(p);
  197.         pn2 = Node_Reverse2(p);
  198.         myprint(pn2);

  199.         printf("開始銷毀鏈表:\n");
  200.         Node_Destroy(pn2);
  201.         myprint(pn2);

  202.         free(p);
  203.         free(q);
  204.         free(pn1);
  205.         free(pn2);

  206.         return 0;
  207. }
復(fù)制代碼

求職 : 機(jī)器學(xué)習(xí)
論壇徽章:
79
2015年亞洲杯紀(jì)念徽章
日期:2015-05-06 19:18:572015七夕節(jié)徽章
日期:2015-08-21 11:06:172015亞冠之阿爾納斯?fàn)?日期:2015-09-07 09:30:232015亞冠之薩濟(jì)拖拉機(jī)
日期:2015-10-21 08:26:3915-16賽季CBA聯(lián)賽之浙江
日期:2015-12-30 09:59:1815-16賽季CBA聯(lián)賽之浙江
日期:2016-01-10 12:35:21技術(shù)圖書徽章
日期:2016-01-15 11:07:2015-16賽季CBA聯(lián)賽之新疆
日期:2016-02-24 13:46:0215-16賽季CBA聯(lián)賽之吉林
日期:2016-06-26 01:07:172015-2016NBA季后賽紀(jì)念章
日期:2016-06-28 17:44:45黑曼巴
日期:2016-06-28 17:44:4515-16賽季CBA聯(lián)賽之浙江
日期:2017-07-18 13:41:54
2 [報(bào)告]
發(fā)表于 2014-09-29 17:01 |只看該作者
沒有看懂你的問題

論壇徽章:
7
天秤座
日期:2014-08-07 13:56:30丑牛
日期:2014-08-27 20:34:21雙魚座
日期:2014-08-27 22:02:21天秤座
日期:2014-08-30 10:39:11雙魚座
日期:2014-09-21 20:07:532015年亞洲杯之日本
日期:2015-02-06 14:00:282015亞冠之大阪鋼巴
日期:2015-11-02 14:50:19
3 [報(bào)告]
發(fā)表于 2014-09-29 17:29 |只看該作者
貌似有個(gè)值傳遞的問題,destroy沒有把NULL返回給pn2,所以你的pn2還是非空,print出來還是有表在。

論壇徽章:
2
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
4 [報(bào)告]
發(fā)表于 2014-09-30 17:06 |只看該作者
沒有表頭結(jié)構(gòu)體的鏈表都得用二維指針來插入或者刪除吧!否則,就沒有下文了(直接coredump)。

論壇徽章:
2
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
5 [報(bào)告]
發(fā)表于 2014-09-30 17:43 |只看該作者
我的版本。僅供參考。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct student
  5. {
  6.         char sname[10];
  7.         int iage;
  8.         struct student *next;
  9. }stNode;

  10. int Node_Delete(stNode **io_tab, int inum)
  11. {
  12.         stNode **node, *temp;

  13.         if (io_tab != NULL && inum >= 0) {
  14.                 node = io_tab;
  15.                 while (*node != NULL) {
  16.                         if (inum == 0) {
  17.                                 temp = *node;
  18.                                 *node = (*node)->next;
  19.                                 free(temp);
  20.                                 return 0;
  21.                         }
  22.                         node = &(*node)->next;
  23.                         --inum;
  24.                 }
  25.         }
  26.         return -1;
  27. }

  28. int Node_Insert(stNode **io_tab, const char sname[10], int iage, stNode **o_node)
  29. {
  30.         stNode *node, *temp;

  31.         if (io_tab != NULL && sname != NULL) {
  32.                 /* get the last node */
  33.                 node = *io_tab;
  34.                 if (node != NULL) {
  35.                         while (node->next != NULL) {
  36.                                 node = node->next;
  37.                         }
  38.                 }
  39.                 /* append a new node */
  40.                 temp = (stNode *)malloc(sizeof(stNode));
  41.                 if (temp != NULL) {
  42.                         memset(temp, 0, sizeof(stNode));
  43.                         strncpy(temp->sname, sname, 10);
  44.                         temp->iage = iage;
  45.                         if (node == NULL) {
  46.                                 *io_tab = temp;
  47.                         }else {
  48.                                 node->next = temp;
  49.                         }
  50.                         if (o_node != NULL) {
  51.                                 *o_node = temp;
  52.                         }
  53.                         return 0;
  54.                 }
  55.         }
  56.         return -1;
  57. }

  58. int Node_Update(stNode *tab, int inum, const char sname[10], int iage)
  59. {
  60.         if (tab != NULL && inum >= 0 && sname != NULL) {
  61.                 while (tab != NULL) {
  62.                         if (inum == 0) {
  63.                                 strncpy(tab->sname, sname, 10);
  64.                                 tab->iage = iage;
  65.                                 return 0;
  66.                         }
  67.                         tab = tab->next;
  68.                         --inum;
  69.                 }
  70.         }
  71.         return -1;
  72. }

  73. int Node_Destroy(stNode **io_tab)
  74. {
  75.         stNode *node, *temp;

  76.         if (io_tab != NULL) {
  77.                 node = *io_tab;
  78.                 while (node != NULL) {
  79.                         temp = node;
  80.                         node = node->next;
  81.                         free(temp);
  82.                 }
  83.                 *io_tab = NULL;
  84.                 return 0;
  85.         }
  86.         return -1;
  87. }

  88. int Node_Reverse(stNode **io_tab)
  89. {
  90.         stNode *tab, *node, *temp;

  91.         if (io_tab != NULL) {
  92.                 tab = NULL;
  93.                 node = *io_tab;
  94.                 while (node != NULL) {
  95.                         temp = node;
  96.                         node = node->next;
  97.                         temp->next = tab;
  98.                         tab = temp;
  99.                 }
  100.                 *io_tab = tab;
  101.                 return 0;
  102.         }
  103.         return -1;
  104. }

  105. int myprint(stNode *tab)
  106. {
  107.         int cnt;

  108.         cnt = 0;
  109.         while(tab != NULL) {
  110.                 printf("%d: name->[%.*s],age->[%d]\n", cnt, 10, tab->sname, tab->iage);
  111.                 tab = tab->next;
  112.                 ++cnt;
  113.         }
  114.         return 0;
  115. }

  116. int main(void)
  117. {
  118.         char sname[10];
  119.         int iage;
  120.         stNode *tab;
  121.         int iLength = 0;
  122.         int iDelete = 0;
  123.         int iInsert = 0;
  124.         int iUpdate = 0;

  125.         tab = NULL;

  126.         printf("開始創(chuàng)建鏈表\n");
  127.         printf("輸入需要?jiǎng)?chuàng)建鏈表的大小: ");
  128.         scanf("%d", &iLength);
  129.         while (iLength-- > 0) {
  130.                 Node_Insert(&tab, "<null>", iLength, NULL);
  131.         }
  132.         myprint(tab);

  133.         printf("輸入需要?jiǎng)h除的鏈表位置: ");
  134.         scanf("%d", &iDelete);
  135.         Node_Delete(&tab, iDelete);
  136.         myprint(tab);

  137.         printf("輸入需要插入的姓名和年齡:\n");
  138.         scanf("%s%d", sname, &iage);
  139.         Node_Insert(&tab, sname, iage, NULL);
  140.         myprint(tab);

  141.         printf("輸入需要修訂的鏈表位置和姓名年齡:\n");
  142.         scanf("%d%s%d", &iUpdate, sname, &iage);
  143.         Node_Update(tab, iUpdate, sname, iage);
  144.         myprint(tab);

  145.         printf("現(xiàn)在開始逆序輸出鏈表:\n");
  146.         Node_Reverse(&tab);
  147.         myprint(tab);

  148.         printf("開始銷毀鏈表:\n");
  149.         Node_Destroy(&tab);
  150.         myprint(tab);
  151.        
  152.         return 0;
  153. }
復(fù)制代碼

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2014-10-09 10:32 |只看該作者
回復(fù) 5# cobras
前段時(shí)間比較忙,沒來得及回復(fù),不好意思,多謝幫助,等下去試試你的方法


   
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP