栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

栈 顺序存储实现
#include 
#include 
#include 

#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 
#include 

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 
#include 
#include 

#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 
#include 

//链式存储队列
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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/290972.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号