###栈
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef int DataType;
#define Stack_size 10
typedef struct Stack
{
DataType* _array;//数组指针
size_t _top; //栈顶
size_t _end;//最大容量
}Stack;
-----------------------------------------------------------
// 栈的实现接口
void StackInit(Stack* s)//栈的初始化
{
assert(s);
s->_array = (DataType*)malloc(sizeof(DataType)*Stack_size);
s->_end = Stack_size;
s->_top = 0;
}
-----------------------------------------------------------
void StackPush(Stack* s, DataType x)//入栈
{
assert(s);
if (s->_end == s->_top)//栈已满
{
s->_end *= 2;
s->_array = (DataType*)realloc(s->_array, sizeof(DataType)*(s->_end));
s->_array[s->_top] = x;
(s->_top)++;
}
else
{
s->_array[s->_top] = x;
(s->_top)++;
}
}
---------------------------------------------------------
void Stack_print(Stack *s)
{
if (s == NULL)
{
return;
}
while ((s->_top)--)
{
printf("%d\t", s->_array[s->_top]);
}
}
---------------------------------------------------------
void StackPop(Stack* s)//出栈
{
assert(s);
if (s->_top == 0)
{
printf("the stack is empty");
}
else
{
s->_top--;
}
}
---------------------------------------------------------
DataType StackTop(Stack* s)//取栈顶元素
{
assert(s);
if (s->_top == 0)
{
printf("the stack is empty");
return;
}
else
{
int num = s->_top;
int i = s->_array[(--num)];
return i;
}
}
---------------------------------------------------------
size_t StackSize(Stack* s)//栈的长度
{
assert(s);
return s->_end;
}
---------------------------------------------------------
int StackEmpty(Stack* s)//判断栈是否为空
{
if (s->_top == 0)
{
return 1;
}
else
{
return 0;
}
}
###队列
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef int DataType;
typedef struct QueueNode
{
DataType _data; //链式队列
struct QueueNode* _next;
}QueueNode;
typedef struct Queue
{
QueueNode* _head;//队头指针
QueueNode* _tail;//队尾指针
}Queue;
------------------------------------------------
void QueueInit(Queue* q)//队列初始化
{
q->_head = (QueueNode*)malloc(sizeof(QueueNode));
assert(q->_head);
q->_tail = q->_head;
q->_tail->_next = NULL;
}
-------------------------------------------------
void QueuePush(Queue* q, DataType x)//入队
{
assert(q);
QueueNode *newnode = (QueueNode*)malloc(sizeof(QueueNode));
assert(newnode);
newnode->_data = x;
newnode->_next = NULL;
q->_tail->_next = newnode;
q->_tail = newnode;
}
---------------------------------------------------
void QueuePop(Queue* q)//出队
{
assert(q);
if (q->_head == q->_tail)
{
printf("the queue is empty.");
return;
}
QueueNode *p = q->_head->_next;
q->_head = p;
}
---------------------------------------------------
DataType QueueFront(Queue* q)//取队头数据
{
assert(q);
if (q->_head == q->_tail)
{
printf("the queue is empty.");
return 0;
}
return (q->_head->_next)->_data;
}
----------------------------------------------------
DataType QueueBack(Queue* q)//取队尾数据
{
assert(q);
if (q->_head == q->_tail)
{
printf("the queue is empty.");
return 0;
}
QueueNode *p = q->_head->_next;
while (p != q->_tail)
{
p = p->_next;
}
return p->_data;
}
-------------------------------------------------
size_t QueueSize(Queue* q)//计算队列长度
{
assert(q);
if (q->_head == q->_tail)
{
return 0;
}
QueueNode *flag = q->_head->_next;
size_t i = 0;
while (flag != q->_tail)
{
flag = flag->_next;
i++;
}
return i+1 ;
}
-----------------------------------------------
int QueueEmpty(Queue* q)//判断队列是否为空
{
assert(q);
if (q->_head == q->_tail)
{
printf("the queue is empty.");
return 0;
}
return 1;
}