6. The Sorted list class is to be extended by the addition of function, SplitLis
ID: 3600217 • Letter: 6
Question
6. The Sorted list class is to be extended by the addition of function, SplitListl), which inputs a list and an item, and splits the encapsulated list into two lists so that: all elements less than item are kept in the encapsulated list -and all elements greater than item are put into the list passed as a parameter a) Write the specifications for function Splitl ist) b) Write the C++ code for function Splittist c) Describe the algorithm you used for Splitistd in terms of time complexity (Big-Oh)Explanation / Answer
SplitList():-
Functions for splitting and grouping lists into sublists. splitList splits a list l into max(groupAssignment) groups. The integer indices of groupAssignment determine in which group each element of l goes.
GroupListConsecutive splits l into numberOfGroups consecutive sublists (or groups).
GroupListDistributed distributes l into numberOfGroups sublists (or groups).
Flatten flattens a list l of lists into a flat list by concatenation.
If recursive is TRUE (defaults to FALSE), flatten will be recursively called on each argument first. intersperse joins two lists xs and ys into a list of pairs containig every possible pair, i.e. intersperse(xs, ys) equals the product list of xs and ys.
The pairConstructor parameter can be used to change the type of pairs returned.
Assume.,
The linked list looks as such:
15 -> 0 -> 15 -> 10 -> 20 -> 30 -> 60 -> NULL;
User wants to split the link at node 3:
15 -> 0 -> 15 -> NULL;
10 -> 20 -> 30 -> 60 -> NULL;
The problem arises when I ask the user to enter what node to split it, the output is always 15. So it's always showing the very first node and deleting everything else.
Here I am giving an exaple of SplitList():-
{
MyNode *current = head;
MyNode *second = new MyNode;
if (!head) //if empty simply return
{
return;
}
if (choice == 1)
{
second = head;
second->next = NULL;
current->next = head->next;
}
for (int i = 0; i < choice - 1; i++)
{
current = current->next;
second -> next = current; //once it reaches the desired positoin
}
second = current->next;
second->next = NULL;
current = head;
current->next = NULL;
current = second = NULL;
}
int main()
{
int pos, val;
val = 15;
TheNode one;
one.add(10);
one.add(20);
one.add(30);
one.display();
cout << endl;
one.addFront(0);
one.addEnd(60);
one.display();
cout << endl;
cout << "Replace " << val << " at what node position: " << endl;
cin >> pos;
one.insert(val, pos);
one.display();
cout << endl;
I hope the above information will help you.
Thanks..
cout << "Split the node at what node position?" << endl;
cin >> pos;
one.split(pos);
one.display();
system("PAUSE");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.