栈:基本介绍与实现(数组+链表)

栈(stack)又名堆栈,它是一种运算受限的线性表。

栈有这样一种限制:插入和删除元素只能从一端操作,这一端被称为栈顶,相对地,把另一端称为栈底。

 栈有两种基本操作:

1.插入操作:push;

2.删除操作:pop。

还有其他典型的操作:

3.返回栈顶:TOP;

4.判断栈是否为空:ISEMPTY;

Ⅰ数组实现

1.

#define MAX_SIZE 101
int a[MAX_SIZE];
int top=-1;

为了方便先定义全局数组和栈顶top:

2.push(x)函数:

void push(int x)
{
	if(top==MAX_SIZE-1)
	{
		printf("the stack overflow!");
		return ;
	}
	top++;
	a[top]=x;
 } 

如果栈溢出就输出提示语句然后返回,没有溢出则使top自加然后给数组中的top元素赋值x。

3.pop()函数:

void pop()
 {
 	if(top==-1)
 	{
 		printf("no element to pop!");
 		return ;
	 }
 	top--;
 }

如果栈空就输出提示语句然后返回,非空则只使top自减即可,因为下一次调用push函数时赋的值会覆盖需要删除的值。

4.TOP()函数:

int TOP()
 {
 	
 	return a[top];
 }

没啥解释的,直接返回值。

5.ISEMPTY()函数:

int ISEMPTY()
 {
 	if(top==-1)
 	printf("tha stack is empty");
 	else 
 	printf("the stack is not empty");
 }

这个也没啥解释的。

化零为整:

#include<stdio.h>
#define MAX_SIZE 101
int a[MAX_SIZE];
int top=-1;
void push(int x)
{
	if(top==MAX_SIZE-1)
	{
		printf("the stack overflow!");
		return ;
	}
	top++;
	a[top]=x;
 } 
 void pop()
 {
 	if(top==-1)
 	{
 		printf("no element to pop!");
 		return ;
	 }
 	top--;
 }
int TOP()
 {
 	printf("the stack top_element is:");
 	return a[top];
 }
 int ISEMPTY()
 {
 	if(top==-1)
 	printf("tha stack is empty");
 	else 
 	printf("the stack is not empty");
 }
 void print()
 {
 	int i;
 	printf("the stack is:");
 	for(i=0;i<=top;i++)
 	{
 		printf("%d ",a[i]);
	 }
	 printf("\n");
 }
 int main()
 {
 	push(1);print();
 	push(2);print();
 	push(3);print();
 	push(4);print();
 	pop();print();
 	printf("%d\n",TOP());
 	ISEMPTY();
 	return 0;
 }

瞅一眼结果:

 Ⅱ链表实现:

PS:因为链表从尾部插入删除的时间复杂度是O(n),不符合栈的特点(O(1)),

所以我们选择从链表头部插入删除。

1.

typedef struct node{
	int data;
	struct node* next;
}sn;
sn* top=NULL;

2.push(x):

void push(int x)
{
	sn* temp=(sn*)malloc(sizeof(sn));
	temp->data=x;   
	temp->next=top;
	top=temp;
}

3.pop():

void pop()
{
	sn* p=top;
	if(top==NULL)return ;
	top=p->next;
	free(p);
}

4.TOP():

int TOP()
{
	sn* temp=top;
	printf("the stack top_element is:");
	return temp->data;
}

5.ISEMPTY():


int ISEMPTY()
{
	if(top->next==NULL)
	return 1;
	else return 0;
}

化零为整:

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
	int data;
	struct node* next;
}sn;
sn* top=NULL;
void push(int x)
{
	sn* temp=(sn*)malloc(sizeof(sn));
	temp->data=x;
	temp->next=top;
	top=temp;
}
void pop()
{
	sn* p=top;
	if(top==NULL)return ;
	top=p->next;
	free(p);
}
void print()
{
	sn* p=top;
	printf("the stack is:");
	while(p!=NULL)
	{
		printf("%d ",p->data );
		p=p->next;
	}
	printf("\n");
}
int TOP()
{
	sn* temp=top;
	printf("the stack top_element is:");
	return temp->data;
}
int ISEMPTY()
{
	if(top->next==NULL)
	return 1;
	else return 0;
}
int main()
{
	push(2);print();
	push(4);print();
	push(6);print();
	pop();print();
	printf("%d\n",TOP());
	if(ISEMPTY())
	printf("the stack is empty");
	else 
	printf("the stack is not empty"); 
}

瞅一眼结果:

 从某种程度上来说:使用链表实现栈的方式更优雅~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxx_xiyuyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值