一、栈
一种特殊的线性表。顺序表和链表都可以实现。相比而言,顺序表更优一些,因为顺序表尾插的代价比较小。栈只允许在一端进行操作。将进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的元素遵守后进先出原则(LIFO Last In First Out)。
package com.xunpu.datastruct.mystack;
public class MyStack {
private int[] array;
int top;
/**
* 栈的初始化
* 设置栈的最大容量是100
*/
MyStack(){
this.array=new int[100];
this.top=0;
}
/**
* 压栈
* @param v
*/
public void push(int v){
this.array[this.top++]=v;
}
/**
* 弹栈--出数据
* @return栈顶元素
*/
public int pop(){
return this.array[this.top--];
}
/**
* 查看栈顶元素
* @return栈顶元素
*/
public int peek(){
return this.array[this.top-1];
}
/**
* 求栈的容量
* @return 返回栈中元素的个数
*/
public int size(){
return this.top;
}
/**
* 判断栈是否为空
* @return
*/
public boolean isEmpty(){
return this.top==0?true:false;
}
/**
* 销毁栈
*/
public void destry(){
this.top=0;
}
public static void main(String[] args){
MyStack stack=new MyStack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
System.out.println(stack.isEmpty());
System.out.println("栈顶元素为:"+stack.peek());//4
System.out.println(stack.size());
stack.pop();
stack.pop();
System.out.println("栈顶元素为:"+stack.peek());//2
System.out.println(stack.size());
}
}
二、队列
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。将允许插入数据的一端称为队尾,删除数据的一端称为队头。队列中元素遵循先进先出的原则。(FIFO First In First Out).
队列的操作主要有入队列和出队列。入队列在队尾,出队列在队头,如果用顺序表实现,出队列,移动元素的时间复杂度较高,成本较大。因此用链表实现。事实上,入队列用链表实现就是链表的尾插操作,出队列是链表的头删操作。
package com.xunpu.datastruct.myqueue;
public class MyQueue {
//用单向链表实现队列
private class Node{
int value;
Node next;
}
Node head;//队头
Node last;//队尾
/**
* 把数据插入到队尾(尾插)
* @param v
*/
public void push(int v){
Node node=new Node();
node.value=v;
if(this.head==null){
//如果队列为空,则设置队头和队尾都是node.
this.head=this.last=node;
}else{
//尾插
this.last.next=this.last=node;
}
}
/**
* 头删
* @return 队头元素
*/
public int pop() throws Exception {
if(this.head==null){
throw new Exception("队列中没有元素,出队列失败!!");
}
int v=this.head.value;
this.head=this.head.next;
if(this.head==null){
//删除元素后,如果队列中没有一个元素,那么设置队尾也为空
this.last=null;
}
return v;
}
/**
* 返回队首元素
* @return
*/
public int front() throws Exception {
if(this.head==null){
throw new Exception("队列中没有元素,取队头元素失败!!");
}
return this.head.value;
}
public int back() throws Exception {
if(this.head==null&&this.last==null){
throw new Exception("队列中没有元素,取队列元素失败!");
}
return this.last.value;
}
/**
* 返回队列中元素个数
* @return
*/
public int size(){
if(this.head==null){
return 0;
}
int count=0;
Node cur=this.head;
while(cur!=null){
cur=cur.next;
count++;
}
return count;
}
/**
* 判断队列是否为空
* @return
*/
public boolean isEmpty(){
return this.head==null?true:false;
}
public static void main(String[] args) throws Exception {
MyQueue queue=new MyQueue();
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
Node head=queue.head;
Node last=queue.last;
Node cur=head;
while(cur!=null){
System.out.print(cur.value+" ");
cur=cur.next;
}
System.out.println();
System.out.println("删除前,队列长度为:"+queue.size());
System.out.println(queue.pop());
System.out.println("出队列后,队列长度为:"+queue.size());
System.out.println(queue.isEmpty());
System.out.println("队头元素为:"+queue.front());
System.out.println("队尾元素为:"+queue.back());
}
}