链表和队列

  1. 单链表
    (1) 带头结点:head始终不等于NULL, head->next等于NULL的时候链表为空;
    (2) 不带头结点:head等于NULL时,链表为空;
typedef struct LinkNode
{
    int data;
    struct LinkNode *next;
};
  1. 双链表
    (1) 单/双链表为空时,head->next==NULL;
typedef struct DoubleLinkNode
{
    int data;
    struct DoubleLinkNode *prior;
    struct DoubleLinkNode *next;
};
  1. 循环单链表
    (1) 带头结点:为空:head==head->next;
    (2) 不带头结点:为空:head==NULL;
  2. 循环双链表
    (1) 带头结点:为空:head->next==NULL&&head->prior==NULL:
    (2) 不带头结点:为空:head==NULL;
  3. 静态链表:使用数组来实现;
  4. 顺序栈
    (1) 定义
typedef struct SequenceStack
{
    int data[SIZE_MAX];
    int top;
};

(2) 栈空判断

int isEmpty(SequenceStack st)
{
    if (st.top == -1)
    {
        return 1;
    }
    return 0;
}

(3) 进栈

int push(SequenceStack &st, int x)
{
    if (st.top == SIZE_MAX-1)
    {
        return 0;
    }
    ++(st.top);
    st.data[st.top] = x;
    return 1;
}

(4) 出栈

int pop(SequenceStack &st, int &x)
{
    if (st.top == -1)
    {
        return 0;
    }
    x = st.data[st.top];
    --(st.top);
    return 1;
    {
    }
}
  1. 链栈
    (1) 定义
typedef struct LinkStack
{
    int data;
    struct LinkStack *next;
};

(2) 栈空

int isEmpty(LinkStack *lst)
{
    if (lst->next == null)
    {
        return 1;
    }
    return 0;
}

(3) 进栈

void push(LinkStack *lst, int x)
{
    LinkStack *p;
    p = (LinkStack*)malloc(sizeof(LinkStack));
    p->next = NULL;
    //头插法
    p->data = x;
    p->next = lst->next;
    lst->next = p;
}

(4) 出栈

int pop(LinkStack *lst, int &x)
{
    LinkStack *p;
    if (lst->next == NULL)
    {
        return 0;
    }

    p = lst->next;
    x = p->data;
    lst->next = p->next;
    free(p);
    return 1;
}
  1. 顺序队
    (1) 定义
typedef struct SequenceQueue
{
    int data[SIZE_MAX];
    int front;
    int rear;
};

(2) 循环队列
队空:queue.rear == queue.front
队满:(queue.rear+1)%max_size == queue.front
(3) 队空

int isEmpty(SequeenceQueue &queue)
{
    if (queue.front == queue.rear)
    {
        return 1;
    }
    return 0;
}

(4) 入队

int enQueue(SequenceQueue &queue, int x)
{
    if ((queue+1)%max_size==queue.front)
    {
        return 0;
    }
    queue.rear = (queue.rear + 1) % max_size;
    queue.data[queue.rear] = x;
    return 1;
}

(5) 出队

int deQueue(SequenceQueue &queue, int &x)
{
    if (queue.front == queue.rear)
    {
        return 0;
    }
    queue.front = (queue.front + 1) % max_size;
    x = queue.data[queue.front];
    return 1;
    {
    }
}
  1. 链队
    (1) 定义
typedef struct QueueNode
{
    int data;
    struct QueueNode *next;
};
typedef struct LinkQueue
{
    struct LinkQueue *front;
    struct LinkQueue *next;
};

(2)队空

int isEmpty(LinkQueue *lqueue)
{
    if (lqueue.rear == NULL || lqueue.front == NULL)
    {
        return 1;
    }
    return 0;
}

(3) 入队

void enQueue(LinkQueue *lqueue, int x)
{
    QueueNode *p;
    p = (QueueNode*)malloc(sizeof(QueueNode));
    p->data = x;
    p->next = NULL;
    //队列为空,新结点为队首结点,也是队尾结点
    if (lqueue->rear == NULL)
    {
        lqueue->front = lqueue->rear = p;
    }
    else
    {
        lqueue->rear->next = p;
        lqueue->rear = p;
    }
}

(4) 出队

int deQueue(LinkQueue *lqueue, int &x)
{
    QueueNode *p;
    if (lqueue->rear==NULL)
    {
        return 0;
    }
    else
    {
        p = lqueue->front;
    }
    if (lqueue->front == lqueue->rear)
    {
        lqueue->front = lqueue->rear = NULL;
    }
    else
    {
        lqueue->front = lqueue->front->next;
    }
    x = p->data;
    free(p);
    return 1;
    {
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值