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:...
Adding large numbers with linked list Requirement - in C++ - use file for the input...
Adding large numbers with linked list Requirement - in C++ - use file for the input (nums.txt) - (recommended) use only one linked list to hold intermediate answer and final answer. You may use another one to reverse the answer. - store the num reversely in the linked list. For example, the num 123 is stored as 3 (at first node), 2 (at second node) and 1 (at third node) in the linked list. - write a function that performs...
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()...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part. /* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/ #include #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs)//...
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 program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
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...
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....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT