03 数据结构之栈

阅读引言: 只是分享我在复习过程中写的关于栈的代码

顺序栈

/* squence_stack.h */
#ifndef _SQUENCE_STACK
#define _SQUENCE_STACK

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DEBUG(msg) printf("---%s---%d, ---%s---", __func__,__LINE__, msg)

typedef int data_t;
typedef struct {
	data_t *p_data;
	int top;
	int maxlen;
}SqStack_t;

SqStack_t *sqstack_create(int len);
int sqstack_free(SqStack_t *s);
int sqstack_clear(SqStack_t *s);
int sqstack_push(SqStack_t *s, data_t data);
data_t sqstack_pop(SqStack_t *s);
int sqstack_isempty(SqStack_t *s);
int sqstack_isfull(SqStack_t *s);
data_t sqstack_top_value(SqStack_t *s);

#endif
/* squence_stack.c */
#include "squence_stack.h"

SqStack_t *sqstack_create(int len)
{
	SqStack_t *p = (SqStack_t *)malloc(sizeof(SqStack_t));
	if(p == NULL) {
		DEBUG("malloc failed!\n");
		return NULL;
	}

	p->p_data = (data_t *)malloc(sizeof(data_t) * len);
	if(p->p_data == NULL) {
		free(p);
		DEBUG("malloc failed!\n");
		return NULL;
	}
	
	memset(p->p_data, 0, sizeof(data_t) * len);
	p->top = -1;
	p->maxlen = len;

	return p;
}

int sqstack_free(SqStack_t *s)
{
	if(s == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	s->maxlen = 0;
	s->top = -1;
	if(s->p_data != NULL)
		free(s->p_data);
	free(s);

	return 0;
}

int sqstack_clear(SqStack_t *s)
{
	if(s == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	memset(s->p_data, 0, sizeof(data_t) * s->maxlen);
	s->top = -1;
	
	return 0;
}


int sqstack_push(SqStack_t *s, data_t data)
{
	if(s == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	if(s->top < s->maxlen) {
		s->top++;
		s->p_data[s->top] = data;
	} else {
		DEBUG("top beyond maxlen\n");
		return -1;
	}

	return 0;
}




data_t sqstack_pop(SqStack_t *s)
{
	if(s == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	return s->p_data[s->top--];
}

int sqstack_isempty(SqStack_t *s)
{
	if(s == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	return (s->top == -1? 1 : 0);
}


int sqstack_isfull(SqStack_t *s)
{
	if(s == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	return (s->top == s->maxlen - 1 ? 1:0);
	
}


data_t sqstack_top_value(SqStack_t *s)
{
	if(s == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	return (s->p_data[s->top]);
}

链式栈

/* link_stack.h */

#ifndef _LINK_STACK_H
#define _LINK_STACK_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define DEBUG(msg) \
	printf("%s, %s", __func__, msg)

typedef int data_t;
typedef struct node {
	data_t data;
	struct node *next;
}node_stack, *link_stack;

link_stack link_stack_create();
int link_stack_free(link_stack H);
int link_stack_push(link_stack H, data_t value);
data_t link_stack_pop(link_stack H);
void link_stack_show(link_stack H);
data_t get_top_value(link_stack H);
int link_stack_isempty(link_stack H);

#endif
/* link_stack.c */
#include "link_stack.h"

/*brife: create a link stack, return heap address of link stack 
 *
 * */
link_stack link_stack_create()
{
	link_stack H = NULL;

	H = (link_stack)malloc(sizeof(node_stack));
	if(H == NULL) {
		DEBUG("malloc failed\n");
		return NULL;
	}

	H->data = 0;
	H->next = NULL;

	return H;
}


/*brife: release a link stack 
 *
 * */	
int link_stack_free(link_stack H)
{
	if(H == NULL) {
		DEBUG("param is NULL\n");
		return -1;
	}

	link_stack p = NULL;

	while(H != NULL) {
		p = H;
		H = H->next;
		free(p);
	}

	return -1;
}


/*brife: entry stack
 *
 * */
int link_stack_push(link_stack H, data_t value)
{
	if(H == NULL) {
		DEBUG("param is NULL\n");
		return -1;
	}

	link_stack p = (link_stack)malloc(sizeof(node_stack));
	if(p == NULL) {
		DEBUG("malloc failed\n");
		return -1;
	}

	p->next = NULL;
	p->data = value;

	p->next = H->next;
	H->next = p;
	
	return 0;
}

/*brife: out stack
 *
 * */
data_t link_stack_pop(link_stack H)
{
	if(H == NULL) {
		DEBUG("param is NULL\n");
		return -1;
	}

	data_t ret = H->next->data;
	link_stack p = H->next;
	H->next = p->next;
	free(p);

	return ret;
}


/*brife: foreach the link stack
 *
 * */
void link_stack_show(link_stack H)
{
	if(H == NULL) {
		DEBUG("param is NULL\n");
		return ;
	}
		
	link_stack p = H->next;

	while(p != NULL) {
		printf("%d ", p->data);
		p = p->next;
	}
	puts("");

	return ;
}


/*brife: get value of link stack top 
 *
 * */
data_t get_top_value(link_stack H)
{
	if(H == NULL) {
		DEBUG("param is NULL\n");
		return -1;
	}

	if(H->next == NULL) {
		return -1;
	}

	return H->next->data;
}


/*brife: judge link stack is empty?
 *
 * */
int link_stack_isempty(link_stack H)
{
	if(H == NULL) {
		DEBUG("param is NULL\n");
		return -1;
	}

	return (H->next == NULL?1:0);
}

相关推荐

  1. 03 数据结构

    2024-03-11 16:38:03       15 阅读
  2. 数据结构

    2024-03-11 16:38:03       22 阅读
  3. 数据结构

    2024-03-11 16:38:03       9 阅读
  4. 数据结构(LIFO)

    2024-03-11 16:38:03       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-11 16:38:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-11 16:38:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-11 16:38:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-11 16:38:03       18 阅读

热门阅读

  1. SQL 分组查询

    2024-03-11 16:38:03       23 阅读
  2. nginx配置IPV6

    2024-03-11 16:38:03       15 阅读
  3. 服务中心选址问题

    2024-03-11 16:38:03       18 阅读
  4. C语言分支和循环语句—while

    2024-03-11 16:38:03       20 阅读
  5. PokéLLMon 源码解析(二)

    2024-03-11 16:38:03       20 阅读
  6. 解决Git中fatal: refusing to merge unrelated histories

    2024-03-11 16:38:03       23 阅读
  7. 【C/C++ 学习笔记】数组

    2024-03-11 16:38:03       25 阅读
  8. LeetCode:猜数字游戏

    2024-03-11 16:38:03       23 阅读
  9. LeetCode每日一题[C++]-猜数字游戏

    2024-03-11 16:38:03       23 阅读