utstack详解

源码

#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

自己写栈

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;
	el->next = NULL;
	if (head)
	{
		el->next = head;
		head = el;
	}
	else
	{
		head = el;
	}
	stack_size++;
}

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

int main()
{
	Push(111, 1.1);
	Push(11111, 11.11);
	Push(1111111, 111.111);
	printf("size: %3d, int: %8d, float: %10.5f\n", stack_size, head->a, head->b);
	Pop();
	printf("size: %3d, int: %8d, float: %10.5f\n", stack_size, head->a, head->b);
	Pop();
	printf("size: %3d, int: %8d, float: %10.5f\n", stack_size, head->a, head->b);
	Pop();
	Pop();
	Pop();
	Pop();
	return 0;
}

也是可以的,花的时间不是特别多,所以考试自己写也是可以接受的。

后记

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

posted @ 2022-07-24 15:06  mrzono  阅读(233)  评论(0编辑  收藏  举报