栈存储数据的特点是先入后出,它是一种特殊的线性表只允许在一端插入删除数据,进行这些操作的那一端称为栈顶,另一端称为栈底。
入栈:数据压入栈中。
出栈:从栈中删除数据。
栈的C语言实现
typedef char STDataType;
typedef struct Stack
{
STDataType* data;
int top;
long capacity;
}Stack;
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
void StackPrint(Stack* ps);
void StackInit(Stack* ps)
{
assert(ps);
ps->capacity = 5;
ps->data = (STDataType*)malloc(sizeof(STDataType) * ps->capacity);
ps->top = 0;
}
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->capacity == ps->top)
{
ps->data = realloc(ps->data, sizeof(STDataType) * (ps->capacity) * 2);
(ps->capacity) = 2 * ps->capacity;
}
ps->data[ps->top] = data;
ps->top++;
}
void StackPop(Stack* ps)
{
assert(ps);
if (ps->top == 0)
{
printf("stack is nulln");
exit(1);
}
else
{
(ps->top)--;
}
}
STDataType StackTop(Stack* ps)
{
assert(ps);
return ps->data[(ps->top) - 1];
}
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
int StackEmpty(Stack* ps)
{
assert(ps);
if (ps->top == 0)
{
return 0;
}
return -1;
}
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->data);
free(ps);
}
void StackPrint(Stack* ps)
{
assert(ps);
for (int i = 0; i < ps->top; i++)
{
printf("%c ", ps->data[i]);
}
}
队列:
一种只允许在一端删除数据,在另一端插入数据的特殊线性表。队列具有先进先出的特点,插入的一段称作队尾,删除的一端为对头。
#pragma once #include#include #include // 链式结构:表示队列 typedef int QDataType; typedef struct QListNode { struct QListNode* _pNext; QDataType _data; }QNode; // 队列的结构 typedef struct Queue { QNode* _front; QNode* _rear; }Queue; // 初始化队列 void QueueInit(Queue* q); // 队尾入队列 void QueuePush(Queue* q, QDataType data); // 队头出队列 void QueuePop(Queue* q); // 获取队列头部元素 QDataType QueueFront(Queue* q); // 获取队列队尾元素 QDataType QueueBack(Queue* q); // 获取队列中有效元素个数 int QueueSize(Queue* q); // 检测队列是否为空,如果为空返回非零结果,如果非空返回0 int QueueEmpty(Queue* q); // 销毁队列 void QueueDestroy(Queue* q); void QueuePrint(Queue* q); #include"Queue.h" void QueueInit(Queue* q) { assert(q); q->_front = NULL; q->_rear = NULL; } void QueuePush(Queue* q, QDataType data) { if (q->_front == NULL) { QNode* newnode = (QNode*)malloc(sizeof(QNode)); newnode->_pNext = NULL; newnode->_data = data; q->_rear = newnode; q->_front = newnode; return; } QNode* newnode = (QNode*)malloc(sizeof(QNode)); newnode->_pNext = NULL; newnode->_data = data; q->_rear->_pNext = newnode; q->_rear = newnode; } void QueuePop(Queue* q) { assert(q); if (q->_front == NULL) { printf("队列为空n"); return; } QNode* cur = q->_front; q->_front = q->_front->_pNext; free(cur); } QDataType QueueFront(Queue* q) { assert(q); return q->_front->_data; } QDataType QueueBack(Queue* q) { assert(q); return q->_rear->_data; } int QueueEmpty(Queue* q) { if (q->_front == NULL && q->_rear == NULL) { return 1; } return 0; } int QueueSize(Queue* q) { assert(q); int size = 0; QNode* cur = q->_front; while (cur) { size++; cur = cur->_pNext; } return size; } void QueueDestroy(Queue* q) { assert(q); QNode* cur = q->_front; while (cur) { QNode* tmp = cur; cur = cur->_pNext; free(tmp); } } void QueuePrint(Queue* q) { assert(q); QNode* cur = q->_front; while (cur) { printf("%d ", cur->_data); cur = cur->_pNext; } }



