


a7q3.cc File
#include <iostream>
#include <cstring>
#include "ArrayList.h"
using namespace std;
// Algorithm copy(s)
// Pre: s :: refToChar
// Post: memory allocated on heap to store a copy
// Return: reference to new string
char *copy(char *s) {
char *temp = new char[strlen(s)+1];
strcpy(temp,s);
return temp;
}
void test_ListOperations(){
cout << "testing createList" << endl;
List *myList = createList(10);
if (myList == NULL){
cout << "createList failed" << endl;
return;
} else{
cout << "createList succeeded" << endl;
}
cout << "testing destroyList" << endl;
destroyList(myList);
cout << "testing destroyList succeeded" << endl;
cout << "testing emptyList with an empty list" <<
endl;
myList = createList(10);
if (! emptyList(myList)){
cout << "testing emptyList failed when an empty list was
given"<< endl;
return;
}
cout << "testing lengthList with an empty list" <<
endl;
if (lengthList(myList) != 0){
cout << "an empty list should have length 0" <<
endl;
return;
}
cout << "inserting an element at the tail of the list"
<< endl;
char firstElement[] = "first";
char *toInsert = copy(firstElement);
if (!(insertTail(myList, toInsert))){
cout << "insertTail failed to insert first element" <<
endl;
return;
}
if (emptyList(myList)){
cout << "testing emptyList failed when a non-empty list was
given"<< endl;
return;
}
if (lengthList(myList) != 1){
cout << "A list with 1 element should have length 1"<<
endl;
return;
}
cout << "inserting a second element at the tail of the
list" << endl;
char secondElement[] = "second";
toInsert = copy(secondElement);
if (!(insertTail(myList, toInsert))){
cout << "insertTail failed to insert second element" <<
endl;
return;
}
if (emptyList(myList)){
cout << "testing emptyList failed when a non-empty list was
given"<< endl;
return;
}
if (lengthList(myList) != 2){
cout << "A list with 2 elements should have length 2"<<
endl;
return;
}
cout << "deleting the tail of the list (going from 2 to 1
elements in the list)" << endl;
char * retrievedValue;
deleteTail(myList, &retrievedValue);
if (strcmp(retrievedValue, secondElement) != 0){
cout << "wrong value returned when deleting tail of the list"
<< endl;
return;
}
delete []retrievedValue;
if (emptyList(myList)){
cout << "testing emptyList failed when a non-empty list was
given"<< endl;
return;
}
if (lengthList(myList) != 1){
cout << "A list with 1 element should have length 1"<<
endl;
return;
}
cout << "deleting the tail of the list (going from 1 to 0
elements in the list)" << endl;
deleteTail(myList, &retrievedValue);
if (strcmp(retrievedValue, firstElement) != 0){
cout << "wrong value returned when deleting tail of the list"
<< endl;
return;
}
delete []retrievedValue;
if (!emptyList(myList)){
cout << "testing emptyList failed when a non-empty list was
given"<< endl;
return;
}
if (lengthList(myList) != 0){
cout << "A list with 0 elements should have length 0"<<
endl;
return;
}
char words[][6] = {"one", "two", "three", "four", "five",
"six",
"seven", "eight", "nine", "ten"};
cout << "inserting 10 elements" << endl;
for(int i = 0; i < 10; i++){
toInsert = copy(words[i]);
insertTail(myList, toInsert);
}
cout << "deleting 10 elements" << endl;
cout << "should print the words ten nine eight ... one"
<< endl;
for(int i = 0; i < 10; i++){
deleteTail(myList, &retrievedValue);
cout << retrievedValue << endl;
delete []retrievedValue;
}
destroyList(myList);
cout << "testing list operations complete!" <<
endl;
}
int main(){
test_ListOperations();
cout << "testing complete!" << endl;
return 0;
}
ArrayList.cc File
// Implementation of an array-based List ADT
// Michael Horsch and Ian McQuillan, for CMPT115 2012 WT2, March
2013
// Modified by Craig Thompson for CMPT115/117 WT2 2015
//
// This is only a partial implementation of the List ADT.
#include "ArrayList.h"
#include <cstring>
#include <iostream>
using namespace std;
// CONSTRUCTOR and DESTRUCTOR
// create a new empty list of specified size.
// pre: size is the desired maximum capacity of the list.
// returns: a pointer to newly created List, or NULL if List could
not be created.
List *createList(int size) {
List *l = new List;
if (l == NULL) {
return NULL;
}
l->data = new Element[size];
if (l-> data == NULL){
delete l;
return NULL;
}
l->capacity = size;
l->count = 0;
return l;
}
// destroy an existing list
// pre: a pointer to a List
// post: deallocates List . Does not deallocate contents! (may
cause a memory leak)
void destroyList(List* l) {
delete [] l->data;
delete l;
}
// STATUS
// check whether the list is empty
// pre: a pointer to a List.
// returns: true if List is empty, false otherwise.
bool emptyList(List* l) {
return (0 == l->count);
}
// STATUS
// check whether the list is empty
// pre: a pointer to a List.
// returns: true if List is empty, false otherwise.
bool fullList(List* l) {
return (l->capacity == l->count);
}
// return the number of elements in the list
// pre: a pointer to the List.
// returns: number of elements in the List.
int lengthList(List* l) {
return l->count;
}
// insert the element into the list at the end of the list
// pre: a pointer to a List, and an element to insert.
// post: the Element is placed in the List as long as long as the
list is not full.
// returns: true if successful, false otherwise.
bool insertTail(List* l, Element e) {
if (fullList(l)) {
return false;
} else {
l->data[l->count] = e;
l->count++;
return true;
}
}
// delete the element at the end of the list
// pre: a pointer to a list, and a pointer to an Element
// post: if list is non-empty, it removes the last element, and
deallocates memory
// associated with deleted element.
// returns: true if it deleted something, false otherwise.
bool deleteTail(List* l, Element *e) {
if (emptyList(l)) {
return false;
} else {
*e = l->data[l->count-1];
l->count--;
return e;
}
}
// eof
ArrayList.h File
//
// Data structure for List ADT, where the data stored are pointers
to Elements
//
// Michael Horsch and Ian McQuillan, for CMPT115 2012 WT2, March
2013
//
#include "Element.h"
#ifndef _LIST_H_
#define _LIST_H_
// a list contains a pointer to the head node and a count
struct List {
Element *data;
int count;
int capacity;
};
//
// the List Operations
//
// CONSTRUCTOR and DESTRUCTOR
// create a new empty list of specified size.
// pre: size is the desired maximum capacity of the list.
// returns: a pointer to newly created List, or NULL if List could
not be created.
List *createList(int size);
// destroy an existing list
// pre: a pointer to a List
// post: deallocates List. Does not deallocate contents! (may cause
a memory leak)
void destroyList(List*);
// STATUS
// check whether the list is empty
// pre: a pointer to a List.
// returns: true if List is empty, false otherwise.
bool emptyList(List*);
// return the number of elements in the list
// pre: a pointer to the List.
// returns: number of elements in the List.
int lengthList(List*);
// insert the element into the list at the end of the list
// pre: a pointer to a List, and a pointer to an existing
Element.
// post: the Element is placed in the List as long as the list is
not full.
// returns: true if successful, false otherwise.
bool insertTail(List*, Element);
// delete the element at the end of the list
// pre: a pointer to a list, and a pointer to an Element
// post: if list is non-empty, it removes the last element, and
deallocates memory
// associated with deleted element.
// returns: true if it deleted something, false otherwise.
bool deleteTail(List* l, Element *e);
#endif
Element.h File
//
// Element.h
// - define what type gets stored in list nodes
//
// MCH for CMPT115 2015 WT2
// Minor modifications for 2014 (mch)
//
#ifndef _ELEMENT_H_
#define _ELEMENT_H_
//change this depending upon what we want to store
typedef char * Element;
#endif
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
a7q3.cc File #include <iostream> #include <cstring> #include "ArrayList.h" using namespace std; // Algorithm copy(s) // Pre:...
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Node{
private:
int data;
Node* nextNodePtr;
public:
Node(){}
void setData(int d){
data = d;
}
int getData(){
return
data;
}
void setNextNodePtr(Node*
nodePtr){
nextNodePtr = nodePtr;
}
...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...
Use C++
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from the source to the destination. 3 void mystrncpy( ???) 25 26 27 28 29 11- 30 Find the first occurrance of char acter c within a string. 32 ??? mystrchr???) 34 35 36 37 38 39 / Find the last occurrance of character c within a string. 40 II 41 ??? mystrrchr ???) 42 43 45 int main() char userInput[ 81]; char...
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...
#include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node { char c; Node *next; }; class LinkedString { Node *head; public: LinkedString(); LinkedString(char[]); LinkedString(string); char charAt(int) const; string concat(const LinkedString &obj) const; bool isEmpty() const; int length() const; LinkedString substring(int, int) const; //added helper function to add to linekd list private: void add(char c); };...
Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...