2-栈和队列、数组

顺序栈结构

#define MaxSize 50
typedef struct{
    
    
    Elemtype data[MaxSize];
    int top;
}SqStack;
  • 栈空:S.top==-1
  • 栈满:S.top==MaxSize-1
  • 栈长:S.top+1

顺序栈的基本操作

(top指向栈顶的元素数,空栈为-1
  1. 初始化InitStack(&S)
void InitStack(SqStack &S){
    
    
    S.top=-1;   //初始化栈顶指针
}
  1. 栈判空StackEmpty(S)
bool StackEmpty(SqStack S){
    
    
    if(S.top==-1)//栈空
        return true;
    else
        return false;
}
  1. 进栈Push(&S, x)
bool Push(SqStack &S, ElemType x){
    
    
    if(S.top==MaxSize-1)    //栈满,报错
        return false;
    S.data[++S.top]=x;  //先+1,再入栈
    return true;
}
  1. 出栈Pop(&S,&x)(用&为的是返回S修改后的新状态和出栈x的值)
bool Pop(SqStack &S,ElemType &x){
    
    
    if(s.Top==-1)
        return false;
    x=S.data[S.top--];  //先出栈再-1
    return true;
}
  1. 栈顶元素GetTop(S,&x)
bool GetTop(SqStack S,ElemType &x){
    
    
    if(S.top==-1)
        return false;
    x=S.data[S.top];    //x记录栈顶元素
    return true;
}
  1. 销毁栈DestroyStack(&S)

共享栈

  • 栈空:top0 == -1时0号栈空,top1 == MaxSize时1号栈空。
  • 栈满:top1-top0==1

栈的链式存储结构

typedef struct Linknode{
    
    
    ElemType data;  //数据域
    struct Linknode *next; //指针域
} *LiStack;

链栈的基本操作

  1. 入栈
bool Push(LiStack &s,ELemType x)
{
    
    
    Linknode *p;
    p=(Linknode)malloc(sizeof(Linknode));
    if(!p)
        return false;
    p->data=x;
    p->next=s;
    s=p;
    return ture;
}
  1. 出栈
bool Pop(LiStack &s,ElemType &x)
{
    
    
    Linknode *p;
    if(s==NULL)
        return false;
    p=s;
    s=s->next;
    x=p->data;
    free(p);
    return true;
}
  1. 栈顶元素
bool GetTop(LiStack &s,ElemType &x)
{
    
    
    if(s==NULL)
        return false;
    x=s->data;
    return true;
}

队列

队列的基本操作

  • 初始化:InitQueue(&Q)
  • 判空:QueueEmpty(Q)
  • 入队:EnQueue(&Q, x)
  • 出队:DeQueue(&Q, &x)
  • 读队头元素:GetHead(Q, &x)

队列的顺序存储

#define MaxSize 50
typedef struct{
    
    
    ElemType data[MaxSize]; //存队列元素
    int front,rear; //队头指针front,队尾指针rear
}SqQueue;

循环队列的基本操作

front指向第一个元素,rear指向接下来要更新的位置

  • 初始:Q.rear==Q.front
void InitQueue(&Q){
    
    
    Q.rear==Q.front=0;
}
  • 入队:q.rear=(q.rear+1)%MaxSize
bool EnQueue(SqQueue &Q,ElemType x){
    
    
    if((q.rear+1)%MaxSize==Q.front)
        return false;
    Q.data[Q.rear]=x;
    Q.rear=(Q.rear+1)%MaxSize;
    return true;
}
  • 出队:Q.front=(Q.front+1)%MaxSize
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 ture;
}
  • 队列中元素个数(Q.rear-Q.front+MaxSize)%MaxSize
  • 堆满判断
    1. (Q.rear+1)%MaxSize==Q.front
    2. 增设size表示元素数据个数。Q.size==0表示队空,Q.size==MaxSize表示队满。
    3. 增设tag记录最近操作,0表示最近为删除操作,队空;1表示有插入操作,队满。

链式存储队列

存储结构
typedef struct{
    
     //链式队列结点
    ElemType data;
    struct LinkNode *next;
}LinkNode;

typedef struct{
    
     //链式队列
    LinkNode *front, *rear; //队列头结点、尾结点
}LinkQueue;
基本操作
  1. 初始化
void InitQueue(LinkQueue &Q){
    
    
    Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));//建立头结点
    Q.front->next=NULL;
}
  1. 判空
bool IsEmpty(LinkQueue Q){
    
    
    if(Q.front==Q.rear) return true;
    else return false;
}
  1. 入队
//带头结点
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;}
}
  1. 出队
bool DeQueue(LinkQueue &Q,ElemType &x){
    
    
    if(Q.front==Q.rear) return false;   //空队
    LinkNode *p=Q.front->next;//Q的头结点(front)的后继结点 放到p取出
    x=p->data;//p中的值放入x
    Q.front->next=p->next;//头指针后移到p的后继
    if(Q.rear==p)  //如果取出的结点p是尾指针所指点(说明是最后一个数了)
        Q.rear=Q.front; //把尾指针提到头结点,标示该队列为空
    free(p);
    return true;
}

猜你喜欢

转载自blog.csdn.net/qq_41962612/article/details/115082352
今日推荐