C语言实现栈表的初始化、出入栈、销毁、清空等基本操作

C语言实现栈表的初始化、出入栈、销毁、清空等基本操作

引言

简单记录一下新学知识,直接上代码。

代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 3
typedef int Elemtype;

typedef struct stack
{
    
    
	Elemtype *base;
	Elemtype *top;
	int stacksize;
}Sqstack;

//=======功能函数==========//

void Init_stack(Sqstack *q);
void Push_stack(Sqstack *q);
void Pop_stack(Sqstack *q);
void Watch_stack(Sqstack *q);
void Out_stack(Sqstack *q);
void Clear_stack(Sqstack *q);
void Destory_stack(Sqstack *q);
int Len_stack(Sqstack *q);

void main()
{
    
    
	int i;
	Sqstack s;
	int k;
	printf("		《 Welcome to use the founction 》\n");
	printf("	    The founction can offer following service\n");
	printf("		*******Please first Init_stack*******\n\n");
	printf("		1、Initstack     ->初始化操作\n");
	printf("		2、Insertstack   ->入栈操作\n");
	printf("		3、Deletestack   ->出栈操作\n");
	printf("		4、Watchstack    ->查看栈顶元素操作\n");
	printf("		5、Clearstack    ->清空元素操作\n");
	printf("		6、Destorystack  ->销毁操作\n");
	printf("		7、Lenstack      ->查看长度操作\n");
	printf("		0、Exitstack     ->退出操作\n\n\n");
	printf("        Please choose following digital:1、2、3、4、5、6、7、0\n");
	printf("               Finish your idea!!!!!!\n");
	do
	{
    
    
		printf("\n\nPlease the digital --- Finish your idea:");
		scanf("%d",&k);
		switch(k)
		{
    
    
		case 1:
			{
    
    
				Init_stack(&s);
			}break;
		case 2:
			{
    
    

				printf("<Please enter the stack elements in order>\n");

				Push_stack(&s);
				Out_stack(&s);
			}break;
		case 3:
			{
    
    
				Pop_stack(&s);
				printf("======================================\n");
				Out_stack(&s);
			}break;
		case 4:
			{
    
    
				Watch_stack(&s);
			}break;
		case 5:
			{
    
    
				Clear_stack(&s);
				if(s.top == s.base)
					printf("The stack has been Cleared!!!!\n");
				else
					printf("Clear Fail!!!!!!!!!!!!!\n");
			}break;
		case 6:
			{
    
    
				Destory_stack(&s);
				if(s.base)
					printf("Destory fail!!!!!!!!!!!!\n");
				else
					printf("Destory successfully!!!!!!!!\n");
			}break;
		case 7:
			{
    
    
				printf("The lenth of the stack is :%d",Len_stack(&s));
			}break;
		}
	}while(k!=0);
}
//=========初始化函数=========//
void Init_stack(Sqstack *q)
{
    
    
	q->base = (Elemtype*)malloc(MAX * sizeof(Elemtype));
	if(!q->base)
		printf("Apply Fail!!!!!!!!!!\n");
	else
		printf("Apply Succeed!!!!!!!!!\n");
	q->top = q->base;
	q->stacksize = MAX;
}
//=========入栈操作函数==========//
void Push_stack(Sqstack *q)
{
    
    
	Elemtype e;
	int t;
	int i;
	printf("Please Input the amount of stack element that you want to create:");
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
    
    
		printf("Please Input the element of enter stack:");
		scanf("%d",&e);
		if(q->top - q->base >= q->stacksize)
		{
    
    
			//动态增容
			q->base = (Elemtype*)realloc(q->base,(q->stacksize+MAX)*sizeof(Elemtype));
			if(!q->base)
				printf("Apply Fail!!!!!\n");
			q->top = q->base + q->stacksize;
			q->stacksize = q->stacksize+MAX;

			*(q->top) = e;
			q->top++;
		}
		else
		{
    
    
			*(q->top) = e;
			q->top++;
		}
	}
	
}
//==========出栈操作函数=========//
void Pop_stack(Sqstack *q)
{
    
    
	if(--q->top!=q->base)
	{
    
    
		printf("The stack element of Output:%d\n",*(q->top));
	}
}
//==========查看栈顶元素操作函数=========//
void Watch_stack(Sqstack *q)
{
    
    
	if(--q->top != q->base)
		printf("The top stack element:%d\n",*(q->top));
}
//===========输出函数==========//
void Out_stack(Sqstack *q)
{
    
    
	Elemtype *x;
	x = q->top;
	printf("The order of output stack:");
	while(--x >= q->base)
	{
    
    
		printf("%d  ",*(x));
		//q->top--;
	}
}
//============清空操作函数==========//
void Clear_stack(Sqstack *q)
{
    
    
	q->top = q->base;
}
//============摧毁操作函数==========//
void Destory_stack(Sqstack *q)
{
    
    
	if(q->base)
		free( q->base );
	q->base = q->top = NULL;
	q->stacksize = 0;
}
//==========查看长度操作函数==========//
int Len_stack(Sqstack *q)
{
    
    
	return (q->top - q->base);
}

运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/MZYYZT/article/details/113747649