#include< iostream > using namespace std; int front = 0, rear = 0; | |
void enqueue(int queue[], int element, int arrSize) | |
{ | |
if(rear != arrSize){ | |
queue[rear] = element; | |
rear++; | |
} | |
} | |
void dequeue(int queue[]) | |
{ | |
if(front != rear){ | |
queue[front] = 0; | |
front++; | |
} | |
} | |
int frontData(int queue[]) | |
{ | |
return queue[front]; | |
} | |
int size() | |
{ | |
return rear - front; | |
} | |
bool isEmpty() | |
{ | |
return front == rear; | |
} | |
int main() | |
{ | |
int n, k; | |
cin >> n; | |
int queue[n+1]; | |
for(int i = 0; i < n; i++){ | |
cin >> k; | |
enqueue(queue, k, n); | |
} | |
cout << "Queue size : " << size() << endl; | |
cout << "Queue first element = " << frontData(queue) << endl; | |
dequeue(queue); | |
cout << "Queue new first element = " << frontData(queue) << endl; | |
cout << "Queue is empty = " << (isEmpty()? "True":"False") << endl; | |
cout << "Queue data are : " ; | |
while(!isEmpty()) { | |
cout << frontData(queue) << " "; | |
dequeue(queue); | |
} | |
cout << endl; | |
cout << "Queue is empty = " << (isEmpty()? "True":"False") << endl; | |
return 0; | |
} |
Saturday, September 14, 2019
Queue implement by array in c++
Queue implement by array in python3
class Queue: def __init__(self, max_size = 0): | |
self.size = max_size | |
self.arr = [] | |
def enqueue(self, data): | |
if not self.isFull(): | |
self.arr.append(data) | |
def dequeue(self): | |
if not self.isEmpty(): | |
self.arr.pop(0) | |
def isEmpty(self): | |
return len(self.arr) == 0 | |
def isFull(self): | |
return len(self.arr) == self.size | |
def front(self): | |
if not self.isEmpty(): | |
return self.arr[0] | |
def rear(self): | |
if not self.isEmpty(): | |
return self.arr[-1] | |
def Qusize(self): | |
return len(self.arr) | |
q = Queue(5) | |
n = int(input()) | |
for i in range(n): | |
k = int(input()) | |
q.enqueue(k) | |
print("Size of Queue = ", q.Qusize()) | |
print("Queue is Full = ", q.isFull()) | |
print("Queue is empty = ", q.isEmpty()) | |
print("First element of queue = ", q.front()) | |
print("Last element of queue = ", q.rear()) | |
q.dequeue(); | |
print("New First element of queue = ", q.front()) | |
print("Queue element are :",end=' ') | |
while not q.isEmpty(): | |
print(q.front(),end=' ') | |
q.dequeue() | |
print() | |
print("Queue is empty = ", q.isEmpty()) |
Subscribe to:
Posts (Atom)