Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ADDS Practical Exam Please download the adds-pe.cpp file from the following URL and make changes to answer these three questions. You should only upload the

ADDS Practical Exam

Please download the adds-pe.cpp file from the following URL and make changes to answer these three questions.

You should only upload the modified adds-pe.cpp file to your svn repositoryand submit to the websubmission system. The test script will compile your code using g++ -o main.out -std=c++11 -O2 -Wall *.cpp.

In this practical exam, you are asked to realise the following three operations based on the given data structure. The example output of each function is given in the source file.

Question 1: (1 mark for style, 2 mark for functionality)

Please fill in the function Node* add(Node* head, int val) which accepts a head pointer to a list and an integer. A new node is created with the given value and added to the front of the list. The new head pointer is returned.

Question 2: (1 mark for style, 2 mark for functionality)

Please fill in the function void printOdd(Node* head) which accept a head pointer to a list and print out the elements in odd position using std::cout. The head node is defined to be in position 1.

Question 3: (1 mark for style, 3 mark for functionality)

Please fill in the function void clear(Node* n1, Node* n2). In this function, n1 and n2 pointing to two nodes in the same list. Your task is to remove all nodes between these two nodes. And for all deleted nodes, you need to free the memory in the heap.

If there is no node in between n1 and n2, your function should do nothing.

image text in transcribedimage text in transcribedimage text in transcribed

adds-pe-1.cpp 1 include 2 using namespace std; 3 4 / General instructions: Please modify this file and submit the modified one via svn and then websubmission (under the entry "pracExam"). No design documents are needed. struct Node f 9 10 int data; Node* next; 12 13 14 /* Task 1: ImpLement the following function for adding a new node to the 15 16 17 18 19 20 21 front of the given list input Nodes* head: a head pointer to a list int val: an integer that represent the value of the new node return the head pointer of the new list after the insertion Example: add a node of value 9 before after HEAD->1-2->3-NULL 23 24 25 26 27*1 28 29 Nodex add(Node* head, int val){ 30 HEAD?9?1?2?3?NULL 32 34 35 /* Task 2: Implement the following function for printing the elements 36 37 38 39 40 41 42 43 in odd position of the given list. Assume the head node is defined to be in position 1. input Nodes* head: a head pointer to a list return void output The elements in odd position of the given list Example: input HEAD->1?2?3->9-12->NULL 45 46 utput HEAD->1-NA->3->NA->12->NULL

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

Students also viewed these Databases questions