ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

utstack详解

2022-07-24 15:34:47  阅读:131  来源: 互联网

标签:el head utstack next 详解 define STACK size


源码

#define STACK_TOP(head) (head)

#define STACK_EMPTY(head) (!(head))

#define STACK_PUSH(head,add)                                         \
    STACK_PUSH2(head,add,next)

#define STACK_PUSH2(head,add,next)                                   \
do {                                                                 \
  (add)->next = (head);                                              \
  (head) = (add);                                                    \
} while (0)

#define STACK_POP(head,result)                                       \
    STACK_POP2(head,result,next)

#define STACK_POP2(head,result,next)                                 \
do {                                                                 \
  (result) = (head);                                                 \
  (head) = (head)->next;                                             \
} while (0)

#define STACK_COUNT(head,el,counter)                                 \
    STACK_COUNT2(head,el,counter,next)                               \

#define STACK_COUNT2(head,el,counter,next)                           \
do {                                                                 \
  (counter) = 0;                                                     \
  for ((el) = (head); el; (el) = (el)->next) { ++(counter); }        \
} while (0)

#endif /* UTSTACK_H */

utstack的实现特别短,也比较简单,基本思路就是一个单向链表,head就是栈顶,push就把新元素指向head,pop就是把下一个元素当做head并且返回当前元素。
这里唯一需要注意的是,声明的结构体里需要有一个next指针,而且名字必须叫next;

库函数

STACK_PUSH(head,add);
STACK_POP(head,elt);
STACK_TOP(head);
STACK_EMPTY(head);
STACK_COUNT(head,tmp,count);

STACK_PUSH: 压栈
STACK_POP: 出栈并返回,如果head为NULL就发生错误
STACK_TOP: 获取栈顶元素(其实就是head)
STACK_EMPTY: 是否为空,空返回1,否则返回0
STACK_COUNT: 返回栈大小,复杂度O(N),tmp是一个空的元素指针。这个有点鸡肋,还不如自己实现。

正文

typedef struct Item
{
	int a;
	double b;
	struct Item *next;
} Item;
Item *head = NULL;
static int stack_size;

void Push(const int a, const double b)
{
	Item *el = malloc(sizeof(Item));
	el->a = a;
	el->b = b;
	STACK_PUSH(head, el);
	stack_size++;
}

void Pop()
{
	Item *el = NULL;
	if (head)
		STACK_POP(head, el);
	if (el)
	{
		free(el);
		stack_size--;
	}
}

int main()
{
	Push(111, 1.1);
	Push(11111, 11.11);
	Push(1111111, 111.111);
	printf("IsEmpty: %d\n", STACK_EMPTY(head));
	printf("top: %d, %.5f    size: %d\n", head->a, head->b, stack_size);
	Pop();
	printf("top: %d, %.5f    size: %d\n", head->a, head->b, stack_size);
	Pop();
	printf("top: %d, %.5f    size: %d\n\n", head->a, head->b, stack_size);
	Pop();
	Pop();
	Pop();
	printf("IsEmpty: %d\n", STACK_EMPTY(head));
	return 0;
}

把Push和Pop封装成函数,又维护了一个栈大小变量stack_size。
说实话这个库封装的有点简陋了,甚至STACK_POP都没有检测head是否为NULL。
还不如自己实现一个栈来的好用。。。
打印结果:

IsEmpty: 0
top: 1111111, 111.11100    size: 3
top: 11111, 11.11000    size: 2
top: 111, 1.10000    size: 1

IsEmpty: 1

后记

这个库也是可以用的,只是自己还要多封装一下push和pop

标签:el,head,utstack,next,详解,define,STACK,size
来源: https://www.cnblogs.com/mrzono/p/16514552.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有