Sie sind auf Seite 1von 3

DS Assignment No 2

Deadline:- 18-10-19

1. i) Given a list, split it into two sublists — one for the front half, and one for the back half. If the number
of elements is odd, the extra element should go in the front list. Getting this right for all the cases is
harder than it looks. You should check your solution against a few cases (length = 2, length = 3,
length=4) to make sure that the list gets split correctly near the short-list boundary conditions. If it
works right for length=4, it probably works right for length=1000. You will probably need special case
code to deal with the (length<2) cases.

Input:
2-> 3-> 5-> 7-> 11 -> NULL
Output:
2 -> 3 -> 5 -> NULL
7 -> 11 -> NULL

ii) Given a list, divide its nodes to make two smaller lists. The sublists should be made from
alternating elements in the original list. The elements in the new lists may be in any order (for some
implementations, it turns out to be convenient if they are in the reverse order from the original list.)

input:
a -> b -> a -> b -> a -> NULL
output:
a -> a -> a -> NULL
b -> b -> NULL

2. i) For a given singly linked list insert a node:


a. at the beginning
b. at the end
c. at a given position k
Input: value=8, k=4
1 -> 2 -> 5 -> 7-> 4 -> NULL
Output: 8 -> 1 -> 2 -> 5 -> 7 -> 4 -> NULL
1 -> 2 -> 5 -> 7 -> 4 -> 8 -> NULL
1 -> 2 -> 5 -> 8 -> 7 -> 4 -> NULL

ii) For a given singly linked list delete a node:


a. at the beginning
b. at the end
c. at a given position k
Input:
k=3
1 -> 2 -> 5 -> 7 -> 4 -> NULL
Output:
2 -> 5 -> 7 -> 4 -> NULL
1 -> 2 -> 5 -> 7 -> NULL
1 -> 2 -> 7 -> 4 -> NULL

3. i) Print all the elements at the index of multiples of k with the first element assumed to have an
index of 0. Do this for a single pass of the linked list.

Input:
k=3 12 -> 15 -> 18 -> 17 -> 19 -> 20 -> 22 -> NULL
Output:
12 -> 17 -> 22 -> NULL

ii) Extend the above solution assuming that the list is circular and the N-th index is the same as 0-th
index. You may need multiple passes. However, every number should be printed only once during its
first selection.

Input:
k=3
12 -> 15 -> 18 -> 17 -> 19 -> 20 -> 22 -> NULL
Output:
12 -> 17 -> 22 -> 18 -> 20 -> 15 -> 19 -> NULL

4. Implement a queue using 2 stacks.


5. Implement a stack using 2 queues.
6. The only printer in the student union’s office is experiencing an extremely heavy workload.
Sometimes there are a hundred of jobs in the printer queue and one may have to wait for hours to get
a single page of output. The printer follows simple FCFS policy for the pages to get printed and so the
printing of important page document gets delayed. We made a slight change in the printing policy by
changing the simple FCFS policy to priority based printing of pages (request) and consider 1 as high
priority. The printer will be provided with:-

i) A list of request to be printed


ii) Priority corresponding to each request
iii) No of pages per request

Write down a pseudo code to find the time at which each request gets printed (assuming that the
time at which the printing start is 0). Also consider the fact that time taken to print each page is
1 sec.
List of Request → A B C D E F

Priority → 1 5 2 3 4 6

No of page per request → 1 3 10 5 8 7

7. https://www.spoj.com/problems/HISTOGRA/
8. https://www.spoj.com/problems/STPAR/
9. https://www.spoj.com/problems/JNEXT/
10. i) Remove in a linked list all the nodes that have a greater value to their right.
Input:
10 -> 12 -> 15 -> 20 -> 5 -> 16 -> 25 -> 8 -> NULL
10 -> 12 -> 15 -> 20 -> 25 -> 26 -> 30 -> 40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL
Output:
20 -> 25 -> 8 -> NULL
40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL

ii) Perform pair-wise swapping of nodes of a given linked list.


Input: 20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> 7 -> NULL
Output: 18 -> 20 -> 10 -> 15 -> 6 -> 8 -> 3 -> 5 -> 7 -> NULL

11. In recording and maintaining the scores for a cricket tournament, we enter the name and score of
the player as the player finishes. Identify the right data structure and Implement following,
A) Insert Name and Score of the player
B) Sort the score and players name in ascending order
C) Search for the player with the equal score and print, if any.

Das könnte Ihnen auch gefallen