• 队列的链表实现


      1 /*
      2   队列的链表实现。
      3   队列是一种在表头删除,表尾插入的表。
      4   队列的出队和入队操作要更新队尾和队头。
      5   对于带头结点的队列,当队尾指向头结点,队头指向NULL则队列为空对。 
      6 */
      7 
      8 /*接口头文件*/
      9 typedef int ElementType;
     10 #ifndef _QUEUE_H
     11 #define _QUEUE_H
     12 #include <stdbool.h>
     13 
     14 struct Node;
     15 typedef struct Node * PtrToNode;
     16 typedef PtrToNode Queue;
     17 typedef PtrToNode Position;
     18 
     19 /*操作集*/
     20 Queue CreateQueue( void );
     21 void MakeEmpty( Queue Q );
     22 bool IsEmpty( Queue Q );
     23 void Enqueue( ElementType X, Queue Q );
     24 void Dequeue( Queue Q );
     25 ElementType Front( Queue Q );
     26 ElementType FrontAndDequeue( Queue Q );
     27 void DisposeQueue( Queue Q );
     28 void PrintfQueue( Queue Q );
     29 
     30 #endif 
     31 
     32 
     33 /*接口实现*/
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include "queue.h"
     37 
     38 /*特定结构体定义*/
     39 struct Node
     40 {
     41     ElementType Element;
     42     Position Next;
     43     Position Front;   //队头 
     44     Position Rear;    //队尾 
     45 };
     46 
     47 /*创建队列*/
     48 Queue CreateQueue( void )
     49 {
     50     Queue Q;
     51     
     52     Q = ( Queue )malloc( sizeof( struct Node ) );
     53     if ( Q == NULL )
     54     {
     55        printf( "No Space!!!
    " );
     56        exit( 1 );
     57     }
     58     Q->Next = NULL;
     59     /*初始化为空队*/
     60     MakeEmpty( Q );
     61     
     62     return Q; 
     63 }
     64 
     65 /*使队列为空*/
     66 void MakeEmpty( Queue Q )
     67 {
     68     if ( ! IsEmpty( Q ) )
     69        DisposeQueue;
     70     
     71     Q->Front = NULL;
     72     Q->Rear = Q;
     73     Q->Element = 0;
     74 }
     75 
     76 bool IsEmpty( Queue Q )
     77 {
     78     return Q->Next == NULL;
     79 }
     80 
     81 void DisposeQueue( Queue Q )
     82 {
     83     Position Temp;
     84     Position P;
     85     
     86     P = Q->Next;
     87     Q->Next = NULL;
     88     
     89     while ( P != NULL )
     90     {
     91         Temp = P->Next;
     92         free( P );
     93         P = Temp;
     94     }
     95 }
     96 
     97 void Enqueue( ElementType X, Queue Q )
     98 {
     99     Position NewNode;
    100     
    101     NewNode = ( Queue )malloc( sizeof( struct Node ) );
    102     if ( NewNode == NULL )
    103     {
    104        printf( "No Space!!!
    " );
    105        exit( 1 );
    106     }
    107     else
    108     {
    109         NewNode->Element = X;
    110         NewNode->Next = NULL;
    111         
    112         if ( IsEmpty( Q ) )
    113            Q->Front = NewNode;
    114            
    115         Q->Rear->Next = NewNode;
    116         
    117         Q->Rear = NewNode;
    118         Q->Element++;
    119     }
    120 }
    121 
    122 void Dequeue( Queue Q )
    123 {
    124     if ( IsEmpty( Q ) )
    125     {
    126        printf( "Queue is empty
    " );
    127        exit( 1 );
    128     }
    129     else
    130     {
    131         if ( Q->Next->Next == NULL )
    132            Q->Rear = Q;
    133            
    134         Q->Front = Q->Front->Next;
    135         free( Q->Next );
    136         Q->Next = Q->Front;
    137         Q->Element--;
    138     }
    139 }
    140 
    141 ElementType Front( Queue Q )
    142 {
    143     if ( IsEmpty( Q ) )
    144     {
    145        printf( "Queue is empty
    " );
    146        exit( 1 );
    147     }
    148     else
    149         return Q->Front->Element;
    150 }
    151 
    152 ElementType FrontAndDequeue( Queue Q )
    153 {
    154     ElementType n;
    155     
    156     if ( IsEmpty( Q ) )
    157     {
    158        printf( "Queue is empty
    " );
    159        exit( 1 );
    160     }
    161     else
    162     {
    163         n = Q->Front->Element;
    164         if ( Q->Next->Next == NULL )
    165            Q->Rear = Q;
    166            
    167         Q->Front = Q->Front->Next;
    168         free( Q->Next );
    169         Q->Next = Q->Front;
    170         Q->Element--;
    171         return n;
    172     }
    173 }
    174 
    175 void PrintfQueue( Queue Q )
    176 {
    177     while ( ! IsEmpty( Q ) )
    178         printf( "%3d",FrontAndDequeue( Q ) );
    179     
    180     printf( "
    " );
    181 }
  • 相关阅读:
    apache域名跳转
    apache配置防盗链
    apache配置静态缓存
    apache通过rewrite限制某个目录
    apache不记录指定类型的访问日志
    apache访问日志
    apache用户认证
    apache访问控制
    apache禁止默认虚拟主机
    mysql主从复制架构配置
  • 原文地址:https://www.cnblogs.com/weixia-blog/p/7307426.html
Copyright © 2020-2023  润新知