栈的基本用法与实现

/*********************************************/
//stack.h函数的主体
/*********************************************/
#include<stdio.h>
#include<stdlib.h>

#define  TURE   1
#define  OK     1
#define  ERROR  0
//定义函数返回值类型
typedef  int Status;
//定义栈数据类型
typedef  int datetype;
//定义栈结构体类型
typedef struct stack{
	datetype  data;
	struct stack *next;
}Stack,*pStack;
//定义一个指向栈的指针类型
typedef struct sp{
	pStack top;
}Lstack;
//创建一个栈
Status CreateStack(Lstack *s);                                      //声明创建栈的create函数
Status PushStack(Lstack *s,datetype num);			    //声明压入栈的push函数
Status PopStack(Lstack *s,datetype *num);			    //声明出栈函数
Status GetStack(Lstack *s,datetype *num);		            //声明求头指针的下一个元素的值的函数
Status IsEmpty(Lstack *s);					    //声明判栈是否为空的函数
Status PrintStack(Lstack *s);					    //遍历打印栈元素
Status Free(Lstack *s);                                             //释放栈的空间



/****************************************************************/
main.c的函数主体
/****************************************************************/
#include"stack.h"
int main(){
	Lstack s;
	Status temp;
	datetype temp1;

	temp = CreateStack(&s);                                           //create the stack
	if(temp == 1) printf("the stack create successful\n");

	IsEmpty(&s);

	printf("\nplease input the numb you want to push to the stack:");//push the numb into stack
	scanf("%d",&temp);
	PushStack(&s,temp);

	printf("\nplease input the numb you want to push to the stack:");//push the numb into stack
	scanf("%d",&temp);
	PushStack(&s,temp);

	GetStack(&s,&temp1);                                   //get the topdate of the stack
	printf("\nafter getstack temp1=%d",temp1);

	PopStack(&s,&temp1);                                   //pop the numb
	printf("\npopstack the numb:%d",temp1);
	PopStack(&s,&temp1);
	printf("\npopstack the numb:%d",temp1);

	PushStack(&s,6);                                       //printstack
	PushStack(&s,7);
	PushStack(&s,8);
	PrintStack(&s);

	Free(&s);
	return 0;
}

/*********************************************************************************/
stack.c的函数主体
/*********************************************************************************/
#include"stack.h"
Status CreateStack(Lstack *s)
{
	pStack p;
	p = (pStack)malloc(sizeof(Stack));
	p->next = NULL;
	s->top = p;
	if(s->top == NULL)
	{
		printf("stack create failure\n");

		exit(ERROR);
	}
	else
	return TURE;
}
Status IsEmpty(Lstack *s)
{
	if(s->top->next = NULL)
		printf("the stack is empty\n");
	return OK;
}
Status PushStack(Lstack *s,datetype num)
{
	pStack p= (pStack)malloc(sizeof(Stack));
	p->next = NULL;
	if(!p)
	{
		printf("push failure\n");
		exit(ERROR);
	}

	s->top->data = num;
	p->next = s->top;
	s->top = p;
	return OK;
}
Status GetStack(Lstack *s,datetype *num)
{
	if(s->top->next==NULL)
		exit(ERROR);
	*num =s->top->next->data;
	return OK;
}

Status PopStack(Lstack *s,datetype *num)
{
	pStack p= s->top;
	*num = p->next->data;
	s->top = p->next;
	free(p);
	return TURE;
}
Status PrintStack(Lstack *s)
{
	pStack p = s->top;
	while(p->next)
		{
		printf("here");
		printf("printstack the numb is:%d\n",p->next->data);
		p = p->next;
		}
	return TURE;
}
Status Free(Lstack *s)
{
	pStack p = s->top;
	pStack r;
	while(p->next)
	{
		r = p->next;
		free(p);
		p = r;
	}
	printf("free all the space of the stack\n");
	return OK;
}



以上是 栈的一些基本用法的实现
栈是一种数据存储方式,先进后出。
基本操作包括:空栈的建立 createstack
压入数据 pushstack
出栈 popstack
打印栈顶元素 getstack
遍历栈 printstack

作业太多了 花了一晚上才写了这么一个玩意,遗留下来很多东西没来得及好好整理,希望能在周末能整理好思路。学习很紧张啊,数据结构是我们没学过(c高级)的内容,没学过还好些,重复的不喜欢。

栈,单链表,循环链表,双向循环链表等这些基本的数据储存结构得得好好把握,用的非常的频繁,听我们老师说链表就像用 int 定义一个数据一样的频繁,所以要好好练习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值