数据结构--栈和队列

1.1 栈和队列对应顺序栈和链栈的定义

// 顺序栈
typedef struct
{
    int data[maxSize]
    int top;
}SqStack;

// 链栈
typedef struct LNode
{
    int data;
    struct LNode *Next;
}LNode;

// 顺序队列
typedef struct
{
    int data[maxSize];
    int front;
    int rear;
}SqQueue;

// 链队列
typdef struct QNode
{
    int data;
    struct QNode *next;
}QNode

typedef struct
{
    QNode *front;
    QNode *rear;
    
}LiQueue;

/*  栈插入和删除的常用操作
------------------------
int Stack[maxSize];int top =-1;
Stack[++p]= x;
x= Stack[p--];

BTNode *Stack[maxSize] ;int top =-1; 
Stack[++p]= x;
x= Stack[p--];
------------------------
*/

2.1 顺序栈的操作


// 初始化
void  initStack(SqStack &st)
{
    st.top = -1; 
}
// 判断栈空
int isEmpty(SqStack st)
    if(st.top == -1)
        return 1;
    else
        return 0;
// 栈入
int push(SqStack &st,int x)
{
    if(st.top = maxSize -1)
        return 0;
    ++(st.top);
    st.data[st.top] = x;
    return 1;
}
// 栈出
int pop(SqStack &st, int &x)
{
    if(st.top ==-1)
        return 0;
    x = st.data[st.top];
    --st.top;
    return 1;
}

2.2 链栈的操作


// 栈空
lst-> next == NULL
// 进栈 //头插入
p->next =lst->next;lst->next =P;
// 出栈
q=lst->next;x=q->data; lst->next = q->next; free(q);

// 初始化
void initStack(LNode *&lst)
{
    lst =(LNode*)malloc(sizeof(LNode))
    lst-> next = NULL;
}

// 判断栈空
init isEmpty(LNode *lst)
{
    if(lst->next ==NULL)
        return 1;
    else
        return 0;
}

// 进栈
void push(LNode *lst,int x)
{
    LNode *p;
    p=(LNode*)malloc(sizeof(LNode))
    p->data = x;
    p->next = NULL;
    p->next = lst->next;
    lst->next = p;
}
//出栈
int pop(LNode *lst,int &x)
{
    LNode *q;
    if(lst->next == NULL)
        return 0;
    q = lst->next;
    x = q->data;
    lst->next = q->next;
    free(q);
    return 1
}

3.1 顺序队列

/* 顺序队列的常用操作
------------------------
int front,rear;
BTNode *que[maxSize];
front = rear =0
------------------------
*/
;
// 队空:qu.rear =qu.front
// 队满:(qu.rear+1)%maxSize ==qu.front
// 进队:qu.rear  = (qu.rear+1 )%maxSize; qu.data[qu.rear]  = x;
// 出队:qu.front = (qu.front+1)%maxSize; qu.data[qu.front] = x;

// 初始化
void initQueue(SqQueue &qu)
{
    qu.front = qu.rear = 0;
}
// 判断为空
int isQueueEmpty(SqQueue qu)
{
    if (qu.front = qu.rear)
        return 1;
    else
        return 0;
}
//进队
int enQueue(SqQueue &qu,int x)
{
    if((qu.rear+1)%maxSize = qu.front)
        return 1;
    qu.rear = (qu.rear+1)%maxSize;
    qu.data[qu.rear] = x;
    return 1;
}

//出队
int deQueue(Squeue &qu,int &x)
{
    if(qu.front == qu.rear)
        return 0;
    qu.front = (qu.front+1)%maxSize;
    x = qu.data[qu.front]
    return 1;
}

3.2 链队列

// 空队:lqu->rear == NULL;或者lqu->front ==NULL;
// 不存在堆满
// 进队:lqu->rear->next = p; lqu->rear=p;
// 出队:p=lqu->front;lqu->front = p->next;x=p->data;free(p);

// 初始化
void initQueue(LiQueue *&lqu)
{
    lqu =(LiQueue*)malloc(sizeof(LiQueue));
    lqu->front = lqu->rear = NULL;
}
// 判断队空
int  isQueueEmpty(LiQueue *lqu)
{
    if(lqu->rear==NULL||lqu->front==NULL)
        return 1;
    else
        return 0;
}

// 入队
void enQueue(LiQueue *lqu,int x)
{
    QNode *p;
    p = (QNode*)malloc(sizeof(QNode));
    p->data = x;
    p->next = NULL;
    if (lqu->next == NULL)
        lqu->front = lqu->rear = p;
    else
    {
        lque=rear->next = p;
        lqu->rear=p;
    }
}

// 出队
int deQueue(QNode *lqu,int &x)
{
    QNode *p;
    if(lqu->rear=NULL)
        return 0;
    else
        p = lqu->front;
    if(lqu->front==lqu->rear)
        lqu->front = lqu->rear = NULL;
    else
        lqu->front =q->next;
    x = p->data;
    free(p);
    return 1;
}

/* 常用的定义
int  front,rear;
front = rear = 0;
BTNode *que[maxSize]

BTNode *Stack[maxSize];
int top= -1;
/*

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值