LinkList的两种实现方法

最近一段时间在看算法,发现实现链表有联众方法,本科的时候学习数据结构,对于链表来说,会先建立一个头结点,firstNode,而这个first结点本身是一个node,只不过值域为空,而next域则是指向随后的结点。而在Robert Sedgewick的算法书中,是另一种实现方法,first是一个指针,直接指向真正的第一个结点,而在头部插入新的结点时,先用一个oldFirst结点引用老的first,这样,插入的新节点的next域指向oldFirst,而first指向新的Node,这样不会造成循环。

1.本科数据结构类型的linklist

  1 package Chapter1_1_3_LinkList;
  2
  3 import java.util.Iterator;
  4
  5 public class LinkList<Item> implements Iterable<Item>{
  6     private LinkNode<Item> first;
  7
  8     private int size;
  9
 10
 11 public LinkList(){
 12     first=new LinkNode<Item>();
 13     first.setE(null);
 14     first.setNext(null);
 15     //first=null;
 16     size=0;
 17 }
 18 public void addHeadNode(Item e){
 19     LinkNode<Item> newNode=new LinkNode<Item>();
 20     newNode.setE(e);
 21     newNode.setNext(first.getNext());
 22     first.setNext(newNode);
 23     size++;
 24 }
 25
 26 public Item removeNode(int n){
 27     LinkNode<Item> index=first;
 28     int i=0;
 29     while(i<n-1&&index.getNext()!=null){
 30         index=index.getNext();
 31         i++;
 32     }
 33     LinkNode<Item> removeNode=index.getNext();
 34     index.setNext(removeNode.getNext());
 35     Item e= removeNode.getE();
 36
 37     size--;
 38     return e;
 39 }
 40 public void addNode(Item e, int n){
 41     LinkNode<Item> index=first;
 42     int i=0;
 43     while(i<n&&index.getNext()!=null){
 44         index=index.getNext();
 45         i++;
 46     }
 47     LinkNode<Item> newNode=new LinkNode<Item>();
 48     newNode.setE(e);
 49     newNode.setNext(index.getNext());
 50     index.setNext(newNode);
 51     size++;
 52 }
 53 public void addTailNode(Item e){
 54     LinkNode<Item> last=first;
 55     while(last.getNext()!=null)
 56         last=last.getNext();
 57     LinkNode<Item> newNode=new LinkNode<Item>();
 58     newNode.setE(e);
 59     newNode.setNext(last.getNext());
 60     last.setNext(newNode);
 61     size++;
 62 }
 63 public Item removeHeadNode(){
 64     LinkNode<Item> firstNode=first.getNext();
 65     Item e=firstNode.getE();
 66     first=first.getNext();
 67     size--;
 68     return e;
 69 }
 70 public Item removeTailNode(){
 71     LinkNode<Item> lastpre=first;
 72     LinkNode<Item> last;
 73     while(lastpre.getNext().getNext()!=null)
 74         lastpre=lastpre.getNext();
 75         last=lastpre.getNext();
 76         Item e=last.getE();
 77     lastpre.setNext(null);
 78     size--;
 79     return e;
 80 }
 81 @Override
 82 public Iterator<Item> iterator() {
 83     // TODO Auto-generated method stub
 84     return null;
 85 }
 86 private class LinkListIterator implements Iterator<Item>{
 87     LinkNode<Item> index=first;
 88
 89     @Override
 90     public boolean hasNext() {
 91         // TODO Auto-generated method stub
 92         if(index!=null)
 93             return true;
 94         else return false;
 95     }
 96
 97     @Override
 98     public Item next() {
 99         // TODO Auto-generated method stub
100         Item e=index.getE();
101         index=index.getNext();
102         return e;
103     }
104
105     @Override
106     public void remove() {
107         // TODO Auto-generated method stub
108         System.out.println("no remove");
109     }
110
111 }
112 }

这部分主要看addHeadNode的方法,其他方法因为原理差不多就没有修正。

2.书中方法实现LinkList

stack:

 1 package Chapter1_1_3_LinkStack;
 2
 3 public class LinkStack<Item> {
 4 private LinkNode<Item> top;
 5 private int size;
 6 public LinkStack(){
 7     top=null;
 8     size=0;
 9 }
10 public void push(Item e){
11     LinkNode<Item> newNode=new LinkNode<Item>();
12     newNode.e=e;
13     LinkNode<Item> oldTop=top;
14     newNode.next=oldTop;
15     top=newNode;
16     size++;
17 }
18 public Item pop(){
19     if(isEmpty())
20     {
21         System.out.println("Stack is Empty");
22         return null;
23     }
24     Item e=top.e;
25     top=top.next;
26     size--;
27     return e;
28 }
29 public boolean isEmpty(){
30     if(top==null)
31         return true;
32     else return false;
33 }
34 public int getSize(){
35     return size;
36 }
37 }

queue:

 

 1 package Chapter1_1_3_LinkQueue;
 2
 3 public class LinkQueue<Item> {
 4 private LinkNode<Item> head;
 5 //private LinkNode<Item> tail;
 6 private int size;
 7 public LinkQueue(){
 8     head=null;
 9     //tail=null;
10     size=0;
11 }
12 public boolean isEmpty(){
13     if(head==null)
14         return true;
15     else return false;
16 }
17 public void enqueue(Item e){
18     LinkNode<Item> newNode=new LinkNode<Item>();
19     LinkNode<Item> oldHead=head;
20     newNode.element=e;
21     newNode.next=oldHead;
22     head=newNode;
23     size++;
24 }
25 public Item dequeue(){
26     if(isEmpty())
27     {
28         System.out.println("Queue is Empty");
29         return null;
30     }
31     LinkNode<Item> tail=head;
32     LinkNode<Item> preTail = head;
33     while(tail.next!=null){
34         preTail=tail;
35         tail=tail.next;
36     }
37
38     Item e=tail.element;
39     preTail.next=tail.next;
40     if(size==1){
41         head=null;
42     }
43     size--;
44     return e;
45 }
46 public int getSize(){
47     return size;
48 }
49 }

16:00:27

时间: 2024-08-18 02:21:18

LinkList的两种实现方法的相关文章

基于Apache+Tomcat负载均衡的两种实现方法

Apache+Tomcat实现负载均衡的两种实现方法 如果我们将工作在不同平台的apache能够实现彼此间的高效通信,因此它需要一种底层机制来实现--叫做apr Apr的主要目的就是为了其能够让apache工作在不同的平台上,但在linux上安装apache的时候通常都是默认安装的 [[email protected] ~]#rpm -qi aprName                 :apr                                        Relocation

OGG的Director web hang住的两种解决方法

OGG的Director web hang住的两种解决方法: OGG的Director web hang住的解释:是指web界面能登陆进去,但是看得刷新日期是很久之前的日期,并且该日期不变化. OGG的Director web hang住 的情况之一: 参考如下的mos文章: Director web displaying "Error 500-Internal Server Error". Domain log has Cannot open paging store. (Doc I

git两种合并方法 比较merge和rebase

18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用merge,所以好像都是用rebase却别讲解,比如:在服务器上的develop分支有多人在开发,你们同时clone或pull下来最新代码,但是开发进度不一样,你在开发一个任务的时候其他人提交了编号为1,2的commit和push,你现在开发完了也要提交,你的提交编号是3,4(注意:编号不代表顺序现实

Eclipse web工程创建步骤及两种部署方法

1.Eclipse创建web工程步骤 (1)参考1(2)参考2 2.web工程两种部署方法 (1)部署方法同1中所述 (2)部署到Tomcat Server的webapps目录下的方法

JAVA EE 项目经常使用知识 之AJAX技术实现select下拉列表联动的两种使用方法(让你真正理解ajax)

ajax 下拉列表联动的使用方法. ajax的定义: AJAX 是一种用于创建高速动态网页的技术. 通过在后台与server进行少量数据交换,AJAX 能够使网页实现异步更新.这意味着能够在不又一次载入整个网页的情况下,对网页的某部分进行更新. ajax效果的一个样例: 区域为空的时候,维护人情况: 选了一个区域后的情况:(选 舒城县 联带出来的维护人员 小刘) 一.原生态的js实现 XMLHttpRequest 是 AJAX 的基础 XMLHttpRequest 对象 全部现代浏览器均支持 X

MVC4.0中下来列表框的,两种使用方法DropDownList

后台控制器代码 public ActionResult Drop() { var list = new List<SchoolInfo>(); list.Add(new SchoolInfo() { SchoolID = 1, SName = "北京大学" }); list.Add(new SchoolInfo() { SchoolID = 2, SName = "上海大学" }); var model = new UserViewInfo(); mod

uboot学习之二----主Makefile学习之四----两种编译方法:原地编译和单独输出文件夹编译

第57-123行: 57 # 58 # U-boot build supports producing a object files to the separate external 59 # directory. Two use cases are supported: 60 # 61 # 1) Add O= to the make command line 62 # 'make O=/tmp/build all' 63 # 64 # 2) Set environement variable

hdu3572--Task Schedule(最大流+两种优化方法,dinic)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3651    Accepted Submission(s): 1271 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

partition函数两种实现方法

patition函数根据某种比较关系将数组分成两部分,下面根据元素比某个数字大或小,以此为基准划分,给出两种实现方式 1)若数组为a[0]~a[n-1],函数调用如下 partition(a,-1,n-1)a[n-1]一般作为基准元素所在的位置,返回基准元素应该放置的下标 int partition(int *a, int i, int j, int pivot){ do{ while (a[++i] < pivot); while ((j > 0) && (a[--j] &g