0
点赞
收藏
分享

微信扫一扫

顺序栈C语言实现

青青子衿谈育儿 2022-04-30 阅读 69
c语言c++
#include "stdio.h"    
#include "stdlib.h"   

#include "math.h"  
#include "time.h"

#define maxlen 20
typedef struct
{
	int data[maxlen];
	int top;
}stacklist;

int push(stacklist* L, int value)
{
	if (L->top == maxlen - 1)
	{
		printf("栈已满\n");
		return 0;
	}
	L->top++;
	L->data[L->top] = value;
	return 1;
}

int pop(stacklist* L)
{
	if (L->top == -1) //栈顶为-1表示空栈
	{
		printf("栈为空\n");
		return 0;
	}
	L->top--;
	return 1;
}

int initlist(stacklist* L)
{
	L->top = -1;
	return 1;
}

int seelist(stacklist L)
{
	int i, j;
	j = L.top;
	if (j == -1)
	{
		printf("栈为空\n");
		return 0;
	}
	for (i = 0; i <= j; i++)
	{
		printf("%d ", L.data[i]);

	}
	printf("\n");
	return 1;
}

int gettop(stacklist L)
{
	int i;
	i = L.top;
	printf("top:%d\n", i);
	return i;
}

int main()
{
	stacklist L;
	int i, j;
	initlist(&L);
	for (i = 0; i < 3; i++)
	{
		push(&L, i);
	}
	seelist(L);
	gettop(L);

	pop(&L);
	seelist(L);
	pop(&L);
	seelist(L);
	pop(&L);
	seelist(L);
	pop(&L);

}
举报

相关推荐

0 条评论