Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

. Three lists list1, list2 and list3 are created for you in CA3_template.cpp. You need to add the integers store in list1 and list2 and

. Three lists list1, list2 and list3 are created for you in CA3_template.cpp. You need to add the integers store in list1 and list2 and store the result in list3. Refer to Figure 1 for the representation of the three lists. Assume integers in list1 and list2 have the same number of digit. You need to do the following tasks in main():

  1. Go through every element in arr1 and insert to list1. Do the same for arr2 and insert to list.
  2. Print out the integer store in list1 and list2 using the print() function define in List.h.
  3. Perform addition operation to add integers store in list1 and list2. Every node in the list store 1 digit from the integer. You need to add every digit that is stored in a node starting from the last node in list1 and list2 and store result in list3. Use the get() function defined in List.h to get the nodes value in each node. If addition of 2 digits is more than 10 then you need to carry forward 1 for the addition of the digits before it.example.
  4. Print the output of the addition . You can print the integer store in each list using the print() function.

Output

image text in transcribed

Node.cpp

#include #include "Node.h"

Node::Node(int newItem) { item = newItem; next = NULL; }

List.cpp

#include #include #include "List.h"

using namespace std;

List::List() { head = NULL; count = 0; }

bool List::empty() { if (count==0) return true;

return false; }

int List::size() { return count; }

Node *List::find(int position) { Node *cur; if (position > count) return NULL; cur = head; for (int count=1; count cur = cur->next; return cur; }

bool List::get(int position, int &result) { if (position > count) return false; result = find(position)->item; return true; }

bool List::set(int position, int newItem) { if (position > count) return false; find(position)->item = newItem; return true; }

bool List::insert(int at, int newItem) {// Any simplification can be done on code below? Node *pre, *cur, *tmp = new Node(newItem);

if (at count+1) return false; //is position valid if (!tmp) return false; //if memory allocation successful

if (empty()) { //is the list empty head = tmp; count++; return true; } if ((at == 1) || empty()) { //if insert in position 1 in non-empty list tmp->next = head; head = tmp; count++; return true; } /ormal case pre = find(at-1); cur = pre->next; tmp->next = cur; pre->next = tmp; count++; return true; }

bool List::remove(int from) { Node *pre, *cur;

if (from count) return false; if (from == 1) { cur = head; head = head->next; count--; free(cur); return true; }

// normal case pre = find(from-1); cur = pre->next; pre->next = cur->next; free(cur);

count--; return true; }

void List::print() { Node *current = head;

while (current != NULL) { cout item; current = current->next; }

}

CA3_template.cpp

#include #include #include "List.h"

using namespace std;

int main() { List list1, list2, list3; int arr1[5] = { 3, 2, 6, 9, 1 }; //represent integer 32691 to store in list1 int arr2[5] = { 9, 6, 4, 8, 7 }; //represent integer 96487 to store in list2

cout

system("pause"); return 0; }

LIst.h

#ifndef List_type #define List_type

#include "Node.h"

struct List { int count; Node *head; Node *find(int); List(); void print(); bool empty(); int size(); bool get(int, int &); bool set(int, int); bool insert(int, int); bool remove(int); };

#endif

Node.h

#ifndef Node_type #define Node_type

struct Node { int item; Node *next; Node (int); };

#endif

DAuccd10241may2018 Assignmentl\test-roject-jan20191Debugitest-roject-jan2019.exe Integer store in list1: 32691 Integer store in list2: 96487 The following is the result of addition operation of two integers in list1 and list2: 32691 96487 129178 Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

2nd Edition

1597499471, 978-1597499477

More Books

Students also viewed these Databases questions