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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构之单项链表的操作

發布時間:2024/7/5 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构之单项链表的操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以下是自己敲的小demo,方便日后復習時候用.主要實現了對有頭結點的單向鏈表的

增加,刪除,修改,更新以及反轉鏈表,倒敘輸出,查找有效節點數,查找倒數第K個節點的

一些操作,如有冗余,歡迎指正.

package com.ebiz.list;import java.util.Stack;/*** @author YHj* @create 2019-07-14 16:57*/ public class SingleListDemo {public static void main(String[] args) {//創建節點HeroNode heroNode01 = new HeroNode(1, "宋江", "及時雨");HeroNode heroNode02 = new HeroNode(2, "盧俊義", "玉麒麟");HeroNode heroNode03 = new HeroNode(3, "無用", "智多星");HeroNode heroNode04 = new HeroNode(4, "林沖", "豹子頭");//創建鏈表SingleList singleList = new SingleList();//添加節點并按照英雄編號排序 singleList.addById(heroNode01);singleList.addById(heroNode04);singleList.addById(heroNode02);singleList.addById(heroNode03);//遍歷節點 singleList.list(singleList);System.out.println("修改節點----------------------------------------------------");//修改節點singleList.update(new HeroNode(2, "盧俊義111", "玉麒麟111"));//遍歷節點 singleList.list(singleList);//System.out.println("------------------------------------------------------");//刪除節點//singleList.delete(5);//遍歷節點//singleList.list(); System.out.println("倒數第k個節點---------------------------------------------------");System.out.println(SingleList.getDaoHeroNode(singleList,4));System.out.println("鏈表倒敘----------------------------------------------------------");singleList.reverseList(singleList);System.out.println("從尾到頭打印單鏈表 不是反轉,會破壞數據結構 采用stack--------------");singleList.reverseStack(singleList);} }//管理節點的類 即鏈表 class SingleList {//初始化頭結點private static HeroNode headhero = new HeroNode(0, "", "");//新增一個節點public void add(HeroNode heroNode) {//臨時節點HeroNode temp = headhero;//找到最后一個節點while (true) {if (temp.next == null) {break;}temp = temp.next;}//while循環退出,找到最后一個節點temp.next = heroNode;}//遍歷節點public void list(SingleList singleList) {//判斷是否為空if (null == singleList.headhero.next) {System.out.println("鏈表為空");}//臨時節點HeroNode temp = headhero.next;while (true) {if (null == temp) {break;}System.out.println("temp = " + temp);temp = temp.next;}}//按照英雄編號來插入 從小到大public void addById(HeroNode heroNode) {//臨時節點HeroNode temp = headhero;//boolean 判斷鏈表中是否已經存在boolean isExist = false;while (true) {if (null == temp.next) {break;}if (temp.next.no > heroNode.no) {break;}if (temp.next.no == heroNode.no) {isExist = true;break;}temp = temp.next;}if (isExist) {System.out.println("該英雄已存在!");} else {heroNode.next = temp.next;temp.next = heroNode;}}//修改對應節點public void update(HeroNode heroNode) {//臨時節點HeroNode temp = headhero;//boolean 判斷鏈表中是否存在對應節點boolean isExist = false;while (true) {if (null == temp) {break;}if (temp.no == heroNode.no) {isExist = true;break;}temp = temp.next;}if (isExist) {temp.name = heroNode.name;temp.nickname = heroNode.nickname;} else {System.out.println("沒有對應英雄!!!");}}//刪除節點public void delete(int no) {//臨時節點HeroNode temp = headhero;//boolean 判斷鏈表中是否存在對應節點boolean isExist = false;while (true) {if (null == temp) {break;}if (temp.next.no == no) {isExist = true;break;}temp = temp.next;}if (isExist) {temp.next = temp.next.next;} else {System.out.println("沒有該節點!");}}//獲取節點個數,不包含頭結點public static int getCount(SingleList singleList){if (null == singleList.headhero.next){return 0;}//臨時節點HeroNode temp=singleList.headhero.next;int count=0;while (temp != null){count++;temp=temp.next;}return count;}//獲取倒數第k個節點public static HeroNode getDaoHeroNode(SingleList singleList,int k){//判斷是否為空if (null == singleList.headhero.next) {System.out.println("鏈表為空");}//得到鏈表有效節點int len=getCount(singleList);if (k<0 || k>len){return null;}//倒數第k個節點的前一個節點int s=len-k;if (s ==0 ){return singleList.headhero.next;}//計數器,找到第s個節點int count =0;HeroNode temp=singleList.headhero.next;while (true){count++;if (count ==s){break;}temp=temp.next;}return temp.next;}//鏈表反轉并輸出 頭插法public void reverseList(SingleList singleList){if (0 == this.getCount(singleList) || 1 == this.getCount(singleList)){System.out.println("該鏈表沒有節點!");return;}//臨時節點 遍歷原來的鏈表HeroNode cur=singleList.headhero.next;//臨時節點的下一個節點HeroNode cur_next=null;//新鏈表的頭結點HeroNode newHead = new HeroNode(0, "", "");while (cur != null){cur_next=cur.next; //找到當前節點的下一個節點cur.next=newHead.next; //將頭節點的下一個節點給要插入的節點newHead.next=cur; //要插入的節點給頭結點的下一個節點cur=cur_next; //將原來當前節點的下一個節點在賦值給臨時節點,因為現在的臨時節點在新的頭結點上 }singleList.headhero.next=newHead.next;this.list(singleList);}//從尾到頭打印單鏈表 不是反轉,會破壞數據結構 采用stackpublic void reverseStack(SingleList singleList){if (null == singleList.headhero.next){System.out.println("鏈表為空");return;}//定義臨時節點HeroNode temp=headhero.next;//定義棧Stack<HeroNode> stack = new Stack<>();while (null != temp){stack.push(temp);temp=temp.next;}while (stack.size() > 0){System.out.println(stack.pop());}}}//節點類 class HeroNode {public int no;//英雄編號public String name;//英雄的名字public String nickname;//英雄的名稱public HeroNode next;//下一個英雄節點//構造器public HeroNode(int no, String name, String nickname) {this.no = no;this.name = name;this.nickname = nickname;}@Overridepublic String toString() {return "HeroNode{" +"no=" + no +", name='" + name + '\'' +", nickname='" + nickname + '\'' +'}';} }

?

轉載于:https://www.cnblogs.com/jiushixihuandaqingtian/p/11203944.html

總結

以上是生活随笔為你收集整理的数据结构之单项链表的操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。