基础数据结构与算法之栈和队列-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
    评论
C语言数据结构与算法中,实现顺序队列的基本操作包括入队、出队、判断队列是否为空、判断队列是否已满、获取队头元素等操作。其中,顺序队列是一种基于数组实现队列,它的特点是元素在队列中的位置是固定的,队列的头和尾都是可以移动的指针。以下是实现顺序队列的各种基本操作的方法: 1. 入队操作:将元素插入队列尾部,同时移动队列尾指针。 2. 出队操作:将队列头部元素删除,同时移动队列头指针。 3. 判断队列是否为空:当队列头指针和队列尾指针相同时,队列为空。 4. 判断队列是否已满:当队列尾指针指向队列的最后一个元素时,队列已满。 5. 获取队头元素:返回队列头部元素的值,但不删除该元素。 6. 获取队列长度:通过队列头指针和队列尾指针的位置关系计算得出队列长度。 以下是实现顺序队列的基本操作的代码实现: ``` #define MAXSIZE 100 // 定义队列的最大长度 typedef int DataType; // 定义队列元素的数据类型 typedef struct { DataType data[MAXSIZE]; // 队列的元素数组 int front; // 队列头指针 int rear; // 队列尾指针 } SeqQueue; // 初始化队列 void InitQueue(SeqQueue *Q) { Q->front = Q->rear = 0; } // 判断队列是否为空 int IsEmpty(SeqQueue Q) { return Q.front == Q.rear; } // 判断队列是否已满 int IsFull(SeqQueue Q) { return (Q.rear + 1) % MAXSIZE == Q.front; } // 入队操作 int EnQueue(SeqQueue *Q, DataType x) { if (IsFull(*Q)) { return 0; // 队列已满,入队失败 } Q->data[Q->rear] = x; Q->rear = (Q->rear + 1) % MAXSIZE; return 1; // 入队成功 } // 出队操作 int DeQueue(SeqQueue *Q, DataType *x) { if (IsEmpty(*Q)) { return 0; // 队列为空,出队失败 } *x = Q->data[Q->front]; Q->front = (Q->front + 1) % MAXSIZE; return 1; // 出队成功 } // 获取队头元素 int GetHead(SeqQueue Q, DataType *x) { if (IsEmpty(Q)) { return 0; // 队列为空,获取队头元素失败 } *x = Q.data[Q.front]; return 1; // 获取队头元素成功 } // 获取队列长度 int GetLength(SeqQueue Q) { return (Q.rear - Q.front + MAXSIZE) % MAXSIZE; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值