Stack interface implementation (with attached solution and source code)
Article directory
foreword
This article mainly introduces the implementation of interfaces such as adding, deleting, checking and modifying in the stack , and the total source code is attached at the end ! Here we use the array stack method.
1. Define the structure
2. Interface implementation (attached solution + source code)
There are a total of 7 interfaces here, and I will explain them to you one by one ( illustration + source code )
1. Initialize the stack
The initialization here is the same as the sequence table, for details, please refer to the sequence table !
The code is as follows (example):
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
2. Destroy the stack
You can also directly refer to the sequence table for destroying the stack , so I won’t introduce too much here!
The code is as follows (example):
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
3. Push into the stack
Here we need to consider the issue of capacity expansion :
The code is as follows (example):
void StackPush(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, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
4. Determine whether the stack is empty
If ps->top=0 , the stack is empty! You can also use the if statement here, I am directly returning true of false here
The code is as follows (example):
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
5. Pop out
Note here: You need to use StackEmpty to judge whether the stack is empty, because there is no need to pop the stack after it is empty!
The code is as follows (example):
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
--ps->top;
}
6. Get the top element of the stack
Also note here: you need to use StackEmpty to judge whether the stack is empty, because you can't get the top element of the stack after it is empty!
I used 0 for top when I initialized, so when getting the top data of the stack, directly ps->a[ps->top - 1]
The code is as follows (example):
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
7. Get the number of elements in the stack
Just return to top directly, so I won’t introduce too much here!
The code is as follows (example):
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
3. Source code display
(1) test.c (test + main function)
The code is as follows (example):
//#include <stdio.h>
//
//int f(int n)
//{
// return n == 1 ? 1 : f(n - 1) + n;
//}
//
//int main()
//{
// printf("%d\n", f(10000));
//
// return 0;
//}
#include <stdio.h>
#include "Stack.h"
#include "Queue.h"
// 解耦 -- 低耦合 高内聚
// 数据结构建议不要直接访问结构数据,一定要通过函数接口访问
void TestStack()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
printf("%d ", StackTop(&st));
StackPop(&st);
printf("%d ", StackTop(&st));
StackPop(&st);
StackPush(&st, 4);
StackPush(&st, 5);
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("\n");
}
void TestQueue()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
printf("%d ", QueueFront(&q));
QueuePop(&q);
printf("%d ", QueueFront(&q));
QueuePop(&q);
QueuePush(&q, 4);
QueuePush(&q, 4);
QueuePush(&q, 4);
while (!QueueEmpty(&q))
{
printf("%d ", QueueFront(&q));
QueuePop(&q);
}
printf("\n");
QueueDestroy(&q);
}
int main()
{
//TestStack();
TestQueue();
return 0;
}
(2) Stack.h (declaration of interface functions)
The code is as follows (example):
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
//#define N 100
//typedef int STDataType;
//struct Stack
//{
// STDataType a[N];
// int top;
//};
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void StackInit(ST* ps);//初始化栈
void StackDestroy(ST* ps);//销毁
void StackPush(ST* ps, STDataType x);//入栈
void StackPop(ST* ps);//出栈
STDataType StackTop(ST* ps);//获取栈顶元素
bool StackEmpty(ST* ps);//检测栈是否为空
int StackSize(ST* ps);//获取栈中有效元素个数
(3) Stack.c (implementation of interface functions)
The code is as follows (example):
#include "Stack.h"
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackPush(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, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
--ps->top;
}
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
Summarize
The above is what I want to talk about today. This article introduces the interface implementation of the stack (with attached solution and source code)!
If my blog is helpful to you, remember to support it three times, thank you for your support!