链式队列的实现(头文件及源程序)
Linkedqueue.h
#ifndef __LINKEDQUEUE_H__
#define __LINKEDQUEUE_H__
typedef int ElemType_Queue;
typedef struct Node_Queue
{ ElemType_Queue data; struct Node_Queue * next; } Node_Queue;
typedef struct Linkedqueue
{ int num; Node_Queue * rear; Node_Queue * front;
} Linkedqueue;
Linkedqueue * Init_Linkedqueue ( ) ;
Node_Queue * Create_New_Node_Queue ( ElemType_Queue value) ;
void push_Queue ( Linkedqueue * linkedqueue, ElemType_Queue value) ;
void pop_Queue ( Linkedqueue * linkedqueue) ;
ElemType_Queue get_QueueHead ( Linkedqueue * linkedqueue) ;
int empty_Queue ( Linkedqueue * linkedqueue) ;
void destroy_Queue ( Linkedqueue * linkedqueue) ;
int length_Queue ( Linkedqueue * linkedqueue) ; #endif
Linkedqueue.c
#include <stdio.h>
#include <stdlib.h>
#include "linkedqueue.h"
Linkedqueue * Init_Linkedqueue ( )
{ Linkedqueue * linkedqueue = malloc ( sizeof ( Linkedqueue) ) ; linkedqueue-> num = 0 ; linkedqueue-> rear = NULL ; linkedqueue-> front = NULL ; return linkedqueue;
}
Node_Queue * Create_New_Node_Queue ( ElemType_Queue value)
{ Node_Queue * new_node = malloc ( sizeof ( Node_Queue) ) ; new_node-> data = value; new_node-> next = NULL ; return new_node;
}
void push_Queue ( Linkedqueue * linkedqueue, ElemType_Queue value)
{ if ( linkedqueue == NULL ) { printf ( "队列不存在!\n" ) ; return ; } Node_Queue * new_node = Create_New_Node_Queue ( value) ; if ( linkedqueue-> num == 0 ) { linkedqueue-> rear = new_node; linkedqueue-> front = new_node; } else { linkedqueue-> rear-> next = new_node; linkedqueue-> rear = new_node; } linkedqueue-> num++ ;
}
void pop_Queue ( Linkedqueue * linkedqueue)
{ if ( linkedqueue == NULL ) { printf ( "队列不存在!\n" ) ; return ; } Node_Queue * p = linkedqueue-> front; linkedqueue-> front = p-> next; free ( p) ; linkedqueue-> num-- ;
}
void destroy_Queue ( Linkedqueue * linkedqueue)
{ if ( linkedqueue == NULL ) { printf ( "队列不存在!\n" ) ; return ; } else { while ( linkedqueue-> num != 0 ) { pop_Queue ( linkedqueue) ; } linkedqueue = NULL ; }
}
ElemType_Queue get_QueueHead ( Linkedqueue * linkedqueue)
{ if ( linkedqueue == NULL ) { printf ( "队列不存在!\n" ) ; return - 1 ; } if ( linkedqueue-> num == 0 ) { printf ( "队已空,获取队头元素失败!" ) ; return 0 ; } else { return linkedqueue-> front-> data; }
}
int length_Queue ( Linkedqueue * linkedqueue)
{ if ( linkedqueue == NULL ) { printf ( "队列不存在!\n" ) ; return 0 ; } return linkedqueue-> num;
}
int empty_Queue ( Linkedqueue * linkedqueue)
{ if ( linkedqueue == NULL ) { return - 1 ; } if ( linkedqueue-> num == 0 ) { return 1 ; } else { return 0 ; }
}