赞
踩
单链表的数据是由一个个结点组成的,每个结点包含自身值和下一个节点的地址,只能从前向后遍历的结构。
链表就是由链表结点构成的大实体对象就叫链表
这里展示的是5个整形数据在链表中的存放方式
class Node{
int data;//结点保存的数据
Node next;//保存下一个结点的地址
//三个构造方法
public Node(){}
public Node(int data){
this.data = data;
}
public Node(int data, Node next){
this.data = data;
this.next = next;
}
}
public class SingleLinkedNote {
//创建一个头结点,有了头才能往后遍历
private Node head = null;
//记录结点的个数
private int size = 0;
}
public String toString(){
String ret = "";
for (Node node = head; node != null ; node = node.next) {
ret += node.data;
ret += "->";
}
ret += "NULL";
return ret;
}
public void addFir(int data){
Node node = new Node(data);
//,链表为空该结点就是第一个结点
if (head == null) {
head = node;
} else {
//链表不为空
node.next = head;
head = node;
}
size ++;
}
public void addByIndex(int index, int data) { Node node = new Node(data); if (index < 0 || index > size) { System.out.println("输入错误"); } if (index == 0){ addFir(data); } else { Node pre = head; //找前驱结点 for (int i = 0; i < index - 1; i++) { pre = pre.next; } node.next = pre.next; pre.next = node; size ++; } }
public void addTail(int data) {
addByIndex(size, data);
}
public boolean legal(int index) {
//index == size索引越界了
if (index < 0 || index >= size){
System.out.println("不合法");
return false;
}
return true;
}
public int findIndex (int data) {
int index = 0;
for (Node x = head; x != null; x = x.next) {
if (x.data == data) {
return index;
}
index ++;
}
return -1;
}
public boolean contains(int data) {
return findIndex(data) != -1;
}
public int findByIndex(int index) {
if (legal(index)) {
Node x = head;
for (int i = 0; i < index; i++) {
x = x.next;
}
return x.data;
}
return -1;
}
public int setByIndex(int index, int newVal) {
//判断合法性
if (legal(index)) {
int oldVal = findByIndex(index);
Node x = head;
//找索引位置的结点
for (int i = 0; i < index; i++) {
x = x.next;
}
//修改结点值
x.data = newVal;
return oldVal;
}
return -1;
}
public int removeByIndex(int index) { if (legal(index)) { //删除头结点 if (index == 0) { Node x = head; head = head.next; x.next = null; size --; return x.data; } //删除其他结点,找前驱结点 Node pre = head; for (int i = 0; i < index - 1; i++) { pre = pre.next; } int oldVal = pre.next.data; pre.next = pre.next.next; size --; return oldVal; } return -1; }
public void removeValOnce(int val) {
int index = findIndex(val);
removeByIndex(index);
}
public void removeAllVal(int val) { //头节点不为空,循环判断头节点是否为要删除的 while (head!= null && head.data == val ) { head = head.next; size --; } //链表里没元素了 if (head == null) { return; } else { //找前驱 for (Node pre = head; pre != null; pre = pre.next) { //循环判断是否是要删除的结点 while(pre.next != null && pre.next.data == val) { pre.next = pre.next.next; size --; } } } }
public void removeFir(){
removeByIndex(0);
}
public void removeTail(){
removeByIndex(size - 1);
}
public class SingleLinkHead {
//虚拟节点,不存放任何值
private Node dummyHead = new Node();
private int size;
}
public void addFir(int data) {
//由下图可知,插入的结点的next就是虚拟节点的下一个结点
Node node = new Node(data,dummyHead.next);
dummyHead.next = node;
size ++;
}
public void addByIndex(int index, int data) {
if(index < 0 || index > size){
System.out.println("输入错误");
}
//找前驱结点
Node pre = dummyHead;
//因为虚拟结点为索引0,所以找前驱的时候要+1,所以<index
for (int i = 0; i < index; i++) {
pre = pre.next;
}
pre.next = new Node(data,pre.next);
size ++;
}
public int removeByIndex(int index){ if(legal(index)){ //找前驱结点 Node pre = dummyHead; for (int i = 0; i < index; i++) { pre = pre.next; } Node node = pre.next; int oldVal = node.data; pre.next = node.next; node.next = null; size --; return oldVal; } return -1; }
public void removeAllVal(int val) {
for (Node pre = dummyHead; pre != null; pre = pre.next) {
while (pre.next != null && pre.next.data == val) {
pre.next = pre.next.next;
size --;
}
}
}
private boolean legal(int index) {
if (index < 0 || index >= size) {
return false;
}
return true;
}
public String toString(){
String ret = "";
for (Node x = dummyHead; x.next != null; x= x.next) {
ret += x.next.data;
ret += "->";
}
ret += "NULL";
return ret;
}
数据结构与算法比较复杂,需要多画图。链表题多判断边界。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。