Queue chain implementation and queue chain implementation

Source: Internet
Author: User

Queue chain implementation and queue chain implementation

Queue chain implementation:

 

 

In this queue: r is low, f is the top

 

 

// Queue (chained) # include <iostream> using namespace std; typedef int DataType; struct QNode {DataType data; struct QNode * link;}; typestrudef CT QNode * PNode; // if r is low, the top is struct LinkQueue {PNode f; PNode r ;}; typedef struct LinkQueue * PLinkQueue; PLinkQueue createEmptyQueue () {PLinkQueue plqueue = (PLinkQueue) malloc (sizeof (struct LinkQueue); if (plqueue! = NULL) {plqueue-> f = NULL; plqueue-> r = NULL;} else printf ("Out of space \ n"); return plqueue ;} int isEmptyQueue (PLinkQueue plqueue) {return (plqueue-> f = NULL);} void enQueue (PLinkQueue plqueue, DataType x) {PNode p = (PNode) malloc (sizeof (struct QNode); if (p! = NULL) {p-> data = x; p-> link = NULL; if (plqueue-> f = NULL) plqueue-> f = p; else plqueue-> r-> link = p; plqueue-> r = p;} else printf ("Out of space \ n");} void deQueue (PLinkQueue plqueue) {PNode p = (PNode) malloc (sizeof (struct QNode); if (plqueue-> f = NULL) printf ("Empty Queue \ n "); else {p = plqueue-> f; plqueue-> f = p-> link; free (p) ;}} DataType getFront (PLinkQueue plqueue) {if (plqueue-> f = NULL) printf ("Empty Queue \ n"); else return (plqueue-> f-> data);} int main () {PLinkQueue lqueue = createEmptyQueue (); cout <"Create an nelement queue \ n input n" <endl; int n, t; cin> n; t = n; while (n) {int data; cin> data; enQueue (lqueue, data); n --;} cout <"take the team header and leave the team" <endl; while (t) {cout <getFront (lqueue) <"; deQueue (lqueue); t --;} cout <endl; system (" pause "); 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.