In: Computer Science
In C++
Write the definition for following methods of List data structure.
1. setList – The function set the value of list elements equal to a value passed as the function input.
2. getAt – The function returns an element of the list referred by its position which is given to the function as input.
3. insertAt – The function inserts a given element passed as function input in the list at the specified position also passed as second function input. The insertion should happen only on the existing elements of the array. The operation increases the number of elements in the list by one.
4. removeAt – The function remove an element from the list at the specified position. The removal should happen only on the existing elements of the array. The operation decreases the number of elements in the list by one
ANSWER-
THE FUNCTIONS ARE DEFINED AS FOLLOWS----
void setList(int n)
{
list<int>::iterator p =
demo_list.begin();//declare a iterator of the list to the start of
list
while(p != demo_list.end()) {//loop through the list
*p = n;//change every element of the list to the
number n
p++;//increment the iterator
}
}
int getAt(int n)
{
list<int>::iterator p =
demo_list.begin();//declare a iterator of the list to the start of
list
advance(p,n-1);//advance function increases the
iterator by n-1 places
return *p;//returns the value of the iterator
}
void insertAt(int x , int n)
{
list<int>::iterator p =
demo_list.begin();//declare a iterator of the list to the start of
list
for(int i=0;i<n-1;i++)//another method to increase
the iterator
p++;
demo_list.insert(p,x);//insert function inserts the
number x you want at the position p
}
void removeAt(int n)
{
list<int>::iterator p =
demo_list.begin();//declare a iterator of the list to the start of
list
advance(p,n-1);
demo_list.erase(p);//erases the number at position
n
}
------------------------------------------------------------------------------------
DEMO PROGRAM TO SHOW THE USE OF THE METHODS
#include<bits/stdc++.h>
using namespace std;
list<int> demo_list; //declare the list globally to use it
throught the code
void setList(int n)
{//N IS THE NUMBER YOU WANT THE ELMENTS OF THE LIST TO BE EQUAL
TO
list<int>::iterator p =
demo_list.begin();//declare a iterator of the list to the start of
list
while(p != demo_list.end()) {//loop through the list
*p = n;//change every element of the list to the
number n
p++;//increment the iterator
}
}
int getAt(int n)
{//N IS THE POSITION OF THE VALUE YOU WANT
list<int>::iterator p =
demo_list.begin();//declare a iterator of the list to the start of
list
advance(p,n-1);//advance function increases the
iterator by n-1 places
return *p;//returns the value of the iterator
}
void insertAt(int x , int n)
{//X IS THE NUMBER YOU WANT TO ADD , N IS THE POSITION YOU WANT TO
ADD AT
list<int>::iterator p =
demo_list.begin();//declare a iterator of the list to the start of
list
for(int i=0;i<n-1;i++)//another method to increase
the iterator
p++;
demo_list.insert(p,x);//insert function inserts the
number x you want at the position p
}
void removeAt(int n)
{//N IS THE POSITION OF THE LIST YOU WANT TO ERASE
list<int>::iterator p =
demo_list.begin();//declare a iterator of the list to the start of
list
advance(p,n-1);
demo_list.erase(p);//erases the number at position
n
}
int main()
{
demo_list.push_back(10);//just for checking purpose
demo_list.push_back(20);
setList(100);//using the function
int ans = getAt(1);
cout<<ans<<endl;
insertAt(10,2);
removeAt(3);
for(int value : demo_list)
{
cout<<value<<endl;
}
return 0;
}