听了国嵌唐老师的课,用C语言实现如下:

数据结构:顺序栈_#include


SeqList.c

SeqList.h
之前博客有,请读者自行查找
SeqStack.c

#include "SeqStack.h"
#include "SeqList.h"

SeqStack* SeqStack_Create(int cpapacity)
{
return SeqList_Create(cpapacity);
}

void SeqStack_Destroy(SeqStack* stack)
{
SeqList_Destroy(stack);
}

void SeqStack_Clear(SeqStack* stack)
{
SeqList_Clear(stack);
}

int SeqStack_Push(SeqStack* stack,void* item)
{
return SeqList_Insert(stack,item,SeqList_Length(stack));
}

void* SeqStack_Pop(SeqStack* stack)
{
return SeqList_Delete(stack,SeqList_Length(stack)-1);
}

void* SeqStack_Top(SeqStack* stack)
{
return SeqList_Get(stack,SeqList_Length(stack)-1);
}

int SeqStack_Size(SeqStack* stack)
{
return SeqList_Length(stack);
}

int SeqStack_Capacity(SeqStack* stack)
{
return SeqList_Capacity(stack);
}

SeqStack.h

#ifndef _SEQSTACK_H_
#define _SEQSTACK_H_

typedef void SeqStack;

SeqStack* SeqStack_Create(int capacity);

void SeqStack_Destroy(SeqStack* stack);

void SeqStack_Clear(SeqStack* stack);

int SeqStack_Push(SeqStack* stack,void* item);

void* SeqStack_Pop(SeqStack* stack);

void* SeqStack_Top(SeqStack* stack);

int SeqStack_Size(SeqStack* stack);

int SeqStack_Capacity(SeqStack* stack);

#endif

main.c

#include <stdio.h>
#include <stdlib.h>
#include "SeqStack.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
SeqStack* stack = SeqStack_Create(20);
int a[10];
int i = 0;

for(i=0;i<10;i++)
{
a[i] = i;

SeqStack_Push(stack,a+i);
}

printf("Top: %d\n",*(int*)SeqStack_Top(stack));

printf("Capacity: %d\n",SeqStack_Capacity(stack));

printf("Length: %d\n",SeqStack_Size(stack));

while(SeqStack_Size(stack)>0)
{
printf("Pop: %d\n",*(int*)SeqStack_Pop(stack));
}

SeqStack_Destroy(stack);

return 0;
}

运行效果:

数据结构:顺序栈_C语言_02