package JavaProject;
public class TestDuLinkList {
public static void main(String[] args) {
// TODO Auto-generated method stub
DuLinkList<String> list = new DuLinkList<String>();
list.insert("aaaa" , 0);
System.out.println("插入aaaa:");
list.traverse();
list.addTail("bbbb");
System.out.println("尾插法插入bbbb:");
list.traverse();
list.insert("cccc" , 0);
System.out.println("插入cccc:");
list.traverse();
list.insert("dddd" , 1);
System.out.println("插入dddd:");
list.traverse();
//删除索引为2处的元素
list.delete(2);
System.out.println("删除索引为2的元素");
list.traverse();
//获取cccc字符串在顺序线性表中的位置
System.out.println("cccc在顺序线性表中的位置:"
+ list.getIndex("cccc"));
System.out.println("链表中索引1处的元素:"
+ list.getNode(1).data);
list.delete(0);//删除链表第一个元素
System.out.println("调用delete(0)后的链表:" );
list.traverse();
}
}
class DuLinkList<T>{
class Node{
public T data;
public Node pre;
public Node next;
public Node(T data,Node pre,Node next){
this.data = data;
this.pre = pre;
this.next = next;
}
}
private Node header;
private Node tail;
private int size;
public DuLinkList(){
header = null;
tail = null;
}
public int length(){
return size;
}
public Node getNode(int index){
if(index < 0 || index > size - 1){
System.out.println("位置非法");
}
if(index <= size / 2){
Node tempNode = header;
for(int i=0;i<=size/2&&tempNode != null;tempNode=tempNode.next,i++){
if(i == index){
return tempNode;
}
}
}
else{
Node tempNode = tail;
for(int i=size-1;i>size/2&&tempNode!=null;tempNode=tempNode.pre,i--){
if(i == index){
return tempNode;
}
}
}
return null;
}
public int getIndex(T element){
Node tempNode = header;
if(tempNode != null){
for(int i=0;i<size;i++){
if(tempNode.data.equals(element)){
return i;
}
tempNode = tempNode.next;
}
}
return -1;
}
public void addTail(T element){
if(header == null){
header = new Node(element,null,null);
tail = header;
}
else{
Node tempNode = new Node(element,tail,null);
tail.next = tempNode;
tail = tempNode;
}
size++;
}
public void addHead(T element){
if(tail == null){
tail = new Node(element,null,null);
header = tail;
}
else{
Node tempNode = new Node(element,null,header);
header = tempNode;
}
size++;
}
public void insert(T element,int index){
if(index < 0 || index > size){
System.out.println("位置非法");
}
if(header == null){
addTail(element);
}
else{
if(index == 0){
addHead(element);
}
else{
Node Pre = getNode(index - 1);
Node newNode = new Node(element,Pre,Pre.next);
Pre.next = newNode;
newNode.next.pre = newNode;
size++;
}
}
}
public T delete(int index){
if(index < 0 || index > size -1){
System.out.println("位置非法");
}
Node del = null;
if(index == 0){
del = header;
header = header.next;
header.pre = null;
}
else{
Node Pre = getNode(index - 1);
del = Pre.next;
Pre.next = del.next;
if(del.next != null){
del.next.pre = Pre;
}
del.next = null;
del.pre = null;
}
size--;
return del.data;
}
public boolean empty(){
return size == 0;
}
public void clear(){
header = null;
tail = null;
size = 0;
}
public void traverse(){
for(Node tempNode=header;tempNode!=null;tempNode=tempNode.next){
System.out.printf("%s ",tempNode.data.toString());
}
System.out.println();
}
}
Java数据结构之双向链表
最新推荐文章于 2024-11-09 12:43:52 发布