Question

Add this four functions to the program:   push_back, pop_back   push_front, pop_front The functions should add or...

Add this four functions to the program:

  push_back, pop_back

  push_front, pop_front

The functions should add or delete from the front or back. The array will need to be resized and copied.

Program:

main.cpp:

#include <iostream> //Input/Output Library
using namespace std;

//User Libraries
#include "SimpleObject.h"

//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...

//Function Prototypes
Simple *fillSmp(int);
void prntSmp(Simple *,int);
void dstrSmp(Simple *);
void push_front(Simple *,int&n,int);
//Execution Begins Here!
int main(int argc, char** argv) {
//Declare Variables and Initialize
int size=5;
Simple *simple=fillSmp(size);
  
//Display the outputs
prntSmp(simple,size);

//Reallocate Memory
dstrSmp(simple);

//Exit stage right or left!
return 0;
}

void dstrSmp(Simple *s){
delete []s;
}

void prntSmp(Simple *s,int n){
for(int i=0;i<n;i++){
cout<<s[i].getData()<<endl;
}
}

Simple *fillSmp(int n){
Simple *simp=new Simple[n];
for(int i=0;i<n;i++){
simp[i].setData(i);
}
return simp;
}

//////////////////////////////////

SimpleObject.h:


#ifndef SIMPLEOBJECT_H
#define SIMPLEOBJECT_H


class Simple{
private:
int data;
public:
void setData(int d){data=d;};
int getData(){return data;};
};

#endif /* SIMPLEOBJECT_H */

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// SimpleObject.h
#ifndef SIMPLEOBJECT_H
#define SIMPLEOBJECT_H

class Simple{
private:
int data;
public:
void setData(int d){data=d;};
int getData(){return data;};
};


#endif // SIMPLEOBJECT_H

//end of SimpleObject.h

// main.cpp
#include <iostream> //Input/Output Library
using namespace std;

//User Libraries
#include "SimpleObject.h"

//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
//Function Prototypes

Simple *fillSmp(int);
void prntSmp(Simple *,int);
void dstrSmp(Simple *);
void push_front(Simple *&s,int&n,int);
void push_back(Simple *&s,int&n,int);
void pop_front(Simple *&s,int &n);
void pop_back(Simple *&s,int &n);


//Execution Begins Here!
int main(int argc, char** argv)
{
//Declare Variables and Initialize
int size=5;
Simple *simple=fillSmp(size);

//Display the outputs
prntSmp(simple,size);
cout<<"After push back"<<endl;
push_back(simple,size,10);
prntSmp(simple,size);
cout<<"After push front"<<endl;
push_front(simple,size,12);
prntSmp(simple,size);
cout<<"After pop front"<<endl;
pop_front(simple,size);
prntSmp(simple,size);
cout<<"After pop back"<<endl;
pop_back(simple,size);
prntSmp(simple,size);
//Reallocate Memory
dstrSmp(simple);
//Exit stage right or left!
return 0;

}

void dstrSmp(Simple *s){
delete []s;
}

void prntSmp(Simple *s,int n){
for(int i=0;i<n;i++){
cout<<s[i].getData()<<endl;
}
}

Simple *fillSmp(int n){
Simple *simp=new Simple[n];
for(int i=0;i<n;i++){
simp[i].setData(i);
}
return simp;
}

// function to push data at the front of s
void push_front(Simple *&s,int &n, int data)
{
Simple *temp = new Simple[n+1]; // create a new Simple array with size n+1
// copy the values from s to temp from index 1 in temp
for(int i=0;i<n;i++)
{
temp[i+1] = s[i];
}
temp[0].setData(data); // set the data of index 0 to data
// delete s
delete [] s;
s = temp; // point s to temp
n++; // increment n
}

// function to push the data at the back of s
void push_back(Simple *&s,int &n,int data)
{
Simple *temp = new Simple[n+1]; // create a new Simple array with size n+1
// copy the values from s to temp from index 0 in temp
for(int i=0;i<n;i++)
{
temp[i] = s[i];
}
temp[n].setData(data); // set the data of index n to data
delete [] s; // delete s
s = temp; // point s to temp
n++; // increment n
}

// function to remove the data at index 0
void pop_front(Simple *&s,int &n)
{
Simple *temp = new Simple[n-1]; // create a new Simple array with size n-1
// copy the data from s to temp starting from index 1
for(int i=1;i<n;i++)
temp[i-1] = s[i];
delete [] s; // delete s
n--; // decrement n
s = temp; // point s to temp
}

// function to remove the data at index n-1
void pop_back(Simple *&s,int &n)
{
Simple *temp = new Simple[n-1]; // create a new Simple array with size n-1
// copy the data from s to temp starting from 0 to n-2
for(int i=0;i<n-1;i++)
temp[i] = s[i];
delete [] s; // delete s
n--; // decrement n
s = temp; // point s to temp
}

//end of main.cpp

Output:

Add a comment
Know the answer?
Add Answer to:
Add this four functions to the program:   push_back, pop_back   push_front, pop_front The functions should add or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for...

    Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for push_back() function as below: void push_back(string str) { // increase vector size by one // initialize the new element with str } In addition, the standard library vector doesn't provide push_front(). Implement push_front() for your vector. Test your code with the main function below. int main() {    vector v1(3);    cout<<"v1: ";    v1.print(); // this should display -, -, -    for...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • above in the image is a linked list with 2 nodes. You can use this or...

    above in the image is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers C++ language Make sure its corect and runs without errors and I promise to give you a thumbs up. #include iostream» using namespace std; class Node int data;...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

    1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT