数据结构系列-栈和队列
发布日期:2021-05-15 01:05:28 浏览次数:15 分类:精选文章

本文共 2839 字,大约阅读时间需要 9 分钟。

������������������������

�����������������������������������������������������������������������������������������������ck���������������������������������������������������������������������������.

���������

���������������������������������������������������������

������������

������������������������������������������������������������������������������top���������������������������������

���������

void InitStack(STstack* st) {
st->top = 0;
st->arr =malloc(CAP*sizeof(STData);
st->capacity = CAP;
}

���������

void StackPush(STstack* st, STData n) {
if (st->top == st->capacity) {
StackExpansion(st);
}
st->arr[st->top++] = n;
}

���elements

STData StackPop(STstack* st) {
assert(st);
assert(!StackEmpty(st));
return st->arr[--st->top];
}

������������stack

int StackEmpty(STstack* st) {
if (st->top == 0) return 1; return 0;
}

������������

void StackExpansion(STstack* st) {
STData* tmp = (STData*)realloc(st->arr, sizeof(STData) * st->capacity *2);
if (tmp == NULL) {
printf("Expansion Error\n");��exit(-1);
}
st->arr = tmp; st->capacity *=2;
}

������������

������������������������������������������top���������������������������

���������

void initstack(sta* a) {
top= NULL;
}

####������

void pushstack(sta* a, int num) {
sta* p = (sta*)malloc(sizeof(sta));
p->n = num;
p->point = top;
top = p;
}

���Stack

int popstack(sta* a) {
emptystack(a);
sta* des = top;
top = top->point;
date = des->n;
free(des);
des = NULL;
return date;
}

������������stack

void emptystack(sta* a) {
if (top == NULL) {
printf("Stack empty");��exit(0);
}
}

������������

���������������������������������������������������������������������������

���������������

������������������������������������������������������Dequeue���������������Enqueue���

���������������

void InitQueue(QUqueue* qu) {
qu->Dequeue = qu->Enqueue = NULL;
}

####������������

void QueuePush(QUqueue* qu, QUData n) {
queue* newcell = (QUData*)malloc(sizeof(QUData));
newcell->data = n;
newcell->next = NULL;
if (qu->Dequeue == NULL) {
qu->Enqueue = qu->Dequeue = newcell;
} else {
qu->Enqueue->next = newcell;
qu->Enqueue = newcell;
}
}

���elements from������

QUData QueuePop(QUqueue* qu) {
if (QueueEmpty(qu)) {
printf("Queue Is Empty");��exit(-1);
}
QUData ret = qu->Dequeue->data;
qu->Dequeue = qu->Dequeue->next;
return ret;
}

������������������

int QueueEmpty(QUqueue* qu) {
if (qu->Dequeue == qu->Enqueue) return 1; return 0;
}

������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:C++ 继承 详解
下一篇:C语言文件的基础操作

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月16日 04时21分53秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章