数据结构与算法[栈与队列]


提示:以下是本篇文章正文内容,下面案例可供参考

一、堆栈结构和队列结构

1、什么是堆栈和队列?

堆栈和队列都是储存数据的结构,二者的不同在于储存方式不同,堆栈的储存是单开口,像是一个木桶,往里面放东西也只能从开口把东西拿出来,后进先出,队列是双开口,顺序储存数据,又顺序拿出数据,先进先出。
堆栈支持的操作:push pop
队列支持的操作:in out

2、堆栈和队列的结构

在这里插入图片描述

3、数组栈和链表栈的实现(java语言实现)

数组实现栈

public class stack
{
static final int CAPACITY=100;//容量定义
int top;
int stack[];//声明stack
public stack()
{
top = -1;
stack = new int[capacity];
}//初始化stack
}
//push
public boolean push(int val)
{
if(top>=(capacity -1))
{
system.out.println("stack overdflow.");
return false;
}
stack[++top]=val;
return true;
}
//pop
public int pop()
{
if(top<0)
{
system.out.println("stack unflow");
return 0;
}
int element =tack[top--];//后进先出 ,将top元素输出,top--
return element;
}
//Peek获取头元素

public int peek()
{
if(top<0)
{
system.out.println(“stack underflow”);
return 0;
}
int element = stack[top];
return element;
}

//empty判空
public boolean isempty()
{
return top<0;
}

链表实现栈

public class ListStack
{
static class stackNode
{
int val;
stackNode next;//节点
stackNode(int val)//节点的值
{
this.val=val;
}

}
stackNode top;//因为栈的顺序是后进先出,因此链表栈只用记录头节点即可
public ListStack()//节点指针
{
top =null;
}
}

//链表堆栈的push
public void push(int val)
{
stackNode newNode =new stackNode(val);
if(top ==null)
{
top = newNode;
}
else
{
stacNode temp = top;
top = newNode;
newNode.next= temp;
}
system.our.println(va+“is pushed to stack”);
}
//链表栈的pop
public int pop()
{
if(top ==null)
{
system.our.println(“stack is empty”);
return integer.min_value;
}
int poped = top.val;
top = top.next;
return poped;
}
//链式栈返回头节点 peek
public int peek()
{
if(top ==null)
{
system.out.println(“stack is empty”);
return integer.minvalue;
}
return top.val;
}
//判空方法
public boolean isEmpty()
{
return top ==null;
}

4、数组队列和链表队列的实现(java语言实现)

数组实现队列

public class ArrayQueue
{
int front,rear,size;
int capacity;
int array[];
public ArrayQueue(int capacity)
{
this.capacity=capacity;
front=rear=size=0;
array=new int[capacity];
}
}
//enqueue
public void enqueue(int item)
{
if(isFull())return;
array[rear]=item;
rear=(rear+1)%capacity;
size++;
system.out.println(item+"is enqueue");
//dequeue
public int dequeue()
{
if(isEmpty())
return Integer.min_value;
int item=array[front];
front =(front +1)%capacity;
size--;
return item;
}

}
//peek返回队列的头节点
public int peek()
{
if(isempty())
return integer.min_value;
return array[front];//直接返回头节点
public boolean isFull()
{
return size == capacity;
}
public boolean isEmpty()
{
return size ==0;
}
}

链式实现队列

public class ListQueue
{
QueueNode front;
QueueNode rear;
static class QueueNode
{
int value;
QueueNode next;
public QueueNode(int value)
{
this.value=value;
}
}
}
//enqueue
public void enqueue(int value)
{
QueueNode newNode = new QueueNode(value);
if(this.rear==null)
{
this.front=this.rear=newNode;
return;
}
this.rear.next=newNode;
this.rear=newNode;
}
//dequeue链表队列从头删除元素 
public int dequeue()
{
if(this.front==null)
{
system.out.println("the queue is empty");
return integer.min_value;
}
QueueNode frontNode =this.front;
this.front =this.front.next;
if(this.front==null)
{
this.rear=null;
}
return frontNode.value;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值