栈的介绍
- 栈是一种特殊的线性表,仅在线性表的一端操作(栈顶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;
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;
}
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");
}
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* 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;
}