基础数据结构与算法之栈和队列-C语言实现

顺序存储实现

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define MAXSTACKSIZE 128
typedef int ElementType;

//栈及其操作集
struct StackNode {
    ElementType data[MAXSTACKSIZE];
    int top;
};
typedef struct StackNode* Stack;

Stack CreateStack() {
    Stack s = (Stack)malloc(sizeof(struct StackNode));
    if (s != NULL)
        s->top = -1;
    return s;
}

void DestoryStack(Stack s) {
    if (s != NULL)
        free(s);
    return;
}

bool Push(Stack s, ElementType x) {
    if (s == NULL || s->top == MAXSTACKSIZE - 1)
        return false;
    s->data[++s->top] = x;
    return true;
}

bool Pop(Stack s, ElementType* x) {
    if (s == NULL || s->top == -1)
        return false;
    *x = s->data[s->top--];
    return true;
}

bool Top(Stack s, ElementType* x) {
    if (s == NULL || s->top == -1)
        return false;
    *x = s->data[s->top];
    return true;
}

bool StackEmpty(Stack s) {
    return (s == NULL) || (s->top == -1);
}

bool StackFull(Stack s) {
    return (s == NULL) || (s->top == MAXSTACKSIZE - 1);
}

链式存储实现

采用一个带头结点的单链表。

#include <stdbool.h>
#include <stdlib.h>

typedef int ElementType;

struct SNode {
    ElementType Data;
    struct SNode* Next;
};
typedef struct SNode SNode;
typedef struct SNode* Stack;

Stack CreateStack() {
    Stack s = (Stack)malloc(sizeof(SNode));
    if (s != NULL) {
        s->Next = NULL;
    }
    return s;
}

bool StackEmpty(Stack s) {
    return (s != NULL) && (s->Next == NULL);
}

bool StackFull(Stack s) {
    return false;
}

bool Push(Stack s, ElementType x) {
    if (s == NULL)
        return false;
    SNode* t = (SNode*)malloc(sizeof(SNode));
    t->Data = x;
    t->Next = s->Next;
    s->Next = t;
    return true;
}

bool Pop(Stack s, ElementType* x) {
    if (s == NULL || StackEmpty(s))
        return false;
    SNode* firstCell = s->Next;
    s->Next = firstCell->Next;
    *x = firstCell->Data;
    free(firstCell);
    return true;
}

bool Top(Stack s, ElementType* x) {
    if (s == NULL || StackEmpty(s)) {
        return false;
    } else {
        *x = s->Next->Data;
        return true;
    }
}

void DestoryStack(Stack s) {
    int t;
    if (s != NULL) {
        while (Pop(s, &t))
            ;
        free(s);
    }
    return;
}

队列

顺序存储实现

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define MAXQUEUESIZE 64
typedef int ElementType;

//队列及其操作集
struct QueueNode {
    ElementType data[MAXQUEUESIZE];
    int front;
    int rear;
};
typedef struct QueueNode* Queue;

Queue CreateQueue() {
    Queue q = (Queue)malloc(sizeof(struct QueueNode));
    if (q != NULL) {
        q->front = 0;
        q->rear = 0;
    }
    return q;
}

void DestoryQueue(Queue q) {
    if (q != NULL)
        free(q);
    return;
}

bool EnQueue(Queue q, ElementType x) {
    if ((q == NULL) || (q->front == (q->rear + 1) % MAXQUEUESIZE))
        return false;
    q->data[q->rear] = x;
    q->rear = (q->rear + 1) % MAXQUEUESIZE;
    return true;
}

bool DeQueue(Queue q, ElementType* x) {
    if ((q == NULL) || (q->front == q->rear))
        return false;
    *x = q->data[q->front];
    q->front = (q->front + 1) % MAXQUEUESIZE;
    return true;
}

bool QueueEmpty(Queue q) {
    return (q != NULL) && (q->front == q->rear);
}

bool QueueFull(Queue q) {
    return (q != NULL) && (q->front == (q->rear + 1) % MAXQUEUESIZE);
}

链式存储实现

采用一个不带头结点的单链表,队列结构体内保存有链表的头指针和尾指针。

#include <stdbool.h>
#include <stdlib.h>

//链式存储队列
typedef int ElementType;

struct LinkListNode {
    ElementType data;
    struct LinkListNode* next;
};

struct QueueNode {
    struct LinkListNode* front;
    struct LinkListNode* rear;
};
typedef struct QueueNode* Queue;

Queue CreateQueue() {
    Queue q = (Queue)malloc(sizeof(struct QueueNode));
    if (q != NULL) {
        q->front = q->rear = NULL;
    }
    return q;
}

bool EnQueue(Queue q, ElementType x) {
    struct LinkListNode* newNode = (struct LinkListNode*)malloc(sizeof(struct LinkListNode));
    newNode->data = x;

    if (q->front == NULL) {
        q->front = q->rear = newNode;
    } else {
        q->rear->next = newNode;
        q->rear = newNode;
    }
    return true;
}

bool DeQueue(Queue q, ElementType* x) {
    struct LinkListNode* frontCell;
    if (q->front == NULL) {
        return false;
    }
    frontCell = q->front;
    if (q->front == q->rear)
        q->front = q->rear = NULL;
    else
        q->front = q->front->next;
    *x = frontCell->data;
    free(frontCell);
    return true;
}

void DestoryQueue(Queue q) {
    int t;
    if (q != NULL) {
        while (DeQueue(q, &t))
            ;
        free(q);
    }
    return;
}

bool QueueEmpty(Queue q) {
    return (q != NULL) && (q->front == NULL);
}

//跟顺序队列保持一致而已
bool QueueFull(Queue q) {
    return false;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值