package com.pra0710.base;
import java.util.Scanner;
public class Node {
public int value;
public Node next;
private Node head;
public Node() {
}
public Node(int value,Node node) {
this.value=value;
this.next=node;
}
public Node createLink(Scanner sc) {
int m=sc.nextInt();
while(m>0) {
Node p=new Node();
p.value=m;
p.next=this.head;
this.head=p;
m=sc.nextInt();
}
return this.head;
}
public Node createLinkBytail(Scanner sc) {
int m=sc.nextInt();
Node tail=null;
while(m>0) {
Node p=new Node();
p.value=m;
if(this.head==null)
tail=head=p;
tail.next=p;
tail=p;
m=sc.nextInt();
}
return this.head;
}
public void visit() {
Node p=this.head;
while(p!=null) {
System.out.print(p.value+" ");
p=p.next;
}
}
public int getValue() {
return value;
}
public Node getHead() {
return this.head;
}
public Node getNext() {
return next;
}
}
package com.pra0710.base;
public class LinkedList {
private Node head;
private Node tail;
private int size;
public LinkedList() {
}
public boolean add(int index,int value) {
if(index<0||index>this.size)return false;
Node p=new Node();
p.value=value;
Node prev=this.head;
Node cur=this.head;
if(this.head==null) {
this.head=this.tail=p;
this.size++;
}
else{
if(index==0){
p.next=this.head;
this.head=p;
this.size++;
return true;
}
prev=this.getIndex(index-1);
cur=this.getIndex(index);
if(cur==null){
this.tail.next=p;
this.tail=p;
this.size++;
return true;
}
prev.next=p;
p.next=cur;
this.size++;
}
return true;
}
public boolean addByhead(int value) {
this.add(0, value);
return true;
}
public boolean addBytail(int value) {
this.add(this.size,value);
return true;
}
public boolean add2(int index,int value) {
Node p=new Node();
p.value=value;
if(index<0||index>this.size)return false;
if(index==0)
{
p.next=this.head;
this.head=p;
this.size++;
}else {
Node prev=this.getIndex(index-1);
Node cur=this.getIndex(index);
if(cur==null) {
prev.next=p;
this.size++;
return true;
}
prev.next=p;
p.next=cur;
this.size++;
}
return true;
}
public boolean remove(int value) {
if(this.head==null)return false;
if(this.head.value==value) {
this.head=this.head.next;
this.size--;
if(this.size==0)
this.tail=this.head;
}else {
int index=this.indexOf(value);
Node prev=this.getIndex(index-1);
if(index==(this.size-1))
{
prev.next=null;
this.tail=prev;
this.size--;
return true;
}
Node cur=this.getIndex(index);
prev.next=cur.next;
this.size--;
}
return true;
}
public boolean set(int index,int value) {
if(index<0||index>this.size-1)return false;
Node cur=this.getIndex(index);
cur.value=value;
return true;
}
public void clear() {
this.size=0;
this.head=this.tail=null;
}
public int indexOf(int value) {
Node p=this.head;
int index=0;
while(p!=null) {
if(p.value==value)
return index;
index++;
p=p.next;
}
return -1;
}
public Node getIndex(int index) {
Node cur=this.head;
for(int i=0;i<index;i++)
cur=cur.next;
return cur;
}
public void visit() {
Node p=this.head;
while(p!=null) {
System.out.print(p.value+" ");
p=p.next;
}
}
public Node getHead() {
return head;
}
public Node getTail() {
return tail;
}
public int getSize() {
return size;
}
}
package com.pra0710.base;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
LinkedList list=new LinkedList();
list.addByhead(7);
list.addBytail(8);
System.out.println(list.add(2,2));
list.add(3, 3);
list.add2(1, 4);
list.add2(3, 5);
list.addBytail(0);
list.addByhead(0);
list.visit();
System.out.println();
list.remove(3);
list.visit();
System.out.println();
list.set(0, 3);
list.visit();
System.out.println();
System.out.println("size:"+list.getSize());
System.out.println("head:"+list.getHead().value);
System.out.println("tail:"+list.getTail().value);
System.out.println("index:"+list.indexOf(3));
}
}