Basic queue operations

Source: Internet
Author: User

Two types of queue storage structures are available: Linear table Storage and chained storage. When using a linear table for storage, note whether the queue length exceeds the preset size. In this program, the queue length can be automatically increased when the queue is full. If you use a linked list for storage, there is no length limit. The following describes the implementation of these two storage structures.

Linear table Storage:

# Include
 
  
# Include
  
   
# Define QUEUE_INIT_SIZE 10 # define QUEUE_REALLOCATION 2 typedef int ElemType;/* Storage Structure Definition of ordered queues */typedef struct SqQueue {ElemType * base; int front; int rear; int QueueLength ;} sqQueue; void Init_SqQueue (SqQueue * S) {S-> base = (ElemType *) malloc (QUEUE_INIT_SIZE * sizeof (ElemType); if (! S-> base) exit (1); S-> rear = S-> front = 0; S-> QueueLength = QUEUE_INIT_SIZE;} void En_SqQueue (SqQueue * S, ElemType e) {if (S-> rear + 1) % S-> QueueLength = S-> front) {S-> base = (ElemType *) malloc (S-> base, (S-> QueueLength + QUEUE_REALLOCATION) * sizeof (ElemType); if (! S-> base) exit (1); if (S-> front> S-> rear) {int I; for (I = S-> front; I <S-> QueueLength; ++ I) {S-> base [I + QUEUE_REALLOCATION] = S-> base [I];} S-> front + = QUEUE_REALLOCATION ;} s-> QueueLength + = QUEUE_REALLOCATION;} S-> base [S-> rear] = e; S-> rear = (S-> rear + 1) % S-> QueueLength;} int De_SqQueue (SqQueue * S, ElemType * e) {if (S-> rear = S-> front) return 0; * e = S-> base [S-> front]; S-> front = (S-> front + 1) % S-> Queue Length; return 1;} int SqQueueLength (SqQueue S) {return (S. rear-S. front + S. queueLength) % S. queueLength;} int is_SqQueueEmpty (SqQueue S) {if (S. front = S. rear) return 1; elsereturn 0;} void SqQueueTest () {SqQueue S; Init_SqQueue (& S); printf ("Please input some numbers to enqueue (Ctrl + Z to end ): \ n "); int e; while (scanf (" % d ", & e )! = EOF) En_SqQueue (& S, e); printf ("The dequeue sequence is: \ n"); while (! Is_SqQueueEmpty (S) {De_SqQueue (& S, & e); printf ("% d", e);} printf ("\ n");} int main () {SqQueueTest (); return 0 ;}
  
 


Linked List storage:

# Include
 
  
# Include
  
   
/* Storage Structure Definition of chain queue */typedef struct LinkQueue_Node {ElemType data; struct LinkQueue_Node * next;} * Queue_pNode, Queue_Node; typedef struct LinkQueue {struct LinkQueue_Node * front; struct LinkQueue_Node * rear;} LinkQueue; void Init_LinkQueue (LinkQueue * L) {L-> front = L-> rear = (Queue_pNode) malloc (sizeof (Queue_Node); if (! L-> front) exit (1); L-> front-> data = 0; L-> front-> next = NULL;} void En_LinkQueue (LinkQueue * L, elemType e) {Queue_pNode p = (Queue_pNode) malloc (sizeof (Queue_Node); if (! P) exit (1); p-> data = e; p-> next = NULL; L-> rear-> next = p; L-> rear = p ;} int De_LinkQueue (LinkQueue * L, ElemType * e) {Queue_pNode p = L-> front-> next; if (L-> front = L-> rear) return 0; * e = p-> data; L-> front-> next = p-> next; if (p = L-> rear) l-> rear = L-> front; free (p); return 1;} int LinkQueueLength (LinkQueue L) {Queue_pNode p = L. front; if (L. front = L. rear) return 0; int length = 0; while (p! = L. rear) {++ length; p = p-> next;} return length;} int is_LinkQueueEmpty (LinkQueue L) {if (L. front = L. rear) return 1; elsereturn 0;} void LinkQueueTest () {LinkQueue L; Init_LinkQueue (& L); printf ("Please input some numbers to enqueue (Ctrl + Z to end ). \ n "); int e; while (scanf (" % d ", & e )! = EOF) En_LinkQueue (& L, e); printf ("The dequeue sequence is: \ n"); while (! Is_LinkQueueEmpty (L) {De_LinkQueue (& L, & e); printf ("% d", e);} printf ("\ n");} int main () {LinkQueueTest (); return 0 ;}
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.