C的数据结构---栈的链式存储

1.

chain_stack.h

#pragma once
#include <crtdbg.h> 
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>

typedef int States;       //状态值

#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULLPTR -3

typedef int ElemType;     //栈内数据类型

typedef struct StackNode
{
	ElemType data;
	struct StackNode *next;
}StackNode;

typedef struct LinkStack
{
	StackNode *top;    //栈顶指针
	int cursize;       //数据元素的个数
}LinkStack;

States InitStack(LinkStack *pstack);    //创建一个空链栈

void DestroyStack(LinkStack *pstack);   //摧毁链栈

void ClearStack(LinkStack *pstack);     //清空链栈

int StackLength(LinkStack *pstack);     //栈的长度

bool StackEmpty(LinkStack *pstack);     //判空

States Push(LinkStack *pstack, ElemType val);       //入栈

States GetTop(LinkStack *pstack, ElemType *pval);     //取栈顶元素

States Pop(LinkStack *pstack, ElemType *pval);        //取栈顶元素并删除栈顶元素

.cpp

#include "Chain_Stack.h"

StackNode* BuyNode()
{
	StackNode *node = (StackNode*)malloc(sizeof(StackNode));
	if (node != NULL)
	{
		memset(node, 0, sizeof(StackNode));
	}
	return node;
}

void FreeNode(StackNode *Node)
{
	free(Node);
}

States InitStack(LinkStack *pstack)        //创建一个空链栈
{
	assert(pstack != NULL);
	pstack->top = BuyNode();
	if (pstack->top == NULL)
	{
		return OVERFLOW;
	}
	pstack->cursize = 0;
	return OK;
}


void ClearStack(LinkStack *pstack)     //清空链栈
{
	assert(pstack != NULL);
	while (pstack->top->next != NULL)
	{
		StackNode *tmp = pstack->top->next;
		pstack->top->next = tmp->next;
		FreeNode(tmp);
	}
	pstack->cursize = 0;
}

void DestroyStack(LinkStack *pstack)     //摧毁链栈
{
	assert(pstack != NULL);
	ClearStack(pstack);
	FreeNode(pstack->top);
	pstack->top = NULL;
}

int StackLength(LinkStack *pstack)     //栈的长度
{
	assert(pstack != NULL);
	return pstack->cursize;
}

bool StackEmpty(LinkStack *pstack)     //判空
{
	assert(pstack != NULL);
	return StackLength(pstack) == 0;
}

States Push(LinkStack *pstack, ElemType val)       //入栈
{
	assert(pstack != NULL);
	StackNode *tmp = BuyNode();
	if (tmp == NULL)
	{
		return OVERFLOW;
	}
	tmp->data = val;
	tmp->next = pstack->top->next;
	pstack->top->next = tmp;
	pstack->cursize += 1;
	return OK;
}

States GetTop(LinkStack *pstack, ElemType *pval)     //取栈顶元素
{
	assert(pstack != NULL);
	if (pval == NULL)
	{
		return NULLPTR;
	}

	if (StackEmpty(pstack))
	{
		return ERROR;
	}

	*pval = pstack->top->next->data;
	return OK;
}

States Pop(LinkStack *pstack, ElemType *pval)        //取栈顶元素并删除栈顶元素
{
	assert(pstack != NULL);
	if (pval == NULL)
	{
		return NULLPTR;
	}
	if (StackEmpty(pstack))
	{
		return NULLPTR;
	}

	StackNode *tmp = pstack->top->next;
	*pval = tmp->data;
	pstack->top->next = tmp->next;
	pstack->cursize -= 1;
	FreeNode(tmp);
	return OK;
}

main.cpp

int main()
{
	LinkStack my_linkstack;
	int val;
	InitStack(&my_linkstack);
	for (int i = 0; i < 5; i++)
	{
		Push(&my_linkstack, i);
	}
	GetTop(&my_linkstack, &val);
	printf("%d \n", val);
	for (int i = 0; i < 5; i++)
	{
		Pop(&my_linkstack, &val);
		printf("%d  ", val);
	}
	printf("\n");
	DestroyStack(&my_linkstack);
	_CrtDumpMemoryLeaks();
	system("pause");
	return 0;
}

2.

​​​​​​​head.h

#include "Lead_gen_link_list.h"

typedef int States;       

#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULLPTR -3
typedef void LinkStack;


LinkStack* LinkStack_Create();

void LinkStack_Destroy(LinkStack *pstack);

void LinKStack_Clear(LinkStack *pstack);

States LinkStack_Push(LinkStack *pstack, void *item);

void* LinkStack_Pop(LinkStack *pstack);

void* LinkStack_Top(LinkStack *pstack);

int LinkStack_Size(LinkStack *pstack);

.cpp

#include "head.h"

typedef struct StackNode   //链栈的结点
{
	LinkListNode node;     //链表的结点
	void *item;            //栈的结点
}StackNode;


LinkStack* LinkStack_Create()
{
	return LinkList_Create();
}


void LinkStack_Destroy(LinkStack *pstack)
{
	assert(pstack != NULL);
	LinKStack_Clear(pstack);
	LinkList_Destroy(pstack);
}

//把栈中所有元素弹出,并释放结点内存
void LinKStack_Clear(LinkStack *pstack)
{
	assert(pstack != NULL);

	while (LinkStack_Size(pstack) > 0)
	{
		LinkStack_Pop(pstack);
	}
	HeadNode *sp = (HeadNode*)pstack;
	sp->length = 0;
}

// void *item 栈的结点转化为链表的结点
States LinkStack_Push(LinkStack *pstack, void *item)
{
	assert(pstack != NULL && item != NULL);
	StackNode *tmp = (StackNode*)malloc(sizeof(StackNode));
	if (tmp == NULL)
	{
		return OVERFLOW;
	}
	memset(tmp, 0, sizeof(StackNode));
	tmp->item = item;

    LinkList_Insert(pstack, (LinkListNode*)tmp, 0);

	return OK;
}

void* LinkStack_Pop(LinkStack *pstack)
{
	assert(pstack != NULL);
	StackNode *tmp = (StackNode*)LinkList_Delete(pstack, 0);
	if (tmp == NULL)
	{
		return NULL;
	}
	void *item = tmp->item;
	free(tmp);
	tmp = NULL;
	return item; //主函数中传过来的item
}

void* LinkStack_Top(LinkStack *pstack)
{
	assert(pstack != NULL);
	StackNode *tmp = (StackNode*)LinkList_Get(pstack, 0);
	if (tmp == NULL)
	{
		return NULL;
	}
	
	return tmp->item;
}

int LinkStack_Size(LinkStack *pstack)
{
	assert(pstack != NULL);
	return LinkList_Length(pstack);
}

main.cpp

int main()
{
	LinkStack *pstack = LinkStack_Create();
	int arr[10];
	for (int i = 0; i < 5; i++)
	{
		arr[i] = i + 1;
		LinkStack_Push(pstack, &arr[i]);
	}
	
	printf("LinkStack_Size: %d \n", LinkStack_Size(pstack));
	printf("LinkStack_Top: %d \n", *(int*)LinkStack_Top(pstack));
	while (LinkStack_Size(pstack)>0)
	{
		printf("LinkStack_Pop:");
		printf("%d  ", *(int*)LinkStack_Pop(pstack));
	}
	LinKStack_Clear(pstack);
	LinkStack_Destroy(pstack);
	_CrtDumpMemoryLeaks();
	system("pause");
	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值