/Data_Structure_C-

Include classes of node, cstack and CQueue

Primary LanguageC++

#data structures programs A Queue is a linear structure which follows particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.

#Queue using Stacks

// CPP program to implement Queue using // two stacks with costly enQueue() #include <bits/stdc++.h> using namespace std;

struct Queue { stack s1, s2;

void enQueue(int x)
{
	// Move all elements from s1 to s2
	while (!s1.empty()) {
		s2.push(s1.top());
		s1.pop();
	}

	// Push item into s1
	s1.push(x);

	// Push everything back to s1
	while (!s2.empty()) {
		s1.push(s2.top());
		s2.pop();
	}
}

// Dequeue an item from the queue
int deQueue()
{
	// if first stack is empty
	if (s1.empty()) {
		cout << "Q is Empty";
		exit(0);
	}

	// Return top of s1
	int x = s1.top();
	s1.pop();
	return x;
}

};

// Driver code section int main() { Queue q; q.enQueue(1); q.enQueue(2); q.enQueue(3);

cout << q.deQueue() << '\n';
cout << q.deQueue() << '\n';
cout << q.deQueue() << '\n';

return 0;

}