Stack Queue Array

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Chapter-Three-Stack&Quen

Stack

动态链接可以有效解决同名函数的问题,具体见:
C/C++多个链接库含有同名函数,编译会报错吗
生成动态库步骤如下:
1. 通过编译器编译源文件:在 Linux 环境下一般使用 GCC 进行编译。编译时需要添加-
fPIC 参数来生成位置独立的代码。
gcc -fPIC -c source.c -o source.o
2. 生成动态链接库:在 Linux 环境下,使用 GCC 编译器的-shared 参数即可生成动态链接
库。
gcc -shared -fPIC -o libsource.so source.o

//5/12 20:00
判断栈的出栈顺序是否合法:
入栈元素按入栈顺序由 1 到 n 进行编号,凡是合法的出栈序列,每个数后面
的比它小的数,必然是按递减次序排列的

//TODO
5/13 完成栈的课后习题
看队列,尽量完成课后系统
Queue

//5/13 21:46
队列注重在,初始化、判空和判满。一共有三种实现方式,
一是,浪费一块内存空间用于判断队列是否满,既:
if((queue.rear +1) % MAX_SIZE == queue.front)

二是,给队列结构体设置 size 成员,以 size==MAX_SIZE 来判断


三是,设置 tag 成员,tag 为 0 时,若因删除操作导致 rear==front,则为
队空。tag 为 1 时,若因插入操作导致 rear==front,则为队满。
因此这种方法在初始化时需要设置 tag = 0。

//TO DO
完成以上三种判空满操作,继续看第三章。
//5*14 20:33 ACHIEVE!

//5/14
完成了队列的链式存储、双端队列的习题。(没有写代码)
队列的链式存储:
判空:rear == front == null
判满:链式存储为动态申请内存,一般不会出现满的情况。
初始化时 rear = front = null
插入第一个节点时需要判断,rear==front == null,若队列为空那么
在插入时需要特别处理,既第一个节点 rear=front = firstNode.
若不是第一个节点,那么执行的操作是:
rear->next = newNode;
newNode->next = null;
rear = newNode.

双端队列:
输出受限的双端队列、输入受限的双端队列。
输入受限双端队列的输入排列与原输入顺序一致,单输出可以往两
边输出。
输出受限的双端队列的输出只能从一端输出,写出输出序列,看能
否从两边插入形成这个序列。
//TODO
5/15 继续。。。。
//2023 年 5 月 22 日星期一
队列做题方法:
1. 队列逆序输出则先入队的元素必须大于后入队的元素;队列顺序输
出则先入队的元素必须小于后入队的元素。
Summarize Queue

队列是先进先出的线性表,主要考点是出队与入队序列是否合法、判断队列满、队列空、
初始化。初始化不同,对应的判断队空队满也有所不同。
1. 入队出队顺序是否合法
(1) 先进先出
(2) 队列逆序输出则先入队的元素必须大于后入队的元素;队列顺序输出则先入队的
元素必须小于后入队的元素。
2. Build the queue
Queue is belong to linear list, so we can build it by sequence list or linked list.
Using linked list build queue, you should concern the follwing points:
1) Initialize front = rear = null
2) rear pointer point the last element
3) the first insert is special

3. Define the queue


The basic constitute for queue conclude:
struct Qeuee{
int* rear;
int* front;
int data;
};

In order to save a space, we can add a element for this struct.


1) On way is add a tag variable to signifly last operation, which is one of the pop and
push.In this cases, when ‘rear == front’, we can jugement full and empty by tag.
if(rear == front && tag == 1) // full
if(rear == front && tage == 0) //empty

2) The other way to defined the queue is add size variable, which can signify the queue
now size.

4. Initialize

There are seversal common ways to initialize the queue.


//The front pointer points the first element
//The rear pointer points the next insert space
rear = 0;
front = 0;
//The front pointer points the first element
//The rear pointer points the last space
//In this case, rear will always points the last element, and the front points the first elemrnt
rear = MAX_SIZE - 1
front = 0;

5. Judge Full & Empty


1) Define the queue in basic way:
(rear + 1) % MAX_SIZE == front // watse a space
(rear == front) // empty

2) Define the queue in tag


(rear == front && tag == 1)
(rear == front && tag == 0)

3) Define the queue in size


size == 0 //empty
size == MAX_SIZE //full

6. Push & Pop


bool Push(SequenceQueue* queue, ELEMENT_TYPE value)
{
if(Full(*queue))
return false;
queue->data[queue->rear] = value;
//the rear pointer points the space where next element will be inserted.
queue->rear = (queue->rear + 1) % MAX_SIZE; // 0~MAX_SIZE-1
queue->tag = 1;
return true;
}

bool Pop(SequenceQueue* queue, ELEMENT_TYPE* value)


{
if(Empty(*queue))
return false;

*value = queue->data[queue->front];
//like rear
queue->front = (queue->front + 1) % MAX_SIZE;
queue->tag = 0;
return true;
}

You might also like