链式队列小结

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("
");

虽然在函数里面,如果这样子写
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
");
#if 0
    int i= 0;
    for(i= 0 ; i<4 ;i++)
    {
        in_queue(L,i);
    }

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

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

    out_queue(L,&tmp);
    printf("out_queue value is %d 
",tmp);    
    out_queue(L,&tmp); 
    printf("out_queue value is %d 
",tmp);    
    printf("%p %p 
",L->front ,L->rear);
    show_queue(L);
#endif 
   while(1)
   {
       printf("please input data 
");
    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()!='
');
    }
   
   
   }

    return 0;
}
#include "linkqueue.h"

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

	L->front =(plinknode)malloc(sizeof(linknode));
	if(L->front ==NULL)
	{
		 printf("creat node fail 
");
		 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("
");
}
int in_queue(plinkqueue L, datatype data )
{
	plinknode Q;
	Q=(plinknode)malloc(sizeof(linknode));
	if(Q==NULL)
	{
		printf("in_queue fail 
");
		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 
");
		return 0 ;

	}

	plinknode tmp ;
	
	tmp =L->front;

	*data =tmp->next->data ;
	//printf("--%d-- 
" ,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