Can you convert this code into a C++ code? /////////////////////////////////////
ID: 3780603 • Letter: C
Question
Can you convert this code into a C++ code?
////////////////////////////////////////////
DVD.java
package movie;
public class DVD {
private String movieTitle;
private String movieStars[];
private String movieDirector;
private String movieProducer;
private String movieProductionCo;
private int numberOfCopies;
DVD(String title, String[] stars, String director, String producer, String production, int copies)
{
movieTitle = title;
movieStars = stars;
movieDirector = director;
movieProducer = producer;
movieProductionCo = production;
numberOfCopies = copies;
}
public String getMovieTitle() {
return movieTitle;
}
public void setMovieTitle(String movieTitle) {
this.movieTitle = movieTitle;
}
public String[] getMovieStars() {
return movieStars;
}
public void setMovieStars(String[] movieStars) {
this.movieStars = movieStars;
}
public String getMovieDirector() {
return movieDirector;
}
public void setMovieDirector(String movieDirector) {
this.movieDirector = movieDirector;
}
public String getMovieProducer() {
return movieProducer;
}
public void setMovieProducer(String movieProducer) {
this.movieProducer = movieProducer;
}
public String getMovieProductionCo() {
return movieProductionCo;
}
public void setMovieProductionCo(String movieProductionCo) {
this.movieProductionCo = movieProductionCo;
}
public int getNumberOfCopies() {
return numberOfCopies;
}
public void setNumberOfCopies(int numberOfCopies) {
this.numberOfCopies = numberOfCopies;
}
public void incrementCopies()
{
numberOfCopies++;
}
public boolean checkOut()
{
if(numberOfCopies>0)
{
numberOfCopies--;
return true;
}
else return false;
}
public void checkIn()
{
numberOfCopies++;
}
@Override
public String toString() {
return "Movie: " + movieTitle + ", Stars: " + movieStars[0] + " and " + movieStars[1] + ", Director: " + movieDirector +
", Producer: " + movieProducer + ", Production Company: "+movieProductionCo;
}
}
PersonType.java
package movie;
public class PersonType {
private String firstName;
private String lastName;
PersonType(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Customer.java
package movie;
import java.util.ArrayList;
import java.util.List;
public class Customer extends PersonType{
List<DVD> rentedDVDs;
List<Integer> rentalCount;
int accountNumber;
private static int counter = 1001;
Customer(String firstName, String lastName) {
super(firstName, lastName);
rentedDVDs = new ArrayList<DVD>();
rentalCount = new ArrayList<Integer>();
accountNumber = counter++;
}
public int getAccountNumber()
{
return accountNumber;
}
public String getName()
{
return getFirstName() + " " + getLastName();
}
public void setName(String firstName, String lastName)
{
setFirstName(firstName);
setLastName(lastName);
}
public void rentDVD(DVD dvd)
{
for(int i=0; i<rentedDVDs.size(); i++)
{
if(rentedDVDs.get(i).getMovieTitle().equals(dvd.getMovieTitle()))
{
rentalCount.set(i, rentalCount.get(i)+1);
return;
}
}
rentedDVDs.add(dvd);
rentalCount.add(1);
}
public void returnDVD(DVD dvd)
{
for(int i=0; i<rentedDVDs.size(); i++)
{
if(rentedDVDs.get(i).getMovieTitle().equals(dvd.getMovieTitle()))
{
if(rentalCount.get(i)>1)
rentalCount.set(i, rentalCount.get(i)-1);
else if(rentalCount.get(i)==1)
{
rentalCount.remove(i);
rentedDVDs.remove(i);
}
return;
}
}
System.out.println("The DVD was not rented to the customer !");
}
public int rentalCount()
{
int total = 0;
for(int i: rentalCount)
total += i;
return total;
}
@Override
public String toString() {
return "Customer Name : " + getName() + "Account Number: " + accountNumber + "Total rental Count: " + rentalCount();
}
}
DVDStore.java
package movie;
import java.util.ArrayList;
import java.util.List;
public class DVDStore {
List<DVD> listOfDVDs;
List<Customer> customers;
DVDStore()
{
listOfDVDs = new ArrayList<DVD>();
customers = new ArrayList<Customer>();
}
public void addDVD(DVD newdvd)
{
DVD dvd = searchDVD(newdvd.getMovieTitle());
if(dvd == null)
{
listOfDVDs.add(newdvd);
}
else
listOfDVDs.get(listOfDVDs.indexOf(newdvd)).incrementCopies();
}
public void addCustomer(Customer customer)
{
customers.add(customer);
}
public DVD searchDVD(String title)
{
DVD dvd = null;
for(DVD currentDVD: listOfDVDs)
{
if(currentDVD.getMovieTitle().equals(title))
{
dvd = currentDVD;
}
}
return dvd;
}
private int findCustomer(int accountNumber)
{
for(int i=0; i<customers.size(); i++)
{
if(customers.get(i).getAccountNumber() == accountNumber)
{
return i;
}
}
return -1;
}
public boolean rentDVD(String title, int accountNumber)
{
DVD dvd = searchDVD(title);
boolean rentalStatus = false;
if(dvd != null)
{
int DVDIndex = listOfDVDs.indexOf(dvd);
int CustomerIndex = findCustomer(accountNumber);
if(CustomerIndex != -1)
{
dvd.checkOut();
listOfDVDs.set(DVDIndex, dvd);
rentalStatus = true;
customers.get(CustomerIndex).rentDVD(dvd);
}
else
System.out.println("Customer not found in the database !");
}
return rentalStatus;
}
public boolean returnDVD(String title, int accountNumber)
{
DVD dvd = searchDVD(title);
boolean returnStatus = false;
if(dvd != null)
{
int DVDIndex = listOfDVDs.indexOf(dvd);
int CustomerIndex = findCustomer(accountNumber);
if(CustomerIndex != -1)
{
dvd.checkIn();
listOfDVDs.set(DVDIndex, dvd);
returnStatus = true;
customers.get(CustomerIndex).returnDVD(dvd);
}
else
System.out.println("Customer not found in the database !");
}
return returnStatus;
}
public void printAllDVDs()
{
for(DVD dvd: listOfDVDs)
{
System.out.println(dvd);
}
}
public void printMovieTitles()
{
for(DVD dvd: listOfDVDs)
{
System.out.println(dvd.getMovieTitle());
}
}
}
DVDDriver.java
package movie;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DVDDriver {
public static void menu(DVDStore store)
{
System.out.println("1. Search a DVD");
System.out.println("2. Check out a DVD");
System.out.println("3. Check in a DVD");
System.out.println("4. Check whether a DVD is in stock");
System.out.println("5. Print all titles of DVD");
System.out.println("6. Print all the DvDs");
System.out.println("9. Exit");
System.out.println("Enter your choice: ");
Scanner consoleInt = new Scanner(System.in);
Scanner consoleString = new Scanner(System.in);
int choice = consoleInt.nextInt();
int accNumber;
DVD dvd = null;
String title = "";
if(choice != 9 && choice != 5 && choice != 6)
{
System.out.println("Enter the movie title: ");
title = consoleString.nextLine();
}
switch(choice)
{
case 1:
dvd = store.searchDVD(title);
if(dvd != null)
{
System.out.println(dvd);
}
menu(store);
break;
case 2: System.out.println("Enter account number of Customer: ");
accNumber = consoleInt.nextInt();
if(store.rentDVD(title, accNumber))
System.out.println("Movie rented !");
menu(store);
break;
case 3: System.out.println("Enter account number of Customer: ");
accNumber = consoleInt.nextInt();
store.returnDVD(title, accNumber);
System.out.println("Movie returned !");
menu(store);
break;
case 4: dvd = store.searchDVD(title);
if(dvd != null)
{
if(dvd.getNumberOfCopies()>0)
System.out.println(title + " is in stock");
}
menu(store);
break;
case 5:store.printMovieTitles();
menu(store);
break;
case 6: store.printAllDVDs();
menu(store);
break;
case 9: return;
}
}
public static void main(String[] args) {
DVDStore store = new DVDStore();
try {
Scanner filereader = new Scanner(new File("movies.txt"));
while(filereader.hasNextLine())
{
String title = filereader.nextLine().replace("DVD ", "");
String star1 = filereader.nextLine().replace("movie ", "");
String star2 = filereader.nextLine().replace("movie ", "");
String producer = filereader.nextLine().replace("movie ", "");
String director = filereader.nextLine().replace("movie ", "");
String production = filereader.nextLine().replace("movie ", "");
String copies = filereader.nextLine();
store.addDVD(new DVD(title, new String[]{star1, star2}, director, producer, production, Integer.valueOf(copies)));
}
} catch (FileNotFoundException e) {
System.out.println("File format is wrong !");
e.printStackTrace();
}
System.out.println("Enter a customer first name: ");
Scanner consoleReader = new Scanner(System.in);
String fName = consoleReader.next();
System.out.println("Enter a customer last name: ");
String lname = consoleReader.next();
Customer customer = new Customer(fName, lname);
store.addCustomer(customer);
menu(store);
}
}
Explanation / Answer
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "dvdType.h"
#include "dvdListType.h"
using namespace std;
void createDVDList(ifstream& infile,
dvdListType& dvdList);
void displayMenu();
int main()
{
dvdListType dvdList;
int choice;
char ch;
string title;
ifstream infile;
//open the input file
infile.open("dvdDat.txt");
if (!infile)
{
cout << "The input file does not exist. "
<< "The program terminates!!!" << endl;
return 1;
}
//create the DVD list
createDVDList(infile, dvdList);
infile.close();
//show the menu
displayMenu();
cout << "Enter your choice: ";
cin >> choice; //get the request
cin.get(ch);
cout << endl;
//process the requests
while (choice != 9)
{
switch (choice)
{
case 1:
cout << "Enter the title: ";
getline(cin, title);
cout << endl;
if (dvdList.dvdSearch(title))
cout << "The store carries " << title
<< endl;
else
cout << "The store does not carry "
<< title << endl;
break;
case 2:
cout << "Enter the title: ";
getline(cin, title);
cout << endl;
if (dvdList.dvdSearch(title))
{
if (dvdList.isDVDAvailable(title))
{
dvdList.dvdCheckOut(title);
cout << "Enjoy your movie: "
<< title << endl;
}
else
cout << "Currently " << title
<< " is out of stock." << endl;
}
else
cout << "The store does not carry "
<< title << endl;
break;
case 3:
cout << "Enter the title: ";
getline(cin, title);
cout << endl;
if (dvdList.dvdSearch(title))
{
dvdList.dvdCheckIn(title);
cout << "Thanks for returning "
<< title << endl;
}
else
cout << "The store does not carry "
<< title << endl;
break;
case 4:
cout << "Enter the title: ";
getline(cin, title);
cout << endl;
if (dvdList.dvdSearch(title))
{
if (dvdList.isDVDAvailable(title))
cout << title << " is currently in "
<< "stock." << endl;
else
cout << title << " is currently out "
<< "of stock." << endl;
}
else
cout << "The store does not carry "
<< title << endl;
break;
case 5:
dvdList.dvdPrintTitle();
break;
case 6:
dvdList.print();
break;
default:
cout << "Invalid selection." << endl;
}//end switch
displayMenu(); //display menu
cout << "Enter your choice: ";
cin >> choice; //get the next request
cin.get(ch);
cout << endl;
}//end while
return 0;
}
void createDVDList(ifstream& infile,
dvdListType& dvdList)
{
string title;
string star1;
string star2;
string producer;
string director;
string productionCo;
char ch;
int inStock;
dvdType newDVD;
getline(infile, title);
while (infile)
{
getline(infile, star1);
getline(infile, star2);
getline(infile, producer);
getline(infile, director);
getline(infile, productionCo);
infile >> inStock;
infile.get(ch);
newDVD.setDVDInfo(title, star1, star2, producer,
director, productionCo, inStock);
dvdList.insertFirst(newDVD);
getline(infile, title);
}//end while
}//end createDVDList
void displayMenu()
{
cout << "Select one of the following:" << endl;
cout << "1: To check whether the store carries a "
<< "particular DVD." << endl;
cout << "2: To check out a DVD." << endl;
cout << "3: To check in a DVD." << endl;
cout << "4: To check whether a particular DVD is "
<< "in stock." << endl;
cout << "5: To print only the titles of all the DVDs."
<< endl;
cout << "6: To print a list of all the DVDs." << endl;
cout << "9: To exit" << endl;
} //end displayMenu
dvdListType.h
#ifndef H_DVDLinkedListType
#define H_DVDLinkedListType
#include <string>
#include "unorderedLinkedList.h"
#include "dvdType.h"
using namespace std;
class dvdListType:public unorderedLinkedList<dvdType>
{
public:
bool dvdSearch(string title) const;
bool isDVDAvailable(string title) const;
void dvdCheckOut(string title);
void dvdCheckIn(string title);
bool dvdCheckTitle(string title) const;
void dvdUpdateInStock(string title, int num);
void dvdSetCopiesInStock(string title, int num);
void dvdPrintTitle() const;
private:
void searchDVDList(string title, bool& found,
nodeType<dvdType>* ¤t) const;
};
#endif
dvdListTypeImp.cpp
#include <iostream>
#include <string>
#include "dvdListType.h"
using namespace std;
void dvdListType::searchDVDList(string title, bool& found,
nodeType<dvdType>* ¤t) const
{
found = false; //set found to false
current = first; //set current to point to the first node
//in the list
while (current != nullptr && !found) //search the list
if (current->info.checkTitle(title)) //the item is found
found = true;
else
current = current->link; //advance current to
//the next node
}//end searchDVDList
bool dvdListType::isDVDAvailable(string title) const
{
bool found;
nodeType<dvdType> *location;
searchDVDList(title, found, location);
if (found)
found = (location->info.getNoOfCopiesInStock() > 0);
else
found = false;
return found;
}
void dvdListType::dvdCheckIn(string title)
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location); //search the list
if (found)
location->info.checkIn();
else
cout << "The store does not carry " << title
<< endl;
}
void dvdListType::dvdCheckOut(string title)
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location); //search the list
if (found)
location->info.checkOut();
else
cout << "The store does not carry " << title
<< endl;
}
bool dvdListType::dvdCheckTitle(string title) const
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location); //search the list
return found;
}
void dvdListType::dvdUpdateInStock(string title, int num)
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location); //search the list
if (found)
location->info.updateInStock(num);
else
cout << "The store does not carry " << title
<< endl;
}
void dvdListType::dvdSetCopiesInStock(string title, int num)
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location);
if (found)
location->info.setCopiesInStock(num);
else
cout << "The store does not carry " << title
<< endl;
}
bool dvdListType::dvdSearch(string title) const
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location);
return found;
}
void dvdListType::dvdPrintTitle() const
{
nodeType<dvdType>* current;
current = first;
while (current != nullptr)
{
current->info.printTitle();
current = current->link;
}
}
dvdType.h
#ifndef H_dvdType
#define H_dvdType
#include <iostream>
#include <string>
using namespace std;
class dvdType
{
friend ostream& operator<< (ostream&, const dvdType&);
public:
void setDVDInfo(string title, string star1,
string star2, string producer,
string director, string productionCo,
int setInStock);
int getNoOfCopiesInStock() const;
void checkOut();
void checkIn();
void printTitle() const;
void printInfo() const;
bool checkTitle(string title);
void updateInStock(int num);
void setCopiesInStock(int num);
string getTitle() const;
dvdType(string title = "", string star1 = "",
string star2 = "", string producer = "",
string director = "", string productionCo = "",
int setInStock = 0);
//Overload the relational operators.
bool operator==(const dvdType&) const;
bool operator!=(const dvdType&) const;
private:
string dvdTitle; //variable to store the name
//of the movie
string movieStar1; //variable to store the name
//of the star
string movieStar2; //variable to store the name
//of the star
string movieProducer; //variable to store the name
//of the producer
string movieDirector; //variable to store the name
//of the director
string movieProductionCo; //variable to store the name
//of the production company
int copiesInStock; //variable to store the number of
//copies in stock
};
#endif
dvdTypeImp.cpp
#include <iostream>
#include <string>
#include "dvdType.h"
using namespace std;
void dvdType::setDVDInfo(string title, string star1,
string star2, string producer,
string director,
string productionCo,
int setInStock)
{
dvdTitle = title;
movieStar1 = star1;
movieStar2 = star2;
movieProducer = producer;
movieDirector = director;
movieProductionCo = productionCo;
copiesInStock = setInStock;
}
void dvdType::checkOut()
{
if (getNoOfCopiesInStock() > 0)
copiesInStock--;
else
cout << "Currently out of stock" << endl;
}
void dvdType::checkIn()
{
copiesInStock++;
}
int dvdType::getNoOfCopiesInStock() const
{
return copiesInStock;
}
void dvdType::printTitle() const
{
cout << "DVD Title: " << dvdTitle << endl;
}
void dvdType::printInfo() const
{
cout << "DVD Title: " << dvdTitle << endl;
cout << "Stars: " << movieStar1 << " and "
<< movieStar2 << endl;
cout << "Producer: " << movieProducer << endl;
cout << "Director: " << movieDirector << endl;
cout << "Production Company: " << movieProductionCo
<< endl;
cout << "Copies in stock: " << copiesInStock
<< endl;
}
bool dvdType::checkTitle(string title)
{
return(dvdTitle == title);
}
void dvdType::updateInStock(int num)
{
copiesInStock += num;
}
void dvdType::setCopiesInStock(int num)
{
copiesInStock = num;
}
string dvdType::getTitle() const
{
return dvdTitle;
}
dvdType::dvdType(string title, string star1,
string star2, string producer,
string director,
string productionCo, int setInStock)
{
setDVDInfo(title, star1, star2, producer, director,
productionCo, setInStock);
}
bool dvdType::operator==(const dvdType& other) const
{
return (dvdTitle == other.dvdTitle);
}
bool dvdType::operator!=(const dvdType& other) const
{
return (dvdTitle != other.dvdTitle);
}
ostream& operator<< (ostream& osObject, const dvdType& dvd)
{
osObject << endl;
osObject << "DVD Title: " << dvd.dvdTitle << endl;
osObject << "Stars: " << dvd.movieStar1 << " and "
<< dvd.movieStar2 << endl;
osObject << "Producer: " << dvd.movieProducer << endl;
osObject << "Director: " << dvd.movieDirector << endl;
osObject << "Production Company: "
<< dvd.movieProductionCo << endl;
osObject << "Copies in stock: " << dvd.copiesInStock
<< endl;
osObject << "_____________________________________"
<< endl;
return osObject;
}
linkedList.h
#ifndef H_LinkedListType
#define H_LinkedListType
#include <iostream>
#include <cassert>
using namespace std;
//Definition of the node
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class linkedListIterator
{
public:
linkedListIterator();
linkedListIterator(nodeType<Type> *ptr);
Type operator*();
linkedListIterator<Type> operator++();
bool operator==(const linkedListIterator<Type>& right) const;
bool operator!=(const linkedListIterator<Type>& right) const;
private:
nodeType<Type> *current; //pointer to point to the current
//node in the linked list
};
template <class Type>
linkedListIterator<Type>::linkedListIterator()
{
current = nullptr;
}
template <class Type>
linkedListIterator<Type>::
linkedListIterator(nodeType<Type> *ptr)
{
current = ptr;
}
template <class Type>
Type linkedListIterator<Type>::operator*()
{
return current->info;
}
template <class Type>
linkedListIterator<Type> linkedListIterator<Type>::operator++()
{
current = current->link;
return *this;
}
template <class Type>
bool linkedListIterator<Type>::operator==
(const linkedListIterator<Type>& right) const
{
return (current == right.current);
}
template <class Type>
bool linkedListIterator<Type>::operator!=
(const linkedListIterator<Type>& right) const
{ return (current != right.current);
}
template <class Type>
class linkedListType
{
public:
const linkedListType<Type>& operator=
(const linkedListType<Type>&);
void initializeList();
bool isEmptyList() const;
void print() const;
int length() const;
void destroyList();
Type front() const;
Type back() const;
virtual bool search(const Type& searchItem) const = 0;
virtual void insertFirst(const Type& newItem) = 0;
virtual void insertLast(const Type& newItem) = 0;
virtual void deleteNode(const Type& deleteItem) = 0;
linkedListIterator<Type> begin();
linkedListIterator<Type> end();
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
//copy constructor
~linkedListType();
protected:
int count; //variable to store the number of
//elements in the list
nodeType<Type> *first; //pointer to the first node of the list
nodeType<Type> *last; //pointer to the last node of the list
private:
void copyList(const linkedListType<Type>& otherList);
};
template <class Type>
bool linkedListType<Type>::isEmptyList() const
{
return(first == nullptr);
}
template <class Type>
linkedListType<Type>::linkedListType() //default constructor
{
first = nullptr;
last = nullptr;
count = 0;
}
template <class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp; //pointer to deallocate the memory
//occupied by the node
while (first != nullptr) //while there are nodes in the list
{
temp = first; //set temp to the current node
first = first->link; //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
last = nullptr; //initialize last to nullptr; first has already
//been set to nullptr by the while loop
count = 0;
}
template <class Type>
void linkedListType<Type>::initializeList()
{
destroyList(); //if the list has any nodes, delete them
}
template <class Type>
void linkedListType<Type>::print() const
{
nodeType<Type> *current; //pointer to traverse the list
current = first; //set current so that it points to
//the first node
while (current != nullptr) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print
template <class Type>
int linkedListType<Type>::length() const
{
return count;
} //end length
template <class Type>
Type linkedListType<Type>::front() const
{
assert(first != nullptr);
return first->info; //return the info of the first node
}//end front
template <class Type>
Type linkedListType<Type>::back() const
{
assert(last != nullptr);
return last->info; //return the info of the last node
}//end back
template <class Type>
linkedListIterator<Type> linkedListType<Type>::begin()
{
linkedListIterator<Type> temp(first);
return temp;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::end()
{
linkedListIterator<Type> temp(nullptr);
return temp;
}
template <class Type>
void linkedListType<Type>::copyList
(const linkedListType<Type>& otherList)
{
nodeType<Type> *newNode; //pointer to create a node
nodeType<Type> *current; //pointer to traverse the list
if (first != nullptr) //if the list is nonempty, make it empty
destroyList();
if (otherList.first == nullptr) //otherList is empty
{
first = nullptr;
last = nullptr;
count = 0;
}
else
{
current = otherList.first; //current points to the
//list to be copied
count = otherList.count;
//copy the first node
first = new nodeType<Type>; //create the node
first->info = current->info; //copy the info
first->link = nullptr; //set the link field of
//the node to nullptr
last = first; //make last point to the
//first node
current = current->link; //make current point to
//the next node
//copy the remaining list
while (current != nullptr)
{
newNode = new nodeType<Type>; //create a node
newNode->info = current->info; //copy the info
newNode->link = nullptr; //set the link of
//newNode to nullptr
last->link = newNode; //attach newNode after last
last = newNode; //make last point to
//the actual last node
current = current->link; //make current point
//to the next node
}//end while
}//end else
}//end copyList
template <class Type>
linkedListType<Type>::~linkedListType() //destructor
{
destroyList();
}//end destructor
template <class Type>
linkedListType<Type>::linkedListType
(const linkedListType<Type>& otherList)
{
first = nullptr;
copyList(otherList);
}//end copy constructor
//overload the assignment operator
template <class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
(const linkedListType<Type>& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
}//end else
return *this;
}
#endif
unorderedLinkedList.h
#ifndef H_UnorderedLinkedList
#define H_UnorderedLinkedList
#include "linkedList.h"
using namespace std;
template <class Type>
class unorderedLinkedList: public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the
// list, otherwise the value false is
// returned.
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
};
template <class Type>
bool unorderedLinkedList<Type>::
search(const Type& searchItem) const
{
nodeType<Type> *current; //pointer to traverse the list
bool found = false;
current = first; //set current to point to the first
//node in the list
while (current != nullptr && !found) //search the list
if (current->info == searchItem) //searchItem is found
found = true;
else
current = current->link; //make current point to
//the next node
return found;
}//end search
template <class Type>
void unorderedLinkedList<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = first; //insert newNode before first
first = newNode; //make first point to the
//actual first node
count++; //increment count
if (last == nullptr) //if the list was empty, newNode is also
//the last node in the list
last = newNode;
}//end insertFirst
template <class Type>
void unorderedLinkedList<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = nullptr; //set the link field of newNode
//to nullptr
if (first == nullptr) //if the list is empty, newNode is
//both the first and last node
{
first = newNode;
last = newNode;
count++; //increment count
}
else //the list is not empty, insert newNode after last
{
last->link = newNode; //insert newNode after last
last = newNode; //make last point to the actual
//last node in the list
count++; //increment count
}
}//end insertLast
template <class Type>
void unorderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if (first == nullptr) //Case 1; the list is empty.
cout << "Cannot delete from an empty list."
<< endl;
else
{
if (first->info == deleteItem) //Case 2
{
current = first;
first = first->link;
count--;
if (first == nullptr) //the list has only one node
last = nullptr;
delete current;
}
else //search the list for the node with the given info
{
found = false;
trailCurrent = first; //set trailCurrent to point
//to the first node
current = first->link; //set current to point to
//the second node
while (current != nullptr && !found)
{
if (current->info != deleteItem)
{
trailCurrent = current;
current = current-> link;
}
else
found = true;
}//end while
if (found) //Case 3; if found, delete the node
{
trailCurrent->link = current->link;
count--;
if (last == current) //node to be deleted
//was the last node
last = trailCurrent; //update the value
//of last
delete current; //delete the node from the list
}
else
cout << "The item to be deleted is not in "
<< "the list." << endl;
}//end else
}//end else
}//end deleteNode
#endif
dvdDat.txt
Titanic
Kate Winslet
Leonardo DiCaprio
Cameron
Cameron
20th Century Fox
2
One Fine Day
George Clooney
Michelle Pfeiffer
Obset
Hoffman
20th Century Fox
19
Sister Act
Whoopi GoldBerg
Maggie Smith
Schwartz
Ardolino
Touch Stone Pictures
14
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.