a. constructor/destructor b. empty() –check if the list isempty c. insertAtPos()
ID: 3613844 • Letter: A
Question
a. constructor/destructor
b. empty() –check if the list isempty
c. insertAtPos() – insert at a particular position
d. deletePos() – delete the node at particularposition
e. display()- Display the data in the list along with theposition.
- overload the display() function, such that if position isspecified, the corresponding no is returned, else the complete listwith position is returned.
f. Writelistdatatofile()- When this function is calledwrite the data in the list along with their positions(position inthe list) in a file.
Explanation / Answer
#include<iostream.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
//using namespace std;
class Link_List{
private:
static float list[9]; // C++ Array of type float with maximum size= 10
public:
Link_List(); //Constructor for the C++ tutorial
~Link_List(); //destructor for the C++ Tutorial
void empty(void); //checkif the list is empty
void insertAtPos(int x, float val); //– insert at aparticular posi
void deletePos(int x); //– delete the node atparticular position
void display(void); //- Display thedata in the list along with theposition.
void display(int x); //overloadeddisplay
void Writelistdatatofile(); //- When this function is calledwrite the data in the list along with their positions(position inthe list) in a file.
};
Link_List::Link_List(void)
{
//list[]=new float;
for(int i=0;i<=9;i++)
{
list[i]=0;
}
}
Link_List::~Link_List(void)
{
delete list;
}
void Link_List::display(void)
{
for(int i=0;i<=9;i++)
{
cout<<"On Index "<<i<<""<<list[i]<<" ";
}
}
voidLink_List::display(int x)
{
cout<<"On Index "<< x<<" "<<list[x]<<" ";
}
voidLink_List::deletePos(int x)
{
list[x]=0;
}
voidLink_List::insertAtPos(int x, float val)
{
list[x]=val;
}
void Link_List::Writelistdatatofile(void)
{
ofstream myfile;
myfile.open("example.txt"); // opens the file
if( !myfile ) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
for(int i=0;i<=9;i++)
{
myfile<<"On Index"<<i<<" "<<list[i]<<" ";
}
myfile.close();
}
void main()
{
//you can use this classin this main body
//
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.