日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【C语言】单链表的相关热点面试题(包括:从尾到头打印,逆置,冒泡,寻找中间节点,倒数k节点)

發(fā)布時間:2023/11/30 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C语言】单链表的相关热点面试题(包括:从尾到头打印,逆置,冒泡,寻找中间节点,倒数k节点) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

https://blog.csdn.net/hanjing_1995/article/details/51539599

  • 從尾到頭打印單鏈表

  • [cpp]?view plaincopy
  • void?FromTailToHeadPrint(SListNode*&?head)??
  • {??
  • ????stack<SListNode*>?s;??
  • ????SListNode*?cur?=?head;??
  • ????while?(cur)??
  • ????{??
  • ????????s.push(cur);??
  • ????????cur?=?cur->_next;??
  • ????}??
  • ??
  • ????while?(!s.empty())??
  • ????{??
  • ????????cout?<<?s.top()->_data?<<"->";??
  • ????????s.pop();??
  • ????}??
  • ????cout?<<?""<<endl;??
  • }??


  • 2.除一個無頭單鏈表的非尾節(jié)點

    [cpp]?view plaincopy
  • void?Erase(SListNode*&?head,SListNode*?pos)??
  • {??
  • ????//分情況:空節(jié)點、一個節(jié)點、兩個節(jié)點、三個節(jié)點??
  • ????if?(head?==?NULL?||?pos==NULL)??
  • ????{??
  • ????????return;??
  • ????}??
  • ??????
  • ????if?(head?==?pos)??
  • ????{??
  • ????????free(head);??
  • ????????head?=?NULL;??
  • ????????return;??
  • ????}??
  • ??
  • ????SListNode*?cur?=?head;??
  • ????while?(cur)??
  • ????{??
  • ????????SListNode*?next?=?cur->_next;??
  • ??????????
  • ????????if?(next?==?pos)??
  • ????????{??
  • ????????????//若只有兩個節(jié)點,pos=next,nextnext=NULL,cur->_next?=?nextnext;??
  • ????????????//(即使題設(shè)未說明刪除非尾節(jié)點)依然可以刪除成功。??
  • ????????????SListNode*?nextnext?=?next->_next;??
  • ????????????cur->_next?=?nextnext;??
  • ????????????free(next);??
  • ????????????next?=?NULL;??
  • ????????}??
  • ????????cur?=?cur->_next;??
  • ????}??
  • }??


  • 3.逆置、反轉(zhuǎn)單鏈表

    [cpp]?view plaincopy
  • void?Reverse(SListNode*&?head)??
  • {??
  • ????if?(head?==?NULL)??
  • ????{??
  • ????????return;??
  • ????}??
  • ????SListNode*?cur?=?head;??
  • ????SListNode*?next?=?NULL;??
  • ????while?(cur)??
  • ????{??
  • ????????SListNode*?tmp?=?cur;??
  • ????????cur?=?cur->_next;??
  • ????????tmp->_next?=?next;??
  • ????????next?=?tmp;??
  • ????????head?=?tmp;??
  • ????}??
  • }??


  • 4.單鏈表冒泡排序

    [cpp]?view plaincopy
  • void?BubbleSort(SListNode*&?head)??
  • {??
  • ????//空節(jié)點或一個節(jié)點直接返回??
  • ????if?(head?==?NULL?||?head->_next?==?NULL)??
  • ????{??
  • ????????return;??
  • ????}??
  • ??????
  • ????SListNode*?begin?=?head;??
  • ????SListNode*?cur?=?head;??
  • ??
  • ????while?(begin->_next)??
  • ????{??????????
  • ????????while?(cur->_next)??
  • ????????{??
  • ????????????if?(cur->_data?>?cur->_next->_data)??
  • ????????????{??
  • ????????????????swap(cur->_data,?cur->_next->_data);??
  • ????????????}??
  • ????????????cur?=?cur->_next;??
  • ????????}??
  • ????????begin?=?begin->_next;??
  • ????}??
  • }??


  • 5.查找單鏈表的中間節(jié)點,要求只能遍歷一次鏈表

    [cpp]?view plaincopy
  • SListNode*?FindMiddle(SListNode*&?head)??
  • {??
  • ????if?(head?==?NULL)??
  • ????{??
  • ????????return?NULL;??
  • ????}??
  • ????SListNode*?slow?=?head;??
  • ????SListNode*?fast?=?head;??
  • ??
  • ????while?(fast->_next)??
  • ????{??
  • ????????if?(fast->_next)??
  • ????????{??
  • ????????????fast?=?fast->_next;??
  • ????????????if?(fast->_next)??
  • ????????????{??
  • ????????????????fast?=?fast->_next;??
  • ????????????}??
  • ????????????else??
  • ????????????{??
  • ????????????????return?slow;??
  • ????????????}??
  • ????????????slow?=?slow->_next;??
  • ????????}??
  • ????????else??
  • ????????{??
  • ????????????return?NULL;??
  • ????????}??
  • ????}??
  • ??????
  • ????return?slow;??
  • }??


  • 6.查找單鏈表的倒數(shù)第k個節(jié)點,要求只能遍歷一次鏈表

    [cpp]?view plaincopy
  • SListNode*?FindLastK(SListNode*&?head,int?k)??
  • {??
  • ????assert(k?>?0);??
  • ????if?(head?==?NULL)??
  • ????{??
  • ????????return?NULL;??
  • ????}??
  • ????SListNode*?slow?=?head;??
  • ????SListNode*?fast?=?head;??
  • ????while?(k--)??
  • ????{??
  • ????????if?(fast->_next)??
  • ????????{??
  • ????????????fast?=?fast->_next;??
  • ????????}??
  • ????????else??
  • ????????{??
  • ????????????return?NULL;??
  • ????????}??
  • ????}??
  • ????slow?=?slow->_next;??
  • ????while?(fast->_next)??
  • ????{??
  • ????????fast?=?fast->_next;??
  • ????????slow?=?slow->_next;??
  • ????}??
  • ????return?slow;??
  • }??

  • 本文出自 “Han Jing's Blog” 博客,請務(wù)必保留此出處http://10740184.blog.51cto.com/10730184/1772313



    總結(jié)

    以上是生活随笔為你收集整理的【C语言】单链表的相关热点面试题(包括:从尾到头打印,逆置,冒泡,寻找中间节点,倒数k节点)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。