C++ Recall that the ADT list class methods are; ?void List() ?bool isEmpty() ?in
ID: 3706949 • Letter: C
Question
C++
Recall that the ADT list class methods are;
?void List()
?bool isEmpty()
?int size()
?void add(int item, int pos)//inserts item at specified position (first postion is 1)
?void remove(int index)//removes item from specified position
?void removeAll()
?int indexOf(int item)//returns the index of item
?int itemAt(int index)//returns the item in position specified by index
Implementation of LIST ADT operations and details are hidden. Only the methods listed above are given.
Assume list contains some integer numbers and this list may contain repeated consecutive integer numbers. Write a function removeRepeated(LIST &) to remove those consecutively repeated integer numbers from the list by leaving only one copy of them.
Consider following example:
Before:{10,50,10,60,60,60,60,40,10,10,10,10,80,3,3,3}
After:{10,50,10,60,40,10,80,3}
Explanation / Answer
/*The function definition is given below:*/
/*NOTE: If you want to change something or want in a different way, please let me know through comments; I will surely revert back to you.*/
/*
Implementation of LIST ADT operations and details are hidden. Only the methods listed above are given.
Assume list contains some integer numbers and this list may contain repeated consecutive integer numbers.
Assumption: Index of list starts at 0.
*/
void removeRepeated(List &l)
{
int count=l.size();
for(int i=0;i<count;i++)
{
int x= l.itemAt(i);
for(int j=i+1;j<count;j++)
{
if(x== l.itemAt(j) && i==j-1)
{
l.remove(j);
count--;
j--;
}
}
}
}
/*Hope this helps.*/
/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.