Question

In: Computer Science

so the assigment is is a data strucutre using c++ to make a doubly linked list...

so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code but still having trouble as i am unexpreinced with coding.

#include <iostream>
#include <string>
#include <fstream>
#include "ArgumentManager.h"
// DoubleLinkedList.h
struct Node {
   long long num;
   Node* prev;
   Node* next;
   Node(long long n)
   {
       num = n;
       prev = NULL;
       next = NULL;
   }

};

class DoubleLinkedList {
public:
   DoubleLinkedList() {
       head = tail = NULL;
       size = 0;
   }// default construct

   ~DoubleLinkedList() {
       freeMemory();
   };
   void freeMemory() {
       if (!head)
           return;
       while (head) {
           Node *p = head->next;
           delete head;
           head = p;
       }
       head = tail = NULL;
   }// deconstruct
   DoubleLinkedList(const std::string& num, int digitsPerNode)
   { //num: "1231322432123", digitPerNode:3
       int c1 = num.length() / digitsPerNode;
       int c2 = num.length() % digitsPerNode;
       // list: [12],[345],[678]
       if (c2) {
           string str1 = num.substring(0, c2); //error with .substring aswell
           Node* node = new Node(stoi(str1));
           Append(node);

       }
       for (int i = 0; i < c1; i++) {
           string str1 = num.substring(c2+i*digitsPerNode, digitsPerNode);
           Node* node = new Node(stoi(str1));
           Append(node);
       }

   }; // user defined construct
   DoubleLinkedList(int digitsPerNode);
   DoubleLinkedList(const DoubleLinkedList& list)
   {

   }; // copy construct
   DoubleLinkedList& operator = (const DoubleLinkedList& list)
   {
       if (this != &list)
       {// copy constructor?
  
       }
       return *this;
   }; // assignment consturct
public:
   //mandatory
   DoubleLinkedList& operator + (const DoubleLinkedList& list) const
   {
      
   };
   DoubleLinkedList operator * (const DoubleLinkedList& list) const {
   };

   // optional
   DoubleLinkedList operator - (const DoubleLinkedList& list) const {
   };
   // 10% extra
   DoubleLinkedList operator / (const DoubleLinkedList& list) const {
   };
   // 20% extra
   DoubleLinkedList Sqrt(const DoubleLinkedList& list) const {
   };
public:
   const Node* GetHead() const
   {
   };
   const Node* GetTail() const
   {
   };
   void Append(Node* node) // appead the tail
   {
       size++;
       if (!tail)
       {
           head = tail = node;
           return;
       }
       tail->next = node;
       node->prev = tail;
       tail = node;
   }
   void Add(Node *node) {
       size++;
       if (!head) {
           head = tail = node;
           return;
       }
       node->next = head;
       head->prev = node;
       head = node;
   }; // add before head
   void Print() const {
       // list [1] [2] [3[; // 1 002 003

   };
   void toString(int digitsPerNode)
   {
       string res = "";
       Node *cur = head;
       if (!cur)
           return res;   
       //head: no leading "0"
       while (cur) {
           int numstr = to_string(cur->num); // res, numstr, and to_string are errors
           int l = numstr.length();
           for (int i = 0; i < digitsPerNode - 1; i++)
           {
               res += "0";
               // "00"
               res += numstr;
               // "002"
               cur = cur->next;
           }
           return res;
       }
   }
private:
   int size;
   Node* head;
   Node* tail;
   int m_digitsPerNode;
   long long remainder; // for / operator
   float decimal; // for sqrt() 7 valid digits.
}

// main.cpp
#include "ArgumentManager.h"
int main(int argc, char* argv[])
{
   if (argc < 2) {
       std::cerr << "Usage: infinitearithmetic \"input=xyz.txt;digitsPerNode=<number>\"\n";
   }
   ArgumentManager am(argc, argv);
   std::string filename = am.get("input");
   int digitsPerNode = std::stoi(am.get("digitsPerNode"));
   std::ifstream ifs(filename.c_str());
   std::string line;
   while (getline(ifs, line)) {
       std::string num1;
       std::string num2;
       std::string op;
       // get num1 num2 and operator in line
       // ...
       DoubleLinkedList l1(num1, digitsPerNode); // DoubleLinkedList(const std::string& num, int digitsPerNode)
       DoubleLinkedList l2(num2, digitsPerNode);
       DoubleLinkedList l; // DoubleLinkedList();
       if (/* plus */) //not sure what to enter says you need bool
       {
           l = l1 + l2;
       } // DoubleLinkedList operator + (const DoubleLinkedList& list) const; DoubleLinkedList& operator = (const DoubleLinkedList& list);
       else if (/* mult */) // DoubleLinkedList operator * (const DoubleLinkedList& list) const;
       {
           l = l1 * l2;
       }
       else if (/* div */)
       {
           l = l1 / l2;
       } // DoubleLinkedList operator / (const DoubleLinkedList& list) const;
       else if (/*subtract*/)
       {//...
       }
       else if (/* squareroot */)
       {
           // ...
       }

       // output result
       }

       return 0;
   }

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <fstream>
#include "ArgumentManager.h"
// DoubleLinkedList.h
struct Node {
   long long num;
   Node* prev;
   Node* next;
   Node(long long n)
   {
       num = n;
       prev = NULL;
       next = NULL;
   }

};

class DoubleLinkedList {
public:
   DoubleLinkedList() {
       head = tail = NULL;
       size = 0;
   }// default construct

   ~DoubleLinkedList() {
       freeMemory();
   };
   void freeMemory() {
       if (!head)
           return;
       while (head) {
           Node *p = head->next;
           delete head;
           head = p;
       }
       head = tail = NULL;
   }// deconstruct
   DoubleLinkedList(const std::string& num, int digitsPerNode)
   { //num: "1231322432123", digitPerNode:3
       int c1 = num.length() / digitsPerNode;
       int c2 = num.length() % digitsPerNode;
       // list: [12],[345],[678]
       if (c2) {
           string str1 = num.substring(0, c2); //error with .substring aswell
           Node* node = new Node(stoi(str1));
           Append(node);

       }
       for (int i = 0; i < c1; i++) {
           string str1 = num.substring(c2+i*digitsPerNode, digitsPerNode);
           Node* node = new Node(stoi(str1));
           Append(node);
       }

   }; // user defined construct
   DoubleLinkedList(int digitsPerNode);
   DoubleLinkedList(const DoubleLinkedList& list)
   {

   }; // copy construct
   DoubleLinkedList& operator = (const DoubleLinkedList& list)
   {
       if (this != &list)
       {// copy constructor?
  
       }
       return *this;
   }; // assignment consturct
public:
   //mandatory
   DoubleLinkedList& operator + (const DoubleLinkedList& list) const
   {
      
   };
   DoubleLinkedList operator * (const DoubleLinkedList& list) const {
   };

   // optional
   DoubleLinkedList operator - (const DoubleLinkedList& list) const {
   };
   // 10% extra
   DoubleLinkedList operator / (const DoubleLinkedList& list) const {
   };
   // 20% extra
   DoubleLinkedList Sqrt(const DoubleLinkedList& list) const {
   };
public:
   const Node* GetHead() const
   {
   };
   const Node* GetTail() const
   {
   };
   void Append(Node* node) // appead the tail
   {
       size++;
       if (!tail)
       {
           head = tail = node;
           return;
       }
       tail->next = node;
       node->prev = tail;
       tail = node;
   }
   void Add(Node *node) {
       size++;
       if (!head) {
           head = tail = node;
           return;
       }
       node->next = head;
       head->prev = node;
       head = node;
   }; // add before head
   void Print() const {
       // list [1] [2] [3[; // 1 002 003

   };
   void toString(int digitsPerNode)
   {
       string res = "";
       Node *cur = head;
       if (!cur)
           return res;   
       //head: no leading "0"
       while (cur) {
           int numstr = to_string(cur->num); // res, numstr, and to_string are errors
           int l = numstr.length();
           for (int i = 0; i < digitsPerNode - 1; i++)
           {
               res += "0";
               // "00"
               res += numstr;
               // "002"
               cur = cur->next;
           }
           return res;
       }
   }
private:
   int size;
   Node* head;
   Node* tail;
   int m_digitsPerNode;
   long long remainder; // for / operator
   float decimal; // for sqrt() 7 valid digits.
}

// main.cpp
#include "ArgumentManager.h"
int main(int argc, char* argv[])
{
   if (argc < 2) {
       std::cerr << "Usage: infinitearithmetic \"input=xyz.txt;digitsPerNode=<number>\"\n";
   }
   ArgumentManager am(argc, argv);
   std::string filename = am.get("input");
   int digitsPerNode = std::stoi(am.get("digitsPerNode"));
   std::ifstream ifs(filename.c_str());
   std::string line;
   while (getline(ifs, line)) {
       std::string num1;
       std::string num2;
       std::string op;
       // get num1 num2 and operator in line
       // ...
       DoubleLinkedList l1(num1, digitsPerNode); // DoubleLinkedList(const std::string& num, int digitsPerNode)
       DoubleLinkedList l2(num2, digitsPerNode);
       DoubleLinkedList l; // DoubleLinkedList();
       if (/* plus */) //not sure what to enter says you need bool
       {
           l = l1 + l2;
       } // DoubleLinkedList operator + (const DoubleLinkedList& list) const; DoubleLinkedList& operator = (const DoubleLinkedList& list);
       else if (/* mult */) // DoubleLinkedList operator * (const DoubleLinkedList& list) const;
       {
           l = l1 * l2;
       }
       else if (/* div */)
       {
           l = l1 / l2;
       } // DoubleLinkedList operator / (const DoubleLinkedList& list) const;
       else if (/*subtract*/)
       {//...
       }
       else if (/* squareroot */)
       {
           // ...
       }

       // output result
       }

       return 0;
   }


Related Solutions

TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from...
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from the input and insert them into the linked list. • If an input integer is 0, the program should print all the integers in the list(from tail to head) and then exit.
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT