You have to pile clothes in a box in such a
way that cloths can be added and removed
from only one end. Determine the most
suitable data structure. Provide both array
based and linked list based implementation.
#include <iostream>
using namespace std;
template <class T>
class List {
private:
class Node {
public:
Node* next;
T value;
Node(T data = T(), Node* ptr = nullptr) {
this->next = ptr;
this->value = data;
}
};
public:
Node* head;
int size = 0;
List();
~List();
void display();
void popfront();
void popback();
void pushfront(T value);
void pushback(T value);
void insert_at(T value, int index);
void remove_at(int index);
void clear();
T& operator[] (int c);
int Getsize() {
return size;
}
};
template <class T>
List<T>::List() {
head = nullptr;
}
template <class T>
void List<T>::popfront() {
--size;
Node* temp = head;
head = head->next;
delete temp;
}
template <class T>
void List<T>::popback() {
remove_at(size - 1);
}
template <class T>
void List<T>::pushfront(T value) {
head = new Node(value, head);
++size;
}
template <class T>
void List<T>::insert_at(T value, int index) {
if (index == 0) {
pushfront(value);
return;
}
Node* previous = head;
for (int i = 0; i != index - 1; ++i) {
previous = previous->next;
}
Node* newnode = new Node(value, previous->next);
previous->next = newnode;
size++;
}
template <class T>
void List<T>::remove_at(int index) {
if (index == 0) {
popfront();
return;
}
Node* current = head;
Node* prevCurrent = head;
int i = 0;
while (i < index) {
prevCurrent = current;
current = current->next;
++i;
}
Node* nextCurrent = current->next;
delete current;
prevCurrent->next = nextCurrent;
--size;
}
template <class T>
void List<T>::pushback(T value) {
Node* newNode = new Node(value);
if (!head) {
head = newNode;
}
else {
Node* current = head;
while (current->next) {
current = current->next;
}
current->next = newNode;
}
++size;
}
template <class T>
void List<T>::clear() {
while (size) {
this->popfront();
}
}
template <class T>
T& List<T>::operator[](int c) {
Node* current = head;
int count = 0;
while (count < c) {
current = current->next;
++count;
}
return current->value;
}
template <class T>
void List<T>::display() {
Node* current = head;
while (current) {
cout << current << "\t" << current->value << "\t" << current->next << endl;
current = current->next;
}
}
template <class T>
List<T>::~List() {
this->clear();
}
const int MAX = 3;
template <class T>
class Stack {
private:
int top;
T st[MAX];
public:
class Full :public exception {};
class Empty :public runtime_error {
public: Empty(const char* msg, int f) :runtime_error(msg) {
this->n = f;
cout << n << endl;
}
int n;
};
Stack();
void push(T value);
T pop();
};
template <class T>
Stack<T>::Stack() :top(-1) {}
template <class T>
void Stack<T>::push(T value) {
st[++top] = value;
if (top > MAX - 1) {
throw Full();
}
}
template <class T>
T Stack<T>::pop() {
if (top < 0) {
throw Empty("Stack is empty!", 5);
}
return st[top--];
}
int main()
{
Stack<string> box1;
box1.push("skirt");
box1.push("shirt");
box1.pop();
box1.pop();
List<string> box2;
box2.pushback("skirt");
box2.pushback("shirt");
box2.popback();
box2.popback();
return 0;
}
Comments
Leave a comment