IN C++ PLEASE!!!
What needs to be done is in the code itself where it is written TO DO List!
#include<iostream>
using namespace std;
template<typename T> class DoubleList{
class Node{
public: T value; Node* next; Node*
prev;
Node(T value = T(),
Node* next = nullptr, Node* prev = nullptr){
this->value
= value;
this->next
= next;
this->next
= next;
}
};
int size;
Node* head;
Node* tail;
public:
DoubleList(){
size = 0;
head =
nullptr;
}
int length(){
return size;
}
void push_front(T element){
if(head ==
nullptr)
head
= new Node(element);
else {
Node*
temp = new Node(element, head);
head
= temp;
}
size++;
}
void push_back(T element){//0(n)
if(head ==
nullptr)
head
= new Node(element);
else {
Node*
cur = head;
while(cur->next
!= nullptr){
cur
= cur->next;
}
cur->next
= new Node(element);
}
size++;
}
void check(){
delete head;
if(head ==
nullptr)
cout
<< "yes";
}
void pop_front(){
if(size <=
0)
throw
out_of_range("");
else {
Node*
temp = head;
head
= head->next;
delete
temp;
}
size--;
}
void clearAll(){
while(size !=
0)
pop_front();
}
T operator [] (int index) {
T res = T();
Node* cur =
head;
int counter =
0;
while(cur !=
nullptr){
if(counter
== index){
res
= cur->value;
break;
}
cur
= cur->next;
counter++;
}
return res;
}
DoubleList(){
clearAll();
}
};
/*
TODO List:
1. method: push_back(T element), pop_back(), add(T element, int
index),
set(T element, int index), remove(int
index)
2. operator: ==, !=, =
*/
int main() {
DoubleList<int> sl;
sl.push_front(4);
sl.push_front(2);
sl.push_front(3);
sl.push_front(1);
sl.push_back(5);
for(int i = 0; i < sl.length();
i++)
cout << sl[i]
<< " ";
return 0;
}
2. part is unclear. please quote it again.
#include<iostream>
using namespace std;
template<typename T> class DoubleList{
class Node{
public: T value; Node* next; Node* prev;
Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){
this->value = value;
this->next = next;
this->next = next;
}
};
int size;
Node* head;
Node* tail;
public:
DoubleList<T>(){
size = 0;
head = nullptr;
}
int length(){
return size;
}
void push_front(T element){
if(head == nullptr)
head = new Node(element);
else {
Node* temp = new Node(element, head);
head = temp;
}
size++;
}
void push_back(T element){//0(n)
if(head == nullptr)
head = new Node(element);
else {
Node* cur = head;
while(cur->next != nullptr){
cur = cur->next;
}
cur->next = new Node(element);
}
size++;
}
void check(){
delete head;
if(head == nullptr)
cout << "yes";
}
void pop_front(){
if(size <= 0)
throw out_of_range("");
else {
Node* temp = head;
head = head->next;
delete temp;
}
size--;
}
void clearAll(){
while(size != 0)
pop_front();
}
T operator [] (int index) {
T res = T();
Node* cur = head;
int counter = 0;
while(cur != nullptr){
if(counter == index){
res = cur->value;
break;
}
cur = cur->next;
counter++;
}
return res;
}
~DoubleList(){
}
void add(T element, int index){
Node* curr=head;
int i=0;
while(i<index-1)
{
curr=curr->next;
i++;
}
Node *node=new Node(element);
node->next=curr->next;
curr->next=node;
size++;
}
void set(T element, int index){
Node* curr=head;
int i=0;
while(i<index-1)
{
curr=curr->next;
i++;
}
curr->value=element;
}
void remove(int index){
Node* fast=head;
Node *slow=nullptr;
int i=0;
while(i<index-1)
{
slow=fast;
fast=fast->next;
i++;
}
slow->next=fast->next;
delete fast;
size--;
}
};
/*
TODO List:
1. method: push_back(T element), pop_back(), add(T element, int index),
set(T element, int index), remove(int index)
2. operator: ==, !=, = // please quote it again; ambiguous
*/
int main() {
DoubleList<int> sl;
sl.push_front(4);
sl.push_front(2);
sl.push_front(3);
sl.push_front(1);
sl.push_back(5);
sl.add(7,3); // will place node after 3rd element; list now is 1 3 2 7 4 5
sl.set(8,4); // will set value at 4th node ; list now is 1 3 2 8 4 5
sl.remove(3); // will remove node at 3rd index; list now is 1 3 8 4 5
for(int i = 0; i < sl.length(); i++)
cout << sl[i] << " ";
return 0;
}
KINDLY UPVOTE. ? IT'D REALLY MEAN A LOT
Get Answers For Free
Most questions answered within 1 hours.