Details For this assignment you will be developing a C++ program which implement
ID: 3876441 • Letter: D
Question
Details
For this assignment you will be developing a C++ program which implements a linked list. Your program will read in a
text file called “cmd.txt” that will instruct your program what operations to run. Your program will take this file as a
command line argument. Your program should implement a C++ class which will be used to represent your linked list. I
have provided the following files for you:
ll.h Defines the interface your class should implement
main.cpp This is a minimum file which contains the driving logic for your program
makefile This is the makefile which can be used to build your program
ll.cpp This is the implementation file for your linked list. This file is currently empty
cmd.txt This is the file that your program will read input from and determines what your program will do
output.txt Contains the expected output for a correctly implemented program which runs the provided
cmd.txt
Your Task
You are to look at the class defined in ll.h and create the corresponding implementation in ll.cpp . You may not
change any of the existing declarations but you may add more functions as you deem necesarry.
cmd File Format
The cmd file for this assignment consists of two columns of numbers. The first column indications the function that
should be called, and the second column indications the operand that that function may take. If the function takes no
operand then the entry will be blank. The first column number to function mapping is as follows
1 prepend
2 append
3 removeFront
4 removeBack
5 search
6 printlist
7 remove
How I will test your program
To test your program I will use your ll.h and ll.cpp files and combine them with my own main.cpp file. My version
of main.cpp will be similiar to the one that is provided with a few extra grading features and test cases added in.
Deliverables
Add ll.h , ll.cpp , main.cpp , makefile , and cmd.txt to a zip file called student_id.zip where student_id
has been replaced with your student id. Submit that file to the dropbox. The submitted file MUST be named correctly
and should only contain the specified files, no embeded folders or extraneous files.
Expections
No use of any prebuilt linked list libraries
Your code should be well formatted, points may be taken for sloppy code
Your output should match the output provided in output.txt
You must submit a zip file named as described above
Your code should be fairly robust and be able to handle obvious edge cases such as if I remove every element
from the list and begin to add new elements. You are responsible for comming up with and testing for possible
edge cases.
II.h
--------
#ifndef LL_H
#define LL_H
// include this library to use NULL, otherwise use nullptr instead
#include <cstddef>
// include iostream so anything that includes this file can use cout
#include <iostream>
// Struct which will be the building block of our list
struct node{
int val;
node* next;
};
// Linked list class definition
class LL{
public:
LL();
void prepend(int);
void append(int);
bool remove(int);
bool removeFront();
bool removeBack();
node* search(int);
void print();
private:
node* head;
};
#endif
main.cpp
--------------
#include <fstream> // Include to use ifstream
#include "ll.h" // Include so can access our class
using namespace std; // Include so we don't need to put std:: infront
// of cout and endl
int main(int argc, char* argv[])
{
LL myList;
ifstream input;
int cmd, argument, ret;
node* searchResult = NULL;
if(argc < 2)
{
cout << "useage: ./a1.out cmd.txt ";
return -1;
}
//input.open(argv[1]);
// while there is something to read from the file, read
while (input >> cmd)
{
// switch on the command we read from the file
switch (cmd)
{
// if the cmd requires a parameter, read it from the file and call the
// associated function
case 1:
input >> argument;
myList.prepend(argument);
cout << "Prepended " << argument << endl;
break;
case 2:
input >> argument;
myList.append(argument);
cout << "Apended " << argument << endl;
break;
case 3:
if(myList.removeFront())
{
cout << "Succesfully removed the front of the list ";
}
else
{
cout << "List is empty, nothing to remove ";
}
break;
case 4:
if(myList.removeBack())
{
cout << "Succesfully removed the back of the list ";
}
else
{
cout << "List is empty, nothing to remove ";
}
break;
case 5:
input >> argument;
searchResult = myList.search(argument);
if(NULL != searchResult)
cout << "Found " << searchResult->val <<" in list!"<<endl;
else
cout << "Did not find " << argument << " in the list"<<endl;
break;
case 6:
myList.print();
break;
case 7:
input >> argument;
cout << "Attempting to remove " << argument << endl;
if(myList.remove(argument))
{
cout << "Succesfully removed the element from the list ";
}
else
{
cout << "Could not remove the element from the list ";
}
break;
}
}
input.close();
return 0;
}
cmd.txt
-------------
1 11 32 362 42 52 66363636363636
output
Prepended 1
Prepended 2
Prepended 3
Your List: 3 -> 2 -> 1 -> NULL
Apended 4
Apended 5
Apended 6
Your List: 3 -> 2 -> 1 -> 4 -> 5 -> 6 -> NULL
Succesfully removed the front of the list
Your List: 2 -> 1 -> 4 -> 5 -> 6 -> NULL
Succesfully removed the front of the list
Your List: 1 -> 4 -> 5 -> 6 -> NULL
Succesfully removed the front of the list
Your List: 4 -> 5 -> 6 -> NULL
Succesfully removed the front of the list
Your List: 5 -> 6 -> NULL
Succesfully removed the front of the list
Your List: 6 -> NULL
Succesfully removed the front of the list
Your List: NULL
Explanation / Answer
here is your ll.cpp file : -------------------------->>>>>>>>>>>>>>>>>
#include "ll.h"
#include<iostream>
using namespace std;
LL::LL(){
head = NULL;
}
void LL::append(int val){
node* temp = new node;
temp->next = NULL;
temp->val = val;
if(head == NULL){
head = temp;
}else{
node *tmp = head;
while(tmp != NULL){
tmp = tmp->next;
}
tmp = temp;
}
}
void LL::prepend(int val){
node* temp = new node;
temp->val = val;
temp->next = head;
head = temp;
}
bool LL::remove(int val){
if(head == NULL){
return false;
}
node *tmp = head;
node *prev = NULL;
while(tmp != NULL){
if(tmp->val == val){
if(prev == NULL){
head = tmp->next;
delete tmp;
return true;
}else{
prev->next = tmp->next;
delete tmp;
return true;
}
}
}
return false;
}
bool LL::removeBack(){
if(head == NULL){
return false;
}
node *tmp = head;
node *prev = NULL;
while(tmp->next != NULL){
prev = tmp;
tmp = tmp->next;
}
prev->next = NULL;
delete tmp;
return true;
}
bool LL::removeFront(){
if(head == NULL){
return false;
}
node *tmp = head;
head = tmp->next;
delete tmp;
return true;
}
node* LL::search(int val){
if(head == NULL){
return NULL;
}
node *tmp = head;
node *prev = NULL;
while(tmp != NULL){
if(tmp->val == val){
return tmp;
}
}
}
void LL::print(){
node *tmp = head;
cout<<" Your List : ";
while(tmp != NULL){
cout<<tmp->val;
if(tmp->next != NULL){
cout<<" -> ";
}
tmp = tmp->next;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.