1 package com.liu.Link;
2
3 //双端链表
4 public class FirstLastList {
5 private LinkLast first;
6 private LinkLast last;
7 public FirstLastList()
8 {
9 first = null;
10 last = null;
11 }
12
13 public boolean isEmpty()
14 {
15 return first == null;
16 }
17
18 public void insertFirst(long d)
19 {
20 LinkLast newLink = new LinkLast(d);
21 if(isEmpty())
22 {
23 last = newLink;
24 }
25 newLink.next = first;
26 first = newLink;
27 }
28
29 public long deleteFirst()
30 {
31 long temp = first.dData;
32 if(first.next == null)
33 last = null;
34 first = first.next;
35 return temp;
36 }
37
38 public void insertLast(long d)
39 {
40 LinkLast newLink = new LinkLast(d);
41 if(isEmpty())
42 {
43 first = newLink;
44 }else
45 {
46 last.next = newLink;
47 }
48 last = newLink;
49 }
50
51 public void displayLast()
52 {
53 System.out.print("List (first-->last):");
54 LinkLast current = first;
55 while(current!=null)
56 {
57 current.display();
58 current = current.next;
59 }
60 System.out.println();
61 }
62 }
63 class LinkLast
64 {
65 public long dData;
66 public LinkLast next;
67
68 public LinkLast(long dd)
69 {
70 dData = dd;
71 }
72
73 public void display()
74 {
75 System.out.print(dData+" ");
76 }
77 }
78
79 class FirstLastApp
80 {
81 public static void main(String[] args)
82 {
83 FirstLastList theList = new FirstLastList();
84 theList.insertFirst(22);
85 theList.insertFirst(44);
86 theList.insertFirst(66);
87
88 theList.insertLast(11);
89 theList.insertLast(33);
90 theList.insertLast(55);
91
92 theList.displayLast();
93 theList.deleteFirst();
94 theList.deleteFirst();
95
96 theList.displayLast();
97 }
98 }