Data Structures in C++: Queue, Stack, and Linked List with Arrays
In C++, there are several common data structures used for storing and manipulating data. Three
fundamental ones are Queue, Stack, and Linked List. Below, I will explain each of them along
with their theory and implementation using arrays in C++.
---
1. Queue
A Queue is a linear data structure that follows the FIFO (First In, First Out) principle. This means
that the element added first will be the first one to be removed.
Operations on a Queue:
Enqueue: Adds an element to the end of the queue.
Dequeue: Removes an element from the front of the queue.
Front: Returns the element at the front of the queue without removing it.
Rear: Returns the element at the end of the queue.
isEmpty: Checks if the queue is empty.
isFull: Checks if the queue is full.
Code Implementation using Array:
#include <iostream>
using namespace std;
class Queue {
int front, rear, capacity;
int *queue;
public:
Queue(int size) {
capacity = size;
front = -1;
rear = -1;
queue = new int[capacity];
}
bool isFull() {
return (rear == capacity - 1);
}
bool isEmpty() {
return (front == -1 || front > rear);
}
void enqueue(int value) {
if (isFull()) {
cout << "Queue is full!" << endl;
return;
}
if (front == -1) front = 0;
queue[++rear] = value;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return;
}
front++;
}
int peekFront() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return -1;
}
return queue[front];
}
void display() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return;
}
for (int i = front; i <= rear; i++) {
cout << queue[i] << " ";
}
cout << endl;
}
};
int main() {
Queue q(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
cout << "Queue after enqueuing 5 elements: ";
q.display();
q.dequeue();
cout << "Queue after dequeuing 1 element: ";
q.display();
cout << "Front element: " << q.peekFront() << endl;
return 0;
}
2. Stack
A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle. This means
that the element added last will be the first one to be removed.
Operations on a Stack:
Push: Adds an element to the top of the stack.
Pop: Removes the top element from the stack.
Top: Returns the top element without removing it.
isEmpty: Checks if the stack is empty.
isFull: Checks if the stack is full.
Code Implementation using Array:
#include <iostream>
using namespace std;
class Stack {
int top, capacity;
int *stack;
public:
Stack(int size) {
capacity = size;
top = -1;
stack = new int[capacity];
}
bool isFull() {
return top == capacity - 1;
}
bool isEmpty() {
return top == -1;
}
void push(int value) {
if (isFull()) {
cout << "Stack Overflow!" << endl;
return;
}
stack[++top] = value;
}
void pop() {
if (isEmpty()) {
cout << "Stack Underflow!" << endl;
return;
}
top--;
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty!" << endl;
return -1;
}
return stack[top];
}
void display() {
if (isEmpty()) {
cout << "Stack is empty!" << endl;
return;
}
for (int i = top; i >= 0; i--) {
cout << stack[i] << " ";
}
cout << endl;
}
};
int main() {
Stack s(5);
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
cout << "Stack after pushing 5 elements: ";
s.display();
s.pop();
cout << "Stack after popping 1 element: ";
s.display();
cout << "Top element: " << s.peek() << endl;
return 0;
}
---
3. Linked List
A Linked List is a linear data structure where each element is a separate object, each containing
data and a reference (or link) to the next element in the sequence.
In a Linked List:
Each element (called a node) contains two parts:
1. Data: The actual data.
2. Next: A pointer (or reference) to the next node.
A Singly Linked List is a type of linked list where each node points to the next node, and the last
node points to NULL.
Operations on Linked List:
Insert: Adds a new node at the beginning, end, or after a specific node.
Delete: Removes a node from the list.
Search: Finds a node containing a specific value.
Display: Prints the entire list.
Code Implementation of Singly Linked List using Array (Nodes can be managed dynamically):
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class LinkedList {
Node* head;
public:
LinkedList() {
head = nullptr;
}
void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
void insertAtBeginning(int value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
}
void deleteNode(int value) {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* temp = head;
while (temp->next != nullptr && temp->next->data != value) {
temp = temp->next;
}
if (temp->next == nullptr) {
cout << "Value not found!" << endl;
return;
}
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
}
void display() {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
LinkedList list;
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtBeginning(5);
cout << "Linked List: ";
list.display();
list.deleteNode(20);
cout << "Linked List after deleting 20: ";
list.display();
return 0;
}
---
Summary:
1. Queue: Follows FIFO (First In, First Out) order for operations. Implemented using an array
and supports operations like enqueue, dequeue, front, and rear.
2. Stack: Follows LIFO (Last In, First Out) order. Implemented using an array and supports
operations like push, pop, and peek.
3. Linked List: Consists of nodes where each node contains data and a pointer to the next node.
Operations include insert, delete, search, and display.
These are basic implementations of these data structures in C++ using arrays and pointers.