Data Structure -- queue's C array implementation, data structure -- queue Array
A queue is a linear table with limited operations. It can only be inserted in one segment of the table and retrieved in another segment.
This is also known as the First-In-First-Out data structure (FIFO --- First In First Out)
The C code is as follows (a small bug does not need to be called, as a reference ):
#include<stdio.h>#define maxsize 5typedef int ElemType;typedef struct queue{ int head; int tail; ElemType Data[maxsize];}Queue;void InitQueue(Queue *Q){ Q->tail=0; Q->head=0;}void EnQueue(Queue *Q){ int value; int i; printf("Input Queue Value:\n"); scanf("%d",&value); Q->head++; int len=Q->head-Q->tail; if(len<maxsize) { for(i=len-1;i>=0;i--) Q->Data[i+1]=Q->Data[i]; } Q->Data[Q->tail]=value; printf("\n");}void DeQueue(Queue *Q){ int len=Q->head-Q->tail-1; Q->head=Q->head-1; if(len<=maxsize) { printf("Out put Value:\n"); printf("%d ",Q->Data[len]); } printf("\n");}void IsEmpty(Queue *Q){ if(Q->head==0&&Q->tail==0) printf("Queue is empty.\n"); else printf("Queue is not empet.\n "); printf("\n");}void IsFull(Queue *Q){ if(Q->head-Q->tail>=maxsize) printf("Queue is Full.\n"); else printf("Queue is not Full.\n"); printf("\n");}void main(){ Queue Q; InitQueue(&Q); EnQueue(&Q); EnQueue(&Q); EnQueue(&Q); EnQueue(&Q); EnQueue(&Q); IsEmpty(&Q); IsFull(&Q); DeQueue(&Q); DeQueue(&Q); DeQueue(&Q); DeQueue(&Q); DeQueue(&Q); IsEmpty(&Q); IsFull(&Q);}
Result chart:
Reprinted by Liu
Use arrays to implement the data structure of stacks and queues
# Include <stdlib. h>
# Include <stdio. h>
Class Stack
{
Private:
Int ele [100];
Int top;
Public:
Stack (){
Top = 0;
}
Void push (int v ){
Ele [top ++] = v;
}
Int pop (){
If (isEmpty ()){
Printf ("The stack is Empty. \ n ");
Return-1;
}
Return ele [-- top];
}
Bool isEmpty (){
Return top = 0;
}
~ Stack (){
}
};
# Define SIZE 100
Class Queue
{
Private:
Int ele [SIZE];
Int head;
Int tail;
Public:
Queue (){
Head = 0;
Tail = 0;
}
Bool isFull (){
Return (tail + 1) % SIZE = head;
}
Bool isEmpty (){
Return head = tail;
}
Void enqueue (int val ){
If (isFull ()){
Printf ("The queue is full, can not enqueue for: % d \ n", val );
Return;
}
Ele [tail] = val;
Tail = (tail + 1) % SIZE;
}
Int dequeue (){
If (isEmpty ()){
Printf ("The queue is empty, can not dequeue. \ n ");
Return-1;
}
Int value = ele [head];
Head = (head + 1) % SIZE;
Return value;
}
};
Int main ()
{
Stack s;
S. push (1, 123 );
S. push (12 );
While (! S. isEmpty ()){
Printf ("% d", s. pop ());
}
Printf ("\ n ");
Queue q;
Q. enqueue (88 );
Q. enqueue (13 );
Q. enqueue (123 );
While (! Q. isEmpty () printf ("% d", q. dequeue ());
Printf ("\ n ");
System ("pause ");
}... Remaining full text>
Implementation of basic queue operations in data structure (c)
0.0