Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ PROGRAMMING ONLY: I need two functions written so they pass the tests in the makefile, also given. Guidelines also given for the two functions.

C++ PROGRAMMING ONLY:

I need two functions written so they pass the tests in the makefile, also given. Guidelines also given for the two functions. I couldn't figure out the two.

Write a function InsertNth() which can insert a new node at any index within the list. The index should in the range [0...length] (but its not garanteed), and the new node should be inserted so as to be at that index.

For example: The linked list looks like this: 1 -> 3 InsertNth(1, 2) will insert 2 into the 1st index The result looks like this: 1 -> 2 -> 3

Keep note: If the requested index is out of bounds, do nothing Do not mess with function declaration Only code within the InsertNth function

AND

Write a RemoveDuplicates() function which deletes any duplicate nodes from the list. Ideally, the list should only be traversed once. Code only assesed on ascending order.

Keep Note:

Assume list is sorted like the examples above Make sure you consider this case 1 -> 1 -> 1 -> 1 Do not mess with the function declaration Only code within the RemoveDuplicates function

For example:

This one:

8 -> 9 -> 10 -> 11 8 -> 8 -> 9 -> 9 8 -> 8 -> 10 -> 42 empty

to

8 -> 9 -> 10 -> 11 8 -> 9 8 -> 10 -> 42 empty

Here is the code:

class Node { public: Node* next; int data; };

class LinkedList { public: LinkedList(); ~LinkedList(); void add(int data); void InsertNth(int index, int data); void RemoveDuplicates(); Node* head;

};

LinkedList::LinkedList() { this->head = nullptr; }

LinkedList::~LinkedList() { while(head != nullptr) { Node* temp = head->next; delete head; head = temp; } }

void LinkedList::add(int data) { Node* node = new Node(); node->data = data; node->next = this->head; this->head = node; }

void LinkedList::InsertNth(int index, int data) { //TODO }

void LinkedList::RemoveDuplicates() { //TODO }

HERE IS THE MAKEFILE :

CC = g++

CPPFLAGS = -g -Wall -std=c++11

BIN_DIR = bin

GTEST_LL = -I /usr/include/gtest/ -l gtest -l gtest_main -pthread

all: $(BIN_DIR) $(BIN_DIR)/DestructorTest $(BIN_DIR)/InsertNthTest $(BIN_DIR)/RemoveDuplicatesTest

valgrind --leak-check=yes ./$(BIN_DIR)/DestructorTest

valgrind --leak-check=yes ./$(BIN_DIR)/InsertNthTest

valgrind --leak-check=yes ./$(BIN_DIR)/RemoveDuplicatesTest

$(BIN_DIR)/DestructorTest: $(BIN_DIR)/LinkedList.o

$(CC) $(CPPFLAGS) $(BIN_DIR)/DestructorTest.o $^ $(GTEST_LL) -o $@

$(BIN_DIR)/InsertNthTest: $(BIN_DIR)/LinkedList.o

$(CC) $(CPPFLAGS) $(BIN_DIR)/InsertNthTest.o $^ $(GTEST_LL) -o $@

$(BIN_DIR)/RemoveDuplicatesTest: $(BIN_DIR)/LinkedList.o

$(CC) $(CPPFLAGS) $(BIN_DIR)/RemoveDuplicatesTest.o $^ $(GTEST_LL) -o $@

$(BIN_DIR)/LinkedList.o: LinkedList.cpp

$(CC) $(CPPFLAGS) -c $< -o $@

$(BIN_DIR):

mkdir $(BIN_DIR)

.phony: clean test

clean:

-@rm -rf $(BIN_DIR)

DestructorTest: $(BIN_DIR) $(BIN_DIR)/DestructorTest

valgrind --leak-check=yes ./$(BIN_DIR)/DestructorTest

InsertNthTest: $(BIN_DIR) $(BIN_DIR)/InsertNthTest

valgrind --leak-check=yes ./$(BIN_DIR)/InsertNthTest

RemoveDuplicatesTest: $(BIN_DIR) $(BIN_DIR)/RemoveDuplicatesTest

valgrind --leak-check=yes ./$(BIN_DIR)/RemoveDuplicatesTest

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_2

Step: 3

blur-text-image_3

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

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions

Question

1. Who will you assemble on the team?

Answered: 1 week ago