Implementation includes:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LinkedList {
Node* head;
public:
LinkedList() { head = NULL; }
void insertFirst(int val) {
Node* newnode = new Node();
newnode->data = val;
newnode->next = head;
head = newnode;
}
void deleteFirst() {
if (head == NULL) return;
Node* temp = head;
head = head->next;
delete temp;
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int getMin() {
if (head == NULL) return -1;
int minVal = head->data;
Node* temp = head;
while (temp != NULL) {
if (temp->data < minVal)
minVal = temp->data;
temp = temp->next;
}
return minVal;
}
int count() {
int counter = 0;
Node* temp = head;
while (temp != NULL) {
counter++;
temp = temp->next;
}
return counter;
}
};
Implementation includes:
#include <iostream>
using namespace std;
class Stack {
int* stack_arr;
int top;
int size;
public:
Stack(int s) {
size = s;
stack_arr = new int[size];
top = -1;
}
void push(int item) {
if (top == size - 1)
cout << "Stack Overflow\n";
else
stack_arr[++top] = item;
}
int pop() {
return (top == -1) ? -1 : stack_arr[top--];
}
int peak() {
return (top == -1) ? -1 : stack_arr[top];
}
void display() {
for (int i = top; i >= 0; i--)
cout << stack_arr[i] << " ";
cout << endl;
}
int getMin() {
int minVal = stack_arr[0];
for (int i = 1; i <= top; i++)
if (stack_arr[i] < minVal)
minVal = stack_arr[i];
return minVal;
}
};
Implementation includes:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Queue {
Node *front, *rear;
public:
Queue() { front = rear = NULL; }
void enqueue(int item) {
Node* newnode = new Node();
newnode->data = item;
newnode->next = NULL;
if (front == NULL)
front = rear = newnode;
else {
rear->next = newnode;
rear = newnode;
}
}
void dequeue() {
if (front == NULL) return;
Node* temp = front;
front = front->next;
delete temp;
}
int getRear() {
return rear->data;
}
void display() {
Node* temp = front;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
double getAverage() {
int sum = 0, count = 0;
Node* temp = front;
while (temp != NULL) {
sum += temp->data;
count++;
temp = temp->next;
}
return (count == 0) ? 0 : (double)sum / count;
}
};