栈的多种C语言实现

欢迎访问我的新博客:http://www.milkcu.com/blog/

原文地址:http://www.milkcu.com/blog/archives/1370916840.html

标题:栈的多种C语言实现

内容:栈是一种后进先出(LIFO)的数据结构,C语言中可以使用数组、全局变量、指针传参、引用传参等方法实现。

作者:MilkCu

概念

栈的定义

我们可以使用下面的结构体来定义栈:

typedef struct stack {
	int top;
	int key[M];
} stack;

栈的属性

以栈s为例讨论。

s.top指向最新插入的元素。
当栈中包含的元素为s.key[1..s.top],其中s.key[1]是栈底元素,s.key[s.top]是栈顶元素。

栈的操作

压入(push):将数据放在栈顶;

弹出(pop):返回弹出值,并删除元素。

栈的状态

s.top = 0时,栈中不包含任何元素,即栈是空的。

实现

普通数组实现

最简单的实现方法,不会涉及到结构体的参数传递问题。

使用s[0]表示s.top。

# include <stdio.h>
# define M 100
int stackEmpty(int s[]);
void push(int s[], int x);
int pop(int s[]);
int main(void)
{
	int s[M];
	s[0] = 0;
	printf("stackEmpty - %d\n", stackEmpty(s));
	push(s, 2);
	push(s, 5);
	printf("stackEmpty - %d\n", stackEmpty(s));
	printf("pop - %d\n", pop(s));
	return 0;
}
int stackEmpty(int s[])
{
	if(s[0] == 0) {
		return 1;
	} else {
		return 0;
	}
}
void push(int s[], int x)
{
	s[0]++;
	s[s[0]] = x;
}
int pop(int s[])
{
	if(s[0] == 0) {
		return -1;
	} else {
		return s[s[0]--];
	}
}

指针传参实现

传递参数的时候要用指针,否则不能改变参数的值!

# include <stdio.h>
# define M 100
typedef struct stack {
	int top;
	int key[M];
} stack;
int stackEmpty(stack s);
void push(stack * s, int x);
int pop(stack * s);
int main(void)
{
	stack s;
	s.top = 0;
	/* 测试代码 */
	push(&s, 2);
	push(&s, 3);
	printf("pop - %d\n", pop(&s));
	printf("stackEmpty - %d", stackEmpty(s));
}
int stackEmpty(stack s)
{
	if(s.top == 0) {
		return 1;
	} else {
		return 0;
	}
}
void push(stack * sp, int x)
{
	sp -> top += 1;
	sp -> key[sp -> top] = x;
}
int pop(stack * sp)
{
	if(sp -> top == 0) {
		return -1;
	} else {
		sp -> top--;
		return sp -> key[sp -> top + 1];
	}
}

引用传参实现

C++中的引用某种程度上,可以用C的指针替代。

# include <stdio.h>
# define M 100
typedef struct stack {
	int top;
	int key[M];
} stack;
int stackEmpty(stack s);
void push(stack & s, int x);
int pop(stack & s);
int main(void)
{
	stack s;
	s.top = 0;
	/* 测试代码 */
	push(s, 2);
	push(s, 3);
	printf("pop - %d\n", pop(s));
	printf("stackEmpty - %d", stackEmpty(s));
}
int stackEmpty(stack s)
{
	if(s.top == 0) {
		return 1;
	} else {
		return 0;
	}
}
void push(stack & s, int x)
{
	s.top += 1;
	s.key[s.top] = x;
}
int pop(stack & s)
{
	if(s.top == 0) {
		return -1;
	} else {
		s.top--;
		return s.key[s.top + 1];
	}
}

全局变量实现

注意:全局变量传递参数的时候就不要传递了。

全局变量可以一直保存变量的值,不会因某个函数的结束而销毁,并且不用作为参数传递给函数。

# include <stdio.h>
# define M 100
typedef struct stack {
	int top;
	int key[M];
} stack;
stack s;
int stackEmpty(void);
void push(int x);
int pop(void);
int main(void)
{
	s.top = 0;
	/* 测试代码 */
	push(2);
	push(3);
	printf("pop - %d\n", pop());
	printf("stackEmpty - %d", stackEmpty());
}
int stackEmpty(void)
{
	if(s.top == 0) {
		return 1;
	} else {
		return 0;
	}
}
void push(int x)
{
	s.top += 1;
	s.key[s.top] = x;
}
int pop(void)
{
	if(s.top == 0) {
		return -1;
	} else {
		s.top--;
		return s.key[s.top + 1];
	}
}

结构体数组实现

引入一个变量,stop相当于s.top,让代码逻辑简单了不少。

# include <stdio.h>
# define M 100
int s[M];
int stop;
//stop = 0;    //外部变量不能赋值吗?? 
int stackEmpty(void);
void push(int x);
int pop();
int main(void)
{
	stop = 0; 
	printf("stackEmpty - %d\n", stackEmpty());
	push(3);
	printf("stackEmpty - %d\n", stackEmpty());
	push(5);
	//printf("pop - %d - %d\n", pop(), pop());    //这是一个未定义行为
	printf("pop - %d\n", pop());
	printf("pop - %d\n", pop());
	printf("stackEmpty - %d\n", stackEmpty());
	return 0;
}
int stackEmpty(void)
{
	if(stop == 0) {
		return 1;
	} else {
		return 0;
	}
}
void push(int x)
{
	s[++stop] = x;
}
int pop(void)
{
	return s[stop--];
}

后记

上面程序代码中,main函数为测试函数,且均省略了对上溢和下溢的检验!

虽然方法较多,但核心思想都是相同的。

本文根据我的文章《栈的C语言实现》及硬盘上凌乱的代码总结而来。原来的那篇直接从Word粘贴的,面目全非。

(全文完)

原文链接: https://www.cnblogs.com/milkcu/archive/2013/06/11/3808885.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    栈的多种C语言实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/91905

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午1:24
下一篇 2023年2月10日 上午1:24

相关推荐