//顺序表publicclassSequenceList<T>implementsIterable<T>{//存储元素的数组private T[] eles;//记录当前顺序表中元素的个数privateint N;//构造方法publicSequenceList(int capacity){//初始化数组this.eles =(T[])newObject[capacity];//初始化长度this.N =0;}//将一个线性表置为空表publicvoidclear(){for(int i =0; i <this.eles.length; i++){this.eles[i]= null;}this.N =0;}//获取线性表的长度publicintlength(){returnthis.N;}//判断当前线性表是否为空表publicbooleanisEmpty(){returnthis.N ==0;}//获取指定位置的元素public T get(int i){returnthis.eles[i];}//向线性表中添加元素tpublicvoidinsert(T t){if(this.N ==this.eles.length){this.resize(2*this.eles.length);}this.eles[this.N++]= t;}//在i元素处插入元素tpublicvoidinsert(int i, T t){if(this.N ==this.eles.length){this.resize(2*this.eles.length);}//先把i索引处的元素及其后面的元素依次向后移动一位for(int index =this.N; index > i; index--){this.eles[index]=this.eles[index -1];}//再把t元素放到i索引处即可this.eles[i]= t;//元素个数加一this.N++;}//删除指定位置i处的元素,并返回该元素public T remove(int i){//记录索引i处的值
T current =this.eles[i];//索引i后面的元素依次向前移动一位即可for(int index = i; index <this.N -1; index++){this.eles[index]=this.eles[index +1];}//元素个数减一this.N--;if(this.N <this.eles.length /4){this.resize(this.eles.length /2);}return current;}//返回线性表中首次出现该元素的序号,不存在则返回-1publicintindexOf(T t){for(int i =0; i <this.N; i++){if(t.equals(this.eles[i])){return i;}}return-1;}//根据参数newsize重置eles数组的大小publicvoidresize(int newsize){//定义一个临时数组,指向原数组
T[] temp =this.eles;//创建新数组this.eles =(T[])newObject[newsize];//把原数组的数据拷贝到新数组即可for(int i =0; i <this.N; i++){this.eles[i]= temp[i];}}//提供迭代器遍历顺序表@Overridepublic Iterator<T>iterator(){returnnewSiterator();}privateclassSiteratorimplementsIterator{privateint cusor;publicSiterator(){this.cusor =0;}@OverridepublicbooleanhasNext(){returnthis.cusor < SequenceList.this.N;}@Overridepublic Object next(){return SequenceList.this.eles[this.cusor++];}}}
2 单向链表
使用Node结点类实现
链表中记录了头结点且每个结点中记录了下一个结点
代码实现:
//单链表publicclassLinkList<T>implementsIterable<T>{//记录头结点private Node head;//记录链表的长度privateint N;publicLinkList(){//初始化头结点this.head =newNode(null, null);//初始化元素个数this.N =0;}publicvoidclear(){this.head.next = null;this.N =0;}//清空链表publicintlength(){returnthis.N;}//获取链表的长度publicbooleanisEmpty(){returnthis.N ==0;}//判断链表是否为空public T get(int i){//通过循环,从头结点开始往后找,依次找i次,就可以找到对应的元素
Node n =this.head.next;for(int index =0; index < i; index++){
n = n.next;}return n.item;}//获取指定位置i处的元素//向链表中添加元素tpublicvoidinsert(T t){//找到当前最后一个结点
Node n =this.head;while(n.next != null){
n = n.next;}//创建新结点,保存元素t
Node newNode =newNode(t, null);//让当前最后一个结点指向新结点
n.next = newNode;//元素个数+1this.N++;}publicvoidinsert(int i, T t){//找到i位置前一个结点
Node pre =this.head;for(int index =0; index < i -1; index++){
pre = pre.next;}//找到i位置的结点
Node curr = pre.next;//创建新结点,并且新结点需要指向原来i位置的结点
Node newNode =newNode(t, curr);//原来i位置的前一个结点指向新结点即可
pre.next = newNode;//元素个数+1this.N++;}//向指定位置i处添加元素tpublic T remove(int i){//找到i位置前一个结点
Node pre =this.head;for(int index =0; index < i -1; index++){
pre = pre.next;}//找到i位置的结点
Node curr = pre.next;//找到i位置的下一个结点
Node nextNode = curr.next;//前一个结点指向下一个结点
pre.next = nextNode;//元素个数减一this.N--;return curr.item;}//删除指定位置i处的元素,并返回被删除的元素publicintindexOf(T t){//从头结点开始,依次找到每一个结点,取出item,和t比较,如果相同,返回index
Node n =this.head;for(int i =0; n.next != null; i++){if(n.next.item.equals(t)){return i;}}return-1;}//查找元素在链表中第一次出现的位置//对整个链表反转publicvoidreverse(){//判断当前链表是否为空链表,如果是空链表,则结束运行,如果不是,则调用重载的reverse方法完成反转if(this.isEmpty()){return;}this.reverse(this.head.next);}//反转链表中的某个结点curr,并把反转后的结点返回public Node reverse(Node curr){if(curr.next == null){this.head.next = curr;return curr;}//递归的反转当前结点curr的下一个结点,返回值就是链表反转后,当前结点的上一个结点
Node pre =this.reverse(curr.next);//让返回的结点的下一个结点指向当前结点curr
pre.next = curr;//把当前结点的下一个结点变为null
curr.next = null;return curr;}@Overridepublic Iterator<T>iterator(){returnnewLitrator();}//结点类privateclassNode{//存储数据
T item;//下一个结点
Node next;publicNode(T item, Node next){this.item = item;this.next = next;}}//提供迭代器遍历链表privateclassLitratorimplementsIterator<T>{private Node n;publicLitrator(){this.n = LinkList.this.head;}@OverridepublicbooleanhasNext(){returnthis.n.next != null;}@Overridepublic T next(){this.n =this.n.next;returnthis.n.item;}}}
3 双向链表
使用Node结点类实现
链表中记录了头结点和尾结点且每个结点中记录了当前结点的前驱和后继
代码实现:
//双向链表publicclassTwoWayLinkList<T>implementsIterable<T>{//首结点private Node head;//最后一个结点private Node last;//链表的长度privateint N;publicTwoWayLinkList(){//初始化头结点和为结点this.head =newNode(null, null, null);this.last = null;//初始化元素个数this.N =0;}//清空链表publicvoidclear(){this.head.next = null;this.last = null;this.N =0;}//获取链表长度publicintlength(){returnthis.N;}//判断链表是否为空publicbooleanisEmpty(){returnthis.N ==0;}//获取第一个元素public T getFirst(){if(this.isEmpty()){return null;}returnthis.head.next.item;}//获取最后一个元素public T getLast(){if(this.isEmpty()){return null;}returnthis.last.item;}//插入元素tpublicvoidinsert(T t){//如果链表为空if(this.isEmpty()){//创建新的结点
Node newNode =newNode(t,this.head, null);//让新结点成为尾结点this.last = newNode;//让头结点指向尾结点this.head.next =this.last;}else{//如果链表不为空//创建新的结点
Node newNode =newNode(t,this.last, null);//让当前的尾结点指向新结点this.last.next = newNode;//让新结点成为尾结点this.last = newNode;}//元素个数+1this.N++;}//想指定位置i插入元素tpublicvoidinsert(int i, T t){//找到i位置的前一个结点
Node pre =this.head;for(int index =0; index < i; index++){
pre = pre.next;}//找到i位置的结点
Node curr = pre.next;//创建新结点
Node newNode =newNode(t, pre, curr);//让i位置的前一个结点的下一个结点指向新结点
pre.next = newNode;//让i位置的前一个结点指向新结点
curr.pre = newNode;//元素个数+1this.N++;}//获取指定位置i处的元素public T get(int i){
Node n =this.head.next;for(int index =0; index < i; index++){
n = n.next;}return n.item;}//找到元素t在链表中第一次出现的位置publicintindexOf(T t){
Node n =this.head;for(int index =0; n.next != null; index++){
n = n.next;if(n.item.equals(t)){return index;}}return-1;}//删除位置i处的元素,并返回该元素public T remove(int i){//找到i位置的前一个结点
Node pre =this.head;for(int index =0; index < i; index++){
pre = pre.next;}//找到i位置的结点
Node curr = pre.next;//找到i位置的下一个结点
Node nextNode = curr.next;//让i位置的前一个结点的下一个结点指向i位置的下一个结点
pre.next = nextNode;//让i位置的下一个结点的上一个结点指向i位置的上一个结点
nextNode.pre = pre;//元素个数-1this.N--;return curr.item;}@Overridepublic Iterator iterator(){returnnewTIterator();}//结点类privateclassNode{public T item;//存储数据public Node pre;//指向前一个结点public Node next;//指向下一个结点publicNode(T item, Node pre, Node next){this.item = item;this.pre = pre;this.next = next;}}//提供迭代器遍历双向链表privateclassTIteratorimplementsIterator{private Node n;publicTIterator(){this.n = TwoWayLinkList.this.head;}@OverridepublicbooleanhasNext(){returnthis.n.next != null;}@Overridepublic Object next(){this.n =this.n.next;returnthis.n.item;}}}