链表定义:
链表是一种物理存储结构上非连续的存储结构,数据元素的逻辑顺序是通过链表中的引用的链接次序实现的。
链表操作代码:
链表操作方法
class Node{
public int target;
public Node next;
public Node(){
}
public Node(int target){
this.target = target;
}
}
class myLinkedList{
public Node head;
// public Node cur;
public void createLinked(){
Node node1 = new Node(6);
Node node2 = new Node(16);
Node node3 = new Node(26);
Node node4 = new Node(36);
this.head = node1;
this.head.next = node2;
node2.next = node3;
node3.next = node4;
}
/*
1.打印单链表的值
*/
public void display(){
Node cur = this.head;
while(cur != null){
System.out.print(cur.target+" ");
cur = cur.next;
}
System.out.println();
}
/*
2.寻找单链表最后一个结点
关键思想:从头遍历,链表最后一个结点的next为空
*/
public Node findLastNode(){
if(this.head == null){
System.out.println("结点为空!");
return null;
}
Node cur = this.head; //定义结点cur,指向头结点
while(cur.next != null){ //当结点对象的next为空时,表明找到尾结点
cur = cur.next;
}
return cur; //返回尾结点
}
/*
3.寻找单链表倒数第二个结点
关键思想:从头遍历,倒数第二个结点的next.next为空,实际上就是走两步指向尾结点,尾结点的标志为next为空
*/
public Node findNode(){
if(this.head == null){
System.out.println("结点为空!");
return null;
}
if(this.head.next == null){
System.out.println("Sorry 只有一个结点!");
return null;
}
Node cur = this.head;
while(cur.next.next != null){
cur = cur.next;
}
return cur;
}
/*
4.获得单链表的长度
*/
public int getLength(){
Node cur = this.head;
int count = 0; //count代表链表的长度
while(cur != null){
cur = cur.next;
count++;
}
return count;
}
/*
5.寻找单链表的第n个结点
关键思想:判断n的合法性(n<=0和n>len都是不合法的)
遍历循环
*/
public Node findNode1(int n){
if(n <= 0){
System.out.println("我giao n太小不合法了!");
return null;
}
int len = getLength();
if(n > len){
System.out.println("n太大超出范围了老铁!");
}
Node cur = this.head;
int count = 1;
while(count != n){
cur = cur.next;
count++;
}
return cur;
}
/*
6.查找关键字key是否在单链表中
*/
public boolean searchKey(int key){
if(this.head == null){
System.out.println("结点为空");
return false;
}
Node cur = this.head;
while(cur != null){
if(cur.target == key){
return true;
}
cur = cur.next;
}
return false;
}
public boolean searchKey1(int key){
if(this.head == null){
return false;
}
Node cur = this.head;
while(cur.target != key) {
cur = cur.next;
//判断新的cur
if (cur == null) {
return false;
}
}
return true;
}
/*
7.头插法
关键思想:①先判断头结点是否为空
②插入新的结点
*/
public void insertFirst(int data){
Node newNode = new Node(data); //创建新的结点
if(this.head == null){
this.head = newNode;
}else{
newNode.next = this.head; //将新的结点next引用指向原来的头结点
this.head = newNode; //头结点引用指向新的结点
}
}
/*
8.尾插法
关键思想:①先判断头结点是否为空
②找尾结点
③在尾结点后插入新的结点
*/
public void insertLast(int data){
Node newNode = new Node(data);
if(this.head == null){
this.head = newNode;
}else {
Node cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = newNode;
}
}
/*
9.在任意位置插入新的结点,第一个结点为0号下标
关键思想:①构造一个方法,先找到index-1位置的结点
②判断index的值( 等于0的情况采用头插法 等于单链表长度的情况采用尾插法)
③插入新的结点(先绑后面 再绑前面)
*/
public Node findIndex(int index){
int count = 0; //注意count的值 下标从0开始所以count初始为0
Node cur = this.head;
while(count != index-1){
cur = cur.next;
count++;
}
return cur;
}
public void randomInsertNode(int index, int data){
if(index < 0 || index > this.getLength()){
System.out.println("index位置不合法");
return ;
}
if(index == 0){
insertFirst(data);
return ; //这个return非常重要 要不然程序不会结束
}
if(index == getLength()){
insertLast(data);
return ;
}
Node cur = findIndex(index); //cur是index-1位置结点的引用
Node newNode = new Node(data);
newNode.next = cur.next;
cur.next = newNode;
}
方法测试
public class LianBiao {
// public static void main(String[] args) {
// myLinkedList test = new myLinkedList();
// test.createLinked();
// test.display();
// } 16 26 36 46
public static void main(String[] args) {
myLinkedList test = new myLinkedList();
System.out.print("头插法插入结点: ");
test.insertFirst(16);
test.insertFirst(26);
test.insertFirst(36);
test.insertFirst(46);
test.display();
System.out.println();
System.out.print("尾插法插入结点: ");
test.insertLast(56);
test.insertLast(66);
test.insertLast(76);
test.insertLast(86);
test.display();
System.out.println();
System.out.print("0,5,10位置插入结点: ");
test.randomInsertNode(0, 100); //0下标
test.randomInsertNode(5, 200); //5下标
test.randomInsertNode(10, 300); //10下标
test.display();
System.out.println();
System.out.println("单链表此时的长度为: " + test.getLength());
System.out.println();
System.out.println("单链表的倒数第二个结点为: "+test.findNode());
System.out.println("该结点的数据为: "+test.findNode().target);
System.out.println();
System.out.println("查找单链表中是否包含关键字66: "+test.searchKey(66));
System.out.println();
System.out.println("单链表中第8个结点为: "+test.findNode1(8));
System.out.println("该结点的数据为: "+test.findNode1(8).target);
}
}
运行结果
原创 欢迎讨论!