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...
C++ Write a definition for a structure type for records for books. The record contains ISBN...
C++ Write a definition for a structure type for records for books. The record contains ISBN number, price, and book cover (use H for Hard cover and P for paper cover). Part of the problem is appropriate choices of type and member names. Then declare two variables of the structure. Write a function called GetData use the structure as function argument to ask the user to input the value from the keyboard for structure variables.
Write a program to implement linked list data structure that will have following functions: a. Append...
Write a program to implement linked list data structure that will have following functions: a. Append a node in the list b. Insert a node in the list c. Delete a node from the list d. Display list e. Find maximum value in the list f. Find how many times a value exists in the list. g. Search Portion of the code is give below. You have to write code for the items (e, f, g) Program: #include<stdlib.h> #include<stdio.h> #include<iostream>...
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...
Using classes and array data structure write methods with algorithms for a software that an airline...
Using classes and array data structure write methods with algorithms for a software that an airline can use to view available/booked seats, management booking, canceling booking and reorder seats. The solution should consist of a minimum of two classes with one class containing the main method and second class for managing a manipulating data. The choice of data structure for this assignment will be static one dimension arrays. in C++
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]
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT