顺序表实现栈_单链表实现栈

一、Sysutil.h//系统头文件

#ifndef _SYSUTIL_H_
#define _SYSUTIL_H_

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<stdbool.h>
#include<windows.h>

#include<vld.h>  //内存泄漏工具的头文件

#endif

二、顺序表实现栈

#include"Sysutil.h"

#define StackElemType int

#define SEQSTACK_DEFAULT_SIZE 8
//顺序表实现栈结构
typedef struct SeqStack
{
	StackElemType *base; //
	size_t         capacity;
	int            top;
}SeqStack;

void SeqStackInit(SeqStack *pst, int sz);
void SeqStackDestroy(SeqStack *pst);
void SeqStackPush(SeqStack *pst, StackElemType x);
void SeqStackPop(SeqStack *pst);
StackElemType SeqStackTop(SeqStack *pst);
void SeqStackShow(SeqStack *pst);

bool IsFull(SeqStack *pst)
{return pst->top >= pst->capacity;}
bool IsEmpty(SeqStack *pst)
{return pst->top == 0;}

void SeqStackInit(SeqStack *pst, int sz){
	assert(pst);
	pst->capacity = sz > SEQSTACK_DEFAULT_SIZE ? sz : SEQSTACK_DEFAULT_SIZE;
	pst->base = (StackElemType *)malloc(sizeof(SeqStack)*pst->capacity);
	assert(pst->base);
	pst->top = 0;
}
void SeqStackPush(SeqStack *pst, StackElemType x){
	assert(pst);
	if (IsFull(pst)){
		printf("栈已满,%d无法插入。\n", x);
	}
	pst->base[pst->top++] = x;
}
void SeqStackPop(SeqStack *pst){
	assert(pst);
	if (IsEmpty(pst)){
		printf("栈已空,无法删除。\n");
	}
	pst->top--;
}
StackElemType SeqStackTop(SeqStack *pst){
	assert(pst&&!IsEmpty(pst));
	return pst->base[pst->top - 1];
}
void SeqStackShow(SeqStack *pst){
	assert(pst);
	for (int i = pst->top - 1; i >= 0; --i)
		printf("%d\n", pst->base[i]);
}
void SeqStackDestroy(SeqStack *pst){
	assert(pst);
	free(pst->base);
	pst->base = NULL;
	pst->capacity=pst->top = 0;
}

三、顺序表主函数

#include"Stack.h"
void main(){
	SeqStack st;
	SeqStackInit(&st, 0);
	SeqStackPush(&st, 1);
	SeqStackPush(&st, 2);
	SeqStackPush(&st, 3);
	SeqStackPush(&st, 4);
	SeqStackPush(&st, 5);
	SeqStackShow(&st);
	printf("============\n");

	while (!IsEmpty(&st))
	{
		StackElemType val = SeqStackTop(&st);
		SeqStackPop(&st);
		printf("%d 出栈.\n", val);
	}
	printf("出栈完毕.\n");

	SeqStackDestroy(&st);

	system("pause");
	return 0;
}

四、单链表实现栈

//链栈
typedef struct LinkStackNode
{
	StackElemType data;
	struct LinkStackNode *next;
}LinkStackNode;
typedef struct LinkStack
{
	LinkStackNode *head;
}LinkStack;
void LinkStackInit(LinkStack *pst);
void LinkStackDestroy(LinkStack *pst);
void LinkStackPush(LinkStack *pst, StackElemType x);
void LinkStackPop(LinkStack *pst);
StackElemType LinkStackTop(LinkStack *pst); 
void LinkStackShow(LinkStack *pst);
               

void LinkStackInit(LinkStack *pst){
	assert(pst);
	pst->head = NULL;
}
void LinkStackPush(LinkStack *pst, StackElemType x){
	assert(pst);
	LinkStackNode *s = (LinkStackNode *)malloc(sizeof(LinkStackNode));
	s->data = x;
	
	s->next = pst->head;
	pst->head = s;
}
void LinkStackPop(LinkStack *pst){
	assert(pst);
	if (pst->head != NULL){
		LinkStackNode *s = pst->head;
		pst->head = s->next;
		free(s);
	}
}
StackElemType LinkStackTop(LinkStack *pst){
	assert(pst&&pst->head != NULL);
	return pst->head->data;
}
void LinkStackShow(LinkStack *pst){
	assert(pst);
	LinkStackNode *p = pst->head;
	while (p!=NULL){
		printf("%d-->", p->data);
		p = p->next;
	}
	printf("Nill..\n");
}

void LinkStackDestroy(LinkStack *pst){
	assert(pst);
	while (pst->head != NULL){
		LinkStackNode* p = pst->head;
		pst->head = p->next;
		free(p);
	}
}

五、单链表主函数


#include"Stack.h"
void main()
{
	LinkStack st;
	LinkStackInit(&st);
	LinkStackPush(&st, 1);
	LinkStackPush(&st, 2);
	LinkStackPush(&st, 4);
	LinkStackPush(&st, 3);
	LinkStackPush(&st, 5);
	LinkStackShow(&st);
	printf("============\n");

	StackElemType val = LinkStackTop(&st);
	LinkStackPop(&st);
	//LinkStackShow(&st);
	printf("%d 出栈.\n", val);

	LinkStackDestroy(&st);
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值