单链表
package practice;
import java.util.Objects;
public class Linked {
private Node first;
private int size;
public int size() {
return size;
}
public boolean isEmpty() {
return size==0;
}
public boolean contains(Object o) {
Node t = first;
while (t.next!=null){
if (t.value.equals(0)){
return true;
}
t = t.next;
}
return false;
}
public boolean add(Object o) {
Node node = new Node(o, null);
if (first == null) {
first = node;
size++;
return true;
}
Node t = first;
while (t.next != null) {
t = t.next;
}
t.next = node;
size++;
return true;
}
public boolean remove(Object o) {
if (size==0){
return false;
}
if (Objects.equals(o,first.value)&&first.next==null){
first=null;
size--;
return true;
}
if (first.value.equals(o)){
first = first.next;
size--;
return true;
}
Node t = first;
while (!t.next.value.equals(o)){
t = t.next;
if (t==null){
return false;
}
}
t.next = t.next.next;
size--;
return true;
}
public Object get(int index) {
if (index>size){
return null;
}
if (index==1){
return false;
}
Node t = first;
for (int i = 1; i <index ; i++) {
t = t.next;
}
return t;
}
public int indexOf(Object o) {
int index = 1;
Node t = first;
for (int i = 0; i < size; i++) {
if (t.value.equals(o)){
return index;
}
t = t.next;
index++;
}
return 0;
}
@Override
public String toString() {
String str = "";
Node t = first;
for (int i = 0; i < size; i++) {
if (t.next==null){
str = str+t.value;
}else {
str=str+t.value+",";
}
t = t.next;
}
return "Linked["+ str +"]";
}
class Node {
private Object value;
private Node next;
public Node(Object value, Node next) {
this.value = value;
this.next = next;
}
public String toString() {
return "值是"+value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return Objects.equals(value, node.value) && Objects.equals(next,
node.next);
}
}
}
package practice;
public class LinkedTest {
public static void main(String[] args) {
Linked link = new Linked();
link.add(1);
link.add(2);
link.add(3);
link.add(4);
link.remove(4);
System.out.println(link.size());
System.out.println(link.isEmpty());
System.out.println(link.contains(2));
System.out.println(link.get(2));
System.out.println(link.indexOf(4));
System.out.println(link.toString());
}
}
双链表
package pm.structure;
import java.util.Objects;
public class DoubleLinked {
private Node first;
private Node last;
private int size;
public boolean add(Object obj){
Node node = new Node(null,obj,null);
if (first == null){
first = node;
last = first;
size++;
return true;
}
last.next=node;
node.prev = last;
last = node;
size++;
return true;
}
public Object removeFirst(){
if (first==null){
return false;
}
Node node = first;
first.next.prev = null;
first = first.next;
size--;
return node.value;
}
public Object removeLast(){
if (first==null){
return false;
}
Node node = last;
last.prev.next = null;
last = last.prev;
size--;
return node.value;
}
public Object remove(Object obj){
if (Objects.equals(first.value,obj)){
removeFirst();
}
if (Objects.equals(last.value,obj)){
removeLast();
}
Node t = first;
while (!Objects.equals(t.next.value,obj)){
t = t.next;
if (t.next==null){
return false;
}
}
Node node = t.next;
t.next = t.next.next;
t.next.prev = t;
size--;
return node.value;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size==0;
}
public boolean contains(Object obj){
Node t = first;
while(!Objects.equals(t.value,obj)){
t = t.next;
if (t==null){
return false;
}
}
return true;
}
public Object get(int index){
if (index>=size){
return null;
}
Node t = first;
for (int i = 0;i<index;i++){
t = t.next;
}
return t.value;
}
public int indexOf(Object obj){
Node t = first;
int index = 0;
while (!Objects.equals(t.value,obj)){
t = t.next;
index++;
if (t == null){
return -1;
}
}
return index;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder("doubleLinked[");
Node t = first;
while (t.next!=null){
sb=sb.append(t.value+",");
t = t.next;
}
sb = sb.append(t.value+"]");
return sb.toString();
}
class Node{
private Node prev;
private Object value;
private Node next;
public Node(Node prev, Object value, Node next) {
this.prev = prev;
this.value = value;
this.next = next;
}
}
}
package pm.structure;
public class DoubleLinkedTest {
public static void main(String[] args) {
DoubleLinked doubleLink = new DoubleLinked();
doubleLink.add("lyy");
doubleLink.add("fhh");
doubleLink.add("gtx");
doubleLink.add("lmy");
doubleLink.add("lyn");
System.out.println(doubleLink.contains("yx"));
}
}