数据结构——04栈

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

一、栈

栈是限定仅在表尾进行插入和删除操作的线性表。
遵循的原则,先进后出、后进先出。

	栈顶:允许操作的一端
	栈底:不允许操作的一端

二、使用步骤

1.文件结构

代码如下(示例):

.
├── all
├── fun.c
├── fun.o
├── head.h
├── main.c
├── main.o
└── Makefile

0 directories, 7 files

2.源码

头文件

#ifndef __HEAD_H__
#define __HEAD_H__

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct person {
	char name[32];
	char sex;
	int age;
	int score;
}DATATYPE;

typedef struct list {
	DATATYPE*head;
	int top;
	int tlen;
}SeqStack;

SeqStack*CreateSeqStack(int size);
extern int DestroySeqStack(SeqStack**Stack_list);
extern int IsEmptySeqStack(SeqStack* Stack_list);
extern int IsFullSeqStack(SeqStack* Stack_list);
extern int PushSeqStack(SeqStack*Stack_list,DATATYPE *data);
extern int PopSeqStack(SeqStack*Stack_list);
extern int ClearSeqStack(SeqStack*Stack_list);
extern int GetSizeStack(SeqStack*Stack_list);
extern int GetTopDataStack(SeqStack*Stack_list,DATATYPE **data);

#endif

fun.c

#include "head.h"

SeqStack* CreateLinkList(int size)
{
	SeqStack *temp = malloc(sizeof(SeqStack));
	if(NULL == temp)
	{
		perror("malloc error 1");
		return NULL;
	}
	temp->head = malloc(sizeof(DATATYPE)*size);
	if(NULL == temp)
	{
		perror("malloc error 2");
		return NULL;
	}
	temp->top = 0;
	temp->tlen = size;
	return temp ;
}

int DestroySeqStack(SeqStack** Stack_list)
{
	free((*Stack_list)->head);
	free(*Stack_list);
	*Stack_list = NULL;
	return 0;
}
int IsEmptySeqStack(SeqStack* Stack_list)
{
	return 0 == Stack_list->top;
}
int IsFullSeqStack(SeqStack* Stack_list)
{
	return Stack_list->top == Stack_list->tlen;
}
int PushSeqStack(SeqStack*Stack_list,DATATYPE *data)
{
	if(IsFullSeqStack(Stack_list))
	{
		return -1;
	}
	memcpy(&Stack_list->head[Stack_list->top],data,sizeof(DATATYPE));
	Stack_list->top++;
	return 0;
}
int PopSeqStack(SeqStack*Stack_list)
{
	if(IsEmptySeqStack(Stack_list))
	{
		printf("this stack IsEmptySeqStack\n");
		return -1;
	}
	Stack_list->top--;
	return 0;
}
int ClearSeqStack(SeqStack*Stack_list)
{
	Stack_list->top = 0;
	return 0;
}
int GetSizeStack(SeqStack*Stack_list)
{
	return Stack_list->top;
}

int GetTopDataStack(SeqStack*Stack_list,DATATYPE **data)
{
	if(IsEmptySeqStack(Stack_list))
	{
		printf("this stack IsEmptySeqStack\n");
		return -1;
	}

	*data = &Stack_list->head[Stack_list->top-1];
	return 0;
}

main.c

#include "head.h"

int main(int argc, const char *argv[])
{
	
	DATATYPE data[] = {
		{"zhangsan",'f',20,100},
		{"lisi",'f',20,100},
		{"wangmazi",'f',20,100},
		{"guanerge",'f',20,100},
		{"niuda",'f',20,100},

	};
	SeqStack* Stack_list = CreateLinkList(10);
	DATATYPE *temp =NULL;
	int i =0 ;
	for(i=0;i<5;++i)
	{
		PushSeqStack(Stack_list,&data[i]);
	}
	for(i=0;i<5;++i)
	{
		GetTopDataStack(Stack_list,&temp);
		printf("%s %d\n",temp->name,temp->score);
		PopSeqStack(Stack_list);
	}

	return 0;
}

makefile

OBJ=all
SRC=main.o fun.o
CC=gcc
LFLAG=-g
$(OBJ):$(SRC)
	$(CC) $^ -o $@ $(LFLAG)
%.o:%.c
	$(CC) -c $< -o $@

clean:
	rm $(OBJ)
disclean:
	rm $(OBJ) *.o

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值