数据结构第四天

本文详细介绍了如何在C语言中使用链表数据结构实现一个队列,包括创建队列、尾插法插入元素、头部删除以及遍历队列的操作。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>
#include "./004.funcle.h"

//创建链式链表队列 
poslink* queue_like()
{
	poslink* ont = (poslink*)malloc(sizeof(poslink));
	ont->frunt = (linkequeue*)malloc(sizeof(linkequeue));
	if(ont->frunt == NULL)
	{
		printf("创建失败,链表队列创建失败\n");
		return NULL;
	}
	ont->frunt->text.len = 0;
	ont->frunt->next = NULL;

	ont->rear = ont->frunt;
	return ont;
	
}

//尾插法插入
void poptext_make(poslink* heald,datatype unte)
{
	linkequeue* temp = (linkequeue*)malloc(sizeof(linkequeue));
	if(temp == NULL)
	{
		printf("插入失败,链表插入失败\n");
		return;
	}
	heald->frunt->text.data = unte;
	heald->frunt->next = NULL;

	temp->next = heald->rear->next;
	heald->rear->next = temp;

	heald->rear = heald->rear->next;
	
	heald->frunt->text.len++;

	return;

}

//头删
void make_poptext(poslink* heald)
{
		if(heald->frunt->next == NULL)
		{
			printf("头删失败\n");
			return;
		}

		datatype tack;
		tack = heald->frunt->text.data;
		poslink* p = heald->frunt->next;
		heald->frunt->next = heald->frunt->next->next;
		
		heald->frunt->text.len--;
		free(p);
		return;
}

//遍历
void redun_link(poslink *hald)
{

     if(hald->frunt->next==NULL)                              
     {                                                       
         printf("链表为空");                                 
         return ;                                            
     }                                                       
     linkequeue*p=hald->frunt->next;                                  
     while(p->next!=NULL)                                    
     {                                                       
         p=p->next;                                          
         printf("%d ",p->text.data);                        
     }                                                       
     printf("\n");                                           
     printf("长度%d\n",hald->frunt->text.len);               
 } 

头文件

#ifndef __LIANSHI_H__
#define __LIANSHI_H__

typedef int datatype;
union masg
{
	datatype data;
	int len;
};
typedef struct node
{
	union masg text;
	struct node* next;
}linkequeue;

typedef struct
{
	linkequeue* frunt;
	linkequeue* rear;
}poslink;

poslink* queue_like();

void poptext_make(poslink* heald,datatype unte);

void make_poptext(poslink* heald);

void redun_link(poslink*hald);
#endif
#include <stdio.h>
#include "./004.funcle.h"
int main(int argc, const char *argv[])
{

	poslink* max = queue_like();
	
	poptext_make(max,500);

	make_poptext(max);
	redun_link(max);
		return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值