链表-栈(个人学习记录)

本文介绍了如何在C语言中使用结构体和链表实现一个简单的链式栈,包括创建栈、入栈、出栈、遍历和清除功能,以及相应的错误处理机制。

目录

1.结构体创建

2.创建链表

3.入栈

4.出栈

5.遍历

6.清除

程序

LinkStack.h

LinkStack.c

main.c


1.结构体创建

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

2.创建链表

linkstack *create_linkstack(void)
{
	linkstack *l;
	l = (linkstack *)malloc(sizeof(linkstack));
	if(l == NULL)
	{
		printf("l is error\n");
		exit(0);
	}
	l->data = 0;
	l->next = NULL;
	return l;
}

3.入栈

void push_linkstack(linkstack *l,data_t data)
{
	if(l == NULL)
	{
		printf("l_push is error\n");
		exit(0);
	}

	linkstack *p;
	p = (linkstack *)malloc(sizeof(linkstack));
	p->next = NULL;
	p->data = data;
	if(l->next == NULL)
	{
		l->next = p;
	}
	else
	{
		p->next = l->next;
		l->next = p;
	}
	return ;
}

4.出栈

data_t pop_linkstack(linkstack *l)
{
	if(l == NULL)
	{
		printf("l_pop is error\n");
		exit(0);
	}
	linkstack *q;
	data_t t;
	q = l->next;
	l->next = q->next;
	t = q->data;
	free(q);
	q = NULL;
	return t;
}

5.遍历

void show_linkstack(linkstack *l)
{
	if(l == NULL)
	{
		printf("l_show is error\n");
		exit(0);
	}
	linkstack *q;
	q = l->next;
	while(q)
	{
		printf("%d ",q->data);
		q = q->next;
	}
	printf("\n");
	return ;
}

6.清除

void clear_linkstack(linkstack *l)
{
	if(l == NULL)
	{
		printf("l_show is error\n");
		exit(0);
	}
	linkstack *q;
	q = l;
	int value;
	while(q->next)
	{
		value = pop_linkstack(q);
		printf("free(%d)\n",value);
	}
	return ;
}

程序

LinkStack.h

#include <stdio.h>
#include "stdlib.h"
typedef int data_t;
typedef struct next_t{
	data_t data;
	struct next_t *next;
}linkstack;

linkstack *create_linkstack(void);
void push_linkstack(linkstack *l,data_t data);
data_t pop_linkstack(linkstack *l);
void show_linkstack(linkstack *l);
void clear_linkstack(linkstack *l);

LinkStack.c

#include "LinkStack.h"

linkstack *create_linkstack(void)
{
	linkstack *l;
	l = (linkstack *)malloc(sizeof(linkstack));
	if(l == NULL)
	{
		printf("l is error\n");
		exit(0);
	}
	l->data = 0;
	l->next = NULL;
	return l;
}
void push_linkstack(linkstack *l,data_t data)
{
	if(l == NULL)
	{
		printf("l_push is error\n");
		exit(0);
	}

	linkstack *p;
	p = (linkstack *)malloc(sizeof(linkstack));
	p->next = NULL;
	p->data = data;
	if(l->next == NULL)
	{
		l->next = p;
	}
	else
	{
		p->next = l->next;
		l->next = p;
	}
	return ;
}
data_t pop_linkstack(linkstack *l)
{
	if(l == NULL)
	{
		printf("l_pop is error\n");
		exit(0);
	}
	linkstack *q;
	data_t t;
	q = l->next;
	l->next = q->next;
	t = q->data;
	free(q);
	q = NULL;
	return t;
}
void show_linkstack(linkstack *l)
{
	if(l == NULL)
	{
		printf("l_show is error\n");
		exit(0);
	}
	linkstack *q;
	q = l->next;
	while(q)
	{
		printf("%d ",q->data);
		q = q->next;
	}
	printf("\n");
	return ;
}
void clear_linkstack(linkstack *l)
{
	if(l == NULL)
	{
		printf("l_show is error\n");
		exit(0);
	}
	linkstack *q;
	q = l;
	int value;
	while(q->next)
	{
		value = pop_linkstack(q);
		printf("free(%d)\n",value);
	}
	return ;
}

main.c

#include<stdio.h>
#include "LinkStack.h"
int main(int argc,char *argv[])
{
	linkstack *l;
	l = create_linkstack();
	push_linkstack(l,0);
	push_linkstack(l,1);
	push_linkstack(l,2);
	push_linkstack(l,3);
	push_linkstack(l,4);
	push_linkstack(l,5);
	show_linkstack(l);
	clear_linkstack(l);
	show_linkstack(l);


	
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值