王道2025数据结构代码总结:第三章 栈、队列和数组

3.1.2

栈的顺序存储

#define MaxSize 50
typedef struct{
    ElemType data[MaxSize];
    int top;
}SqStack;

顺序栈常用操作

①初始化

void InitStack(SqStack &S){
    S.top=-1;
}

②判栈空

bool StackEmpty(SqStack S){
    if(S.top==-1)
        return true;
    else
        return false;
}

③进栈

bool Push(SqStack &S,ElemType x){
    if(S.top==MaxSize-1)
        return false;

    S.data[++S.top]=x;//S.top=S.top+1; S.data[S.top]=x;
/*上述代码栈顶指针初始化位置为-1,若为0,则代码为
    S.data[S.top++]=x;//S.data[S.top]=x; S.top=S.top+1;*/

    return true;
}

④出栈

bool Pop(SqStack &S,ElemType &x){
    if(S.top==-1)    
        return false;
    x=S.data[S.top--];
    return true;
}

⑤读栈顶元素

bool GetTop(SqStack S,ElemType &x){
    if(S.top==-1)
        return false;
    x=S.data[S.top];
    return true;
}

栈的链式存储

typedef struct Linknode{
    ElemType data;
    struct Linknode *next;
}LiStack;

3.2.1

队列的顺序存储

#define MaxSize 50
    typedef struct{
    ElemType data[MaxSize];
    int front,rear;
}SqQueue;

循环队列的操作

①初始化

void InitQueue(SqQueue &Q){
    Q.rear=Q.front=0;
}

②判队空

bool isEmpty(SqQueue Q){
    if(Q.rear==Q.front)
        return true;
    else
        return false;
}

③入队(增)

bool EnQueue(SqQueue &Q,ElemType x){
    if((Q.rear+1)%MaxSize==Q.front)
        retuen false;
    Q.data[Q.rear]=x;
    Q.rear=(Q.rear+1)%MaxSize;
    return true;
}

④出队(删)

bool DeQueue(SqQueue &Q,ElemType &x){
    if(Q.rear==Q.front)
        return false;
    x=Q.data[Q.front];
    Q.front=(Q.front+1)%MaxSize;
    return true;
}

3.2.3

队列的链式存储

typedef struct LinkNode{
    ElemType data;
    struct LinkNode *next;
}LinkNode;
typedef struct{
    LinkNode *front,*rear;
}LinkQueue;

链式队列的基本操作

①初始化

void InitQueue(LinkQueue &Q){
    Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
    Q.front->next=NULL;
}

②判队空

bool IsEmpty(LinkQueue Q){
    if(Q.front==Q.rear)
        return true;
    else
        return false;
}

③队尾入队

//带头结点
void EnQueue(LinkQueue &Q,ElemType x){
    LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
    s->data=x;
    s->next=NULL;
    Q.rear->next=s;
    Q.rear=s;
}

//不带头结点
void EnQueue(LinkQueue &Q,ElemType x){
    LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
    s->data=x;
    s->next=NULL;
    if(Q.front==NULL)
        Q.front=s;
        Q.rear=s;
    else
        Q.rear->next=s;
        Q.rear=s;
}

④队头出队

//带头结点
bool DeQueue(LinkQueue &Q,ElemType &x){
    if(Q.front==Q.rear)
        return false;
    LinkNode *p=Q.front->next;
    x=p->data;
    Q.front->next=p->next;
    if(Q.rear==p)
        Q.rear=Q.front;
    free(p);
    return true;
}

//不带头结点
bool DeQueue(LinkQueue &Q,ElemType &x){
    if(Q.front==Q.rear)
        return false;
    LinkNode *p=Q.front;
    x=p->data;
    Q.front=p->next;
    if(Q.rear==p)
        Q.front=NULL;
        Q.rear=NULL;
    free(p);
    return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值