1.定义

  

  栈:后进先出(LIFO-last in first out):最后插入的元素最先出来。

  队列:先进先出(FIFO-first in first out):最先插入的元素最先出来。

2.用数组实现栈和队列

实现栈:

  由于数组大小未知,如果每次插入元素都扩展一次数据(每次扩展都意味着构建一个新数组,然后把旧数组复制给新数组),那么性能消耗相当严重。

  这里使用贪心算法,数组每次被填满后,加入下一个元素时,把数组拓展成现有数组的两倍大小。

  每次移除元素时,检测数组空余空间有多少。当数组里的元素个数只有整个数组大小的四分之一时,数组减半。

  为什么不是当数组里的元素个数只有整个数组大小的二分之一时,数组减半?考虑以下情况:数组有4个元素,数组大小为4个元素空间。此时,加一个元素,数组拓展成8个空间;再减一个元素,数组缩小为4个空间;如此循环,性能消耗严重。

  具体代码(Java):

  

public ResizingArrayStackOfStrings()
{
s=new String[1];
int N = 0;
} pubilc void Push(String item)
{
//如果下一个加入元素超出数组容量,拓展数组
if(N == s.length) Resize(2 * s.length);
s[N++] = item;
} private void Resize(int capacity)
{
String[] copy = new String[capacity];
//将旧数组元素复制给新数组
for(int i=0; i<N; i++) copy[i] = s[i];
s = copy;
} public String Pop()
{
String item = s[--N];
s[N] = null;
//剩余元素只占数组四分之一空间时,数组减半
if(N>0 && N=s.length/4) Resize(s.length/2);
return item;
}

效果如下图:

实现队列

  与栈类似:

数组每次被填满后,加入下一个元素时,把数组拓展成现有数组的两倍大小。

  每次移除元素时,检测数组空余空间有多少。当数组里的元素个数只有整个数组大小的四分之一时,数组减半。

  不同之处在于:

由于是先进先出,移除是从队列的最前端开始的。所以当我们移除数个数据后,队列数据是存储在数组的中间部分的。令队列数据的尾端数据ID为Num,首端数据ID为HeadIndex,则Num - HeadIndex为队列数据元素个数。

当队列数据元素个数为整个数组空间的四分之一时,数组减半,且队列数据左移至数组最左端。即Num-=HeadIndex;HeadIndex=0;

  图中,HeadIndex=2;Num=5;

具体代码:

.h:

UCLASS()
class ALGORITHM_API AStackAndQueuesExerciseTwo : public AActor
{
GENERATED_BODY() public:
// Sets default values for this actor's properties
AStackAndQueuesExerciseTwo();
// Called every frame
virtual void Tick(float DeltaTime) override;
//输入
void Enqueue(int Input);
//重构数组(拓展或缩小)
void Resize(int Capacity);
//输出且移除
int Dequeue();
//队列里没元素了?
bool IsEmpty(); protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override; public: private:
//记录数组中有多少个Int
int Num;
//队列数组
TArray<int> MyIntArray;
//记录下一个移除的数据ID
int HeadIndex;
}; .cpp: AStackAndQueuesExerciseTwo::AStackAndQueuesExerciseTwo()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//一开始数组没成员
Num = ;
HeadIndex = ;
//数组中有一个假元素
MyIntArray.Add();
} // Called when the game starts or when spawned
void AStackAndQueuesExerciseTwo::BeginPlay()
{
Super::BeginPlay();
//测试
Enqueue();
Enqueue();
Enqueue();
Enqueue();
Enqueue();
Dequeue();
Dequeue();
Dequeue();
//队列数组成员
for (int i = HeadIndex; i < Num; i++)
{
UKismetSystemLibrary::PrintString(this, "i: " + FString::FromInt(i) + " End: " + FString::FromInt(MyIntArray[i]));
}
//队列数组的容量
UKismetSystemLibrary::PrintString(this, "MyIntArray.Num(): " + FString::FromInt(MyIntArray.Num()));
} // Called every frame
void AStackAndQueuesExerciseTwo::Tick(float DeltaTime)
{
Super::Tick(DeltaTime); } void AStackAndQueuesExerciseTwo::Enqueue(int Input)
{
//如果队列数组已满,拓展数组
if (Num == MyIntArray.Num())
{
Resize( * MyIntArray.Num());
}
//拓展或者数组有空位时,添加元素
if (Num < MyIntArray.Num())
{
MyIntArray[Num] = Input;
}
Num++;
} void AStackAndQueuesExerciseTwo::Resize(const int Capacity)
{
//int a[] = new int[Capacity];
TArray<int> Copy;
//添加数个假元素填充数组
for (int i = ; i < Capacity; i++)
{
Copy.Add();
}
//将队列数组赋值给Copy数组,如果是缩小数组,则把队列数组左移,节省空间
for (int i = HeadIndex; i < Num; i++)
{
Copy[i - HeadIndex] = MyIntArray[i];
}
MyIntArray = Copy;
} int AStackAndQueuesExerciseTwo::Dequeue()
{
//判断数组是否为空
if (IsEmpty())
{
UKismetSystemLibrary::PrintString(this, "No Element Exist!!!");
return ;
}
else
{
UKismetSystemLibrary::PrintString(this, "Dequeue: " + FString::FromInt(MyIntArray[HeadIndex]));
}
HeadIndex++;
//如果移除元素后,所剩元素为数组空间的四分之一,则数组减半
if ((Num - HeadIndex) != && (Num - HeadIndex) == (MyIntArray.Num() / ))
{
Resize(MyIntArray.Num() / );
//移除空间后,队列数组左移,节省空间
Num -= HeadIndex;
HeadIndex = ;
return MyIntArray[HeadIndex];
}
else
{
return MyIntArray[HeadIndex - ];
} }
//如果下一个要移除的数据不存在,则为空数组
bool AStackAndQueuesExerciseTwo::IsEmpty()
{
return HeadIndex >= Num;
}

栈与队列(Stack and Queue)的更多相关文章

  1. Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks

    题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...

  2. LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解

    题目链接 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 题目描述 使用栈实现队列的下列操作: push(x) -- 将一 ...

  3. C#LeetCode刷题之#232-用栈实现队列​​​​​​​​​​​​​​(Implement Queue using Stacks)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4108 访问. 使用栈实现队列的下列操作: push(x) -- ...

  4. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  5. Leedcode算法专题训练(栈和队列)

    1. 用栈实现队列 232. Implement Queue using Stacks (Easy) Leetcode / 力扣 class MyQueue { Stack<Integer> ...

  6. lintcode :implement queue by two stacks 用栈实现队列

    题目 用栈实现队列 正如标题所述,你需要使用两个栈来实现队列的一些操作. 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素. pop和t ...

  7. [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  8. LeetCode 232:用栈实现队列 Implement Queue using Stacks

    题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...

  9. LeetCode#232-Implement Queue using Stacks-用栈实现队列

    一.题目 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列 ...

随机推荐

  1. js中如何通过身份证号计算出生日期和年龄

    在html中有如下标签 身份证号:<input type="text" id="Gra_IDCard" onChange="IDCardChan ...

  2. 强制ubuntu登陆用户退出

    #skill -KILL -u user1 杀死并注销user1. #skill -CONT -u user1 恢复user1. 在Windows 2003默认情况下,三个以上就远程不了,必须强制登录 ...

  3. Win10系列:JavaScript 数据绑定

    使用数据绑定可以使页面中元素的属性值与数据源中的数据同步,其中数据源可以来自数据库.文件以及自定义的数据等.在常用的数据绑定方法中,简单对象绑定是将HTML元素与一个仅包含数据的简单对象相绑定,模板绑 ...

  4. 尚学堂java答案解析 第二章

    本答案为本人个人编辑,仅供参考,如果读者发现,请私信本人或在下方评论,提醒本人修改 一.选择题: 1.CD 解析:A public是关键字. B 第一个不能是数字 2.C 解析:j=i++  < ...

  5. Java工厂方法模式

    工厂方法模式: /** * 工厂方法模式:也叫工厂模式,属于创建型模式,父类工厂(接口)负责定义产品对象的公共接口, * 而子类工厂负责创建具体的产品对象. * 目的:是为了把产品的实例化操作延迟到子 ...

  6. commons-logging 与log4j的关系

    参考:http://zachary-guo.iteye.com/blog/361177

  7. Java实现将数字转为大写汉字

    public class Int2Big { static String int2big(int src) { final String num[] = {"零", "壹 ...

  8. Python返回值不同格式的取值方式

    例: { "success": true, "topic_id": "5c89021773798770589936b0"} 转换成text, ...

  9. xilinx 高速收发器Serdes深入研究-Comma码(转)

    一.为什么要用Serdes 传统的源同步传输,时钟和数据分离.在速率比较低时(<1000M),没有问题. 在速率越来越高时,这样会有问题 由于传输线的时延不一致和抖动存在,接收端不能正确的采样数 ...

  10. Excel日常操作

    1.固定表头 视图+冻结窗口+选择 2.下拉列表 数据+数据验证+序列+来源 筛选值也可是函数,函数值区间可以选择,然后隐藏该列数据即可 使用函数: 如果需要函数的值其他列也使用类似函数则拖动同样格式 ...