链表实现栈和队列的方法

来源:互联网 发布:黑马程序员薪资 编辑:程序博客网 时间:2024/06/02 02:55

一,栈和队列的概念

栈和队列都是数据结构中比较重要的线性表,其中,栈是“先进后出”的线性表,队列是“先进先出”的线性表。

二,栈的链表实现

栈是指限定在表尾进行插入或者删除操作的线性表,栈的表尾成为栈顶(top),表头成为栈底(bottom)。这里先介绍栈的四个基本操作
1、push()方法是将新元素插入表内
2、pop()方法是删除并返回栈顶
3、peek()方法取出栈顶并且不以任何形式改变栈
4、isEmpty()如果栈为空则返回true
public interface StackInterface {/** * 功能描述:将新元素插入表内 * @param 新元素 */public void push(Object newEntity);/** * 功能描述:删除并返回栈顶 * @return  */public Object pop();/** * 功能描述:取出栈顶并且不以任何形式改变栈 * @return */public Object peek();/** * 功能描述:栈为空则返回true * @return */public boolean isEmpty();
创建链表
import java.io.Serializable;public class Node implements Serializable{//当前的节点的内容private Object data;//链表下一个节点private Node nextNode;public Node(Object newNode,Node topNode){this.data=data;this.nextNode=topNode;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public Node getNextNode() {return nextNode;}public void setNextNode(Node nextNode) {this.nextNode = nextNode;}}

对接口方法进行实现
public class Stack implements StackInterface{    //栈顶节点private Node topNode;public Stack(){    topNode=null;    }@Overridepublic void push(Object newEntity) {Node newNode=new Node(newEntity,topNode);topNode=newNode;}@Overridepublic Object pop() {Object top=null;if(topNode!=null){top=topNode.getData();topNode=topNode.getNextNode();}return top;}@Overridepublic Object peek() {Object top=null;if(topNode!=null){top=topNode.getData();}return top;}@Overridepublic boolean isEmpty() {return topNode==null;}    }

三,队列的链表实现

队列的所有插入都是在后端进行的,因而队列的后端为最近插入的元素,队列的前端为最早插入的元素。队列数据操作的四个方法
enqueue(newEntity)方法将新元素插入队列后端。
dequeue()方法删除并返回队列前端元素
getFront()方法是返回前端元素并且不以任何元素改变队列
isEmpty()方法是否为空
public interface QueueInterface {/** * 功能描述:将新元素插入队列后端 * @param newEntity */public void enqueue(Object newEntity);/** * 功能描述:删除对象并返回队列的前端对象 * @return */public Object dequeue();/** * 功能描述:提取队列的前端对象 * @return */public Object getFront();/** * 功能描述:检查队列是否为空 * @return */public boolean isEmpty();}

节点的实现方法
public class Node {//当前的节点的内容private Object data;//链表下一个节点private Node nextNode;public Node(Object newNode,Node topNode){this.data=data;this.nextNode=topNode;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public Node getNextNode() {return nextNode;}public void setNextNode(Node nextNode) {this.nextNode = nextNode;}}
接口的实现方法如下
public class LinkedQueue implements Serializable,QueueInterface {//队列前端的节点private Node firstNode;//对列后端的节点private Node lastNode;//构造方法初始化public LinkedQueue(){firstNode=null;lastNode=null;}@Overridepublic void enqueue(Object newEntity) {Node newNode=new Node(newEntity, null);if(isEmpty()){firstNode=newNode;}else{lastNode.setNextNode(newNode);}}@Overridepublic Object dequeue() {Object front=null;if(!isEmpty()){front=firstNode.getData();firstNode=firstNode.getNextNode();if(firstNode==null)lastNode=null;}       return front;}@Overridepublic Object getFront() {Object front =null;if(!isEmpty())front=firstNode.getData();return front;}@Overridepublic boolean isEmpty() {return firstNode==null;}}

四、总结

通过对frank M.Carrano等人《数据结构与抽象》的学习,对上文的实现方法深有体会,在此跟大家分享,大家有空可以阅读该书。











原创粉丝点击