Question

In: Computer Science

In C++ Write the definition for following methods of List data structure. 1. setList – The...

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

Solutions

Expert Solution

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;
}


Related Solutions

In C++ Write the definition for following methods of List data structure. 5. pushFront – The...
In C++ Write the definition for following methods of List data structure. 5. pushFront – The function appends an element in the list at the front. The operation increases the number of elements in the list by one. 6. pushback - The function appends an element in the list at the end. The operation increases the number of elements in the list by one. 7.popFront - The function returns and then removes an element in the list from the front....
Write the following task in C++1) Write the definition of a function numOccurrences thatsearches...
Write the following task in C++1) Write the definition of a function numOccurrences that searches for a character in a character array and returns the number of times it occurs in the array. The function has three formal parameters: a char array array, an int variable arraySize representing the size of the array, and a character variable letter representing the character to be searched for in the array.2) Assume the array numbers is an int array of size 10 and...
it should be c++ data structure Write a recursive function that draws the following shape. X...
it should be c++ data structure Write a recursive function that draws the following shape. X XXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXX XXXXXXX XXXXX XXX X The length of the longest row in the shape and the shape's character are function parameters. In above shape the longest row is 9 and the pattern's character is "X”.
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information...
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information of COVID 19 cases of countries around the world. It keeps the name of the country, number of COVID 19 cases, number of deaths, current test positive rate, etc. After defining the structure, declare a variable of Covid19Info type with some initial values for Bangladesh. [8]
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
A deque is a data structure consisting of a list of items, on which the following...
A deque is a data structure consisting of a list of items, on which the following operations are possible: • push(x): Insert item x on the front end of the deque. • pop(): Remove the front item from the deque and return it. • inject(x): Insert item x on the rear end of the deque. • eject(): Remove the rear item from the deque and return it. Write routines to support the deque that take O(1) time per operation. In...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to store a date, which includes day(int), month(int), and year(int). 2.Define a structure to store an address, which includes address(house number and street)(string), city(string), state(string), zip code (string). 3.Define a class to store the following information about a student. It should include private member variables: name(string), ID (int), date of birth (the first structure), address (the second structure), total credit earned (int), and GPA (double)....
In C++, Write the definition of the function MaximumCount() whose header is int MaximumCount(Array<double>& data) It...
In C++, Write the definition of the function MaximumCount() whose header is int MaximumCount(Array<double>& data) It returns the amount of times the maximum value of data appears in data. If data is empty, it returns 0. For instance, if data = [7, 1, 4, 9, 6, 7, 7, 3, 2, 6, 9, 5, 9], it will return 3 since 9 appears three times
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT