C language array stack implementation template
- 2020-06-12 10:11:40
- OfStack
This article is an example of C language array stack template for you to share the specific code, for your reference, specific content is as follows
SeqStack.h
#pragma once
#define MAX_SIZE 1024
typedef struct SEQSTACK
{
void* data[MAX_SIZE];
int size;
}SeqStack;
SeqStack* Init_SeqStack(); // Initialize the stack
void Push_SeqStack(SeqStack* stack, void* data); // Into the stack
void* Top_SeqStack(SeqStack* stack); // Returns the top element of the stack
void Pop_SeqStack(SeqStack* stack); // Out of the stack
int IsEmpty(SeqStack* stack); // Determine if it is empty
int Size_SeqStack(SeqStack* stack); // Returns the number of elements in the stack
void Clear_SeqStack(SeqStack* stack); // Empty stack
void FreeSpace_SeqStack(SeqStack* stack); // Destruction of the stack
SeqStack. cpp function implementation
#include "SeqStack.h"
#include <stdio.h>
#include <stdlib.h>
SeqStack* Init_SeqStack()
{
SeqStack* stack = (SeqStack*)malloc(sizeof(SeqStack));
for (int i = 0; i < MAX_SIZE; i++)
{
stack->data[i] = NULL;
}
stack->size = 0;
return stack;
}
void Push_SeqStack(SeqStack* stack, void* data)
{
if (stack == NULL)
return;
if (stack->size == MAX_SIZE) // The stack is full
return;
if (data == NULL)
return;
stack->data[stack->size] = data;
stack->size++;
}
void* Top_SeqStack(SeqStack* stack) // Returns the top element of the stack , Don't pop up
{
if (stack == NULL)
return NULL;
if (stack->size == 0)
return NULL;
return stack->data[stack->size - 1];
}
void Pop_SeqStack(SeqStack* stack) // Out of the stack
{
if (stack == NULL)
return;
if (stack->size == 0)
return;
stack->data[stack->size - 1] = NULL;
stack->size--;
}
int IsEmpty(SeqStack* stack) // Determine if it is empty
{
if (stack == NULL)
return -1;
if (stack->size == 0)
return 1;
return 0;
}
int Size_SeqStack(SeqStack* stack)
{
if (stack == NULL)
return -1;
return stack->size;
}
void Clear_SeqStack(SeqStack* stack)
{
if (stack == NULL)
return;
for (int i = 0; i < stack->size; i++)
{
stack->data[i] = NULL;
}
stack->size = 0;
}
void FreeSpace_SeqStack(SeqStack* stack)
{
if (stack == NULL)
return;
free(stack);
}
Array stack tester
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SeqStack.h"
typedef struct PERSON
{
char name[64];
int age;
}Person;
int main()
{
SeqStack* stack = Init_SeqStack(); // Create a stack
Person p1, p2, p3, p4, p5;
strcpy(p1.name, "aaa");
strcpy(p2.name, "bbb");
strcpy(p3.name, "ccc");
strcpy(p4.name, "ddd");
strcpy(p5.name, "eee");
p1.age = 10;
p2.age = 20;
p3.age = 30;
p4.age = 40;
p5.age = 50;
Push_SeqStack(stack, &p1); // Into the stack
Push_SeqStack(stack, &p2);
Push_SeqStack(stack, &p3);
Push_SeqStack(stack, &p4);
Push_SeqStack(stack, &p5);
while (Size_SeqStack(stack) > 0)
{
Person* person = (Person*)Top_SeqStack(stack);
printf("Name:%s Age:%d\n", person->name, person->age);
Pop_SeqStack(stack);
}
// Free memory
FreeSpace_SeqStack(stack);
system("pause");
}