数据结构算法学习-队列-栈

栏目: 数据库 · 发布时间: 5年前

内容简介:线性表抽象是存储具有先后顺序元素数据的结构,支持任意位置的插入,删除操作。队列和栈限制插入删除操作,队列只能从尾部插入,首部取出(删除),既先入先出;栈限制插入和取出操作只能在尾部进行,既先入后出。队列和栈同一般线性表相同,可用数组和链式结构体实现。由于限制了插入和取出的位置,没有频繁的中间元素操作,数组实现比链式实现效率更高。对应缺点为初始化时要定义数组大小,无法动态分配大小。

队列栈与一般线性表区别

线性表抽象是存储具有先后顺序元素数据的结构,支持任意位置的插入,删除操作。队列和栈限制插入删除操作,队列只能从尾部插入,首部取出(删除),既先入先出;栈限制插入和取出操作只能在尾部进行,既先入后出。

实现方式

队列和栈同一般线性表相同,可用数组和链式结构体实现。由于限制了插入和取出的位置,没有频繁的中间元素操作,数组实现比链式实现效率更高。对应缺点为初始化时要定义数组大小,无法动态分配大小。

代码实现

struct stack;
typedef struct stack sStack;
typedef sStack *pStack;

#define EmptyTOS -1;

struct stack {
    int capacity;
    int topOfStack;
    long long int *data;
};

pStack elrInitStackInt(int capaticy) {
    pStack s;

    s = malloc(sizeof(sStack));
    if (s == NULL) {
        printf("out of space!");
    }

    s->data = malloc(sizeof(long long int) * capaticy);
    if (s->data == NULL) {
        printf("out of space!");
    }

    s->capacity = capaticy;
    elrMakeStackEmpty(s);

    return s;
}

void elrFreeStackInt(pStack stack) {
    if (stack != NULL) {
        free(stack->data);
        free(stack);
    }
}

int elrIsStackEmpty(pStack stack) {
    return stack->topOfStack == EmptyTOS;
}

int elrIsStackFull(pStack stack) {
    return stack->topOfStack == (stack->capacity - 1);
}

void elrMakeStackEmpty(pStack stack) {
    stack->topOfStack = EmptyTOS;
}

void elrPushStackInt(pStack stack, long long int data) {
    if (elrIsStackFull(stack)) {
        printf("full stack");
    } else {
        stack->data[++stack->topOfStack] = data;
    }
}

long long int elrPopStackInt(pStack stack) {
    if (elrIsStackEmpty(stack)) {
        printf("empty stack");
    } else {
        return stack->data[--stack->topOfStack];
    }
}

队列

struct queue;
typedef struct queue sQueue;
typedef sQueue *pQueue;

struct queue {
    int capacity;
    int front;
    int rear;
    int size;
    long long int *data;
};

pQueue elrInitQueueInt(int capaticy) {
    pQueue s;

    s = malloc(sizeof(sQueue));
    if (s == NULL) {
        printf("out of space!");
    }

    s->data = malloc(sizeof(long long int) * capaticy);
    if (s->data == NULL) {
        printf("out of space!");
    }

    s->capacity = capaticy;
    elrMakeQueueEmpty(s);

    return s;
}

void elrFreeQueueInt(pQueue queue) {
    if (queue != NULL) {
        free(queue->data);
        free(queue);
    }
}

int elrIsQueueEmpty(pQueue queue) {
    return queue->size == 0;
}

int elrIsQueueFull(pQueue queue) {
    return queue->size == queue->capacity;
}

void elrMakeQueueEmpty(pQueue queue) {
    queue->size = 0;
    queue->front = 1;
    queue->rear = 0;
}

int succ(pQueue queue, int value) {
    if (++value == queue->capacity) {
        value = 0;
    }
    return value;
}

void elrEnqueuekInt(pQueue queue, long long int data) {
    if (elrIsQueueFull(queue)) {
        printf("full queue");
    } else {
        queue->size++;
        queue->rear = succ(queue, queue->rear);
        queue->data[queue->rear] = data;
    }
}

long long int elrDequeueInt(pQueue queue) {
    if (elrIsQueueEmpty(queue)) {
        printf("empty queue");
    } else {
        queue->size--;
        int data = queue->data[queue->front];
        queue->front = succ(queue, queue->front);
    }
}

以上所述就是小编给大家介绍的《数据结构算法学习-队列-栈》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

新零售新市场

新零售新市场

吴宇 / 人民邮电 / 2017-8

本书全面而实操地解析汽车后市场,帮助诸多正在或将要转型的企业科学转型,在竞争激烈的市场中赢得一席之地,真正实现“互联网+”的飞跃与升级。本书适合汽车后市场相关领域从业者、汽车后市场创业者,以及对汽车后市场有兴趣的读者阅读。一起来看看 《新零售新市场》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具