栈
一:栈也是一种泛型数据结构,它准确的定义是Stack,其中E表示某种数据类型。
表 Stack类的常用方法
| 方法 | 描述 |
|---|---|
| push(value) | 将给定的值压入栈的顶端 |
| pop() | 删除并返回栈顶的值 |
| peek() | 返回栈顶端的值,但是不将其从栈中删除 |
| isEmpty() | 如果栈为空则返回true |
| size() | 返回栈中元素的个数 |
package stackDemo;
import java.util.Stack;
public class StackDemo1 {
public static void main(String[] args) {
String[] data = {"to","be","or","not","to","be"};
Stack s = new Stack();
for(String str : data){
s.push(str);
}
s.push(data[1]);
s.pop();
System.out.println("stack ="+s);
System.out.println("size ="+s.size());
System.out.println("peek ="+s.peek());
while(!s.isEmpty()){
System.out.print(s.pop()+" ");
}
System.out.println();
}
}
size =6 peek =be be to not or be to
这是Stack栈的操作的完整程序。
第一行输出表明Stack类的toString方法与列表类功能相同,方括号中的第一个值是栈底的元素。程序中调用了push()和pop()方法,添加了一个元素,但是又取出了,没有变化。注意peek()方法并不会修改栈,和pop()不同。最后一行是不断执行pop方法直到栈为空,利用isEmpty()方法。
二:利用数组来模拟栈:
package stackDemo;
import java.util.Arrays;
import java.util.Scanner;
import javax.sound.midi.Synthesizer;
import org.omg.Messaging.SyncScopeHelper;
public class ShuzuStack {
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack(4);
String key = "";
boolean loop = true;
Scanner scanner = new Scanner(System.in);
while(loop){
System.out.println("show:表示显示栈");
System.out.println("exit:退出程序");
System.out.println("push:表示添加数据到栈");
System.out.println("pop:表示从栈取出数据");
System.out.println("请输入");
key = scanner.next();
switch(key){
case"show":
arrayStack.list();
break;
case"push":
System.out.println("请输入一个数");
int value = scanner.nextInt();
arrayStack.push(value);
break;
case"pop":
try {
int res = arrayStack.pop();
System.out.println("出栈的数据是"+res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case"exit":
scanner.close();
System.out.println("拜拜");
loop = false;
break;
default:
break;
}
}
}
}
class ArrayStack{
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack(int maxSize){
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//栈满
public boolean isFull(){
return top == maxSize-1;
}
//栈空
public boolean isEmpty(){
return top == -1;
}
//入栈
public void push(int value){
if(isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
//出栈
public int pop(){
if(isEmpty()){
throw new RuntimeException("栈空,没有数据");
}
int value = stack[top];
top--;
return value;
}
public void list(){
if(isEmpty()){
System.out.println("栈空");
}else{
//System.out.println(Arrays.asList(stack));
for(int i = 0;i
三:用链表创建栈:
采用的是单链表,头插法。设置节点top,采用第一种方法,使得top始终在最顶端,非常巧妙。
package stackDemo;
import org.omg.Messaging.SyncScopeHelper;
public class linkStack {
public static void main(String[] args){
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
SinglelinkedListStack singlelinkedListStack = new SinglelinkedListStack();
singlelinkedListStack.push(node1);
singlelinkedListStack.push(node2);
singlelinkedListStack.push(node3);
singlelinkedListStack.push(node4);
singlelinkedListStack.pop();
singlelinkedListStack.show();
}
}
class SinglelinkedListStack{
private Node top = new Node(0);
//判断栈是否为空
public boolean isEmpty(){
return top.next == null;
}
//入栈
public void push(Node node){
if(isEmpty()){
top.next = node;
return;
}
//定义辅助变量。
//一:将nodet给top的next
//二:将temp的值给node的next域
Node temp = top.next;
top.next = node;
node.next = temp;
}
//出栈
public void pop(){
if(isEmpty()){
System.out.println("栈空");
return;
}
System.out.println("出栈节点为:"+top.next.value);
top = top.next;
}
//遍历
public void show(){
if(isEmpty()){
System.out.println("栈为空");
return;
}
Node temp = top;
while(temp.next != null){
System.out.println("节点为"+temp.next.value);
temp = temp.next;
}
}
}
class Node{
public int value;
public Node next;
public Node(int value){
this.value = value;
}
}



