重学数据结构:队列的链式实现的各种操作(C语言)

本文详细介绍了链式队列的数据结构,包括有头节点和无头节点的定义、初始化、操作(如入队、出队、判断队空、获取队头)以及对应的测试代码和结果。展示了如何使用C语言实现并演示了这两种类型的队列操作。

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

队列的链式定义

typedef struct LNode
{
   struct LNode *next;
   int data;
}LNode;

typedef struct
{
   LNode *front,*rear;
}LinkQueue;

队列的链式实现分类

分为有头节点和无头节点

有头节点的链式队列

链式队列初始化(有头节点)
void InitLinkQueue(LinkQueue *Q)
{
    Q->front=Q->rear=(LNode *)malloc(sizeof(LNode));
    Q->front->next=NULL;
}

判断队空(有头节点)
bool IsLinkQueueEmpty(LinkQueue *Q)
{
    if(Q->front==Q->rear)
        return true;
    else
        return false;
}
链式队列的创建(有头节点)
void CreateLinkQueue(LinkQueue *Q)
{
    int data;
    printf("请输入整数:");
    scanf("%d",&data);
    bool ishead=true;
    while(data!=9999)
    {
       LNode *newNode=(LNode *)malloc(sizeof(LNode));
       newNode->data=data;
       newNode->next=NULL;
     
       if(ishead)
       {
           Q->front->next=newNode;
           ishead=false;
       }else
       {
           Q->rear->next=newNode;
       }
         Q->rear=newNode;
       printf("请输入整数:");
       scanf("%d",&data);
    }

}
打印链式队列(有头节点)
void PrintLinkQueue(LinkQueue *Q)
{
     LNode *node=Q->front->next;
     int i=1;
     while(node!=NULL)
     {
         printf("第%d位:%d\n",i,node->data);
         node=node->next;
         i++;
     }
}
入队(有头节点)
bool EnLinkQueue(LinkQueue *Q,int e)
{
    
    LNode *newNode=(LNode *)malloc(sizeof(LNode));
    newNode->data=e;
    newNode->next=NULL;
    
    Q->rear->next=newNode;
    Q->rear=newNode;
    
    return true;
}
出队(有头节点)
int DeLinkQueue(LinkQueue *Q)
{
    if(IsLinkQueueEmpty(Q))return -1;
    
    LNode *newNode=Q->front->next;
    Q->front->next=newNode->next;
    int e=newNode->data;
    if(newNode==Q->rear)//如果是最后一个节点出队(第一次没写出来)
        Q->rear=Q->front;
    free(newNode);
    return e;
    
}

获取队头元素(有头节点)
int GetTopLinkQueue(LinkQueue *Q)
{
    if(IsLinkQueueEmpty(Q))return -1;
    LNode *newNode=Q->front->next;
    int e=newNode->data;
    return e;
}
测试代码

int main(int argc, const char * argv[])
{    
    LinkQueue *Q=(LinkQueue *)malloc(sizeof(LinkQueue));
    InitLinkQueue(Q);
    CreateLinkQueue(Q);
    PrintLinkQueue(Q);
    
    EnLinkQueue(Q,3);
    printf("EnLinkQueue(Q,3)\n");
    PrintLinkQueue(Q);
    
    DeLinkQueue(Q);
    printf("DeLinkQueue(Q)\n");
    PrintLinkQueue(Q);
    
    int e=GetTopLinkQueue(Q);
    printf("GetTopLinkQueue(Q)=%d\n",e);
    return 0;
}

测试结果
请输入整数:11
请输入整数:12
请输入整数:13
请输入整数:14
1请输入整数:15
请输入整数:9999
第1位:11
第2位:12
第3位:13
第4位:14
第5位:115
EnLinkQueue(Q,3)
第1位:11
第2位:12
第3位:13
第4位:14
第5位:115
第6位:3
DeLinkQueue(Q)
第1位:12
第2位:13
第3位:14
第4位:115
第5位:3
GetTopLinkQueue(Q)=12
Program ended with exit code: 0

无头节点的链式队列

链式队列初始化(无头节点)
void InitLinkQueue_WithoutHead(LinkQueue *Q)
{
    Q->front=Q->rear=NULL;
}

判断队空(无头节点)
bool IsLinkQueueEmpty_WithoutHead(LinkQueue *Q)
{
    if(Q->front==NULL)
        return true;
    else
        return false;
}

链式队列的创建(无头节点)
void CreateLinkQueue_WithoutHead(LinkQueue *Q)
{
    int data;
    printf("请输入整数:");
    scanf("%d",&data);
    bool ishead=true;
    while(data!=9999)
    {
       LNode *newNode=(LNode *)malloc(sizeof(LNode));
       newNode->data=data;
       newNode->next=NULL;
     
       if(ishead)
       {
           Q->front=newNode;
           ishead=false;
       }else
       {
           Q->rear->next=newNode;
       }
         Q->rear=newNode;
       printf("请输入整数:");
       scanf("%d",&data);
    }

}
打印链式队列(无头节点)
void PrintLinkQueue_WithoutHead(LinkQueue *Q)
{
     LNode *node=Q->front;
     int i=1;
     while(node!=NULL)
     {
         printf("第%d位:%d\n",i,node->data);
         node=node->next;
         i++;
     }
}
入队(无头节点)
bool EnLinkQueue_WithoutHead(LinkQueue *Q,int e)
{
    
    LNode *newNode=(LNode *)malloc(sizeof(LNode));
    newNode->data=e;
    newNode->next=NULL;
    
    Q->rear->next=newNode;
    Q->rear=newNode;
    
    return true;
}
出队(无头节点)
int DeLinkQueue_WithoutHead(LinkQueue *Q)
{
    if(IsLinkQueueEmpty_WithoutHead(Q))return -1;
    
    LNode *newNode=Q->front;
    Q->front=newNode->next;
    int e=newNode->data;
    if(newNode==NULL)//如果是最后一个节点出队(第一次没写出来)
        Q->rear=Q->front=NULL;
    free(newNode);
    return e;
    
}
获取队头元素(无头节点)
int GetTopLinkQueue_WithoutHead(LinkQueue *Q)
{
    if(IsLinkQueueEmpty_WithoutHead(Q))return -1;
    LNode *newNode=Q->front;
    int e=newNode->data;
    return e;
}
测试代码
 LinkQueue *Q=(LinkQueue *)malloc(sizeof(LinkQueue));
    InitLinkQueue_WithoutHead(Q);
    CreateLinkQueue_WithoutHead(Q);
    PrintLinkQueue_WithoutHead(Q);
    
    EnLinkQueue_WithoutHead(Q,3);
    printf("EnLinkQueue_WithoutHead(Q,3)\n");
    PrintLinkQueue_WithoutHead(Q);
    
    DeLinkQueue_WithoutHead(Q);
    printf("DeLinkQueue_WithoutHead(Q)\n");
    PrintLinkQueue_WithoutHead(Q);
    
    int e=GetTopLinkQueue_WithoutHead(Q);
    printf("GetTopLinkQueue_WithoutHead(Q)=%d\n",e);
    return 0;
测试数据
请输入整数:11
请输入整数:12
请输入整数:13
请输入整数:14
1请输入整数:15
请输入整数:9999
第1位:11
第2位:12
第3位:13
第4位:14
第5位:115
EnLinkQueue_WithoutHead(Q,3)
第1位:11
第2位:12
第3位:13
第4位:14
第5位:115
第6位:3
DeLinkQueue_WithoutHead(Q)
第1位:12
第2位:13
第3位:14
第4位:115
第5位:3
GetTopLinkQueue_WithoutHead(Q)=12
Program ended with exit code: 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值