Sequential queue storage implementation

Source: Internet
Author: User


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> # include # Include # Include Typedef struct Node {int data; char ch;} * PNODE, NODE; typedef struct QUEUE {int maxsize; int front; int rare; PNODE pBase;} * PQUEUE, QUEUE; // initialize void init (PQUEUE pQ, int length) {pQ-> maxsize = length; pQ-> pBase = (PNODE) malloc (sizeof (NODE) * length ); pQ-> front = pQ-> rare = 0; while (! PQ-> pBase) {printf ("memory allocation failed! \ N "); exit (-1);} return;} // enter the void enqueue (PQUEUE pQ, int val, char ch) {if (pQ-> rare + 1) % (pQ-> maxsize + 1) = pQ-> front) {printf ("the queue is full! % D, % c cannot join \ n ", val, ch); return;} else {pQ-> pBase [pQ-> rare % pQ-> maxsize]. data = val; pQ-> pBase [pQ-> rare % pQ-> maxsize]. ch = ch; printf ("element: % d, % c \ n", val, ch); pQ-> rare ++;} return ;} // traverse void traverse (PQUEUE pQ) {for (int I = pQ-> front; I <pQ-> rare; I ++) {printf ("% d, % c \ n ", pQ-> pBase [I % pQ-> maxsize]. data, pQ-> pBase [I % pQ-> maxsize]. ch);} return;} // void dequeue (PQUEUE pQ) {printf ("element: % d, % c \ n ", pQ-> pBase [pQ-> front], pQ-> pBase [pQ-> front]); pQ-> front ++; return;} int main (void) {QUEUE q; init (& q, 10); enqueue (& q, 0, 'z'); enqueue (& q, 1, 'y '); enqueue (& q, 2, 'x'); enqueue (& q, 3, 'w'); enqueue (& q, 4, 'V '); enqueue (& q, 5, 'U'); enqueue (& q, 6, 'T'); enqueue (& q, 7,'s '); enqueue (& q, 8, 'R'); enqueue (& q, 9, 'q'); enqueue (& q, 10, 'P '); traverse (& q); dequeue (& q); traverse (& q); enqueue (& q, 11, 'n'); traverse (& q ); dequeue (& q); traverse (& q); enqueue (& q, 12, 'M'); traverse (& q); 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.