12.19單向連結串列
1.連結串列(linked list )
- 連結串列是有序的列表
- 連結串列是以節點的方式來儲存,鏈式儲存
- 每個節點包含data域,next域,指向下一個節點
- 如圖:連結串列的各個節點不一定是連續存放的
單向連結串列實現
package zhangsong;
import java.util.PrimitiveIterator;
public class Linked {
public static void main(String[] args) {
HeadLinke headLinke = new HeadLinke();
Hero_Linked_List hero_linked_list = new Hero_Linked_List(1, "張三", "小三");
Hero_Linked_List hero_linked_list1 = new Hero_Linked_List(2, "李四", "小四");
Hero_Linked_List hero_linked_list2 = new Hero_Linked_List(3, "王五", "小五");
Hero_Linked_List hero_linked_list3 = new Hero_Linked_List(4, "趙六", "小劉");
Hero_Linked_List hero_linked_list4 = new Hero_Linked_List(5, "趙六", "小劉");
headLinke.add(hero_linked_list);
headLinke.add(hero_linked_list1);
headLinke.add(hero_linked_list2);
headLinke.add(hero_linked_list3);
headLinke.add(hero_linked_list4);
headLinke.show();
}
}
class HeadLinke{
private Hero_Linked_List head = new Hero_Linked_List(0,"","");
private Hero_Linked_List p = head;
public void add(Hero_Linked_List hero){
p.nextLinked = hero;
System.out.println(p);
p = p.nextLinked;
}
public void show(){
if (p.nextLinked == null){
return;
}
while (true){
if (p.nextLinked == null){
break;
}
System.out.println(p);
}
}
}
class Hero_Linked_List{
private int no;
private String name;
private String smallname;
public Hero_Linked_List nextLinked;
public Hero_Linked_List(int no, String name, String smallname) {
this.no = no;
this.name = name;
this.smallname = smallname;
}
@Override
public String toString() {
return "Hero_Linked_List{" +
"no=" + no +
", name='" + name + '\'' +
", smallname=" + smallname +
'}';
}
}
排序連結串列實現程式碼
package zhangsong;
import java.util.PrimitiveIterator;
public class Linked {
public static void main(String[] args) {
HeadLinke headLinke = new HeadLinke();
Hero_Linked_List hero_linked_list = new Hero_Linked_List(1, "張三", "小三");
Hero_Linked_List hero_linked_list1 = new Hero_Linked_List(5, "李四", "小四");
Hero_Linked_List hero_linked_list2 = new Hero_Linked_List(9, "王五", "小五");
Hero_Linked_List hero_linked_list3 = new Hero_Linked_List(5, "趙六", "小劉");
Hero_Linked_List hero_linked_list4 = new Hero_Linked_List(3, "趙六", "小劉");
headLinke.addsort(hero_linked_list);
headLinke.addsort(hero_linked_list1);
headLinke.addsort(hero_linked_list2);
headLinke.addsort(hero_linked_list3);
headLinke.addsort(hero_linked_list4);
headLinke.show();
}
}
class HeadLinke{
private Hero_Linked_List head = new Hero_Linked_List(0,"","");
private Hero_Linked_List p = head;
public void addsort(Hero_Linked_List hero){
Hero_Linked_List p1 = head;
boolean flag = false;
while (true){
if (p1.nextLinked == null) {
break;
}else if(p1.nextLinked.no > hero.no){
break;
}else if(p1.nextLinked.no == hero.no){
flag = true;
break;
}
p1 = p1.nextLinked;
}
if (flag){
System.out.printf("已有資料,加入失敗");
}else {
hero.nextLinked = p1.nextLinked;
p1.nextLinked = hero;
System.out.println("成功");
}
}
public void add(Hero_Linked_List hero){
p.nextLinked = hero;
// System.out.println(p);
p = p.nextLinked;
}
public void show(){
Hero_Linked_List p1 = head;
while (true){
if (p1.nextLinked == null){
break;
}
p1 = p1.nextLinked;
System.out.println(p1);
}
}
}
class Hero_Linked_List{
public int no;
private String name;
private String smallname;
public Hero_Linked_List nextLinked;
public Hero_Linked_List(int no, String name, String smallname) {
this.no = no;
this.name = name;
this.smallname = smallname;
}
@Override
public String toString() {
return "Hero_Linked_List{" +
"no=" + no +
", name='" + name + '\'' +
", smallname=" + smallname +
'}';
}
}
反轉連結串列,檢視連結串列長度\
package zhangsong;
import java.util.PrimitiveIterator;
public class Linked {
public static void main(String[] args) {
HeadLinke headLinke = new HeadLinke();
Hero_Linked_List hero_linked_list = new Hero_Linked_List(1, "張三", "小三");
Hero_Linked_List hero_linked_list1 = new Hero_Linked_List(5, "李四", "小四");
Hero_Linked_List hero_linked_list2 = new Hero_Linked_List(9, "王五", "小五");
Hero_Linked_List hero_linked_list3 = new Hero_Linked_List(3, "趙六", "小劉");
headLinke.addsort(hero_linked_list);
headLinke.addsort(hero_linked_list1);
headLinke.addsort(hero_linked_list2);
headLinke.addsort(hero_linked_list3);
headLinke.show();
System.out.println("*******************");
Hero_Linked_List reverselinked = headLinke.reverselinked();
headLinke.show();
}
}
class HeadLinke{
public Hero_Linked_List head = new Hero_Linked_List(0,"","");
private Hero_Linked_List p = head;
public Hero_Linked_List reverselinked(){
Hero_Linked_List temp = head.nextLinked;
Hero_Linked_List pointer = null;
Hero_Linked_List head2 = new Hero_Linked_List(0,"","");
if (head.nextLinked == null || head.nextLinked.nextLinked == null){
return temp;
}
while (temp != null){
pointer = temp.nextLinked;
temp.nextLinked = head2.nextLinked;
head2.nextLinked = temp;
temp = pointer;
System.out.println("1");
}
head.nextLinked = head2.nextLinked;
return head;
}
public int getLength(Hero_Linked_List head){
Hero_Linked_List po = head.nextLinked;
int length = 0;
while (true){
if (po == null){
break;
}
length++;
}
return length;
}
public void rem(int no){
Hero_Linked_List p1 = head;
boolean falag = false;
while (true){
if (p1.nextLinked == null){
break;
}
if (p1.nextLinked.no == no){
falag = true;
break;
}
p1 = p1.nextLinked;
}
if (falag){
p1.nextLinked = p1.nextLinked.nextLinked;
}else {
System.out.printf("沒有此元素");
}
}
public void addsort(Hero_Linked_List hero){
Hero_Linked_List p1 = head;
boolean flag = false;
while (true){
if (p1.nextLinked == null) {
break;
}else if(p1.nextLinked.no > hero.no){
break;
}else if(p1.nextLinked.no == hero.no){
flag = true;
break;
}
p1 = p1.nextLinked;
}
if (flag){
System.out.printf("已有資料,加入失敗");
}else {
hero.nextLinked = p1.nextLinked;
p1.nextLinked = hero;
System.out.println("成功");
}
}
public void add(Hero_Linked_List hero){
p.nextLinked = hero;
// System.out.println(p);
p = p.nextLinked;
}
public void show(){
Hero_Linked_List p1 = head;
while (true){
if (p1.nextLinked == null){
break;
}
p1 = p1.nextLinked;
System.out.println(p1);
}
}
}
class Hero_Linked_List{
public int no;
private String name;
private String smallname;
public Hero_Linked_List nextLinked;
public Hero_Linked_List(int no, String name, String smallname) {
this.no = no;
this.name = name;
this.smallname = smallname;
}
@Override
public String toString() {
return "Hero_Linked_List{" +
"no=" + no +
", name='" + name + '\'' +
", smallname=" + smallname +
'}';
}
}
相關文章
- 連結串列 - 單向連結串列
- 棧_單向連結串列
- 資料結構--陣列、單向連結串列、雙向連結串列資料結構陣列
- 簡單的單向連結串列
- 佇列_單向連結串列佇列
- 單向迴圈連結串列
- 10單向連結串列(slist)
- 單向連結串列的建立
- 資料結構與演算法——連結串列 Linked List(單連結串列、雙向連結串列、單向環形連結串列-Josephu 問題)資料結構演算法
- 結構與演算法(03):單向連結串列和雙向連結串列演算法
- 連結串列-雙向連結串列
- 單向連結串列介面設計
- go 實現單向連結串列Go
- 連結串列-雙向通用連結串列
- 連結串列-雙向非通用連結串列
- 單向迴圈連結串列大綱
- 畫江湖之資料結構【第一話:連結串列】單向連結串列資料結構
- 畫江湖之資料結構 [第一話:連結串列] 單向連結串列資料結構
- 雙向連結串列
- Python實現單向連結串列詳解Python
- 單向迴圈連結串列的介面程式
- 單向迴圈連結串列的實現
- 連結串列-單連結串列實現
- 設計單向迴圈連結串列的介面
- 單連結串列
- 資料結構之php實現單向連結串列資料結構PHP
- 資料結構-單連結串列、雙連結串列資料結構
- 演算法與資料結構-連結串列((linked-list)-Java實現單向連結串列演算法資料結構Java
- C語言單向連結串列的增刪操作C語言
- c/c++ 線性表之單向連結串列C++
- Java實現單向連結串列基本功能Java
- 簡單介紹python中的單向連結串列實現Python
- 線性表中的單向連結串列的簡單操作
- 實現雙向連結串列
- 【資料結構與演算法學習】線性表(順序表、單連結串列、雙向連結串列、迴圈連結串列)資料結構演算法
- 資料結構-2.單向連結串列的實現資料結構
- 單向迴圈連結串列——查詢、刪除、插入結點
- 資料結構實驗之連結串列九:雙向連結串列資料結構