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
  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    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*...

  • 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;...

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

    //This program is your final practice exam. //Please fill in the functions at the bottom of the file. (sum and removeItem) //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>...

  • 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...

  • //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;   ...

    //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;        int size;        int capacity;               void resize(){            capacity*=2;            T * newArr = new T[capacity];            for(int i=0;i<size;i++){                newArr[i] = arr[i];            }            delete(arr);            arr = newArr;                   }           public:       ...

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