表,单链表,双链表
来源:互联网 发布:淘宝店铺一件代发流程 编辑:程序博客网 时间:2024/05/27 03:29
表
public class MyArrayList<AnyType> implements Iterable<AnyType>{ private static final int DEFAULT_CAPACITY=10; private int theSize; private AnyType [] theItems; public MyArrayList(){ clear(); } public void clear() { // TODO Auto-generated method stub theSize=0; ensureCapacity(DEFAULT_CAPACITY); } public int size(){ return theSize; } public boolean isEmpty(){ return size()==0; } public void trimToSize(){ ensureCapacity(size()); } public AnyType get(int idx){ if(idx<0||idx>=size()){ throw new ArrayIndexOutOfBoundsException(); } return theItems[idx]; } public AnyType set(int idx,AnyType newVal){ if(idx<0||idx>=size()){ throw new ArrayIndexOutOfBoundsException(); } AnyType old=theItems[idx]; theItems[idx]=newVal; return old; } public void ensureCapacity(int newCapacity) { // TODO Auto-generated method stub if(newCapacity<theSize){ return; } AnyType[] old=theItems; theItems=(AnyType[])new Object[newCapacity]; for(int i=0;i<size();i++){ theItems[i]=old[i]; } } public boolean add(AnyType x){ add(size(),x); return true; } public void add(int idx,AnyType x){ if(theItems.length==size()){ ensureCapacity(size()*2+1); } for(int i=theSize;i>idx;i--){ theItems[i]=theItems[i-1]; } theItems[idx]=x; theSize++; } public void addAll(Iterable<?extends AnyType> items){ Iterator<? extends AnyType> iter=items.iterator(); while(iter.hasNext()){ add(iter.next()); } } public void removeAll(Iterable<?extends AnyType> items){ AnyType item,element; Iterator<? extends AnyType> iter=items.iterator(); while(iter.hasNext()){ item=iter.next(); Iterator<? extends AnyType> iterList=iterator(); while(iterList.hasNext()){ element=iterList.next(); if(element.equals(item)) iterList.remove(); } } } public AnyType remove(int idx){ AnyType removedItem=theItems[idx]; for(int i=idx;i<size()-1;i++){ theItems[i]=theItems[i+1]; } theSize--; return removedItem; } @Override public java.util.Iterator<AnyType> iterator(){ return new ArrayListIterator(); } public class ArrayListIterator implements java.util.Iterator<AnyType>{ private int current=0; public boolean hasNext(){ return current<size(); } public AnyType next(){ if(!hasNext()){ throw new java.util.NoSuchElementException(); } return theItems[current++]; } public void remove(){ MyArrayList.this.remove(--current); } }}
单链表
public class SingleList { SingleList(){ init(); } boolean add(Object x){ if(contains(x)) return false; else{ Node<Object> p=new Node<Object>(x); p.next=head.next; head.next=p; theSize++; } return true; } boolean remove(Object x){ if(!contains(x)) return false; else{ Node<Object> p=head.next; Node<Object> trailer=head; while(!p.data.equals(x)){ trailer=p; p=p.next; } trailer.next=p.next; theSize--; } return true; } int size(){ return theSize; } void print(){ Node<Object> p=head.next; while(p!=null){ System.out.println(p.data+" "); p=p.next; } System.out.println(); } boolean contains(Object x){ Node<Object> p=head.next; while(p!=null){ if(x.equals(p.data)) return true; else p=p.next; } return false; } void init(){ theSize=0; head=new Node<Object>(); head.next=null; } private Node<Object> head; private int theSize; private class Node<Object>{ Object data; Node next; Node(){ this(null,null); } Node(Object d){ this(d,null); } Node(Object d, Node n) { // TODO Auto-generated constructor stub data=d; next=n; } }}
双链表
public class MyLinkedList<AnyType> implements Iterable<AnyType> { private int theSize; private int modCount=0; private Node<AnyType> beginMarker; private Node<AnyType> endMarker; private static class Node<AnyType>{ public Node(AnyType d,Node<AnyType> p,Node<AnyType> n){ data=d; prev=p; next=n; } public AnyType data; public Node<AnyType>prev; public Node<AnyType> next; } public MyLinkedList(){ clear(); } /* * Change the size of this collection to zero */ public void clear(){ beginMarker =new Node<AnyType>(null,null, null); endMarker=new Node<AnyType>(null,beginMarker,null); beginMarker.next=endMarker; theSize=0; modCount++; } public int size(){ return theSize; } public boolean isEmpty(){ return size()==0; } public boolean add(AnyType x){ add(size(),x); return true; } public void add(int idx,AnyType x){ addBefore(getNode(idx),x); } public AnyType get(int idx){ return getNode(idx).data; } public AnyType set(int idx,AnyType newVal){ Node<AnyType> p=getNode(idx); AnyType oldVal=p.data; p.data=newVal; return oldVal; } public AnyType remove(int idx){ return remove(getNode(idx)); } /* * add an item to this collection ,at specified position p */ private void addBefore(Node<AnyType> p,AnyType x){ Node<AnyType> newNode=new Node<AnyType>(x,p.prev,p); newNode.prev.next=newNode; p.prev=newNode; theSize++; modCount++; } /* * Removes the object containted in Node p */ private AnyType remove(Node<AnyType> p){ p.next.prev=p.prev; p.prev.next=p.next; theSize--; modCount++; return p.data; } /* * Gets the Node at position idx,which must range from 0 to size() */ private Node<AnyType> getNode(int idx){ Node<AnyType> p; if(idx<0||idx>size()){ throw new IndexOutOfBoundsException(); } if(idx<size()/2){ p=beginMarker.next; for(int i=0;i<idx;i++){ p=p.next; } }else{ p=endMarker; for(int i=size();i>idx;i--){ p=p.prev; } } return p; } public boolean contains(AnyType x){ Node<AnyType> p=beginMarker.next; while(p!=endMarker&&!(p.data.equals(x))){ p=p.next; } return (p!=endMarker); } @Override public Iterator<AnyType> iterator() { // TODO Auto-generated method stub return new LinkedListIterator(); } private class LinkedListIterator implements java.util.Iterator<AnyType>{ private Node<AnyType> current=beginMarker.next; private int expectedModCount=modCount; private boolean okToRemove=false; public boolean hasNext(){ return current!=endMarker; } public AnyType next(){ if(modCount!=expectedModCount) throw new java.util.ConcurrentModificationException(); if(!hasNext()) throw new java.util.NoSuchElementException(); AnyType nextItem=current.data; current =current.next; okToRemove=true; return nextItem; } public void remove(){ if(modCount!=expectedModCount) throw new java.util.ConcurrentModificationException(); if(!okToRemove) throw new IllegalStateException(); MyLinkedList.this.remove(current.prev); okToRemove=false; expectedModCount++; } }}
0 0
- 顺序表+单链表+双链表
- 表,单链表,双链表
- 单链表、双链表、循环链表总结
- 单链表,双链表,循环链表的区别
- 数据结构链表(单链表和双链表)
- 自己实现的链表(单链表、双链表)
- C++实现顺序表、单链表、双链表
- C++实现顺序表&单链表&双链表
- 单链表,双链表,循环链表的区别
- 【软考】单链表,双链表,循环链表
- 链表(单链表、双链表、内核链表)
- 链表之单链表、双链表、循环链表
- 数据结构基础(一):单链表 双链表 循环链表
- 单链表,双链表和循环链表之间的区别详解
- 基于单链表的循环链表以及双链表的实现
- C语言单链表,双向链表,循环单链表,循环双链表的基本操作
- 单链表改为双链表
- 单链表和双链表
- Activity的Flags
- 轻松入门React和Webpack (React 热插拔)
- Google为macOS量身打造的恶意软件检测系统“Santa”已开源
- java短作业优先调度算法(SJF) ---------> 求平均等待时间
- zabbix常用模板
- 表,单链表,双链表
- 欢迎使用CSDN-markdown编辑器
- left join on左连接的使用
- Error:setLatestEventInfo(AlarmAlertFullScreen,String,String,PendingIntent)(182, 10) 错误: 找不到符号 符号:
- LightOJ 1138 Trailing Zeroes
- Android之ContentProvider总结
- Eclipse运行慢的解决实践
- python在win32/windows下的安装与配置~
- IOS 开发过程中的 消息通知 小红点