[Data structure] queue-array implementation, data structure queue Array
First, define the basic structure of the queue. The difference between the queue and stack is that the queue requires two pointers, one pointing to the header and the other pointing to the end.
String[] queue;int front = 0;int rear = 0;
Constructor
public QueueOfStrings(int capacity) {queue = new String[capacity];}
Queue
public void enqueue(String str) {queue[rear++] = str;if (rear == queue.length)resize(2 * queue.length);}
Outbound queue
public String dequeue() {return queue[front++];}
Empty
public boolean isEmpty() {return front == rear;}
Sentenced to full
public boolean isFull() {return rear == queue.length;}
Dimensions
public int size() {return rear - front;}
Attached resize
public void resize(int capacity) {String[] copy = new String[capacity];for (int i = 0; i < rear; i++)copy[i] = queue[i];queue = copy;}
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>
Data Structure Design queues implement the following algorithms
# Include <stdio. h>
# Include <stdlib. h>
Typedef struct QNode
{
Int data;
Struct QNode * next;
Int Queusize;
}
QNode, * QueuePtr; // defines the queue Node Type
Typedef struct
{
QueuePtr front;
QueuePtr rear;
}
LinkQueue; // queue type
Void InitQueue (LinkQueue * Q) // create a queue
{
Q-> front = Q-> rear = (QueuePtr) malloc (sizeof (QNode ));
Q-> front-> next = NULL;
}
Void EnQueue (LinkQueue * Q, int e) // Insert elements into the queue
{
QueuePtr p;
P = (QueuePtr) malloc (sizeof (QNode ));
P-> data = e;
P-> next = NULL;
Q-> rear-> next = p;
Q-> rear = p;
}
Int DeQueue (LinkQueue * Q) // columns the element and returns the position of the element.
{
Int e;
QueuePtr p;
P = Q-> front-> next;
E = p-> data;
Q-> front-> next = p-> next;
If (Q-> rear = p)
Q-> rear = Q-> front;
Free (p );
Return (e );
}
Int QueueEmpty (LinkQueue * Q) // determines whether the queue is empty.
{
If (Q-> front = Q-> rear)
Return 1;
Else
Return 0;
}
Void Select (LinkQueue * Q, int)
{
Int B, I; QueuePtr p;
If (a = 1)
{
Printf ("enter an element, end with 0: \ n ");
Scanf ("% d", & B );
While (B! = 0)
{
EnQueue (Q, B );
Scanf ("% d", & B );
}
}
If (a = 2)
{
Printf ("Enter the number of elements \ n ");
Scanf ("% d", & B );
For (I = 0; I <B; I ++)
{
If (! QueueEmpty (Q ))
DeQueue (Q );
Else
Printf ("the queue is empty! \ N "); break;
}
}
If (a = 3)
{
Printf ("the internal element is \ n ");
... The remaining full text>