initial commit
This commit is contained in:
137
pr6.cpp
Normal file
137
pr6.cpp
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class Element
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
Element* next;
|
||||||
|
Element* prev;
|
||||||
|
T info;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Element(T data) : next(nullptr), prev(nullptr), info(data) {}
|
||||||
|
Element(Element* Next, Element* Prev, T data) : next(Next), prev(Prev), info(data) {}
|
||||||
|
Element(const Element& el) : next(el.next), prev(el.prev), info(el.info) {}
|
||||||
|
|
||||||
|
template<class T1>
|
||||||
|
friend ostream& operator<<(ostream& s, const Element<T1>& el);
|
||||||
|
|
||||||
|
template<class T1> friend class LinkedList;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T1>
|
||||||
|
ostream& operator<<(ostream& s, const Element<T1>& el)
|
||||||
|
{
|
||||||
|
s << el.info;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class LinkedList
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
Element<T>* head;
|
||||||
|
Element<T>* tail;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
public:
|
||||||
|
LinkedList() : head(nullptr), tail(nullptr), count(0) {}
|
||||||
|
|
||||||
|
virtual Element<T>* pop() = 0;
|
||||||
|
virtual Element<T>* push(T value) = 0;
|
||||||
|
virtual Element<T>& operator[](int index) = 0;
|
||||||
|
|
||||||
|
virtual bool isEmpty() { return count == 0; }
|
||||||
|
|
||||||
|
template<class T1>
|
||||||
|
friend ostream& operator<<(ostream& s, const LinkedList<T1>& list);
|
||||||
|
|
||||||
|
virtual ~LinkedList()
|
||||||
|
{
|
||||||
|
cout << "\nBase class destructor";
|
||||||
|
Element<T>* current = head;
|
||||||
|
while (current != nullptr) {
|
||||||
|
Element<T>* nextElement = current->next;
|
||||||
|
delete current;
|
||||||
|
current = nextElement;
|
||||||
|
}
|
||||||
|
head = tail = nullptr;
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T1>
|
||||||
|
ostream& operator<<(ostream& s, const LinkedList<T1>& list)
|
||||||
|
{
|
||||||
|
Element<T1>* current = list.head;
|
||||||
|
while (current != nullptr) {
|
||||||
|
s << *current << " ";
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, int N = 20>
|
||||||
|
class Stack : public LinkedList<T>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Stack() : LinkedList<T>()
|
||||||
|
{
|
||||||
|
if (N > 0) {
|
||||||
|
for (int i = 0; i < N; i++)
|
||||||
|
this->push(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual Element<T>* push(T value)
|
||||||
|
{
|
||||||
|
if (this->head == nullptr) { // if(count==0)
|
||||||
|
this->tail = new Element<T>(value);
|
||||||
|
this->head = this->tail;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this->tail->next = new Element<T>(value);
|
||||||
|
this->tail->next->prev = this->tail;
|
||||||
|
this->tail = this->tail->next;
|
||||||
|
}
|
||||||
|
this->count++;
|
||||||
|
return this->tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual Element<T>* pop()
|
||||||
|
{
|
||||||
|
if (this->tail == nullptr)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
Element<T>* res = this->tail;
|
||||||
|
|
||||||
|
if (this->head == this->tail) {
|
||||||
|
this->head = this->tail = nullptr;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this->tail = this->tail->prev;
|
||||||
|
this->tail->next = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->count--;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Stack() { cout << "\nStack class destructor"; }
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
if (true)
|
||||||
|
{
|
||||||
|
Stack<double, 20> S;
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
S.push(i);
|
||||||
|
// S.insert(3.5, S.head->next->next->next); // insert method not defined in the provided code
|
||||||
|
cout << S;
|
||||||
|
cout << "\n";
|
||||||
|
// cout<<S.Find_R(5.5, S.head); // Find_R method not defined in the provided code
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user