C Basic Data Structure --- queue ADT

Source: Internet
Author: User

I have nothing to worry about recently and don't want to waste my youth. I will start to read the previous notes and books and make some records.

The queue is implemented in C language as follows:

# Include
 
  
# Include
  
   
Typedef struct Node {int data; struct Node * next;} * Node; typedef struct Queue {int size; struct Node * first; struct Node * end;} * Queue; # define MAX_SIZE 10 Queue CreatEmptyQueue () {Queue queue = NULL; queue = (Queue) malloc (sizeof (struct Queue); if (queue = NULL) {printf ("malloc mem error! \ N "); exit (-1);} queue-> size = 0; queue-> first = NULL; queue-> end = NULL; return queue ;} int isEmpty (Queue queue) {return (queue-> size <= 0);} int isFull (Queue queue) {return (queue-> size> = MAX_SIZE );} int QueueIn (Queue queue, int data) {assert (queue! = NULL); Node p = NULL; if (isFull (queue) {printf ("in queue error! The Queue is full \ n "); return;} p = (Node) malloc (sizeof (struct Node); if (p = NULL) {printf ("malloc mem error! \ N "); return-1;} p-> data = data; p-> next = NULL; if (isEmpty (queue) = 1) {queue-> first = p; queue-> end = p;} else {queue-> end-> next = p; queue-> end = p ;} queue-> size = queue-> size + 1; return 0;} int QueueOut (Queue queue, int * data) {assert (queue! = NULL & data! = NULL); Node p; if (isEmpty (queue) = 1) {printf ("Queue is empty! \ N "); return-1;} p = queue-> first; * data = p-> data; queue-> first = queue-> first-> next; queue-> size = queue-> size-1; if (isEmpty (queue) = 1) {queue-> end = NULL; queue-> first = NULL ;} free (p); return 0;} void printQueue (Queue queue) {assert (queue! = NULL); printf ("queue size id: % d \ n", queue-> size); Node p; p = queue-> first; while (p! = NULL) {printf ("% d", p-> data); p = p-> next;} printf ("\ n ");} void destoryQueue (Queue queue) {assert (queue! = NULL); Node p, q; if (isEmpty (queue )! = 1) {q = p = queue-> first; while (p! = NULL) {queue-> size = queue-> size-1; printf ("have % d node \ n", queue-> size); q = p-> next; free (p); p = q ;}} queue-> first = NULL; queue-> end = NULL; free (queue); queue = NULL ;} int main (int argc, char * argv []) {Queue queue = NULL; int data; int I; int ret; printf ("Enter main! \ N "); queue = CreatEmptyQueue (); ret = QueueOut (queue, & data); if (ret = 0) {printf (" read success data is: % d \ n ", data);} else {printf (" The queue is empty, not havs data \ n ");} for (I = 0; I
   
    
There should be many defects in the code, but the code is compiled by gcc on ubuntu.
If you have any questions, please point them out. Thank you very much.


Related Article

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.