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

Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
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...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
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...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT