stack:
1.top and bottom.统一在top端增加和删除。
Attention:
函数 delete(x) 是将top端的元素删除并且赋值给x
实现:通过linked list实现 。
public class StackLi { public StackLi( ){ topOfStack = null; } public boolean isFull( ){ return false; } public boolean isEmpty( ){ return topOfStack = = null; } public void makeEmpty( ){ topOfStack = null; } public void push( object x ) public object top( ) public void pop( ) throws Underflow public object topAndPop( ) private ListNode topOfStack; }
通过数组实现。
主要的应用:用于进行括号的匹配 尝试编程中因为之前没有怎么用过stack
主要用到的几个方法: add(x),pop(),firstelement()
public void testMatch(String expression){ matchstack=new Stack<Integer>(); int len=expression.length(); for(int i=0;i<len;i++){ char getCh=expression.charAt(i); if(getCh=='('){ matchstack.add(i); }else if(getCh==')'){ try{ matchstack.pop(); }catch(Exception e){ System.out.println(i+"missing 左括号"); } } } while(!matchstack.isEmpty()){ System.out.println(matchstack.firstElement()+"missing 右括号"); matchstack.pop(); } }
表达式:infix expression中缀表达式,也就是我们正常意义上理解的。
posfix expression 后缀表达式 prefix expression 前缀表达式
后缀表达式---->中缀表达式
具体的算法见表达式转换随笔。。
queue:
first in -first out 增加和删除在不同头。
rear增加端 front删除端
数组实现:
删除时有两种方法:
1.直接将头部的坐标修改:front=front+1; O(1) 但是会占用大量的空间
2.将元素将左移动。O(n)
解决1:使用环形的数组。
使用链表实现:摘自别的博客
#include <stdio.h> #include <stdlib.h> #define Error( str ) FatalError( str ) #define FatalError( str ) fprintf( stderr, "%s\n", str ), exit( 1 ) typedef int ElementType; #define MAXQUEUE 10 typedef struct node { ElementType data; struct node* nextNode; } Node; typedef struct queue { Node* front; // 对首指针 Node* rear; // 队尾指针 int items; // 队列中项目个数 } *ptrQueue; typedef ptrQueue Queue; int IsEmpty(Queue q); int IsFull(Queue q); Queue CreateQueue(void); void DisposeQueue(Queue q); void MakeEmpty(Queue q); void Enqueue(ElementType x, Queue q); ElementType Front(Queue q); void Dequeue(Queue q); ElementType FrontAndDequeue(Queue q); /************************************************************************/ // 主程序 /************************************************************************/ int main(void) { Queue sqQueue; //int maxElements = 10; sqQueue = CreateQueue(); if (IsEmpty(sqQueue)) printf("创建了空队列!"); int value = 0; printf("队列中的数据为(front->rear):\n"); while (!IsFull(sqQueue)) { Enqueue(value*value, sqQueue); // 入队 printf("%d ", value*value); value++; } printf("队列已满\n"); ElementType frontQueue; frontQueue = Front(sqQueue); printf("对头元素为:%d\n", frontQueue); Dequeue(sqQueue); frontQueue = Front(sqQueue); printf("执行出队操作Dequeue之后,对头元素是:%d\n", frontQueue); DisposeQueue(sqQueue); return 0; } /************************************************************************/ // 是否为空 /************************************************************************/ int IsEmpty(Queue q) { int size=q->items; return size==0; } /************************************************************************/ // 是否已满 /************************************************************************/ int IsFull(Queue q) { int size=q->items; return size>=MAXQUEUE; } /************************************************************************/ // 创建队列 /************************************************************************/ Queue CreateQueue(void) { Queue q; q=(Queue)malloc(sizeof(struct queue)); if(q==NULL){ Error("空间不足"); } q->front=(Node*)malloc(sizeof(struct node)); if(q->front==NULL ){ Error("对首失败"); } q->rear=(Node*)malloc(sizeof(struct node)); if(q->rear==NULL ){ Error("对首失败"); } q->items=0; return q; } /************************************************************************/ // 释放队列 /************************************************************************/ void DisposeQueue(Queue q) { MakeEmpty(q); free(q); } /************************************************************************/ // 使队列为空 /************************************************************************/ void MakeEmpty(Queue q) { if (q == NULL) Error("必须先使用CreateQueue创建队列!"); while (!IsEmpty(q)) Dequeue(q); } /************************************************************************/ // 入对 /************************************************************************/ void Enqueue(ElementType x, Queue q) { if (IsFull(q)) Error("队列已满!"); Node* pnode; pnode = (Node*)malloc(sizeof(Node)); if (NULL == pnode) Error("新节点分配内存失败!"); pnode->data= x; pnode->nextNode = NULL; if (IsEmpty(q)) q->front = pnode; // 项目位于首端 else q->rear->nextNode = pnode; // 链接到队列尾端 q->rear = pnode; // 记录队列尾端的位置 q->items++; // 项目数加1 return; } /************************************************************************/ // 出对 /************************************************************************/ void Dequeue(Queue q) { if (IsEmpty(q)) Error("队列本身为空!"); Node* pnode; pnode = q->front; q->front = q->front->nextNode; free(pnode); q->items--; if (q->items == 0) q->rear = NULL; return; } /************************************************************************/ // 返回对列头元素 /************************************************************************/ ElementType Front(Queue q) { if (!IsEmpty(q)) return q->front->data; Error("队列为空\n"); return 0; } /************************************************************************/ // 返回对列头元素并使对头元素出对 /************************************************************************/ ElementType FrontAndDequeue(Queue q) { ElementType x = 0; if( IsEmpty(q) ) Error( "队列为空!" ); else { q->items--; x = q->front->data; q->front = q->front->nextNode; } return x; }
应用:
1.打印多米诺
public static void printBinomial(int n){ Queue<Integer>queues=new LinkedList(); queues.add(1); queues.add(1); int s=0; for(int i=1;i<=n;i++){ System.out.println(""); for(int j=i;j<=10-i;j++){ System.out.print(" "); } queues.add(0); for(int k=1;k<=i+2;k++){ int t=queues.poll(); queues.add(s+t); s=t; if(k!=i+2){ System.out.print(s+" "); } } } }
算法:插入一个0 110 --->1210----->13310----->
2.wire routing
详解参加wire routing