在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<abbr id="27omo"></abbr>

<menu id="27omo"><dl id="27omo"></dl></menu>
    • <label id="27omo"><tt id="27omo"></tt></label>

      新聞中心

      EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單向鏈表基本操作的遞歸實(shí)現(xiàn)

      單向鏈表基本操作的遞歸實(shí)現(xiàn)

      作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
      這幾天正在復(fù)習(xí)一些基本的算法和實(shí)現(xiàn),今天看了看遞歸的基本原理,發(fā)現(xiàn)自己對遞歸還不是特別清楚,特別是不清楚遞歸的思想,不能很準(zhǔn)確的把握先分解成小事件,在合并的思想,其實(shí)也是數(shù)學(xué)歸納法的程序體現(xiàn),其實(shí)數(shù)學(xué)歸納法是一種強(qiáng)大的方法,記得高中的時(shí)候最喜歡做的題目就是數(shù)學(xué)歸納方面的證明,現(xiàn)在想過來好多問題我不能采用這種方式思考,可見知識(shí)真的是有聯(lián)系的,只是我們沒有找到聯(lián)系的方式而已。


      為了熟悉遞歸的思想,我嘗試了采用遞歸的方式實(shí)現(xiàn)單向鏈表的基本操作。單向的鏈表是C語言課程中接觸到的中比較復(fù)雜的數(shù)據(jù)結(jié)構(gòu),但是他確實(shí)其他數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ),在一般情況下都是采用迭代的形式實(shí)現(xiàn),迭代的形式相比遞歸要節(jié)省時(shí)間和空間,但是代碼相對來說要復(fù)雜,遞歸往往只是簡單的幾句代碼,我主要是為了熟悉迭代,并不在性能上進(jìn)行分析。

      基本的實(shí)現(xiàn)如下所示:

      本文引用地址:http://www.biyoush.com/article/201612/324524.htm

      #include
      #include

      typedef struct listnode
      {
      int val;
      struct listnode *next;
      }List;

      /*統(tǒng)計(jì)節(jié)點(diǎn)個(gè)數(shù)*/
      int count_listnode(List *head)
      {
      static int count = 0;

      if(NULL != head)
      {
      count += 1;
      if(head->next != NULL)
      {
      count_listnode(head->next);
      }

      return count;
      }
      }

      /*順序打印*/
      void fdprint_listnode(List *head)
      {
      if(NULL != head)
      {
      printf("%d ",head->val);
      if(head->next != NULL)
      {
      fdprint_listnode(head->next);
      }
      }
      }
      /*反向打印*/
      void bkprint_listnode(List *head)
      {
      if(head != NULL)
      {
      if(head->next != NULL)
      {
      bkprint_listnode(head->next);
      }

      printf("%d ",head->val);
      }
      }
      /*刪除一個(gè)節(jié)點(diǎn)的數(shù)據(jù)為d的節(jié)點(diǎn)*/
      List *delete_node(List * head, int d)
      {
      List *temp = head;

      if(head != NULL)
      {
      if(head->val == d)
      {
      temp = head;
      head = head->next;
      free(temp);
      temp = NULL;
      }
      else
      {
      temp = head->next;
      if(temp != NULL)
      {
      temp = delete_node(temp,d);
      head->next= temp;
      }
      }
      }

      return head;
      }

      /*刪除所有val = d的節(jié)點(diǎn)*/
      List* delete_allnode(List *head, int d)
      {
      List *temp = head, *cur = head;
      if(head != NULL)
      {
      /*如果第一個(gè)就是需要?jiǎng)h除的對象*/
      if(cur->val == d)
      {
      temp = cur;
      cur = cur->next;
      free(temp);
      temp = NULL;
      temp = delete_allnode(cur, d);
      head = temp;
      }
      else /*不是刪除的對象*/
      {
      cur = head->next;
      temp = delete_allnode(cur, d);
      /*將得到的鏈表連接到檢測的區(qū)域*/
      head->next= temp;
      }
      }
      return head;
      }


      上一頁 1 2 下一頁

      評論


      技術(shù)專區(qū)

      關(guān)閉