Implementation of linked queue in data structure

In the previous article, we mentioned the basic concept and operation of queues. In this article, we use linked lists to implement queues.
To use the linked list, we find that both the head plug deletion and the tail plug deletion will involve traversing the linked list, so this is inevitable. Moreover, when processing the head and tail of the list, we should introduce a new pointer, one pointing to the head and the other pointing to the tail of the list, so that we can easily enter and leave the team.

Realization

//linkqueue.h

#pragma once

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

typedef char LinkQueueType;

typedef struct LinkQueueNode{
  LinkQueueType data;
  struct LinkQueueNode* next;
}LinkQueueNode;//Element nodes in linked list

typedef struct LinkQueue{
  LinkQueueNode* head;
  LinkQueueNode* tail;
}LinkQueue;//The two pointers here are the head and tail of the list. When initializing, create a pseudo node as the team head. We do not consider the data content of this pseudo node

void LinkQueueInit(LinkQueue* q);//Initialize chain queue

void LinkQueuePush(LinkQueue* q, LinkQueueType value);//Join the team

void LinkQueuePop(LinkQueue* q);//Team out

void DestroyLinkQueue(LinkQueue* q);//Destruction queue

int LinkQueueGetFront(LinkQueue* q, LinkQueueType* value);//Get team leader
//linksqueue.c

#include "linkqueue.h"

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

void LinkQueueInit(LinkQueue* q)//Initialize chain queue
{
  if(q == NULL)
  {
    return;
  }

  q->head = q->tail = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));//This is how to create a pseudo node
  if(q -> head == NULL)
  {
    return;
  }

  q->head->next = NULL;
}

void LinkQueuePush(LinkQueue* q, LinkQueueType value)//Join the team
{
  if(q == NULL)
  {
    return;
  }
  if(q->head == NULL)
  {
    return;
  }

  LinkQueueNode* cur = q->head;

  LinkQueueNode* new_node = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
  new_node->data = value;
  new_node->next = NULL;

  while(cur->next != NULL)
  {
    cur = cur->next;
  }
  cur->next = new_node;
  q->tail = new_node;
  return;
}

void LinkQueuePop(LinkQueue* q)//Team out
{
  if(q == NULL)
  {
    return;
  }
  if(q->head == NULL || q->head->next)
  {
    return;
  }

  LinkQueueNode* cur = q->head;
  LinkQueueNode* to_delete = cur->next;
  cur->next = to_delete->next;
  free(to_delete);
  to_delete = NULL;

  return;
}

void DestroyLinkQueue(LinkQueue* q)//Destruction queue
{
  if(q == NULL)
  {
    return;
  }
  if(q->head == NULL)
  {
    return;
  }

  while(q->head != NULL)
  {
    q->tail = q->head->next;
    free(q->head);
    q->head = q->tail;
  }
  return;
}

int LinkQueueGetFront(LinkQueue* q, LinkQueueType* value)//Get team leader
{
  if(q == NULL)
  {
    return -1;
  }
  if(q->head == NULL || q->head->next == NULL)
  {
    return -1;
  }

  *value = q->head->next->data;
  return 1;
}
//The test function is as follows


void LinkQueuePrint(LinkQueue* q)//Print the contents of the linked list to check whether the team has been successfully joined or out of the team, etc
{
  if(q == NULL)
  {
    return;
  }
  if(q->head->next == NULL)
  {
    return;
  }

  LinkQueueNode* cur = q->head->next;//Here we don't pay attention to the data Content, so print fromnextStart printing
  while(cur != NULL)
  {
    printf("%c ",cur->data);
    cur = cur->next;
  }
  printf("\n");
}

void TestPush()
{
  HEAD;
  LinkQueue q;
  LinkQueueInit(&q);
  LinkQueuePush(&q,'a');  
  LinkQueuePrint(&q);
  LinkQueuePush(&q,'b');  
  LinkQueuePrint(&q);
  LinkQueuePush(&q,'c');  
  LinkQueuePrint(&q);
  LinkQueuePush(&q,'d');  
  LinkQueuePrint(&q);
}

void TestPop()
{
  HEAD;
  LinkQueue q;
  LinkQueueInit(&q);
  LinkQueuePush(&q,'a');  
  LinkQueuePush(&q,'b');  
  LinkQueuePush(&q,'c');  
  LinkQueuePush(&q,'d');  
  LinkQueuePrint(&q);
  LinkQueuePop(&q);
  LinkQueuePrint(&q);
}

void TestGetFront()
{
  HEAD;
  LinkQueue q;
  LinkQueueType value;

  LinkQueueInit(&q);
  int ret = LinkQueueGetFront(&q,&value);
  printf("expected ret -1, actual ret %d\n",ret);
  LinkQueuePush(&q,'a');  
  LinkQueuePush(&q,'b');  
  LinkQueuePush(&q,'c');  
  LinkQueuePush(&q,'d');  
  LinkQueuePrint(&q);

  ret = LinkQueueGetFront(&q,&value);
  printf("expected ret 1, actual ret %d\n",ret);
  printf("expected value a, actual value %c\n",value);
}

int main()
{
  TestPush();
  TestPop();
  TestGetFront();

  printf("\n");
  printf("\n");
  printf("\n");
  printf("\n");
  return 0;
}

Welcome to discuss together. If there is any mistake, please contact the author to point out and correct it. Thank you!

Posted by tnkannan on Wed, 01 Apr 2020 11:43:07 -0700