栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

链式栈的创建 数据结构C语言版

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

链式栈的创建 数据结构C语言版

摘要:链式栈 首先它是链式的,需要用malloc在堆区开辟空间,所以相对于顺序栈来说,就很节省空间,其次,栈的特点是先进后出(FILO),它与队列是相反的操作。就像装子弹,先进去的子弹总是最后打出来。

实现函数:linkstack.c

#include 
#include 
#include 
#include 
#include "linkstack.h"
Linkstack *Create_Link()  //创建头节点
{
	Linkstack *head =(Linkstack *)malloc(sizeof(Linkstack));
	if(NULL == head){
		printf("head is NULLn");
		return NULL;
	}
	head->next=NULL;
	head->data=-1;
	return head;
}
int Linkstack_Is_Empty(Linkstack *head)//判空
{
	if(head->next == NULL){
		return 0;
	}
	return 1;
}
int Linkstack_Lenth(Linkstack *head)//计算表长
{	
	Linkstack *p;
	int i=0;
	p=head->next;
	while(p!= NULL){
		i++;
		p=p->next;
		
	}
	return i;
}	
void Linkstack_posh(Linkstack *head,data_t data)//入栈
{
	Linkstack *new =(Linkstack *)malloc(sizeof(Linkstack));
	if(NULL == new){
		printf("new is NULLn");
		return;
	}
	new->data=data;
	new->next=NULL;
	new->next=head->next;
	head->next=new;
}
void Linkstack_pop(Linkstack *head,data_t *data){//出栈
	if(Linkstack_Is_Empty(head)==0){
		printf("stack is nulln");
		return;
	}
	Linkstack *p=head->next;
	head->next=p->next;
	*data=p->data;
	free(p);
	p=NULL;
}

头文件(函数声明):linkstack.h

#ifndef _LINKSTACK_H
#define _LINKSTACK_H
typedef int data_t;
typedef struct linkstack{
    data_t data;
    struct linkstack *next;
}Linkstack;
Linkstack *Create_Link();

void Linkstack_posh(Linkstack *head,data_t data);//从表头插入数据

int Linkstack_Lenth(Linkstack *head);//计算表长

void Linkstack_pop(Linkstack *head,data_t *data);//pop

int Linkstack_Is_Empty(Linkstack *head);//判空

#endif

主函数:main.c

#include 
#include 
#include 
#include 
#include "linkstack.h"
int main(){
Linkstack *head=Create_Link();
int n=10;
while(n--){
Linkstack_posh(head,n);//从表头插入数据
}

int data=0;
while(head->next!=NULL){
Linkstack_pop(head,&data);
	printf("%d ",data);
}
puts(" ");
	return 0;
}

好!今天的分享到此结束,哪里写的不对的,请各位大佬多多指正,不吝赐教,谢谢!(只因你太美)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1034739.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号