Data structure _P16__ data structure

Source: Internet
Author: User
* * Loop queue: Create, initialize, judge as full or empty, traverse, team, Outbound * Queue (Static queue _ array, chain queue _ list), where the static queue must be a circular queue * June 16, 2016 10:23:05 **/# include <stdio.h>		# include <malloc.h> typedef struct QUEUE {int* pbase; //!
	(int*) type, used to point to an array: malloc the address int front returned after requesting dynamic memory;
int rear;

}queue;
void Init (queue*);
BOOL En_queue (queue*, int);
void Traverse_queue (queue*);
BOOL Out_queue (queue*, int*);
BOOL Is_empty (queue*);


BOOL Is_full (queue*);
	int main (void) {QUEUE q;
	int Val;

	Init (&AMP;Q);
	En_queue (&q, 1);
	En_queue (&q, 2);
	En_queue (&q, 3);
	En_queue (&q, 4);
	En_queue (&q, 5);
	En_queue (&q, 6);
	En_queue (&q, 7);		Traverse_queue (&AMP;Q);
	The above can only be put into 1~5 (full), Output: 1 2 3 4 5 if (En_queue (&q, 123123)) printf ("ok\n");

	else printf ("error\n");
	if (En_queue (&q, 1111)) printf ("ok\n");
		
	else printf ("error\n");
	
	Traverse_queue (&AMP;Q);
	if (Out_queue (&q, &val)) printf ("Out_ok:%d\n", Val);

	else printf ("out_error!\n"); if (Out_queue (&q, &val)) printf ("Out_ok:%d\n ", Val);

	else printf ("out_error!\n");

	Traverse_queue (&AMP;Q);
return 0;
	} void init (queue* q) {q->pbase = (int*) malloc (sizeof (int) * 6);//Now PBase has pointed to that array.
	Q->front = 0;
q->rear = 0;
	BOOL En_queue (queue* q, int val) {if (Is_full (q)) return false;
		else {Q->pbase[q->rear] = val;
		Q->rear = (q->rear+1)% 6;
	return true;
	} bool Is_full (queue* q) {if (q->rear+1)%6 = = Q->front) return true;
else return false;
		} void Traverse_queue (queue* q) {int p = q->front;
			while (P!= (q->rear)) {printf ("%d", q->pbase[p]);
		p = (p+1)% 6;
		printf ("\ n");
Return
	BOOL Is_empty (queue* q) {if (q->rear = = Q->front) return true;
else return false;
	BOOL Out_queue (queue* Q, int* pVal) {if (Is_empty (q)) return false;
		else {*pval = q->pbase[q->front];
		Q->front = (q->front+1)% 6;
	return true; }
}

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.