数据结构之——栈(c语言版)

41 篇文章 0 订阅
17 篇文章 0 订阅

栈的介绍

  • 栈是一种特殊的线性表,仅在线性表的一端操作(栈顶top)。
  • 插入操作:进栈、压栈、入栈。
  • 删除操作:出栈、弹栈。
  • 用线性表的顺序存储模拟栈时,在尾部添加或删除元素,不会涉及到元素的大量移动。
  • 用线性表的链式存储模拟栈,在头部插入或删除元素,不会涉及到元素的大量移动。

栈的链式存储

  • 利用单链表结构创建栈(头插法):
  • 例子:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>

//先定义单链表结构:
typedef struct Node
{
	//定义连接链表的节点:
	Node* next;
}node;

typedef struct Common_list
{
	//定义一个空间域
	node head;
	int length;
}common_list;


//1. 建立空链表:
common_list* creat_list()
{
	common_list*temp_creat;
	temp_creat = (common_list*)malloc(sizeof(common_list));
	if (temp_creat == nullptr)
	{
		printf("空间开辟错误!!!\n");
		return nullptr;
	}
	printf("已成功创建空列表!!1\n");
	temp_creat->head.next = (node*)&temp_creat;//双向链表
	temp_creat->length = 0;
	printf("空列表初始化成功!!1\n");
	return temp_creat;
}

//2. 开始在old中的pos位置处插入元素input:
void insert_list(common_list**old, node*input, int pos)
{
	common_list*temp_insert = *old;
	if (temp_insert == nullptr)
	{
		printf("传入空间错误!!!\n\n");
		return ;
	}
	else if (pos < 0)
	{
		printf("位置号不能小于0");
		return ;
	}

	else if (pos > temp_insert->length)
	{
		pos = temp_insert->length;
	}
	//定义一个辅助指针:
	node*current = &temp_insert->head;
	for (int i = 0; i < pos; i++)
	{
		//开始移动到插入的位置:
		current = current->next;
	}
	//进行插入操作:
	input->next = current->next;
	current->next = input;
	temp_insert->length++;
	printf("号码插入成功!!!\n\n");
}

//查找pos处的元素:
node* find_list(common_list*old, int pos)
{
	common_list*temp_find = old;
	if (temp_find == nullptr)
	{
		printf("传入空间错误!!!\n\n");
		return nullptr;
	}
	else if (pos < 0 || pos >= temp_find->length)
	{
		printf("该位置处无号码!!!\n\n");
		return nullptr;
	}

	//定义一个辅助指针便于寻找:
	node* current = temp_find->head.next;
	for (int i = 0; i < pos; i++)
	{
		current = current->next;
	}
	printf("找到号码啦!!!\n\n");
	return current;
}

//定义删除函数:
node* delete_list(common_list**old, int pos)
{
	common_list*temp_del = *old;
	if (temp_del == nullptr)
	{
		printf("传入空间错误!!!\n\n");
		return NULL;
	}
	else if (pos < 0 || pos >= temp_del->length)
	{
		printf("该位置处无号码!!!\n\n");
		return NULL;
	}

	//定义一个辅助指针定位当前位置:
	node* current = &temp_del->head;
	for (int i = 0; i < pos; i++)
	{
		current = current->next;
	}
	//定义一个辅助指针用于缓存要删除的节点:
	node*del = current->next;
	current->next = del->next;
	temp_del->length--;
	printf("数据删除成功!!!\n\n");
	return del;
}

void clear_list(common_list**old)
{
	common_list* temp_cls = *old;
	if (temp_cls == nullptr)
	{
		printf("传入空间错误!!!\n\n");
		return;
	}
	free(temp_cls);
	temp_cls = nullptr;
	printf("数据已经清空!!!\n\n");
}

int length_list(common_list*old)
{
	return old->length;
}


//以下为用单链表改造栈:

//定义一个用户数据域:
typedef struct Node_stack
{
	node head;
	//使用void保证用户能够输入任何类型的数据
	void* user_data;
}node_stack;

common_list* creat_stack()
{
	common_list*old_stack = creat_list();
	if (old_stack == nullptr)
	{
		printf("栈为空!!!已退出!!!\n\n");
		return nullptr;
	}
	return old_stack;
}

void push_stack(common_list** old_stack, void* user_data)
{
	if (old_stack == nullptr || user_data == nullptr)
	{
		printf("栈为空!!!或用户数据为空!!!已退出!!!\n\n");
		return ;
	}
	common_list*temp_old = *old_stack;
	node_stack* user_temp = (node_stack*)malloc(sizeof(node_stack));
	user_temp->user_data = user_data;

	insert_list(&temp_old,(node*)user_temp,0);
	
	printf("用户数据插入成功!!!\n\n");
}

void* get_top(common_list*old_stack)
{
	if (old_stack == nullptr)
	{
		printf("栈为空!!!已退出!!!\n\n");
		return nullptr;
	}
	node_stack*temp_get = (node_stack*)find_list(old_stack,0);
	if (temp_get == nullptr)
	{
		printf("用户数据已删除完成!!!\n\n");
		return nullptr;
	}
	return temp_get->user_data;
}

void pop_stack(common_list**old_stack)
{
	if (old_stack == nullptr)
	{
		printf("栈为空!!!已退出!!!\n\n");
		return ;
	}
	common_list*temp_del = *old_stack;
	node_stack*buf_del = (node_stack*)delete_list(&temp_del,0);
	free(buf_del);
	buf_del = nullptr;
	printf("用户数据删除成功!!!\n\n");
}

int length_stack(common_list*old_stack)
{
	if (old_stack == nullptr)
	{
		printf("栈为空!!!已退出!!!\n\n");
		return -1;
	}
	int temp_len_stack = length_list(old_stack);
	return temp_len_stack;
}

void cls_stack(common_list**old_stack)
{
	if (old_stack == nullptr)
	{
		printf("栈为空!!!已退出!!!\n\n");
		return ;
	}
	common_list*temp_cls = *old_stack;
	
	while (length_stack(temp_cls)>0)
	{
		//循环弹栈:
		pop_stack(&temp_cls);
		printf("栈中有元素,开始删除!!!\n\n剩余栈的长度是:【%d】\n\n", length_stack(temp_cls));
		Sleep(800);
	}
	printf("元素删除完成,栈为空!!!\n\n");
}

int main()
{
	common_list* old_stack = creat_stack();

	for (int i = 1000; i < 5000; i +=1000)
	{
		//循环入栈数字:
		push_stack(&old_stack, &i);
		int* get_user_data = (int *)get_top(old_stack);
		printf("你的数据是:%d\n\n", *get_user_data);
	}
	
	//插入字符串:
	char name[] = "周杰伦";
	push_stack(&old_stack, name);
	char* get_user_data = (char *)get_top(old_stack);
	printf("你的数据是:%s\n\n", get_user_data);

	strcpy_s(name,"夏婉安");
	push_stack(&old_stack, name);
	get_user_data = (char *)get_top(old_stack);
	printf("你收藏的歌手是:%s\n\n", get_user_data);

	//清除栈操作(一个一个弹栈完成):
	cls_stack(&old_stack);
	get_user_data = (char *)get_top(old_stack);
	printf("你收藏的歌手是:%s\n\n", get_user_data);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值