DATA STRUCTURE
DSA THEORY ASSIGNMENT#1
Instructor Name: Hira Farman
Objective:
This homework assignment is designed to check the basic concepts discussed while studying the
topic regarding Queues, stack ,Linked List, SORTING
Instructions:
1- This is an individual assignment. You will submit your work individually in hard Copy
2- Write your name and roll number at the start of the assignment.
3- Do not copy and paste any thing from the internet. Your work must be original.
4- Please note that you must do your own work. If any one found copying from another
Student, no marks will be given to him/ her.
5- No assignment will be accepted through email before and after the due date.
6- If you have any problems, feel free to mail at [email protected].
Question 1:QUEUE
A. Consider the following QUEUE of characters, where queue is a circular array which is
allocated six memory cells
FRONT = 2 REAR = 4
____, A, C, D, ___, ___
Describe the queue as the following operations take place
a. F is added
b. Two letters are deleted
c. K, L and M are added
d. One letter is deleted
e. R is added
f. S is added
g. T is added
h. One letter deleted
B. Suppose a queue is maintained with N=12 memory cells. Find the number of elements in
queue if
a. FRONT = 3, REAR = 9
b. FRONT = 9, REAR = 4
c. FRONT = 4, REAR = 4
C. Consider the following deque where DEQUE is allocated 6 memory cells:
LEFT =2, RIGHT =5 DEQUE:________, London, Berlin, Rome, Paris,________
Describe the deque , including LEFT and RIGHT ,as the following operation take place:
a. Ali is added on the left.
b. Two cities are deleted from the right.
c. Madrid is added on the left
d. Two cities are deleted from the right.
e. Oslo is added on the left.
Question 2: STACK
A.Consid:er the following stack of characters where STACK is allocated N=8 memory cells
STACK: A, C, D, F, K, __ ,__ ,__.
a) POP(STACK, ITEM)
b) POP(STACK, ITEM)
c) PUSH(STACK,L)
d) PUSH(STACK,P)
e) POP(STACK, ITEM)
f) PUSH(STACK,R)
g) PUSH(STACK,S)
h) POP(STACK, ITEM)
B.In the game of daisy, two players alternate picking petals from a daisy (daisy is the name of a
flower, and that is considered to have five or more petals).Each time, each player gets to pick 1 or 2
petals from the daisy. The person who picks the last petals wins. If the daisy has n petals, we say the
game is an n-petal. If you are asked to implement the simple game using an appropriate algorithm,
which data structure would be the most appropriate one to be used for the purpose? Show how.
C.Suppose STACK is allocated N=6 memory cell and initially STACK is empty ,or in other
words, TOP=0.Find the output of the following module.
1. Set AAA:=2 and BBB:=5.
2. Call PUSH(STACK, AAA).
Call PUSH(STACK, 4).
Call PUSH(STACK, BBB. +2).
Call PUSH(STACK,9).
Call PUSH(STACK, AAA + BBB).
3. Repeat while TOP ≠0;
Call POP(STACK,ITEM).
Write: ITEM.
[End of loop.]
4.Return.
Question 3:LINKED LIST(Singly circular,doubly)
A. Design an algorithm insert at nth place in DOUBLY linked list.
B. Design an algorithm insert at the end in SINGLY linked list.
C..Consider the following double linked list with three nodes A, N and B. Write steps to remove
node N from list with ‘current’ pointer pointing initially towards A. Draw the resulting list.
4
null null
A B
current
N
E.Discuss the advantages ,if any of a two-way list over a one –way list for each of the following
operations:
a. Traversing the list to process each node.
b. Deleting a node whose location LOC is given/
c. Searching an unsorted list for a given element ITEM.
d. Inserting a node before the node with a given location LOC
e. Inserting a node after the node with a given location LOC.
.Question 4: Apply any data structure for this:
A. Given a list, remove all elements equal to a given value from the List. The relative order of
items that are not removed stays the same. For example given the following list:
0 1 2 3 4 5 6
“dog” , “cat” , “dog” , “mouse” , “hippo” , “dog” , ” cat”
Remove all elements equal to “dog”. The resulting list is:
0 1 2 3
“cat”, “mouse”, “hippo” , ” cat”
QNO5:
For Exercises 1-10, indicate which structure would be a more suitable choice for each of the
following applications by marking them as follows:
A. Stack
B. Queue
C. Tree
D. Binary search tree
E. Graph
1. A bank simulation of its teller operation to see how waiting
times would be affected by adding another teller.
2. A program to receive data that is to be saved and processed
in the reverse order.
3. An electronic address book ordered by name.
4. A word processor to have a PF key that causes the preceding
command to be redisplayed. Every time the PF key is
pressed, the program is to show the command that preceded
the one currently displayed.
5. A dictionary of words used by a spelling checker to be built
and maintained.
6. A program to keep track of patients as they check into a
medical clinic, assigning patients to doctors on a first-come,
first-served basis.
7. A program keeping track of where canned goods are located
on a shelf.
8. A program to keep track of the soccer teams in a city
tournament.
9. A program to keep track of family relationships.
10. A program to maintain the routes in an airline.
QNO6:SORTING:
Analyze the given pseudo-code of the sort algorithm (ascending order). Dry run this algorithm
on the given array. Re-write contents of the array whenever swapping takes place. After final
attempt again re-swap the first item into last position and last item into first position.
unknown sort (int arr[n]) {
int temp;
for (int i = 0 ; i < n-1 ; i++) {
int item = i;
for (int j = i+1 ; j < n ; j++) {
if (arr[j] < arr[item])
item= j;
if (item != i) {
temp = arr[i];
arr[i] = arr[item];
arr[item] = temp;}}}
Ans.
25 28 33 27 21