您的位置:首页 > 理论基础 > 数据结构算法

数据结构之-栈(Java实现)

2016-07-16 08:34 555 查看

理解

栈是仅在末尾进行插入和删除操作的线性表。具有LIFO(last in first out)的特性。

也就是说,栈是一种特殊的线性表

点击查看大图



在《大话数据结构》中,作者主要给了两种实现方式:基于数组和基于链表的实现。因为栈是一种特殊的线性表,我们只需要对顺序存储链式存储做一些修改,只让其操作头部和尾部,就可以达到目的。

基于数组的实现(Java实现)

实现代码

//栈(数组实现)
public class MyStack {
//大小
private int size;
//栈的最大容量
private int maxSize;
//默认最大容量为10
private static final int DEFAULT_MAX_SIZE = 10;
//数组(用来保存数据)
String stack[];

public MyStack(int maxSize) {
this.maxSize = maxSize;
stack = new String[maxSize];
}

public MyStack() {
this(DEFAULT_MAX_SIZE);
}

public void push(String s) {
if (size >= maxSize) {
throw new IndexOutOfBoundsException("栈已经满了");
}
stack[size] = s;
size++;
look();
}

public  String pop(){
String rs = stack[size-1];
stack[size-1]=null;
size--;
look();
return rs;
}

public void clear(){
for (int i = 0; i < size; i++) {
stack[i] = null;
}
size=0;
look();
}

public int getSize() {
return size;
}

private void look() {
System.out.println("");
for (int i = 0; i < maxSize; i++) {
System.out.print(stack[i]+" ");
}
}
}


演示

public class Main {
public static void main(String[] args) {
MyStack stack = new MyStack(4);
stack.push("1");
stack.push("2");
stack.push("3");
stack.push("4");
stack.pop();
stack.pop();
stack.push("a");
stack.push("b");
}
}


结果:

1 null null null
1 2 null null
1 2 3 null
1 2 3 4
1 2 3 null
1 2 null null
1 2 a null
1 2 a b


基于链表的实现(Java实现)

实现代码

//栈(链表实现)
public class MyStackNode {
//栈顶的节点
private Node top;
//大小
private int size;
//栈的最大容量
private int maxSize;
//默认最大容量为10
private static final int DEFAULT_MAX_SIZE = 10;

public MyStackNode(int maxSize) {
this.maxSize = maxSize;
}

public MyStackNode() {
this(DEFAULT_MAX_SIZE);
}

//将数据添加到栈中
public void push(String s) {
if (size >= maxSize) {
throw new IndexOutOfBoundsException("栈已经满了");
}
Node node = new Node(s, top);
top = node;
look();
size++;
}

/**
* 移除栈顶的数据
*/
public String pop() {
if (top == null)
return null;
Node oldTop = top;
Node newTop = top.next;
String rs = oldTop.s;
oldTop.next = null;
oldTop.s = null;
oldTop = null;
top = newTop;
look();
size--;
return rs;
}

/**
* 清空栈中的而数据
*/
public void clear() {
Node node = top;
while (node != null) {
Node newTop = node.next;
node.next = null;
node.s = null;
node = newTop;
}
look();
size=0;
}

class Node {
public String s;
public Node next;

public Node(String s, Node next) {
this.s = s;
this.next = next;
}

public void setNext(Node next) {
this.next = next;
}
}

//查看数据
private void look(){
System.out.println("");
Node node = top;
while (node != null) {
System.out.print(node.s + " ");
node = node.next;
}
}

public int getSize() {
return size;
}
}


演示

public class Main {
public static void main(String[] args) {
MyStackNode stack = new MyStackNode(4);
stack.push("1");
stack.push("2");
stack.push("3");
stack.push("4");
stack.pop();
stack.pop();
stack.push("a");
stack.push("b");
}
}


结果(数组实现中查看方式是从底部开始查看,这里是从顶部开始查看,所以视觉顺序不一致)

1
2 1
3 2 1
4 3 2 1
3 2 1
2 1
a 2 1
b a 2 1


两栈共享空间

这种栈的实现逻辑比较奇怪,为了节约空间而无所不用其极。我看了半天没想明白:为了节约空间,为啥不一开始就把开辟的内存空间尽可能的压缩,却要用这种奇怪的方式来节约空间?这种哲学上的思考就不去深究了,这里给出实现的代码,各位可以参考参考。

实现代码

//两栈共享空间
public class ShareStack {
//大小
private int size;
//栈的最大容量
private int maxSize;
//默认最大容量为10
private static final int DEFAULT_MAX_SIZE = 10;
//数组(用来保存数据)
private String[] stack;
private int leftTop = -1;
//右侧栈顶的索引
private int rightTop;

public ShareStack(int maxSize) {
this.maxSize = maxSize;
stack = new String[maxSize];
rightTop = maxSize;
}

public ShareStack() {
this(DEFAULT_MAX_SIZE);
}

public void pushLeft(String s) {
if (leftTop + 1 >= rightTop) {
throw new IndexOutOfBoundsException("栈已经满了");
}
leftTop++;
stack[leftTop] = s;
size++;
look();
}

public void pushRight(String s) {
if (leftTop + 1 >= rightTop) {
throw new IndexOutOfBoundsException("栈已经满了");
}
rightTop--;
stack[rightTop] = s;
size++;
look();
}

public String popLeft() {
if (leftTop == -1)
return null;
String rs = stack[leftTop];
stack[leftTop] = null;
leftTop--;
size--;
look();
return rs;
}

public String popRight() {
if (rightTop == maxSize)
return null;
String rs = stack[rightTop];
stack[rightTop] = null;
rightTop++;
size--;
look();
return rs;
}

public void clearLeft() {
int leftSize = leftTop + 1;
for (int i = 0; i < leftSize; i++) {
stack[i] = null;
}
size = size - leftSize;
leftTop = -1;
look();
}

public void clearRight() {
int rightSize = maxSize - rightTop;
for (int i = maxSize - 1; i >= rightTop; i--) {
stack[i] = null;
}
size = size - rightSize;
rightTop = maxSize;
look();
}

public int getSize() {
return size;
}

private void look() {
System.out.println("");
for (int i = 0; i < maxSize; i++) {
System.out.print(stack[i] + " ");
}
}
}


演示

public class Main {
public static void main(String[] args) {
ShareStack stack = new ShareStack(4);
stack.pushLeft("1");
stack.pushLeft("2");
stack.pushLeft("3");
stack.pushLeft("4");
stack.popLeft();
stack.popLeft();
stack.pushRight("a");
stack.pushRight("b");
}
}


结果

1 null null null
1 2 null null
1 2 3 null
1 2 3 4
1 2 3 null
1 2 null null
1 2 null a
1 2 b a


结语

数据结构-线性表之顺序储存(Java实现)数据结构-线性表之单链表(Java实现)明白的话,栈是很好实现的;因为栈是一种特殊的线性表

转载请标明出处:http://blog.csdn.net/qq_26411333/article/details/51922484
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 java