0
点赞
收藏
分享

微信扫一扫

堆栈(栈)的实现

半秋L 2022-02-14 阅读 111

链表栈

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

typedef int data_t;
typedef struct linkstack{
	data_t data;
	struct linkstack *next;
}linkstack_t;

linkstack_t *creat_linkstack()
{
	linkstack_t *head;
	head = malloc(sizeof(linkstack_t));
	head->next = NULL;

	return head;
}

int push_linkstack(linkstack_t *p,const data_t *data)
{
	linkstack_t *newnode;

	newnode =malloc(sizeof(linkstack_t));
	newnode->data = *data;
	newnode->next = p->next;

	p->next = newnode;

	return 0;
}


int pop_linkstack(linkstack_t *p,data_t *databuf)
{
	linkstack_t *temp;

	if(NULL == p->next)
		return -1;

	temp = p->next;
	p->next = temp->next;

	*databuf = temp->data;

	free(temp);

	return 0;
}


int get_top_linstack(linkstack_t *p,data_t *databuf)
{
	if(NULL == p->next)
		return -1;
	*databuf = p->next->data;

	return 0;
}


void print_linkstack(linkstack_t *p)
{
	while(NULL != p->next){
		p = p->next;

		printf("%d\n",p->data);
	}
	printf("end\n");

	return ;
}




int main(int argc, const char *argv[])
{
	linkstack_t *p;
	int i;
	data_t buf;

	p = creat_linkstack();

	for(i = 0;i < 10;i ++){
		push_linkstack(p,&i);
	}

	print_linkstack(p);

	i = 12;
	while(i --){
		buf = -1;
		pop_linkstack(p,&buf);
		printf("pop:%d\n",buf);
	}

	return 0;
}

范例2

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

typedef int data_t;
typedef struct mystack{
	int *data;
	int size;
	int top;
}my_stack_t;
	
my_stack_t *creat_stack(int size)
{
	my_stack_t *p;
	p = malloc(sizeof(my_stack_t));
	p->data = malloc(sizeof(data_t) * size);

	p->size = size;
	p->top = -1;

	return p;
}


/*入栈: 向栈顶存入一个新的元素*/
int push_stack(my_stack_t *p,const data_t *data)
{
	if(p->size - 1 == p->top)
		return -1;

	p->data[++ p->top] = *data;

	return 0;
}
/*出栈:取栈顶元素,到databuf 指向的变量中*/
int pop_stack(my_stack_t *p,data_t *databuf)
{
	if(-1 == p->top )
		return -1;

	*databuf = p->data[p->top --];
	return 0;
}
int get_top_stack(my_stack_t *p,data_t *databuf)
{
	if(-1 == p->top )
		return -1;

	*databuf = p->data[p->top];
	return 0;
}

int is_empty_stack(my_stack_t *p)
{
	return -1 == p->top;
}
int is_full_stack(my_stack_t *p)
{
	return p->size - 1 == p->top;
}

int clean_stack(my_stack_t *p)
{
	p->top = -1;
}

int dis_stack(my_stack_t *p)  //传指针是为了该表指针指向内容的值
{
	free(p->data);
	free(p);

	p = NULL;

	return 0;
}
int dis_stack(my_stack_t * *pp) //传指针的地址是为了该表指针的值
{                                //两个函数都可以把申请的两块内存释放掉,但是上面的
	free((*pp)->data);              // 不能改变P的值,将其置为NULL
	free(*pp);

	*pp = NULL;


	return 0;
}


void print_stack(my_stack_t *p)
{
	int i;

	for(i = p->top;i >= 0;i --){
		printf("%d\n",p->data[i]);
	}
	printf("end\n");
	
	return ;
}



int main(int argc, const char *argv[])
{
	my_stack_t *p;
	int i;
	data_t buf;

	p = creat_stack(8);

	for(i = 10;i < 20;i ++){
		push_stack(p,&i);
	}

	print_stack(p);

	i = 8;
	while(i --){
		buf = -1;
		pop_stack(p,&buf);
		printf("pop:%d\n",buf);
	}
	print_stack(p);

	for(i = 10;i < 20;i ++){
		push_stack(p,&i);
	}

	print_stack(p);

	dis_stackp(&p);
	// p = NULL;

	if(p != NULL){
		;
	}

	
	return 0;
}

举报

相关推荐

0 条评论