//用链表实现栈(链式储存结构)
public class LLNode {
public static void main(String[] args) {
LLStack llStack = new LLStack();
llStack.push(1);
llStack.push(2);
llStack.push(3);
System.out.println("栈里面的个数为" + llStack.getLength());
llStack.pop();
System.out.println("出栈一个后,栈顶的值为" + llStack.top());
}
private Object data;
private LLNode next;
public LLNode(){
}
public LLNode(Object data){
this.data = data;
}
public Object getData(){
return data;
}
public void setData(Object data){
this.data = data;
}
public LLNode getNext(){
return next;
}
public void setNext(LLNode next){
this.next = next;
}
}
//实现栈的类
class LLStack{
LLNode headnode = null;
public LLStack(){
headnode = new LLNode(null);
}
public boolean isEmpty(){
return headnode==null;
}
//入栈
public void push(Object data){
if(headnode.getData() == null){//头结点为空时
headnode.setData(data);
}else if(headnode == null){
headnode = new LLNode(data);
}else{
LLNode newnode = new LLNode(data);
newnode.setNext(headnode);
headnode = newnode;
}
}
//出栈
public Object pop(){
Object data; //= null;
if(isEmpty()){
System.out.println("栈为空");
return 0;
}
data = headnode.getData();
headnode = headnode.getNext();
return data;
}
//返回栈顶的值
public Object top(){
Object data = null;
if(isEmpty()){
System.out.println("栈为空");
return 0;
}
data = headnode.getData();
return data;
}
//找到栈里面的个数
public int getLength(){
int count = 0;
LLNode tempnode=headnode;
if(isEmpty()||tempnode.getData()==null){
count = 0;
}else{
while(tempnode!=null){
count++;
System.out.printf("栈的第%d个值为%dn",count,tempnode.getData());
tempnode=tempnode.getNext();
}
}
return count;
}
}
//用数组实现栈(顺序储存结构)
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(4);
String key ="";
boolean loop = true;//控制是否退出菜单
Scanner scanner = new Scanner(System.in);
while(loop) {
key = scanner.next();
switch (key) {
case "show":
stack.list();
break;
case "push":
int value = scanner.nextInt();
stack.push(value);
break;
case "pop":
try {
int res = stack.pop();
System.out.printf("出栈的数据为%dn", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "exit":
scanner.close();//scanner是一个流
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
class ArrayStack{
private int maxSize;//栈的大小
private int[] stack;//用数组模拟栈
private int top = -1;//top表示栈顶,初始化为-1
//构造器
public ArrayStack(int maxSize){
this.maxSize = maxSize;
stack = new int[maxSize];
}
//栈满
public boolean isFull(){
return top == maxSize-1;
}
//栈空
public boolean isEmpty(){
return top == -1;
}
//入栈
public void push(int value){
if(isFull()){
return;
}
stack[++top] = value;
}
//出栈
public int pop(){
if(isEmpty()){
throw new RuntimeException("栈空,没有数据");
}
int value = stack[top];
top--;
return value;
}
//遍历栈,ps:从栈顶开始显示数据
public void list(){
if(isEmpty()){
System.out.println("栈空,无数据");
return;
}
for(int i = top;i>=0;i--){
System.out.printf("stack[%d]=%dn",i,stack[i]);
}
}
}
2)栈的应用
逆波兰表达式
二.队列 1)队列的实现
1.链队列的结构
2.typedef struct QNode
3.{
4. int data;
5. struct QNode *next;
6.}QNode,*QueuePtr;
7.
8.typedef struct
9.{
10. QueuePtr front,rear;
11.}LinkQueue;
12.
13.入队操作:
14.int EnQueue (LinkQueue *Q,int e)//EnQueue:元素
15.{
16. QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
17. s->data=e;
18. s->next=NULL;
19. Q->rear->next=s;
20. Q->rear=s;
21. return;
22.}
23.
24.出队操作:
25. int EnQueue (LinkQueue *Q,int *e)
26. {
27. QueuePtr p;
28. if(Q->front==Q->rear)
29. {
30. return ERROR;
31. }
32. p=Q->front->next;//将想删去的结点暂存给P
33. *e=p->data;
34. Q->front->next=p->next;
35. if(Q->rear==p)//队头是队尾,则删除后将rear指向头结点
36. {
37. Q->rear=Q->front;
38. }
39. free(p);
40. return;
41. }
2)应用
思路:定义队列,存放请求的时刻
每次发起请求时,先将新的请求时刻t入队
再循环判断队头的数,是否小于t-3000,若小于,队头出队。这样就可以将3000 ms以外的时刻全部去除
循环完毕,返回队列长度



