栈与队列--判断栈/队列为空/满

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z861269429/article/details/51773960

数组栈
完成int IsEmpty(Stack S)函数,该函数判断栈是否已空,如果空返回1,否则返回0。
完成int IsFull(Stack S)函数,该函数判断栈是否已满,如果满返回1,否则返回0。

typedef int ElemType;
struct StackRecord;
typedef struct StackRecord *Stack;
struct StackRecord
{
    int Capacity; //栈容量
    int Top; //栈顶,初始为1
    ElemType *Array;
};

int IsEmpty(Stack S)
{
    if(S‐>Top==‐1)
    {
        return 1;
    } 
    else
    {
        return 0;
    }
}
int IsFull(Stack S)
{
    if(S‐>Top==S‐>Capacity‐1)
    {
        return 1;
    } 
    else
    {
        return 0;
    }
}

链栈
完成int IsEmpty(Stack S);函数,该函数判断栈S是否为空,空栈返回1,否则返回0,已知S是带头结点的链栈。

typedef int ElemType;
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode Stack;
struct Node
{
    ElemType data;
    PtrToNode next;
};

int IsEmpty(Stack S)
{
    return S‐>next==NULL?1:0;
}

数组队列
完成int IsEmpty(Queue Q);函数,该函数判断队列Q是否已空,如果已空返回1,否则返回0,其中Q是基于数组的非循环队列。
完成int IsFull(Queue Q)函数,该函数判断队列Q是否已满,如果已满返回1,否则返回0,其中Q是基于数组的非循环队列。

typedef int ElemType;
struct QueueRecord;
typedef struct QueueRecord * Queue;
struct QueueRecord
{
    int Capacity; //队列总容量
    int Front; //队首 初始值为0
    int Rear; //队尾,初始值为1
    int Size; //队列中数据数,初始值为0
    ElemType *Array;
};

int IsEmpty(Queue Q)
{
    return Q‐>Size==0;
}
int IsFull(Queue Q)
{
    return Q‐>Rear==Q‐>Capacity‐1?1:0;
}

链队列
完成int IsEmpty(Queue q)函数,该函数判定基于链表的队列q是否为空,空队列返回1,非空队列返回0,其中q是不带头节点的链表队列。

typedef int ElemType;
struct node;
typedef struct node Node;
struct queue;
typedef struct queue * Queue;
struct node
{
    ElemType data;
    Node * next;
};
struct queue
{
    Node * front; //队首
    Node * rear; //队尾
    int size; //队列中数据数int IsEmpty(Queue q)
{
    return q‐>size==0?1:0;
}

猜你喜欢

转载自blog.csdn.net/z861269429/article/details/51773960