欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据结构-栈的顺序存储和链式存储

程序员文章站 2022-03-12 17:28:52
...

栈的特点:先进后出

栈的顺序存储

数据结构-栈的顺序存储和链式存储

stack.h

#pragma once
#include<stdlib.h>
#define MAXSIZE 1024
typedef struct STACK
{
	void* data[MAXSIZE];
	int size;

}SStack;

//初始化栈
void* Init_Stack();
//入栈
void PushStack(void* Stack,void* data);
//出栈
void PopStack(void* Stack);
//获取栈顶元素
void* TopStack(void* Stack);
//获取大小
int GetSize(void* Stack);
//销毁栈
void DestroyStack(void* Stack);


stack.c

#include"stack.h"
//初始化栈
void* Init_Stack()
{
	SStack* Stack = (SStack*)malloc(sizeof(SStack));
	Stack->size = 0;
	int i = 0;
	for (i = 0; i < MAXSIZE; ++i)
	{
		Stack->data[i] = NULL;
	}
	return Stack;
}

//入栈
void PushStack(void* Stack, void* data)
{
	if (NULL == Stack)
	{
		return;
	}
	if (NULL == data)
	{
		return;
	}
	SStack* sstack = (SStack*)Stack;
	//判断栈是否满了
	if (sstack->size == MAXSIZE)
	{
		return;
	}
	sstack->data[sstack->size] = data;
	++sstack->size;	
}

//出栈
void PopStack(void* Stack)
{
	if (NULL == Stack)
	{
		return;
	}
	SStack* sstack = (SStack*)Stack;
	sstack->size--;

}

//获取栈顶元素
void* TopStack(void* Stack)
{
	if (NULL == Stack)
	{
		return;
	}
	SStack* sstack = (SStack*)Stack;
	return sstack->data[sstack->size-1];
}

//获取大小
int GetSize(void* Stack)
{
	if (NULL == Stack)
	{
		return;
	}
	SStack* sstack = (SStack*)Stack;
	return sstack->size;
}

//销毁栈
void DestroyStack(void* Stack)
{
	if (NULL == Stack)
	{
		return;
	}
	free(Stack);

}


maic.c

#include"stack.h"
typedef struct PERSON
{
	char name[64];
	int age;
}person;

int main()
{
	person p1 = { "aaa", 1 };
	person p2 = { "bbb", 2 };
	person p3 = { "ccc", 3 };
	person p4 = { "ddd", 4 };
	SStack* sstack = Init_Stack();
	PushStack(sstack, &p1);
	PushStack(sstack, &p2);
	PushStack(sstack, &p3);
	PushStack(sstack, &p4);

	while (sstack->size > 0)
	{
		person*p = (person*)TopStack(sstack);
		printf("Name:%s,Age:%d\n", p->name, p->age);
		PopStack(sstack);//出栈,栈顶指针-1

	}

	DestroyStack(sstack);
	getchar();
	return 0;

}

打印结果:

数据结构-栈的顺序存储和链式存储



栈的链式存储
stack_link.h

数据结构-栈的顺序存储和链式存储

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

//栈的特点是先进后出,用链式存储的话头查就实现了先进后出的特点

//栈节点
typedef struct STACKNODE
{
	struct STACKNODE* next;
}StackNode;

typedef struct STACK
{
	StackNode header;
	int size;

}SStack;

//初始化栈
void* Init_Stack();
//入栈
void PushStack(void* Stack, StackNode* data);
//出栈
void PopStack(void* Stack);
//获取栈顶元素
void* TopStack(void* Stack);
//获取大小
int GetSize(void* Stack);
//销毁栈
void DestroyStack(void* Stack);

stack_link.c

#include "stack_link.h"
//初始化栈
void* Init_Stack()
{
	SStack* sstack = (SStack*)malloc(sizeof(SStack));
	if (NULL == sstack)
	{
		return NULL;
	}
	sstack->header.next = NULL;
	sstack->size = 0;
	return sstack;
}

//入栈
void PushStack(void* Stack, StackNode* data)
{
	if (NULL == Stack)
	{
		return;
	}
	if (NULL == data)
	{
		return;
	}
	SStack* sstack = (SStack*)Stack;
	data->next = sstack->header.next;//把数据空间的前4个字节串起来,形成链表
	sstack->header.next = data;
	++sstack->size;
}
//出栈
void PopStack(void* Stack)
{
	if (Stack == NULL)
	{
		return;
	}
	SStack* sstack = (SStack*)Stack;
	if (sstack->size == 0)
	{
		return;
	}
	//让头指向第二个节点就行了
	StackNode* pFirst = sstack->header.next;
	sstack->header.next = pFirst->next;//头节点指向第二个节点
	--sstack->size;


}
//获取栈顶元素
void* TopStack(void* Stack)
{
	if (Stack == NULL)
	{
		return NULL;
	}
	SStack* sstack = (SStack*)Stack;
	return sstack->header.next;
}
//获取大小
int GetSize(void* Stack)
{
	if (Stack == NULL)
	{
		return -1;
	}
	SStack* sstack = (SStack*)Stack;
	return sstack->size;
}
//销毁栈
void DestroyStack(void* Stack)
{
	if (Stack == NULL)
	{
		return;
	}
	free(Stack);
}

maic.c

#define _CRT_SECURE_NO_WARNINGS
#include "stack_link.h"
#include<stdio.h>
#include<string.h>
typedef struct STUDENT
{
	StackNode node;
	char name[64];
	int age;

}student;

int main()
{
	SStack* Stack = Init_Stack();
	student s1, s2, s3, s4, s5;
	strcpy(s1.name, "aaa");
	strcpy(s2.name, "bbb");
	strcpy(s3.name, "ccc");
	strcpy(s4.name, "ddd");
	strcpy(s5.name, "eee");
	s1.age = 1;
	s2.age = 2;
	s3.age = 3;
	s4.age = 4;
	s5.age = 5;
	PushStack(Stack, (StackNode*)&s1);
	PushStack(Stack, (StackNode*)&s2);
	PushStack(Stack, (StackNode*)&s3);
	PushStack(Stack, (StackNode*)&s4);
	PushStack(Stack, (StackNode*)&s5);
	while (Stack->size > 0)
	{

		student* s = (student*)TopStack(Stack);
		printf("Name:%s,Age:%d\n", s->name, s->age);
		PopStack(Stack);
	}

	DestroyStack(Stack);
	getchar();
	return 0;
}


打印结果

数据结构-栈的顺序存储和链式存储