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

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

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

队列栈与一般线性表区别

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

实现方式

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

代码实现

栈

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);
    }
}

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

查看所有标签

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

新媒体营销概论

新媒体营销概论

秋叶、刘勇 / 人民邮电出版社 / 2016-12-1 / 36.00

本书共分6章。第1章重点介绍了新媒体的概念和特征,引导读者全面认识新媒体所处的行业;第2章用历史发展的眼光,介绍了不同类型的新媒体,让读者不仅能看到最新的新媒体模式,也能看到这个模式发展背后的脉络;第3章重点介绍了新媒体广告投放载体,便于读者选择适合自己的新媒体运营方式;第4章介绍了新媒体运营的策划思维;第5章介绍了新媒体舆情管理知识;第6章选取了可口可乐、海底捞、恒大冰泉等的新媒体助力传统行业转......一起来看看 《新媒体营销概论》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具