#include<stdio.h>
#include<assert.h>
typedef int ElemType;
typedef struct Stack
{
int top;//栈顶
ElemType* a;//后续使用动态开辟空间
int capacity;//容量
}ST;
void StackInit(ST* ps)//初始化
{
assert(ps);
ps->top = 0;
ps->a = NULL;
ps->capacity = 0;
}
void StackPush(ST* ps, ElemType x)//增
{
assert(ps);
//检查空间,如果满了就二倍增容
//如果是第一次增容则先开辟四个空间
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : (2 * ps->capacity);
ElemType* tmp = (ElemType*)realloc(ps->a, sizeof(ST) * newcapacity);
//如果ps->a为NULL,则此时realloc的作用与malloc一样
if (tmp == NULL)
{
printf("realloc fail!\n");
exit(0);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
bool StackEmpty(ST* ps)//判断是否为空
{
assert(ps);
if (ps->top == 0)//为空
{
return false;
}
else
{
return true;
}
}
void StackPop(ST* ps)//删
{
assert(ps);
assert(StackEmpty(ps));
ps->top--;
printf("栈顶数据删除成功\n");
}
ElemType StackSize(ST* ps)//判断栈的长度
{
assert(ps);
return ps->top;
}
ElemType StackTop(ST* ps)//查看栈顶
{
assert(ps);
//找栈顶的话得保证指向的空间不为空
assert(StackEmpty(ps));
//此时的top-1就是栈顶数据
return ps->a[ps->top - 1];
}
void StackDestory(ST* ps)
{
assert(ps);
if (ps->a)
{
free(ps->a);
}
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
int main()
{
ST st;
int i = 0;
int input = 0;
StackInit(&st);
for (i = 0; i < 5; i++)
{
scanf("%d", &input);
StackPush(&st,input);
}
printf("栈的长度为:%d\n", StackSize(&st));
printf("栈的顶部为:%d\n", StackTop(&st));
StackPop(&st);
printf("栈的长度为:%d\n", StackSize(&st));
printf("栈的顶部为:%d\n", StackTop(&st));
StackDestory(&st);
return 0;
}
typedef int QDataType;
typedef struct QueueNode
{
QDataType data;
QueueNode* next;
}QueueNode;
typedef struct Queue
{
QueueNode* head;
QueueNode* tail;
}Queue;
void QueueInit(Queue* pq)//初始化
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
}
void QueuePush(Queue* pq,QDataType data)//插入
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof(Queue));
if (newnode == NULL)
{
printf("malloc fail!");
exit(0);
}
newnode->data = data;
newnode->next = NULL;
if (pq->head == NULL)//第一次插入
{
pq->head = newnode;
pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
if (pq->head == NULL)
{
return false;
}
else
{
return true;
}
}
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* next = pq->head->next;
while (next)
{
free(pq->head);
pq->head = next;
next = next->next;
}
free(pq->head);
pq->head = NULL;
pq->tail = NULL;
}
void QueuePop(Queue* pq)
{
assert(pq);
assert(QueueEmpty);//为空则无法删除
if (pq->head->next == NULL)//只有一个结点的情况
{
QueueDestory(pq);
}
else
{
QueueNode* next = pq->head->next;
free(pq->head);
pq->head = next;//这里可以尝试以下
}
}
int QueueSize(Queue* pq)
{
assert(pq);
int sz = 0;
QueueNode* cur = pq->head;
while (cur)
{
cur = cur->next;
sz++;
}
return sz;
}
QDataType QueueFront(Queue* pq)//取头
{
assert(pq);
assert(QueueEmpty);//为空则无法取头
return pq->head->data;
}
QDataType QueueBack(Queue* pq)//取尾
{
assert(pq);
assert(QueueEmpty);//为空则无法取尾
return pq->tail->data;
}
int main()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
//删除
QueuePop(&q);
QueuePop(&q);
//取头
QueueFront(&q);
//取尾
QueueBack(&q);
//释放
QueueDestory(&q);
return 0;
}