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

C语言实链队

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

C语言实链队

 源代码如下:

//链队(尾插法,和头弹出,头删除法;需要设立头节点——频繁的加入、退出
#include 
#include 
#define SIZE 10
#define ERROR 0
#define OK 1
#define OVERFLOW 999
#define ElemType int
typedef struct Node
{
	ElemType data;
	struct Node* next;
}QueneNode,*QuenePtr;
typedef struct
{
	QuenePtr front;
	QuenePtr tail;
}Quenelink;
//INIT
int QueneINiT(Quenelink *L)
{
	(*L).front = (QueneNode*)malloc(sizeof(QueneNode));
	if (!(*L).front)
	{
		return ERROR;
	}
	(*L).tail = (*L).front;
	(*L).front->next = NULL;
	return OK;
}
//insert
int insert(Quenelink*L)
{
	int i = 0;
	int num = 0;
	printf("个数>");
	scanf("%d", &num);
	for (i; i < num; i++)
	{
		QuenePtr p = (QuenePtr)malloc(sizeof(QueneNode));

		printf("输入第%d个值>", i + 1);
		scanf("%d", &p->data);
		p->next = (*L).tail->next;
		(*L).tail->next = p;
		(*L).tail = p;
	}
	return OK;
}
//clear
int clear(Quenelink *L)
{
	QuenePtr p,q;
	if ((*L).front == (*L).tail)
	{
		return ERROR;
	}
	p = (*L).front->next;
	while (p)
	{
		q = p->next;
		if ( q== NULL)
		{
			(*L).tail = (*L).front;
		}
		free(p);
		p = q;
	}
	(*L).front->next = NULL;
	(*L).tail = (*L).front;
	//destory销毁链队
	//free((*L).front);
}
//pop
int pop(Quenelink* L)
{
	if ((*L).front == (*L).tail)
	{
		return ERROR;
	}
	QuenePtr p = (*L).front->next;
	int e = p->data;
	QuenePtr q = p->next;
	(*L).front->next = q;
	free(p);
	if (q == NULL)
	{
		(*L).tail = (*L).front;
	}
	

}
void traverse(Quenelink L)
{
	QuenePtr p = L.front->next;
    if (!p)
	{
		printf("it's empty");
		exit(EXIT_FAILURE);
	}
	while (p)
	{
		printf("%dt", p->data);
		p = p->next;
	}
}
int main()
{
	Quenelink L;
	QueneINiT(&L);//一定得传递地址才能够完成初始化,对于链表之类得初始化,都需要传递其头指针的地址完成初始化
	insert(&L);
	delete(&L);
	traverse(L);
	return 0;
}

 

 今天在写链队的时候,主要是在初始化方面遇到了一些问题:单纯传递链队名称,并不会被初始化:

只有传递地址,才能够成功 :

 

 

 

 

 

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

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

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