C++ Problem. I am providing the code. Just Please provide the new function and highlight it.
implement the functions replaceAt, seqSearch, and remove.
Test your new function too in main. Also, Test Old functions in main. Show the output.
Also, modify the functions accordingly which have "See Programming Exercise 22".
main.cpp :
#include <iostream>
using namespace std;
#include "arrayListTypetempl.h"
int main(){
arrayListType<int> intList;
arrayListType<char> charList;
intList.insertEnd(5);
intList.insertEnd(3);
intList.insertEnd(4);
intList.insertEnd(55);
charList.insertEnd('a');
charList.insertEnd('B');
charList.insertEnd('f');
intList.print();
charList.print();
return 0;
}
arrayListTypetempl.h :
#ifndef H_arrayListType
#define H_arrayListType
template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>&
operator=(const arrayListType<elemType>&);
//Overloads the assignment operator
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, const elemType& item) const;
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void removeAt(int location);
void retrieveAt(int location, elemType& retItem)
const;
void clearList();
void replaceAt(int location, const elemType& repItem);
int seqSearch(const elemType& searchItem);
void remove(const elemType& removeItem);
arrayListType(int size = 100);
arrayListType (const arrayListType<elemType>&
otherList);
//Copy constructor
~arrayListType();
//Destructor
//Deallocate the memory occupied by the array.
protected:
elemType *list; //array to hold the list elements
int length; //variable to store the length of the list
int maxSize; //variable to store the maximum
//size of the list
};
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
if (length == 0){
return true;
}
else {
return false;
}
} // //end isEmpty
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
cout << "See Programming Exercise 22." << endl;
return false;
} //end isFull
template <class elemType>
int arrayListType<elemType>::listSize() const
{
cout << "See Programming Exercise 22." << endl;
return -1;
} //end listSize
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
cout << "See Programming Exercise 22." << endl;
return -1;
} //end maxListSize
template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
} //end print
template <class elemType>
bool arrayListType<elemType>::isItemAtEqual(int
location,
const elemType& item) const
{
if (location < 0 || location >= length)
{
cout << "The location of the item to be removed "
<< "is out of range." << endl;
return false;
}
else
return (list[location] == item);
} //end isItemAtEqual
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cout << "The location of the item to be removed "
<< "is out of range." << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
} //end removeAt
template <class elemType>
void arrayListType<elemType>::retrieveAt(int location,
elemType& retItem) const
{
if (location < 0 || location >= length)
cout << "The location of the item to be retrieved is "
<< "out of range" << endl;
else
retItem = list[location];
} //end retrieveAt
template <class elemType>
void arrayListType<elemType>::clearList()
{
cout << "See Programming Exercise 22." << endl;
} //end clearList
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size <= 0)
{
cout << "The array size must be positive. Creating "
<< "an array of the size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
} //end constructor
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
} //end destructor
template <class elemType>
arrayListType<elemType>::arrayListType
(const arrayListType<elemType>& otherList)
{
cout << "See Programming Exercise 22." << endl;
}//end copy constructor
template <class elemType>
const arrayListType<elemType>&
arrayListType<elemType>::operator=
(const arrayListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-assignment
{
delete [] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize];
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
} //end overloading operatror=
template <class elemType>
void arrayListType<elemType>::insertAt(int location, const
elemType& insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted "
<< "is out of range." << endl;
else if (length >= maxSize) //list is full
cout << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at
//the specified position
length++; //increment the length
}
} //end insertAt
template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType&
insertItem)
{
if (length >= maxSize) //the list is full
cout << "Cannot insert in a full list." << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd
#endif
In: Computer Science
Using Java create a program that does the following: Modify the LinkedList1 class by adding sort() and reverse() methods. The reverse method reverses the order of the elements in the list, and the sort method rearranges the elements in the list so they are sorted in alphabetical order. Do not use recursion to implement either of these operations. Extend the graphical interface in the LinkedList1Demo class to support sort and reverse commands, and use it to test the new methods. This should have two separate source files. LinkedList1 Class and LinkedList1Demo.
Include these modifications:
LinkedList1 class:
/**
The LinkedList1 class implements a Linked list.
*/
class LinkedList1
{
/**
The Node class stores a list element
and a reference to the next node.
*/
private class Node
{
String value;
Node next;
/**
Constructor.
@param val The element to store in the node.
@param n The reference to the successor node.
*/
Node(String val, Node n)
{
value = val;
next = n;
}
/**
Constructor.
@param val The element to store in the node.
*/
Node(String val)
{
// Call the other (sister) constructor.
this(val, null);
}
}
private Node first; // list head
private Node last; // last element in list
/**
Constructor.
*/
public LinkedList1()
{
first = null;
last = null;
}
/**
The isEmpty method checks to see
if the list is empty.
@return true if list is empty,
false otherwise.
*/
public boolean isEmpty()
{
return first == null;
}
/**
The size method returns the length of the list.
@return The number of elements in the list.
*/
public int size()
{
int count = 0;
Node p = first;
while (p != null)
{
// There is an element at p
count ++;
p = p.next;
}
return count;
}
/**
The add method adds an element to
the end of the list.
@param e The value to add to the
end of the list.
*/
public void add(String e)
{
if (isEmpty())
{
first = new Node(e);
last = first;
}
else
{
// Add to end of existing list
last.next = new Node(e);
last = last.next;
}
}
/**
The add method adds an element at a position.
@param e The element to add to the list.
@param index The position at which to add
the element.
@exception IndexOutOfBoundsException When
index is out of bounds.
*/
public void add(int index, String e)
{
if (index < 0 || index > size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
// Index is at least 0
if (index == 0)
{
// New element goes at beginning
first = new Node(e, first);
if (last == null)
last = first;
return;
}
// Set a reference pred to point to the node that
// will be the predecessor of the new node
Node pred = first;
for (int k = 1; k <= index - 1; k++)
{
pred = pred.next;
}
// Splice in a node containing the new element
pred.next = new Node(e, pred.next);
// Is there a new last element ?
if (pred.next.next == null)
last = pred.next;
}
/**
The toString method computes the string
representation of the list.
@return The string form of the list.
*/
public String toString()
{
StringBuilder strBuilder = new StringBuilder();
// Use p to walk down the linked list
Node p = first;
while (p != null)
{
strBuilder.append(p.value + "\n");
p = p.next;
}
return strBuilder.toString();
}
/**
The remove method removes the element at an index.
@param index The index of the element to remove.
@return The element removed.
@exception IndexOutOfBoundsException When index is
out of bounds.
*/
public String remove(int index)
{
if (index < 0 || index >= size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
String element; // The element to return
if (index == 0)
{
// Removal of first item in the list
element = first.value;
first = first.next;
if (first == null)
last = null;
}
else
{
// To remove an element other than the first,
// find the predecessor of the element to
// be removed.
Node pred = first;
// Move pred forward index - 1 times
for (int k = 1; k <= index -1; k++)
pred = pred.next;
// Store the value to return
element = pred.next.value;
// Route link around the node to be removed
pred.next = pred.next.next;
// Check if pred is now last
if (pred.next == null)
last = pred;
}
return element;
}
/**
The remove method removes an element.
@param element The element to remove.
@return true if the remove succeeded,
false otherwise.
*/
public boolean remove(String element)
{
if (isEmpty())
return false;
if (element.equals(first.value))
{
// Removal of first item in the list
first = first.next;
if (first == null)
last = null;
return true;
}
// Find the predecessor of the element to remove
Node pred = first;
while (pred.next != null &&
!pred.next.value.equals(element))
{
pred = pred.next;
}
// pred.next == null OR pred.next.value is element
if (pred.next == null)
return false;
// pred.next.value is element
pred.next = pred.next.next;
// Check if pred is now last
if (pred.next == null)
last = pred;
return true;
}
public static void main(String [] args)
{
LinkedList1 ll = new LinkedList1();
ll.add("Amy");
ll.add("Bob");
ll.add(0, "Al");
ll.add(2, "Beth");
ll.add(4, "Carol");
System.out.println("The members of the list are:");
System.out.print(ll);
}
}
Program Output:
The members of the list are:
Al
Amy
Beth
Bob
Carol
In: Computer Science
In: Finance
Given the following reduction potential for potassium, which statement is correct? Li+ + e- → Li(s) E0 = -3.040 V
| . |
Li is a good reducing agent. |
|
| b. |
Li+ is a good oxidizing agent. |
|
| c. |
Li+ is a good reducing agent. |
|
| d. |
Li is a good oxidizing agent. |
In: Chemistry
Discussions
Do you think the harsh estimates China ( see interfaces in link) takes to dispose of white collar crime is pretty much powerful than our framework? https://www.businessinsider.com/chinas-madoff-was-executed-in-secret-2013-7
https://www.youtube.com/watch?v=YZI_5gMNnEU
In: Finance
You are to design a 24-V, all-DC, stand-alone PV system to meet a 2.4 kWh/d demand for a small, isolated cabin. You want to size the PV array to meet the load in a month with average insolation equal to 5.0 kWh/m 2/d
In: Electrical Engineering
Working from first principles, derive an expression for the efficiency of an Otto Cycle engine in terms of compression ratio. Sketch the P-V diagram for this cycle and annotate it using the same station numbers used in your derivation. State why the efficiency must be less than that of the Carnot Cycle.
In: Mechanical Engineering
Calculate the gas constant R.
MassMg = 0.053g
V = 45.8 mL
T = 297 K
Pressuretotal = 757 mmHg
molar mass Mg = 24.31 g/mol
molar mass H2 = 1.01 g/mol
Mg(s)+ 2HCl(g) ---> MgCl2(aq) + H2
In: Chemistry
A squared cross- sectional bar of 3cm by 3cm is under a compressive stress of 300 MPa. The bar deforms elastically, what is the area of the cross section after deformation. Provide your answer in milimeters squared. (Do not type in the unit in your answer.) (E=30GPa, v=0.4)
In: Mechanical Engineering
A current carrying gold wire has diameter of 0.84 mm. the electric field in the wire is 0.49 V/m. What is
a The current carried by the wire?
b The potential difference between two points in the wire 6.4 m apart?
c The resistance of a 6.4 m length of this wire.
In: Physics