(1)简单链表
- packageChapterFive;
- classLink<E>{
- publicEdata;
- publicLink<E>next;
- publicLink(Edata){
- this.data=data;
- }
- }
- classLinkList<E>{
- publicLink<E>first;
- //链表中数据项的个数
- publicintsize;
- publicLinkList(){
- first=null;
- size=0;
- }
- //在表头插入新的数据
- publicvoidinsertFirst(Evalue){
- Link<E>link=newLink<E>(value);
- link.next=first;
- first=link;
- size++;
- }
- //判断链表是否为空
- publicbooleanisEmpty(){
- returnsize==0;
- }
- //删除表头
- publicLink<E>deleteFirst(){
- Link<E>temp=first;
- first=first.next;
- size--;
- returntemp;
- }
- //输出链表中的所有数据
- publicvoiddisplay(){
- Link<E>curr=first;
- while(curr!=null){
- System.out.print(curr.data+"");
- curr=curr.next;
- }
- System.out.println();
- }
- //返回链表中数据项的个数
- publicintsize(){
- returnsize;
- }
- //获取从头至尾的第i个数据项
- publicLink<E>get(inti){
- if(i>size()-1||i<0)
- try{
- thrownewIndexOutOfBoundsException();
- }catch(Exceptione){
- e.printStackTrace();
- }
- Link<E>curr=first;
- for(intn=0;n<size();n++){
- if(n==i)
- returncurr;
- else
- curr=curr.next;
- }
- returnnull;
- }
- //输出从头至尾的第i个数据项
- publicvoidremove(inti){
- if(i==0)
- deleteFirst();
- elseif(i==size()-1)
- get(i-1).next=null;
- else{
- get(i-1).next=get(i+1);
- }
- size--;
- }
- }
- publicclassLink_list{
- publicstaticvoidmain(String[]args){
- LinkList<Long>ll=newLinkList<Long>();
- for(inti=0;i<10;i++){
- Longvalue=(long)(Math.random()*100);
- ll.insertFirst(value);
- }
- ll.display();
- while(!ll.isEmpty()){
- ll.deleteFirst();
- ll.display();
- }
- System.out.println("Ok");
- }
- }
package ChapterFive;
class Link<E> {
public E data;
public Link<E> next;
public Link(E data) {
this.data = data;
}
}
class LinkList<E> {
public Link<E> first;
//链表中数据项的个数
public int size;
public LinkList() {
first = null;
size = 0;
}
//在表头插入新的数据
public void insertFirst(E value) {
Link<E> link = new Link<E>(value);
link.next = first;
first = link;
size++;
}
//判断链表是否为空
public boolean isEmpty() {
return size == 0;
}
//删除表头
public Link<E> deleteFirst() {
Link<E> temp = first;
first = first.next;
size--;
return temp;
}
//输出链表中的所有数据
public void display() {
Link<E> curr = first;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
//返回链表中数据项的个数
public int size() {
return size;
}
//获取从头至尾的第i个数据项
public Link<E> get(int i) {
if (i > size() - 1 || i < 0)
try {
throw new IndexOutOfBoundsException();
} catch (Exception e) {
e.printStackTrace();
}
Link<E> curr = first;
for (int n = 0; n < size(); n++) {
if (n == i)
return curr;
else
curr = curr.next;
}
return null;
}
//输出从头至尾的第i个数据项
public void remove(int i) {
if (i == 0)
deleteFirst();
else if (i == size() - 1)
get(i - 1).next = null;
else {
get(i - 1).next = get(i + 1);
}
size--;
}
}
public class Link_list {
public static void main(String[] args) {
LinkList<Long> ll = new LinkList<Long>();
for (int i = 0; i < 10; i++) {
Long value = (long) (Math.random() * 100);
ll.insertFirst(value);
}
ll.display();
while (!ll.isEmpty()) {
ll.deleteFirst();
ll.display();
}
System.out.println("Ok");
}
}
(2)链栈
- packageChapterFive;
- classLinkStack<E>{
- LinkList<E>linkList;
- intsize;
- publicLinkStack(){
- size=0;
- linkList=newLinkList<E>();
- }
- //入栈
- publicvoidpush(Evalue){
- linkList.insertFirst(value);
- size++;
- }
- //出栈
- publicLink<E>pop(){
- size--;
- returnlinkList.deleteFirst();
- }
- //返回栈顶元素
- publicLink<E>top(){
- returnlinkList.first;
- }
- //判断栈是否为空
- publicbooleanisEmpty(){
- returnsize==0;
- }
- //显示栈中的全部数据
- publicvoiddisplay(){
- linkList.display();
- }
- }
- publicclassLink_stack{
- publicstaticvoidmain(String[]args){
- LinkStack<Long>ls=newLinkStack<Long>();
- for(inti=0;i<10;i++){
- Longvalue=newLong((long)(Math.random()*100));
- ls.push(value);
- }
- while(!ls.isEmpty()){
- ls.pop();
- ls.display();
- }
- System.out.println("Ok");
- }
- }
package ChapterFive;
class LinkStack<E> {
LinkList<E> linkList;
int size;
public LinkStack() {
size = 0;
linkList = new LinkList<E>();
}
//入栈
public void push(E value) {
linkList.insertFirst(value);
size++;
}
//出栈
public Link<E> pop() {
size--;
return linkList.deleteFirst();
}
//返回栈顶元素
public Link<E> top() {
return linkList.first;
}
//判断栈是否为空
public boolean isEmpty() {
return size == 0;
}
//显示栈中的全部数据
public void display() {
linkList.display();
}
}
public class Link_stack {
public static void main(String[] args) {
LinkStack<Long> ls = new LinkStack<Long>();
for (int i = 0; i < 10; i++) {
Long value = new Long((long) (Math.random() * 100));
ls.push(value);
}
while (!ls.isEmpty()) {
ls.pop();
ls.display();
}
System.out.println("Ok");
}
}
(3)有序表
- packageChapterFive;
- classSortedLink{
- publicLink<Long>first;
- intsize;
- publicSortedLink(){
- first=null;
- size=0;
- }
- //向有序链表中插入数据
- publicvoidinsert(longvalue){
- Link<Long>newLink=newLink<Long>(value);
- Link<Long>previous=null;
- Link<Long>curr=first;
- while(curr!=null&&(value>curr.data)){
- previous=curr;
- curr=curr.next;
- }
- if(previous==null)//链表为空(在表头插入)
- first=newLink;
- else
- previous.next=newLink;//插入新的节点
- newLink.next=curr;
- size++;
- }
- //删除第一个节点
- publicLink<Long>remove(){
- Link<Long>temp=first;
- first=first.next;
- size--;
- returntemp;
- }
- //判断链表是否为空
- publicbooleanisEmpty(){
- returnsize==0;
- }
- //输出链表的所有数据
- publicvoiddisplay(){
- Link<Long>curr=first;
- while(curr!=null){
- System.out.print(curr.data+"");
- curr=curr.next;
- }
- System.out.println();
- }
- }
- publicclassSortedLinkApp{
- publicstaticvoidmain(String[]args){
- SortedLinksl=newSortedLink();
- for(inti=0;i<10;i++){
- sl.insert((long)(Math.random()*100));
- }
- while(!sl.isEmpty()){
- sl.remove();
- sl.display();
- }
- }
- }
package ChapterFive;
class SortedLink {
public Link<Long> first;
int size;
public SortedLink() {
first = null;
size = 0;
}
//向有序链表中插入数据
public void insert(long value) {
Link<Long> newLink = new Link<Long>(value);
Link<Long> previous = null;
Link<Long> curr = first;
while (curr != null && (value > curr.data)) {
previous = curr;
curr = curr.next;
}
if (previous == null)// 链表为空(在表头插入)
first = newLink;
else
previous.next = newLink;//插入新的节点
newLink.next = curr;
size++;
}
//删除第一个节点
public Link<Long> remove() {
Link<Long> temp = first;
first = first.next;
size--;
return temp;
}
//判断链表是否为空
public boolean isEmpty() {
return size == 0;
}
//输出链表的所有数据
public void display() {
Link<Long> curr = first;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
}
public class SortedLinkApp {
public static void main(String[] args) {
SortedLink sl = new SortedLink();
for (int i = 0; i < 10; i++) {
sl.insert((long) (Math.random() * 100));
}
while (!sl.isEmpty()) {
sl.remove();
sl.display();
}
}
}
(4)双向链表
- packageChapterFive;
- classDoubleLink<E>{
- publicLink<E>first;
- publicLink<E>last;
- intsize;
- @SuppressWarnings("hiding")
- classLink<E>{
- publicEdata;
- publicLink<E>next;//链表的下一项
- publicLink<E>previous;//链表的前一项
- publicLink(Evalue){
- this.data=value;
- }
- }
- publicDoubleLink(){
- first=null;
- last=null;
- size=0;
- }
- //在链表的首部插入一项
- publicvoidinsertFirst(Evalue){
- Link<E>newLink=newLink<E>(value);
- if(isEmpty())//如果链表为空则first==last
- last=newLink;
- else
- first.previous=newLink;//确定原first与newLink的前后关系
- newLink.next=first;
- first=newLink;//设置新的first值
- size++;
- }
- //在链表的尾部插入一项
- publicvoidinsertLast(Evalue){
- Link<E>newLink=newLink<E>(value);
- if(isEmpty())//如果链表为空则last==first
- first=newLink;
- else{
- last.next=newLink;//确定原last与newLink的前后关系
- newLink.previous=last;
- }
- last=newLink;//设置新的last值
- size++;
- }
- //删除双向链表的表头
- publicLink<E>deleteFirst(){
- Link<E>temp=first;
- if(first.next==null)//链表中只有一项数据
- last=null;
- else
- first.next.previous=null;//销毁原链表的头部
- first=first.next;
- size--;
- returntemp;
- }
- //删除链表的最后一项
- publicLink<E>deleteLast(){
- Link<E>temp=last;
- if(first.next==null)//链表中只有一项数据
- first=null;
- else
- last.previous.next=null;//销毁原链表的尾部
- last=last.previous;
- size--;
- returntemp;
- }
- //判断链表是否为空
- publicbooleanisEmpty(){
- returnsize==0;
- }
- //输出链表中的所有数据项
- publicvoiddisplay(){
- Link<E>curr=first;
- while(curr!=null){
- System.out.print(curr.data+"");
- curr=curr.next;
- }
- System.out.println();
- }
- }
- publicclassDoubleLinkApp{
- publicstaticvoidmain(String[]args){
- DoubleLink<Integer>dl=newDoubleLink<Integer>();
- for(inti=0;i<5;i++){
- dl.insertFirst((int)(Math.random()*100));
- }
- for(inti=0;i<5;i++){
- dl.insertLast((int)(Math.random()*100));
- }
- dl.display();
- while(!dl.isEmpty()){
- dl.deleteFirst();
- dl.deleteLast();
- dl.display();
- }
- System.out.println("Ok");
- }
- }