Design and Implementation of Chain Storage Structure of Queue
1. Concept
A queue is also a special kind of linear table; the chained storage of a queue can be emulated by the chained storage of a linear table.
2. Realize
(1)linkqueue.h
#ifndef _MY_LINKQUEUE_H_ #define _MY_LINKQUEUE_H_ typedef void LinkQueue; LinkQueue* LinkQueue_Create(); void LinkQueue_Destory(LinkQueue* queue); void LinkQueue_Clear(LinkQueue* queue); int LinkQueue_Append(LinkQueue* queue, void* item); void* LinkQueue_Retrieve(LinkQueue* queue); void* LinkQueue_Header(LinkQueue* queue); int LinkQueue_Length(LinkQueue* queue); #endif
(2) linkqueue.c ( linklist.h link )
#include <stdlib.h> #include <stdio.h> #include <string.h> #include "linkqueue.h" #include "linklist.h" // queues are special linear lists //Data structure of the business node of the queue typedef struct _tag_LinkQueueNode { LinkListNode node; void* item; }TLinkQueueNode; //Creating a queue of chained storage is equivalent to creating a linear table of chained storage LinkQueue* LinkQueue_Create() { return LinkList_Create(); } //Destroying the queue is equivalent to destroying the linear table //Involves the memory management of the node void LinkQueue_Destory(LinkQueue* queue) { LinkQueue_Clear(queue); LinkList_Destory(queue); } //Clearing the queue is equivalent to clearing the linear table //Involving memory management of nodes, each node needs to be released void LinkQueue_Clear(LinkQueue* queue) { while (LinkQueue_Length(queue) > 0) { LinkQueue_Retrieve(queue); } LinkList_Clear(queue); } //Adding an element to the queue is equivalent to adding an element to the end of the linear list int LinkQueue_Append(LinkQueue* queue, void* item) { int ret = 0; TLinkQueueNode *tmp = NULL; tmp = (TLinkQueueNode *)malloc(sizeof(TLinkQueueNode)); if (tmp == NULL) { ret = -1; printf("func LinkQueue_Append() malloc() err:%d\n", ret); return ret; } memset(tmp, 0, sizeof(TLinkQueueNode)); tmp->item = item; //Need to convert the item of the stack (the business node of the stack) into the business node LinkListNode of the linked list ret = LinkList_Insert(queue, (LinkListNode*)tmp, LinkList_Length(queue)); if (ret! = 0) { printf("func LinkList_Insert() err:%d\n", ret); if (tmp != NULL) { free(tmp); } return ret; } return ret; } //Removing an element from the queue is equivalent to removing an element from the head of the linear list void* LinkQueue_Retrieve(LinkQueue* queue) { TLinkQueueNode *tmp = NULL; void *ret = NULL;//The business node of the queue tmp = (TLinkQueueNode *)LinkList_Delete(queue, 0); if (tmp == NULL) { printf("func LinkQueue_Retrieve() LinkList_Delete() err!\n"); return NULL; } // cache before deleting ret = tmp->item; if (ret != NULL) { free(tmp); } return ret; } //Getting the head element of the queue is equivalent to taking data from position 0 of the linear table void* LinkQueue_Header(LinkQueue* queue) { TLinkQueueNode *tmp = NULL; tmp = (TLinkQueueNode *)LinkList_Get(queue, 0); if (tmp == NULL) { printf("func LinkQueue_Header() LinkList_Get() err!"); return NULL; } return tmp->item; } // Finding the length of the queue is equivalent to finding the length of the linear table int LinkQueue_Length(LinkQueue* queue) { return LinkList_Length(queue); }
(3) concrete realization
#include <stdlib.h> #include <stdio.h> #include <string.h> #include "linkqueue.h" void maindm009() { int ret = 0; int a[10]; //create queue LinkQueue* queue = NULL; queue = LinkQueue_Create(); if (queue == NULL) { ret = -1; printf("func LinkQueue_Create() err:%d\n", ret); return ; } //insert element printf("The Queue is: "); for (int i = 0; i < 5; i++) { a[i] = i + 1; ret = LinkQueue_Append(queue, &a[i]); printf("%d ", ret); } printf("\n"); // print queue length printf("The Queue's Length:%d\n", LinkQueue_Length(queue)); // print the head element of the queue printf("The Queue's Header:%d\n", *((int*)LinkQueue_Header(queue))); // dequeue while (LinkQueue_Length(queue) > 0) { int tmp = *((int *)LinkQueue_Retrieve(queue)); printf("tmp:%d ", tmp); } printf("\n"); //destroy the queue LinkQueue_Destory(queue); system("pause"); return; }