You are on page 1of 1

Alexandria University Course Name: Data Structures and

Faculty of Computer and Data Science Algorithms


Spring 2021 – 2022 Course Code: 02-24-00108
Section: SEC04 Dr. Sameh Abosamra

Stack, Queue and Linked List


1. Give an algorithm for finding the second-to-last node in a singly linked list in which the last node
is indicated by a null next reference.
2. Let A be an array of size n ≥ 2 containing integers from 1 to n − 1 inclusive, one of which is
repeated. Describe an algorithm for finding the integer in A that is repeated.
3. Let B be an array of size n ≥ 6 containing integers from 1 to n − 5 inclusive, five of which are
repeated. Describe an algorithm for finding the five integers in B that are repeated.
4. Describe a recursive algorithm for computing the nth Harmonic number, defined as
𝑛
𝐻𝑛 = ∑ 1/𝑘
𝑘=1

5. Implement the clone() method for the ArrayStack class. Give a big-Oh characterization, in terms
of n, of the running time of this method.
6. Suppose you have a stack S containing n elements and a queue Q that is initially empty. Describe
how you can use Q to scan S to see if it contains a certain element x, with the additional
constraint that your algorithm must return the elements back to S in their original order. You
may only use S, Q, and a constant number of other primitive variables.
7. Stacks are often used to provide “undo” support in applications like a Web browser or text editor.
While support for undo can be implemented with an unbounded stack, many applications
provide only limited support for such an undo history, with a fixed-capacity stack. When push is
invoked with the stack at full capacity, rather than throwing an exception, a more typical
semantic is to accept the pushed element at the top while “leaking” the oldest element from the
bottom of the stack to make room. Give an implementation of such a LeakyStack abstraction,
using a circular array.
8. Repeat the previous problem using a singly linked list for storage, and a maximum capacity
specified as a parameter to the constructor.

You might also like