队列的实现(顺序表和链表)

本文介绍了队列的基本概念,强调了其先进先出的特性,并详细阐述了如何使用顺序表和链表来实现队列,包括创建、入队、出队、获取队首元素和销毁队列等操作。同时,提供了两种具体实现方式的概述。

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

队列
这里写图片描述

队列其最主要的特点就是先进先出,因此在用顺序表和链表实现它的时候就要注意,它的入栈和出栈就要使用头插和尾删或者尾插和头插,他们的对应关系不能搞错。

队列实现有以下动作:

  1. 创建队列
  2. 入队列
  3. 出队列
  4. 取队列首元素
  5. 销毁队列

队列的两种实现:

顺序表实现:

//SqQueue.h
#pragma once                                                                                                                                                                                   

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

#define MAXSIZE 1000

typedef char SqQueueType;

typedef struct SqQueue{
    SqQueueType data[MAXSIZE];
    size_t size;
    size_t head;
    size_t tail;
}SqQueue;
//初始化
void SqQueueInit(SqQueue* sq);
//入队列
void SqQueuePush(SqQueue* sq,SqQueueType value);
//出队列
void SqQueuePop(SqQueue* );
//取队列首元素
int SqQueueFront(SqQueue* sq, SqQueueType* value);
//销毁队列
void SqQueueDestroy(SqQueue* sq);

//SqQueue.c

  #include "SqQueue.h"

  void SqQueueInit(SqQueue* sq){
      if(sq == NULL){
          return ;//非法输入
      }   
      sq -> head = 0;
      sq -> tail = 0;
      sq -> size = 0;
  }

  void SqQueuePush(SqQueue* sq, SqQueueType value){
      if(sq == NULL){
          return ;//非法输入
      }   
      if(sq -> size >= MAXSIZE){
          return;//队列满
      }   
      sq -> data[sq -> tail++] = value;
      if(sq -> tail >= MAXSIZE){
          sq -> tail = 0;
      }   
      //q -> tail %= MAXSIZE;
      ++sq -> size;
      return;
  }
  void SqQueuePop(SqQueue* sq){
      if(sq == NULL){
          return ;//非法输入
      }
      if(sq -> size == 0){
          return ;//空队列
      }
      ++sq -> head;
      if(sq -> head >= MAXSIZE){
          sq -> head = 0;
      }
      --sq -> size;
      return;
  }


  int SqQueueFront(SqQueue* sq, SqQueueType* value){
      if(sq == NULL){
          return 0;
      }
      if(sq -> size == 0){
          return 0;
      }
      *value = sq -> data[sq -> head];
      return 1;
  }

  void SqQueueDestroy(SqQueue* sq){
      if(sq == NULL){
          return;//非法输入
      }   
      sq -> head = 0;
      sq -> tail = 0;
      sq -> size = 0;

  }
  ///////////////////////////////////////
  /////////////测试代码//////////////////
  /////////////////////////////////////

  #define TESTHEADER printf("##############%s############\n",__FUNCTION__)

  void PrintChar(SqQueue* sq,char* msg){
      if(sq == NULL){
          return;//非法输入
      }   
      printf("%s\n",msg);
      if(sq -> size == 0){ 
          return;//队列满
      }   
      size_t i =0; 
      for(; i < sq -> size; i++){
          printf("%c  ",sq -> data[i]);
      }                                                                                                                                                                                        
      printf("\n");
  }

  void TestInit(){
      TESTHEADER;
      SqQueue sq;
      SqQueueInit(&sq);
      printf("head excepted 0,actual %lu\n",sq.head);
      printf("tail excepted 0,actual %lu\n",sq.tail);
      printf("size excepted 0,actual %lu\n",sq.size);
  }

  void TestSqQueuePush(){
      TESTHEADER;
      SqQueue sq;
      SqQueueInit(&sq);
      SqQueuePush(&sq,'a');
      SqQueuePush(&sq,'b');
      SqQueuePush(&sq,'c');
      SqQueuePush(&sq,'d');
      PrintChar(&sq,"入队四个元素");
  }
  void TestSqQueuePop(){
      TESTHEADER;
      SqQueue sq;
      SqQueueInit(&sq);
      SqQueuePush(&sq,'a');
      SqQueuePush(&sq,'b');
      SqQueuePush(&sq,'c');
      SqQueuePush(&sq,'d');
      PrintChar(&sq,"入队四个元素");
      SqQueuePop(&sq);
      PrintChar(&sq,"出队一个元素");
      SqQueuePop(&sq);
      PrintChar(&sq,"出队一个元素");
      SqQueuePop(&sq);
      PrintChar(&sq,"出队一个元素");
      SqQueuePop(&sq);
      PrintChar(&sq,"尝试对空队列出队列");
  }
  void TestSqQueueFront(){

      TESTHEADER;
      SqQueue sq;
      SqQueueInit(&sq);
      SqQueuePush(&sq,'a');
      SqQueuePush(&sq,'b');
      SqQueuePush(&sq,'c');
      SqQueuePush(&sq,'d');
      PrintChar(&sq,"入队四个元素");
      SqQueueType value;
      int ret = SqQueueFront(&sq, &value);
      printf("ret excepted 1, actual %d\n",ret);
      printf("ret excepted a, actual %c\n",value);
  }
  int main(){
      TestInit();
      TestSqQueuePush();
      TestSqQueuePop();
      TestSqQueueFront();
      return 0;
  }

链表的实现:

//ListQueue.h
#pragma once                                                                                                                                                                                   

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

typedef char ListQueueType;

typedef struct ListQueueNode{
    ListQueueType data;
    struct ListQueueNode* next;
}ListQueueNode;
//初始化
void ListQueueInit(ListQueueNode* head);
//入队列
void ListQueuePush(ListQueueNode* head, ListQueueType value);
//出队列
void ListQueuePop(ListQueueNode* head);
//取队列首元素
int ListQueueTop(ListQueueNode* head,ListQueueType* value);
//销毁队列
void ListQueueDestroy(ListQueueNode* head);

//ListQueue.c
  #include "ListQueue.h"                                                                                                                                                                       

  ListQueueNode* CreateNode(ListQueueType value){
      ListQueueNode* new_node = (ListQueueNode*)malloc(sizeof(ListQueueNode*));
      new_node -> next = NULL;
      new_node -> data = value;
      return new_node;
  }
  void ListQueueInit(ListQueueNode* head){
      if(head == NULL){
          return;//非法输入
      }
      head -> next = NULL;    
  }
  void ListQueuePush(ListQueueNode* head, ListQueueType value){
      if(head == NULL){
          return;
      }
      ListQueueNode* new_node = CreateNode(value);
      ListQueueNode* next = head -> next;
      head -> next = new_node;
      new_node -> next = next;
  }
  void ListQueueNodeDestroy(ListQueueNode* node){
      free(node);
      node = NULL;
  }

  void ListQueuePop(ListQueueNode* head){
      if(head == NULL){
          return;
      }
      ListQueueNode* cur = head;
      if(cur -> next == NULL){
          return;
      }
      for(; cur -> next -> next != NULL; cur = cur -> next){
      }
      cur -> next = NULL;
      ListQueueNodeDestroy(cur -> next);
  }

  int ListQueueTop(ListQueueNode* head,ListQueueType* value){
      if(head == NULL){
          return 0;
      }
      ListQueueNode* cur = head;
      for(; cur -> next -> next != NULL; cur = cur -> next){

      }
      *value = cur -> next -> data;
      return 1;
  }
  void ListQueueDestroy(ListQueueNode* head){
      if(head == NULL){
          return;
      }
      ListQueueNode* cur = head -> next;
      for(; cur -> next != NULL; cur = cur -> next){
          ListQueueNodeDestroy(cur);    
      }
  }
  /////////////////////////////////////////
  //////////////测试代码///////////////////
  ///////////////////////////////////////

  #define TESTHEADER printf("###############%s#############\n",__FUNCTION__)

  void PrintfChar(ListQueueNode* head, char* msg){
      printf("%s",msg);
      if(head == NULL){
          return;
      }
      ListQueueNode* cur = head -> next;
      printf("NULL");
      for(; cur != NULL; cur = cur -> next){
          printf("<-[%c]|[%p]",cur -> data, cur);
      }
      printf("\n");
  }
  void TestInit(){
      TESTHEADER;
      ListQueueNode head;
      ListQueueInit(&head);

  }

  void TestPush(){
      TESTHEADER;
      ListQueueNode head;
      ListQueueInit(&head);
      ListQueuePush(&head,'a');
      ListQueuePush(&head,'b');
      ListQueuePush(&head,'c');
      ListQueuePush(&head,'d');
      PrintfChar(&head,"入队四个元素\n");
  }

  void TestPop(){

      TESTHEADER;
      ListQueueNode head;
      ListQueueInit(&head);
      ListQueuePush(&head,'a');
      ListQueuePush(&head,'b');
      ListQueuePush(&head,'c');
      ListQueuePush(&head,'d');
      PrintfChar(&head,"入队四个元素\n");
      ListQueuePop(&head);
      ListQueuePop(&head);
      PrintfChar(&head,"出队两个元素\n");
      ListQueuePop(&head);
      ListQueuePop(&head);
      PrintfChar(&head,"出队两个元素\n");
      ListQueuePop(&head);
      PrintfChar(&head,"对空队列进行出队列元素\n");
  }
  void TestTop(){
      TESTHEADER;
      ListQueueNode head;
      ListQueueInit(&head);
      ListQueuePush(&head,'a');
      ListQueuePush(&head,'b');
      ListQueuePush(&head,'c');
      ListQueuePush(&head,'d');
      PrintfChar(&head,"入队四个元素\n");
      ListQueueType value;
      int ret = ListQueueTop(&head,&value);
      printf("ret excepted 1,actual %d\n",ret);
      printf("value excepted a, actual %c\n",value);
  }
  int main(){
      TestPush();
      TestPop();
      TestTop();
      return 0;
  }

以上就是队列的实现,如果有神马错误,请各位大神联系下我这个小虾米。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值