java创建节点和单向链表

 1 package datastructure;
 2 
 3 public class Node {
 4     private Object data;
 5     private Node next;
 6     
 7     public Node() {
 8         this(null,null);
 9     }
10 
11     public Node(Object data) {
12         this(data,null);
13     }
14 
15     public Node(Object data, Node next) {
16         
17         this.data = data;
18         this.next = next;
19     }
20     public Object getData() {
21         return data;
22     }
23 
24     public void setData(Object data) {
25         this.data = data;
26     }
27 
28     public Node getNext() {
29         return next;
30     }
31 
32     public void setNext(Node next) {
33         this.next = next;
34     }
35     
36 }
  1 package datastructure;
  2 
  3 import java.util.Scanner;
  4 
  5 public class LinkList implements IList {
  6     private Node head;  //单链表的头指针
  7     public LinkList(){
  8         head=new Node(); //初始化头指针
  9     }
 10     public LinkList(int n,boolean Order) throws Exception{
 11         this();
 12         if(Order)
 13             create1(n); // 尾插法
 14         else
 15             create2(n); // 头插法
 16     }
 17     
 18     private void create1(int n) throws Exception {
 19         Scanner sc=new Scanner(System.in);
 20         for(int j=0;j<n;j++)
 21             insert(0,sc.next());
 22     }
 23     private void create2(int n) throws Exception {
 24         Scanner sc=new Scanner(System.in);
 25         for(int j=0;j<n;j++)
 26             insert(length(),sc.next());
 27     }
 28     public void clear() {
 29         head.setData(null);
 30         head.setNext(null);
 31     }
 32 
 33     public boolean isEmpty() {
 34         
 35         return head.getNext()==null;
 36     }
 37 
 38     public int length() {
 39         Node p=head.getNext();
 40         int length=0;
 41         while(p!=null){
 42             p=p.getNext();
 43             length++;
 44         }
 45         return length;
 46             
 47         
 48     }
 49 
 50     public Object get(int i) throws Exception {
 51         Node p=head.getNext();
 52         int j=0;
 53         while(p!=null&&j<i){
 54             p=p.getNext();
 55             j++;
 56         }
 57         if(j>i||p==null){
 58             throw new Exception("第i个元素不存在");
 59         }
 60         
 61         return p.getData();
 62     }
 63 
 64     public void insert(int i, Object x) throws Exception {
 65         Node p =head;
 66         int j=-1;
 67         while(p!=null&&j<i-1){
 68             p=p.getNext();
 69             j++;
 70         }
 71         if(p==null||j>i-1){
 72             throw new Exception("插入位置不合法");
 73         }
 74         Node s=new Node(x);
 75         s.setNext(p.getNext());
 76         p.setNext(s);
 77     }
 78 
 79     public void remove(int i) throws Exception {
 80         Node p=head;
 81         int j=-1;
 82         while(p.getNext()!=null&&j<i-1){
 83             p=p.getNext();
 84             j++;
 85         }
 86         if(j>i-1||p.getNext()==null)
 87             throw new Exception("删除位置不合法");
 88         p.setNext(p.getNext().getNext());
 89     }
 90 
 91     public int indexOf(Object x) {
 92         Node p=head.getNext();
 93         int j=0;
 94         while(p!=null&&!p.getData().equals(x)){
 95             p=p.getNext();
 96             j++;
 97         }
 98         if(p==null)
 99             return -1;
100         else
101             return j;
102         
103     }
104 
105     public void display() {
106         Node node =head.getNext();
107         while(node!=null){
108             System.out.println(node.getData()+" ");
109             node=node.getNext();
110         }
111         System.out.println();
112     }
113     
114 
115 }

 

posted @ 2016-02-03 18:57  zerocoin  阅读(941)  评论(0编辑  收藏  举报