Data structure-queues-sequential storage complete executable code

Source: Internet
Author: User

data structure-queues-sequential storage complete executable code



#include <stdio.h> #include <stdlib.h> #include <time.h> #define OK 1 #define ERROR 0 #define TRUE 1 #d
Efine FALSE 0 #define MAXSIZE 20/* Storage space Initial allocation * * typedef int STATUS;

/* Qelemtype Type according to the actual situation, here is assumed to be int/typedef int QELEMTYPE;
	/* Cyclic queue sequence Storage structure */typedef struct {Qelemtype data[maxsize];    	int front;		/* head pointer */INT rear;

/* tail pointer, if the queue is not empty, point to the end of the queue element next position * *}SQQUEUE;
	/* Initialize an empty queue Q/Status initqueue (Sqqueue *q) {q->front=0;
	q->rear=0;
return OK;
	Status visit (qelemtype c) {printf ("%d", c);
return OK;
	/* * from Team head to team tail, sequentially output/Status queuetraverse (sqqueue q) {int i) for each element in queue Q;
	I=q.front;
		while ((I+q.front)!=q.rear) {visit (q.data[i]);
	I= (i+1)%maxsize;
	printf ("\ n");
return OK; /* If the queue is not full, insert element E to Q new team tail element */Status EnQueue (Sqqueue *q,qelemtype e) {if (q->rear+1)%maxsize = = Q->front)/* Queue full
	Judge * * return ERROR;			q->data[q->rear]=e; /* Assign element E to Team tail/q->rear= (q->rear+1)%maxsize;/* rear pointer move back one position, */////////////////////////// OK;
		/* If the queue is not empty, then delete Q Squadron HEAD element, return its value by E/Status dequeue (sqqueue *q,qelemtype *e) {if (Q->front = q->rear)/* Queue NULL judgment *
	return ERROR;				*e=q->data[q->front];	/* Assign the team head element to E/q->front= (q->front+1)%maxsize;
/* Front pointer move back one position, * * * If to the end of the array to the head/return OK;
	/* To clear Q to empty queue/Status clearqueue (Sqqueue *q) {q->front=q->rear=0;
return OK;
	/* If queue Q is an empty queue, returns True, otherwise returns false */Status Queueempty (Sqqueue Q) {if (q.front==q.rear)/* queue NULL flag/return TRUE;
else return FALSE;

}/* Returns the number of elements of Q, that is, the current length of the queue/int queuelength (Sqqueue Q) {return (q.rear-q.front+maxsize)%maxsize;}
    int main () {int opp, J;
    Sqqueue Q;
    Qelemtype D;
    
    
    Initqueue (&AMP;Q);
    printf ("\n1.") to the queue with the initial value \n2. Traversal queue \n3. Team \N4.
    printf ("\n5.") Determines whether the queue is empty \n6. Empty queue \n7. Queue Length ");
    printf ("\n0. Exit \ n" Please select your action: \ n ");
        while (opp!= ' 0 ') {scanf ("%d", &opp);
                Switch (OPP) {case 1:srand (0)); ForJ=1; j<=10;
                    J + +) {d = rand ()%100+1;
                EnQueue (&AMP;Q,D);
                } queuetraverse (Q);
                
            Break
                Case 2:queuetraverse (Q);
                
            Break
                Case 3:printf ("Please enter the element to be joined:");
                scanf ("%d", &d);
                EnQueue (&AMP;Q,D);
                Queuetraverse (Q);
                
            Break
                Case 4:dequeue (&AMP;Q,&AMP;D);
                printf ("Deleted element value is%d\n", d);
                
            Break Case 5:printf ("The queue is empty now.")
                %u (1: null 0: NO) \ n ", Queueempty (Q));
                
            Break
                Case 6:clearqueue (&AMP;Q); printf ("The queue is empty after the queue is emptied.")
                %u (1: null 0: NO) \ n ", Queueempty (Q));
                
            Break Case 7:printf ("Queue Length is:%d\n", queuelength(Q));
                
            Break
        Case 0:exit (0);
} return 0; }



Learning on the road, and June mutual encouragement.

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.