对于stack,我们有以下几件操作可以进行: pop ( ) –pop the top item off the stack 将新的元素移至顶部 push ( ) –put another item onto the top 将顶部元素移除 peek ( ) –look at the top item and copy it 复制顶部元素
makeEmpty(): Remove all items from the stack isEmpty():True if stack is empty, false otherwise isFull():True if stack is full, false otherwise
publicclassStack { privateint maxSize; // size of stack array privatelong[] stackArray; privateint top; // top of stack publicStack(int s) { // constructor maxSize = s; // set array size stackArray= newlong[maxSize]; // create array top = -1; // no items yet } }
publicvoidpush(long j) { // put item on top of stack top++; stackArray[top] = j; // increment top, insert item }
publiclongpop() { // take item from top of stack return stackArray[top--]; //access item, decrement top //注意这里是top--,意味着先赋值再--。换言之,先得到这个long,然后再把顶部回压 }
publicbooleaninsert(long j) { // put item at rear of queue if(isFull()) returnfalse; //don’t insert if full if(rear == maxSize-1) // deal with wraparound rear = -1; rear++; queArray[rear] = j; // increment rear and insert nItems++; // one more item returntrue; //successfully inserted }
public Long remove() { // take item from front of queue if(isEmpty()) returnnull; //don’t remove if empty longtemp= queArray[front];// get value and incrfront front++; if(front == maxSize) // deal with wraparound front = 0; nItems--; // one less item return temp; }
publiclongpeekFront() { // peek at front of queue return queArray[front]; } publicbooleanisEmpty() { // true if queue is empty return (nItems==0); }
publicbooleanisFull() { // true if queue is full return (nItems==maxSize); } publicintsize() { // number of items in queue return nItems; }
publicclassLink { publicint data; public Link next; publicLink(int datain) { // constructor data = datain; // initialize data // 'next' is automatically set to null } }
操作
首先自然是插入链表头的操作:找元素,改连接即可
1 2 3 4 5
publicvoidinsertHead(int number) { LinknewLink=newLink(number); newLink.next= first; first = newLink; }
删除链表头:断开与第一个节点的连接,然后设置新的连接
1 2 3 4 5
public Link deleteHead() { Linktemp= first; first = first.next; return temp; }
遍历链表: 使用链表时,我们必须从链的头部开始,按照需求一步一步寻找我们的目标
不可能进行二分查找——链表的一大缺点
1 2 3 4 5 6 7 8
publicvoid display { Linkcurrent= first; // start with first link while(current != null) { current.displayLink(); //print out the link current = current.next; //keep going until you come to the end } }
public Link delete(int key) { // delete link with given ke Linkcurrent= first; // search for link Linkprevious= first; //put these equal to first Link while(current.data!= key) { if(current.next== null) { returnnull; } // didn't find it else { previous = current; // go to next link current = current.next; } } // found it if(current == first){ // if first link, first = first.next; // change first }else{ // otherwise, previous.next = current.next; // bypass it } return current; }
双端链表 Double-Ended Linked Lists
双端列表类似于普通链表,但具有一个附加功能:它引用了最后一个链接以及第一个链接
这允许在末尾和开头插入或删除新链接 便于实现队列(项目在一端到达,在另一端离开)
1 2 3 4 5
publicclassLink { publicint data; public Link next; public Link previous; }
现在每次我们插入或删除一个链接,我们必须处理四个链接而不是两个
换言之,现在我们每次要进行“更新”操作的时候,都需要处理两个节点的两个连接点
现在我们可以在列表中向后或向前移动
各种操作与单向一样,不过都改为修改两个节点。我们这里以插入作示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicvoidinsertOrdered(long data) { //inserts data in order Linkcurrent= first; // start at beginning while(current !=null && data > current.data) { current = current.next; } // move to next link LinknewLink=newLink(data); // make new link if(current == first) { // if insertion at head insertHead(data); } elseif(current == last){ //if insertion at tail insertTail(data); } else { // somewhere in middle newLink.previous = current.previous; // step 1 current.previous.next= newLink; // step 2 newLink.next= current; // step 3 current.previous= newLink; // step 4 } }
publicvoidgetIterator(){ LinkedListtheList=newLinkedList(); ListIteratoriter1= theList.getIterator(); LinkaLink= iter1.getCurrent(); //access link at iterator iter1.nextLink();// move iterator to next Link }