- 理解栈的定义、顺序、链式存储、进出栈运算及双栈操作。
- 熟练掌握栈在非递归和递归算法中的应用。
- 队列的定义、顺序、链式存储、入队和出队运算。
- 熟练掌握栈和队列的基本操作算法和应用。
1. 栈
栈的定义
书P72
堆栈(Stack):具有一定操作约束的线性表,只在一端(栈顶,Top)做插入、删除。
插入数据:入栈(Push)
删除数据:出栈(Pop)
后入先出:Last In First Out(LIFO)
栈的顺序存储
创建:书P74
Stack CreateStack( int MaxSize )
{
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
S->Top = -1;
S->MaxSize = MaxSize;
return S;
}
入栈:书P74
bool IsFull( Stack S )
{
return (S->Top == S->MaxSize-1);
}
bool Push( Stack S, ElementType X )
{
if ( IsFull(S) ) {
printf("堆栈满");
return false;
}
else {
S->Data[++(S->Top)] = X;
return true;
}
}
出栈:书P75
bool IsEmpty( Stack S )
{
return (S->Top == -1);
}
ElementType Pop( Stack S )
{
if ( IsEmpty(S) ) {
printf("堆栈空");
return ERROR; /* ERROR是ElementType的特殊值,标志错误 */
}
else
return ( S->Data[(S->Top)--] );
}
栈的链式存储
书P77
Stack CreateStack( )
{ /* 构建一个堆栈的头结点,返回该结点指针 */
Stack S;
S = malloc(sizeof(struct SNode));
S->Next = NULL;
return S;
}
bool IsEmpty ( Stack S )
{ /* 判断堆栈S是否为空,若是返回true;否则返回false */
return ( S->Next == NULL );
}
bool Push( Stack S, ElementType X )
{ /* 将元素X压入堆栈S */
PtrToSNode TmpCell;
TmpCell = (PtrToSNode)malloc(sizeof(struct SNode));
TmpCell->Data = X;
TmpCell->Next = S->Next;
S->Next = TmpCell;
return true;
}
ElementType Pop( Stack S )
{ /* 删除并返回堆栈S的栈顶元素 */
PtrToSNode FirstCell;
ElementType TopElem;
if( IsEmpty(S) ) {
printf("堆栈空");
return ERROR;
}
else {
FirstCell = S->Next;
TopElem = FirstCell->Data;
S->Next = FirstCell->Next;
free(FirstCell);
return TopElem;
}
}
双栈操作
书P75
bool Push( Stack S, ElementType X, int Tag )
{ /* Tag作为区分两个堆栈的标志,取值为1和2 */
if ( S->Top2-S->Top1 == 1) { /* 堆栈满 */
printf("堆栈满\n");
return false;
}
else {
if ( Tag == 1 ) /* 对第一个堆栈操作 */
S->Data[++(S->Top1)] = X;
else /* 对第二个堆栈操作 */
S->Data[--(S->Top2)] = X;
return true;
}
}
ElementType Pop( Stack S, int Tag )
{ /* Tag作为区分两个堆栈的标志,取值为1和2 */
if ( Tag == 1 ) { /* 对第一个堆栈操作 */
if ( S->Top1 == -1 ) { /* 堆栈1空 */
printf("堆栈1空\n");
return ERROR;
}
else return S->Data[(S->Top1)--];
}
else { /* 对第二个堆栈操作 */
if ( S->Top2 == S->MaxSize ) { /* 堆栈2空 */
printf("堆栈2空\n");
return ERROR;
}
else return S->Data[(S->Top2)++];
}
}
2.栈的应用
非递归
递归
3.队列
队列的定义
书P83
队列(Queue):具有一定操作约束的线性表。
插入和删除操作:只能在一端插入,而在另一端删除。
数据插入:入队列(AddQ)
数据删除:出队列(DeleteQ)
先进先出:FIFO
队列的顺序存储
书P84
队列的顺序存储结构通常由一个一维数组和一个记录队列头元素位置的变量front以及一个记录队列尾元素位置的变量rear组成。
队满的条件:(Rear+1)%数组长度=Front
队空的条件:Rear=Front
Queue CreateQueue( int MaxSize )
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
Q->Front = Q->Rear = 0;
Q->MaxSize = MaxSize;
return Q;
}
bool IsFull( Queue Q )
{
return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}
bool AddQ( Queue Q, ElementType X )
{
if ( IsFull(Q) ) {
printf("队列满");
return false;
}
else {
Q->Rear = (Q->Rear+1)%Q->MaxSize;
Q->Data[Q->Rear] = X;
return true;
}
}
bool IsEmpty( Queue Q )
{
return (Q->Front == Q->Rear);
}
ElementType DeleteQ( Queue Q )
{
if ( IsEmpty(Q) ) {
printf("队列空");
return ERROR;
}
else {
Q->Front =(Q->Front+1)%Q->MaxSize;
return Q->Data[Q->Front];
}
}
队列的链式存储
书P87
队列的链式存储结构也可以用一个单链表实现。插入和删除操作分别在链表的两头进行。
bool IsEmpty( Queue Q )
{
return ( Q->Front == NULL);
}
ElementType DeleteQ( Queue Q )
{
Position FrontCell;
ElementType FrontElem;
if ( IsEmpty(Q) ) {
printf("队列空");
return ERROR;
}
else {
FrontCell = Q->Front;
if ( Q->Front == Q->Rear ) /* 若队列只有一个元素 */
Q->Front = Q->Rear = NULL; /* 删除后队列置为空 */
else
Q->Front = Q->Front->Next;
FrontElem = FrontCell->Data;
free( FrontCell ); /* 释放被删除结点空间 */
return FrontElem;
}
}
4.栈和队列的应用
栈
表达式求值:书P78
中缀表达式转后缀表达式:书P82
迷宫问题:书P91
常见应用:表达式求值、函数调用和递归实现、深度优先搜索等。
队列
常见应用:广度优先搜索、操作系统中各种竞争性资源(如CPU)的管理、实际应用中服务资源的获得(如银行窗口服务)等。