Question

IN C++ PLEASE!!! What needs to be done is in the code itself where it is...

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;
}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...