Assignments 2 BasicDataStructures
Assignments 2 BasicDataStructures
Output: Write to the screen an integer number (i.e., the number of pairs
(i,j) that A[i]=A[j])
Example
Keyboard Screen
6 4
524225
Input:
− The first line contains an integer number n which is the number of
operations.
− The next n lines contain the operation description. One operation is
in one line in either format:
● insert p x
● delete p
where p is the position and x is the number.
Output: Write the resulting linked list to the screen in one line. Numbers
are separated by spaces.
Example
Keyboard Screen
5 123
insert 0 1
insert 1 3
insert 1 4
insert 2 2
delete 1
3. Given a list of integer numbers, your task is to read these numbers into a
doubly linked list, and implement function count_triplets() to count all
positions p such that the sum of elements at positions p-1, p, and p+1 is
zero.
Input:
− The first line contains an integer number n which is the number of
numbers.
− The second line contains n integer numbers to read into
count_triplets function
Output: Write to the screen an integer number that is the result of
function count_triplets().
Example
Keyboard Screen
6 2
5 -3 -2 2 9 -11
Input:
− The first line contains an integer number n which is the number of
operations
− The next n lines contain the operation description. One operation is
in one line in either format:
● enqueue x
● dequeue
where x is the number to enqueue
Output: Write the resulting queue to the screen. Numbers are separated
by spaces.
Example
Keyboard Screen
7 345
enqueue 1
enqueue 2
enqueue 3
enqueue 4
dequeue
dequeue
enqueue 5
Input:
− The first line contains an integer number n which is the number of
operations
− The next n lines contain the operation description. One operation is
in one line in either format:
● push x
● pop
where x is the number to push to the stack.
Output: Write the resulting stack to the screen. Numbers are separated
by spaces.
Example
Keyboard Screen
7 125
push 1
push 2
push 3
push 4
pop
pop
push 5