您的位置:首页 > 理论基础 > 数据结构算法

数据结构---链表操作(线性结构)

2016-05-16 19:23 633 查看
package lianbiao;import java.awt.HeadlessException;import java.util.Scanner;import org.omg.CORBA.NO_IMPLEMENT;interface List{ public void clear(); public boolean isEmpty(); public int length(); public Object get(int i); public void insert(int i,Object x); public void remove(int i); public int indexOf(Object x); public void display();}class Node{ private Object data; private Node next; public Node(){ this(null,null); } public Node(Object data, Node next ) { this.data = data; this.next = next; } public Node(Object data){ this.data = data; } public Object getData(){ return this.data; } public void setData(Object data){ this.data = data; } public Node getNext(){ return this.next; } public void setNext(Node next){ this.next = next; }}

class SingleList implements List{ private Node head; public SingleList() { head=new Node(); } // @Override// public int hashCode() {// final int prime = 31;// int result = 1;// result = prime * result + ((head == null) ? 0 : head.hashCode());// return result;// }////// @Override// public boolean equals(Object obj) {// if (this == obj)// return true;// if (obj == null)// return false;// if (getClass() != obj.getClass())// return false;// SingleList other = (SingleList) obj;// if (head == null) {// if (other.head != null)// return false;// } else if (!head.equals(other.head))// return false;// return true;// }

public SingleList(int n,boolean order){ this(); if (order) { create1(n); //采用尾插 } else create2(n); //采用头插 } public void create1(int n){ Scanner sc= new Scanner(System.in); for(int j=0;j<n;j++){ insert(length(), Integer.valueOf(sc.next())); } } public void create2(int n ){ Scanner sc= new Scanner(System.in); for (int i = 0; i <n; i++) { insert(0,sc.next()); } } @Override //清空链表 public void clear() { head.setData(null); head.setNext(null); }
@Override //判断链表是否为空 public boolean isEmpty() { return head.getNext()==null; }
@Override //获取链表的长度 public int length() { int i=0; Node pNode =head.getNext(); while(pNode != null){ i++; pNode = pNode.getNext(); } return i; }
@Override //获取位置i的元素 public Object get(int i) { int j=0; Node pNode = head.getNext(); while(pNode!=null&&j<i){ pNode = pNode.getNext(); j++; } return pNode.getData(); }
@Override //在链表的位置i插入元素x public void insert(int i, Object x) {// Node p = head;// int j = -1;// while(p!=null && j<i-1){// p = p.getNext();// j++;// }// if(p==null||j>i-1)// try {// throw new Exception("插入的位置不合法");// } catch (Exception e) {// // TODO Auto-generated catch block// e.printStackTrace();// }// Node s = new Node(x);// s.setNext(p.getNext());// p.setNext(s); int pos = -1; Node pNode = new Node(x); Node current = head; Node prior = head; while (pos<i && current!=null) { pos++; prior = current; current=current.getNext(); } prior.setNext(pNode); pNode.setNext(current); } @Override //移除链表位置i的元素 public void remove(int i) { int pos = -1; Node current = head; Node prior = head; while (pos!=i) { pos++; prior = current; current=current.getNext(); } prior.setNext(current.getNext()); }
@Override //查找元素x所在的位置 public int indexOf(Object x) { int j=0; Node p = head.getNext(); while(p!=null&&!p.getData().equals(x)) { p = p.getNext(); ++j; } if(p!=null) return j; else return -1; } @Override //顺序打印链表中的元素 public void display() { Node current = head.getNext(); for (; current!=null; current=current.getNext()) { System.out.print(current.getData()+" "); } System.out.println(" "); } //找到倒数第k个结点的位置,并打印数据 public int search_k(SingleList singleList,int k){ int count = 0; Node p = head.getNext(); Node q = head.getNext(); while(p!=null){ if (count<k) { p=p.getNext(); count++; }else{ p = p.getNext(); q = q.getNext(); } } if (count<k) { return 0; } else{ System.out.println("倒数第k个值是:"+q.getData()); return 1; } } //删除升序链表中相同的元素 void Del_Same(){ Node p = head.getNext(); Node q = p.getNext(); while(q!=null){ if (p.getData().equals(q.getData())) { p.setNext(q.getNext()); q=p.getNext(); }else{ p = p.getNext(); q = q.getNext(); } } }}
public class LinkList { public static void main(String[] args){ SingleList singleList = new SingleList(5,true); singleList.display(); //singleList.search_k(singleList,3); singleList.Del_Same(); singleList.display();// System.out.println(singleList.indexOf(3));// singleList.remove(4);// System.out.println("标号为4的元素删除了");// singleList.display();// Object a= singleList.get(5);// System.out.println("标号为5的元素w为:");// System.out.println(a);// int a1 = singleList.length();// System.out.println("该链表的长度为:"+a1);// boolean b = singleList.isEmpty();// System.out.println("该链表判空为:"+b); }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: