栈
Stack.h
#pragma once
#include <malloc.h>
#include <assert.h>
#include<stdio.h>
#include <stdlib.h>
#pragma once
#ifndef __STACK_H__
#define __STACK_H__
//#pragma once
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
#define START_SIZE 3
#define ADD_SIZE 2
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
} Stack;
void StackInit(Stack* ps);
void StackDestory(Stack* ps);
void StackPush(Stack* ps, STDataType x);
//STDataType Stackprint(Stack *ps);
void StackPop(Stack* ps);
STDataType StackTop(Stack* ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);
void TestStack();
#endif // !__STACK_H__
Stack.c
#include"Stack.h"
void StackInit(Stack* ps)
{
assert(ps);
ps->_a = (STDataType*)malloc(sizeof(STDataType)*START_SIZE);
ps->_capacity = START_SIZE;
ps->_top = 0;
}
void StackDestory(Stack* ps)
{
assert(ps);
if (ps->_a)
{
free(ps->_a);
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}
}
void StackPush(Stack* ps,STDataType x)
{
assert(ps);
if (ps->_capacity == ps->_top)
{
ps->_a = (STDataType*)realloc(ps->_a, sizeof(STDataType)*(ps->_capacity + ADD_SIZE));
ps->_capacity += ADD_SIZE;
assert(ps->_a);
ps->_capacity += ADD_SIZE;
}
ps->_a[ps->_top++] = x;
ps->_top++;
}
void StackPop(Stack* ps)
{
assert(ps->_a);
assert(ps->_top > 0);
ps->_top--;
}
STDataType StackTop(Stack* ps)
{
assert(ps->_a);
assert(ps->_top>0);
return ps->_a[ps->_top - 1];
}
//0表示空、1表示非空
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top == 0 ? 0 : 1;
}
int StackSize(Stack* ps)
{
assert(ps);
return ps->_top;
}
TestStack.c
#include"Stack.h"
void TestStack()
{
Stack s;
StackInit(&s);
StackPush(&s, 11);
StackPush(&s, 22);
StackPush(&s, 33);
StackPush(&s, 44);
while (StackEmpty(&s))
{
printf("%d ", StackTop(&s));
StackPop(&s);
}
StackDestory(&s);
}
int main()
{
TestStack();
system("pause");
return 0;
}
队列
Queue.h
#pragma once
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <limits.h>
//顺序结构和链式结构都能实现
//链式结构更好 便于删除
//链式结构需要一个节点 结构体
typedef int QUDataType;
typedef struct QueueNode
{
struct QueueNode* _next; //节点域
QUDataType _data; //数据域
}QueueNode;
typedef struct Queue
{
QueueNode* _front; // 队头
QueueNode* _back; // 队尾
}Queue;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
QueueNode* BuyQueueNode(QUDataType x);
void QueuePush(Queue* pq, QUDataType x);
void QueuePop(Queue* pq);
QUDataType QueueFront(Queue* pq);
QUDataType QueueBack(Queue* pq);
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
void TestQueue();
Queue.c
#include "Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->_front = NULL;
pq->_back = NULL;
}
void QueueDestory(Queue* pq)
{
QueueNode *cur = NULL;
QUDataType *del = NULL;
assert(pq);
if (pq->_front == NULL)
return;
del = pq->_front;
cur = pq->_front->_next;
while (cur) //cur->_back!=NULL 最后一个节点没有释放
{
free(del); //节点释放,内存还在,空间还在,空间的值被置位随机值
del = cur;
cur = cur->_next;
}
pq->_front = pq->_back = NULL;
}
QUDataType* BuyQUDataType(QUDataType x)
{
QueueNode *newNode = (QUDataType*)malloc(sizeof(QUDataType));
assert(newNode);
newNode->_data = x;
newNode->_next = NULL;
return newNode;
}
void QueuePush(Queue* pq, QUDataType x)
{
QUDataType *newNode = NULL;
assert(pq);
newNode = BuyQUDataType(x);
if (pq->_back == NULL)
{
pq->_front = newNode;
pq->_back = newNode;
}
else
{
pq->_back->_next = pq->_back;
}
}
void QueuePop(Queue* pq)
{
QUDataType *cur = NULL;
assert(pq);
if (pq->_front == NULL)
return;
cur = pq->_front->_next; //先将下一个节点保存,在free front,防止内存泄漏
free(pq->_front);
pq->_front = cur;
if (cur == NULL)
{
pq->_back = NULL;
}
}
//return INT_MAX表示队列为空
QUDataType QueueFront(Queue* pq)
{
assert(pq);
return pq->_front->_data;
}
QUDataType QueueBack(Queue* pq)
{
return pq->_back->_data;
}
//空返回0、非空返回1
int QueueEmpty(Queue* pq)
{
assert(pq);
return pq->_front == NULL ? 0 : 1;
}
int QueueSize(Queue* pq)
{
int count = 0;
QueueNode *cur = pq->_front;
while (cur)
{
count++;
cur = cur->_next;
}
return count;
}
TestQueue.c
#include"Queue.h"
void TestQueue()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
while (QueueEmpty(&q))
{
printf("%d ", QueueFront(&q));
QueuePop(&q);
}
QueueDestory(&q);
}
int main()
{
TestQueue();
system("pause");
return 0;
}