头文件
#ifndef SEQSTACK
#define SEQSTACK
#define MAX_SIZE 1024
typedef struct SEQSTACK
{
void* data[MAX_SIZE];
int size;
}SeqStack;
typedef void (PRINTDATA)(void data);
void PrintStack(void* data, PRINTDATA print);
//初始化栈
SeqStack* InitSeqStack();
//入栈
void PushSeqStack(SeqStack* stack, void* data);
//返回栈顶元素
void* TopSeqStack(SeqStack* stack);
//出栈
void PopSeqStack(SeqStack* stack);
//判断栈是否为空
bool IsEmpty(SeqStack* stack);
//返回栈元素个数
int GetCount(SeqStack* stack);
//清空栈
void clearSeqStack(SeqStack* stack);
//销毁栈
void FreeSeqStack(SeqStack* stack);
#endif
cpp
#include "SeqStack.h"
#include
#include
using namespace std;
void PrintStack(void* data, PRINTDATA print)
{
if (data == NULL)
return;
print(data);
}
//初始化栈
SeqStack* InitSeqStack()
{
SeqStack* stack = (SeqStack*)malloc(sizeof(SeqStack));
for (int i = 0; i data[i]!=NULL)
{
stack->data[i] = nullptr;
}
}
stack->size = 0;
return stack;
}
//入栈
void PushSeqStack(SeqStack* stack, void* data)
{
if (data == NULL || stack == NULL||stack->size==MAX服务器托管网_SIZE )
{
return;
}
stack->data[stack->size] = data;
stack->size++;
}
//返回栈顶元素
void* TopSeqStack(SeqStack* stack)
{
if (stack->size == 0)
{
return NULL;
}
if (stack == NULL)
{
return NULL;
}
return stack->data[stack->size - 1];
}
//出栈
void PopSeqStack(SeqStack* stack)
{
if (stack->size == 0)
{
return ;
}
if (stack == NULL)
{
return ;
}
stack->data[stack->size - 1] = NULL;//可省略.
stack->size--;
}
//判断栈是否为空
bool IsEmpty(SeqStack* stack)
{
if (stack->size > 0)
{
return true;
}
else return false;
}
//返回栈元素个数
int GetCount(SeqStack* stack)
{
return stack->size;
}
//清空
void clearSeqStack(SeqStack* stack)
{
if (stack == NULL)
{
return;
}
for (int i = 0; i size; i++)
{
stack->data[i] = NULL;
}
stack->size = 0;
}
//销毁栈
void FreeSeqStack(SeqStack* stack)
{
if (stack == NULL)
{
return;
}
free( stack);
}
typedef struct PERSON
{
char name[64];
int age;
}Person;
void myprint(void* data)
{
if (data != NULL)服务器托管网
{
Person* p = (Person*)data;
cout name age;
}
else return;
}
int main()
{
//初始化栈
SeqStack* stack = InitSeqStack();
Person p1 = { "aaa",10 };
Person p2 = { "bbb",11 };
Person p3 = { "ccc",12 };
Person p4 = { "ddd",13 };
//入栈
PushSeqStack(stack, &p1);
PushSeqStack(stack, &p2);
PushSeqStack(stack, &p3);
PushSeqStack(stack, &p4);
PrintStack(TopSeqStack(stack), myprint);
//输出
while (stack->size > 0)
{
Person* person = (Person*)TopSeqStack(stack);
cout name age size--;
PopSeqStack(stack);
}
FreeSeqStack(stack);
cout
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: Thread常见方法:interrupt 方法详解
打断 sleep,wait,join 的线程 这几个方法都会让线程进入阻塞状态 打断 sleep 的线程, 会清空打断状态,以 sleep 为例 private static void test1() throws InterruptedException {…