链式队列小结

本文介绍了一种基于链表实现的队列数据结构,并详细解释了创建队列、入队、出队等核心操作的实现过程及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 队列的特性是先进先出;最小单元是一个节点。包含了datatype和next,其中datatype是可以自定义的结构体,包含了多种类型的数据。
2 对队列有队尾指针和队头指针进行封装。后面的操作是对他进行操作。
3 函数的值返回一种是通过函数的返回值进行返回,另外一种是函数的输入传入指针,对这个指针进行操作
4 注意在函数体内,对函数进行的指针操作,特特别注意L->front 在函数里面进行了赋值,此时也会发生变化,因此需要有一个临时变量。

例如说这个函数:

   plinknode Q= L->front;
   while(Q->next!=NULL)
   {
         printf("%d ",Q->next->data);
      Q =Q->next ;
   }
   puts("\n");

虽然在函数里面,如果这样子写
while(L>front->next !=NULL)
{
      L->front =L->front->next //注意此时的头节点以及被改变了。         
} 

 

 

 

 

#include "linkqueue.h"
/*
 *
*/
int main(int argc, const char *argv[])
{
    plinkqueue L;
    int tmp ,value,out_queue_value;
    L=creat_queue();
    printf("creat queue ok\n");
#if 0
    int i= 0;
    for(i= 0 ; i<4 ;i++)
    {
        in_queue(L,i);
    }

    puts("\n");
    printf("%p %p \n",L->front ,L->rear);

    show_queue(L);
    printf("%p %p \n",L->front ,L->rear);

    out_queue(L,&tmp);
    printf("out_queue value is %d \n",tmp);    
    out_queue(L,&tmp); 
    printf("out_queue value is %d \n",tmp);    
    printf("%p %p \n",L->front ,L->rear);
    show_queue(L);
#endif 
   while(1)
   {
       printf("please input data \n");
    value = scanf("%d",&tmp);
    if(value == 1)
    {
        printf("in_queue :");
        in_queue(L,tmp) ;
        show_queue(L);
    }
    else
    {
        printf("out_queue ");
        out_queue(L,&out_queue_value);
        show_queue(L);
        //getchar();
        while(getchar()!='\n');
    }
   
   
   }

    return 0;
}
#include "linkqueue.h"

plinkqueue creat_queue(void)
{
	plinkqueue L;
	L=(plinkqueue)malloc(sizeof(linkqueue));
	if(L==NULL)
	{
		printf("creat fail\n");
		return NULL;
	}

	L->front =(plinknode)malloc(sizeof(linknode));
	if(L->front ==NULL)
	{
		 printf("creat node fail \n");
		 return NULL;
	}

	L->rear =L->front;
	L->front->next =NULL;
	return L;
}
void show_queue(plinkqueue L)
{
   plinknode Q= L->front;
   while(Q->next!=NULL)
   {
   	  printf("%d ",Q->next->data);
	  Q =Q->next ;
   }
   puts("\n");
}
int in_queue(plinkqueue L, datatype data )
{
	plinknode Q;
	Q=(plinknode)malloc(sizeof(linknode));
	if(Q==NULL)
	{
		printf("in_queue fail \n");
		return 0 ;
	}
    
	Q->data = data;
	Q->next =NULL;
	L->rear->next =Q;

	L->rear =Q;
//	printf("%p->",L->rear);

	return 1;
 
}

int out_queue(plinkqueue L,datatype *data)
{
	if(is_queue_empty(L) == 0)
	{
		printf("queue is empty \n");
		return 0 ;

	}

	plinknode tmp ;
	
	tmp =L->front;

	*data =tmp->next->data ;
	//printf("--%d-- \n" ,tmp->next->data);

	
	L->front =L->front->next ;

 	free(tmp);
	
	return 1;
}

int is_queue_empty(plinkqueue L)
{
  if(L->front == L->rear)
	  return 0;
  else
	  return 1 ;
}

  

#ifndef __LINKQUEUE_H__
#define __LINKQUEUE_H__

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

typedef int datatype ;

typedef struct node
{
    datatype data ;
    struct node *next ;
}linknode ,*plinknode ;


typedef struct queue
{
    plinknode front ;
    plinknode rear ;
}linkqueue,*plinkqueue;


plinkqueue creat_queue(void);
void show_queue(plinkqueue L);
int in_queue(plinkqueue L, datatype data );

int out_queue(plinkqueue L,datatype *data);
int is_queue_empty(plinkqueue L);



#endif 

 

转载于:https://www.cnblogs.com/jack-hzm/p/10589200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值