栈的实现
栈的特点是先进后出,只允许在一段插入数据和删除数据,数据插入和数据删除的这一段叫做栈顶,另一端叫栈底。
我们一般用数组的方式来实现栈,因为对于删除尾部的元素来说,在栈不为空的情况下,数组方式可以直接size--来删除,而链表的需要找到尾部的前一个,才能完成删除操作,因此选择数组的方式更为便捷。
比如我们依次插入数据1,2,3,4。栈内的数据如下图所示。
我们要删除数据,也只能一个一个从栈顶的4开始删起走, 删除一次成为了下图这样。
依次类推,要添加要删除,都是在栈顶的位置完成。
一个普通的栈,有以下函数需要实现,下面是头文件Stack.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
typedef int STDataType; //这里数据为整形,可以修改为其他类型
typedef struct Stack
{
STDataType* a; //指针 -指向数组的首地址
int top; //栈顶的位置
int capacity; //容量
}ST;
void STInit(ST* ps); //栈初始化
void STDestroy(ST* ps); //栈销毁
void STPush(ST* ps, STDataType x); //插入
void STPop(ST* ps); //删除
void STPrint(ST* ps); //打印
STDataType STTop(ST* ps); //获取栈顶元素
int STSize(ST* ps); //栈的大小
bool STEmpty(ST* ps); //栈是否为空
下面是栈的具体实现代码 Stack.cpp
#include "Stack.h"
void STInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
void STDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void STPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)*newCapacity);
if (tmp == NULL)
{
perror("malloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void STPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
STDataType STTop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top-1];
}
void STPrint(ST* ps)
{
assert(ps);
cout << "栈底->";
for (int i = 0; i < ps->top; i++)
{
cout << ps->a[i] << " ";
}
cout << "<-栈顶" << endl;
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
最后是栈的测试 Test.cpp
#include"Stack.h"
void test01()
{
ST stack;
STInit(&stack);
STPush(&stack, 2);
STPush(&stack, 3);
STPush(&stack, 1);
STPrint(&stack);
STPop(&stack);
STPop(&stack);
STPop(&stack);
if (STEmpty(&stack))
cout << "为空" << endl;
STPush(&stack, 1);
STPush(&stack, 2);
if (STEmpty(&stack))
cout << "为空" << endl;
STPrint(&stack);
STDestroy(&stack);
}
int main()
{
test01();
}
队列的实现
队列是在一段插入数据,在另一端删除数据,符合先进先出的特性。插入数据的这一段叫队尾,删除数据的这一段叫对头。
队列一般通过链表来实现,对于删除数据来说,在数据不为空的情况下,如果是数组,则是头删,要将后续的数据往前面挪,因此效率比较低,而如果是链表,则直接把head改变了就可以。而插入数据两边都差不多,因此选择了链表的方式。
一样的依次插入1,2,3,4。
删除一次则变成
以此类推。
一个链表队列的实现,有一个重点,要改变头,因此我们要传入二级指针,这里用了一个比较巧妙的方式,,创建了一个结构体,里面有*head,*tail ,size,传入的是结构体指针,来改变结构体里面的head,tail,size。
头文件Queue.h如下
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);
bool QueueEmpty(Queue* pq);
函数文件Queue.cpp如下
#include "Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
pq->size = 0;
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->head == NULL)
pq->head = pq->tail = newnode;
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
pq->size++;
}
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
QNode* next = pq->head->next;
free(pq->head);
if (next == NULL)
pq->tail = NULL;
pq->head = next;
pq->size--;
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head==NULL;
}
测试文件Test.cpp如下
#include"Queue.h"
void Queuetest()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
while (!QueueEmpty(&q))
{
cout << QueueFront(&q)<<" ";
QueuePop(&q);
}
cout << endl;
QueueDestroy(&q);
}
int main()
{
Queuetest();
}