DS Minor1 29-08-2024 3 Answers
DS Minor1 29-08-2024 3 Answers
This list has four triples. If we make the call of switchTriplesOfTriples; the list’s state
should become:
{ 4, 5, 6, 1, 2, 3, 10, 11, 12, 7, 8, 9}
| | | | | | | | | | | |
+------+ +------+ +-------------+ +-------+
triple triple triple triple
Notice that the triple (1, 2, 3) has been switched with the triple (4, 5, 6) and that the triple
(7,8,9) has been switched with the triple (10,11,12). This example purposely used
sequential integers to make the rearrangement clear, but you should not expect that the
list will store sequential integers. It also might have extra values at the end that are not
part of a group of four. Such values should not be moved. For example, if the list had
stored this sequence of values:
{3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4, 99, 2, 7}
Then a call on the function should modify the list to store the following. Note that 99, 2,
7 at the end are not switched:
{42, 7, 26, 3, 8, 19, 204, 6, -4, 19, -8, 193, 99, 2, 7}
Your function should work properly for a list of any size. Note: The goal of this problem
is to modify the list by modifying pointers. It might be easier to solve it in other ways,
such as by changing nodes’ data values or by rebuilding an entirely new list, but such
logics are not allowed.
Note:
Do not modify the data field of any existing nodes.
Do not create any new nodes by calling new Node(...). You may create as many
Node* pointers as you like, though.
Do not use any auxiliary data structures such as arrays, vectors, queues, maps,
sets, strings, etc.
Your code must make a single pass over the list (not multiple passes)
Note that the list has no ”size” function, nor are you allowed to loop over the whole
list to count its size.
You need to write the whole program which include three functions:
create_list
display_list
switchTriplesOfTriples.
Output:
The original contents of the list are:
3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4, 99, 2, 7
struct Node {
int val;
Node *next;
};
Node *prev=NULL;
Node *three=temp;
temp=temp->next->next->next;
if(prev!=NULL){
prev->next=three;
}
three->next->next->next=one;
one->next->next->next=temp;
prev=one->next->next;
while(head!=NULL){
cout<<head->val<<" ";
head=head->next;
}
int main(){
Node* head = NULL;
insert(head,1);
insert(head,2);
insert(head,4);
insert(head,5);
insert(head,6);
insert(head,7);
insert(head,8);
insert(head,9);
insert(head,10);
insert(head,11);
//insert(head,10);
insert(head,555);
print(head);
cout<<endl;
solve(head);
print(head);
cout<<endl;
2 An input-restricted deque is a linear list in which items may be inserted at one end but 1
removed from either end; clearly an input-restricted deque can operate either as a stack
or as a queue, if we consistently remove all items from one of the two ends. Can an
output-restricted deque also be operated either as a stack or as a queue? Justify your
answer
Answer:
Yes. (Consistently insert all items at one of the two ends.)
3 Imagine four cars positioned on the input side of the track in Fig. 1, numbered 1, 2, 3, 1
and 4, from left to right. Suppose we perform the following sequence of operations (which
is compatible with the direction of the arrows in the diagram (a) move car 1 into the stack;
(b) move car 2 into the stack; (c) move car 2 into the output; (d) move car 3 into the stack;
(e) move car 4 into the stack; (f) move car 4 into the output; (g) move car 3 into the
output; (h) move car 1 into the output. As a result of these operations the original order
of the cars, 1234, has been changed into 2431.
Now assume that there are six cars numbered 123456 and S indicates a push and X
indicates a pop operation. Can they be permutated in to the order 154623 (output) and
order 325641? (If a permutation is possible give the order string of operations i.e. as
SSXXXSSXX… if a permutation is not possible not justify your answer)
Answer:
To obtain 325641, do SSSXXSSXSXXX (in the notation of the following exercise).
The order 154623 cannot be achieved, since 2 can precede 3 only if it is removed from
the stack before 3 has been inserted.
4 Find a simple formula for an, the number of permutations on n elements that can be 2
obtained with a stack like that in Question 3.
Answer:
5 Consider the problem of Question 3, with a queue substituted for a stack. What permutations of 1
1, 2, ... n can be obtained with use of a queue?
Answer:
Only the trivial one, 12 ... n, by the nature of a queue.