0% found this document useful (0 votes)
360 views

R

The document discusses algorithms and implementations related to linked lists, including: 1) Algorithms for finding the second to last node in a singly linked list, concatenating two singly linked lists, and counting nodes in a singly linked list. 2) Details on swapping nodes in singly and doubly linked lists. 3) Implementations of common data structures like stacks and queues using singly linked lists. 4) Additional recursive and non-recursive algorithms involving linked lists.

Uploaded by

khanh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
360 views

R

The document discusses algorithms and implementations related to linked lists, including: 1) Algorithms for finding the second to last node in a singly linked list, concatenating two singly linked lists, and counting nodes in a singly linked list. 2) Details on swapping nodes in singly and doubly linked lists. 3) Implementations of common data structures like stacks and queues using singly linked lists. 4) Additional recursive and non-recursive algorithms involving linked lists.

Uploaded by

khanh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

R-7.

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 next reference of None.

R-7.2 Describe a good algorithm for concatenating two singly linked lists L and

M, given only references to the first node of each list, into a single list L ?

that contains all the nodes of L followed by all the nodes of M.

R-7.3 Describe a recursive algorithm that counts the number of nodes in a singly

linked list.

R-7.4 Describe in detail how to swap two nodes x and y (and not just their con-

tents) in a singly linked list L given references only to x and y. Repeat

this exercise for the case when L is a doubly linked list. Which algorithm

takes more time?

C-7.24 Give a complete implementation of the stack ADT using a singly linked

list that includes a header sentinel.

C-7.25 Give a complete implementation of the queue ADT using a singly linked

list that includes a header sentinel.

C-7.26 Implement a method, concatenate(Q2) for the LinkedQueue class that

takes all elements of LinkedQueue Q2 and appends them to the end of the

original queue. The operation should run in O(1) time and should result

in Q2 being an empty queue.

C-7.27 Give a recursive implementation of a singly linked list class, such that an

instance of a nonempty list stores its first element and a reference to a list

of remaining elements.

C-7.28 Describe a fast recursive algorithm for reversing a singly linked list.

C-7.29 Describe in detail an algorithm for reversing a singly linked list L using
only a constant amount of additional space and not using any recursion.

C-7.30 Exercise P-6.35 describes a LeakyStack abstraction. Implement that ADT

using a singly linked list for storage.

Reinforcement

R-4.1 Describe a recursive algorithm for finding the maximum element in a se-

quence, S, of n elements. What is your running time and space usage?

R-4.2 Draw the recursion trace for the computation of power(2,5), using the

traditional function implemented in Code Fragment 4.11.

R-4.3 Draw the recursion trace for the computation of power(2,18), using the

repeated squaring algorithm, as implemented in Code Fragment 4.12.

R-4.4 Draw the recursion trace for the execution of function reverse(S, 0, 5)

(Code Fragment 4.10) on S = [4, 3, 6, 2, 6].

R-4.5 Drawtherecursion trace fortheexecution offunctionPuzzleSolve(3,S,U)

(Code Fragment 4.14), where S is empty andU = {a,b,c,d}.

R-4.6 Describe a recursive function for computing the n th Harmonic number,

Hn

=∑

i=1 1/i.

R-4.7 Describe a recursive function for converting a string of digits into the in-

teger it represents. For example, 13531 represents the integer 13,531.

R-4.8 Isabel has an interesting way of summing up the values in a sequence A of

n integers, where nis a power of two. She creates a new sequence B of half

the size of A and sets B[i] = A[2i]+A[2i+1], for i = 0,1,...,(n/2)−1. If


B has size 1, then she outputs B[0]. Otherwise, she replaces A with B, and

repeats the process. What is the running time of her algorithm?

Creativity

C-4.9 Write a short recursive Python function that finds the minimum and max-

imum values in a sequence without using any loops.

C-4.10 Describe a recursive algorithm to compute the integer part of the base-two

logarithm of n using only addition and integer division.

C-4.11 Describe an efficient recursive function for solving the element unique-

ness problem, which runs in time that is at most O(n 2 ) in the worst case

without using sorting.

C-4.12 Givearecursive algorithm to compute the product of twopositive integers,

m and n, using only addition and subtraction.

C-4.13 In Section 4.2 we prove by induction that the number of lines printed by

a call to draw interval(c) is 2 c −1. Another interesting question is how

many dashes are printed during that process. Prove by induction that the

number of dashes printed by draw interval(c) is 2 c+1 −c−2.

C-4.14 In the Towers of Hanoi puzzle, we are given a platform with three pegs, a,

b, and c, sticking out of it. On peg a is a stack of n disks, each larger than

the next, so that the smallest is on the top and the largest is on the bottom.

The puzzle is to move all the disks from peg a to peg c, moving one disk

at a time, so that we never place a larger disk on top of a smaller one.

See Figure 4.15 for an example of the case n = 4. Describe a recursive

algorithm for solving the Towers of Hanoi puzzle for arbitrary n. (Hint:

Consider first the subproblem of moving all but the n th disk from peg a to

another peg using the third as “temporary storage.”)


C-4.15 Write a recursive function that will output all the subsets of a set of n

elements (without repeating any subsets).

C-4.16 Write a short recursive Python function that takes a character string s and

outputs its reverse. For example, the reverse of pots&pans would be

snap&stop .

C-4.17 Write a short recursive Python function that determines if a string s is a

palindrome, that is, it is equal to its reverse. For example, racecar and

gohangasalamiimalasagnahog are palindromes.

C-4.18 Use recursion to write a Python function for determining if a string s has

more vowels than consonants.

C-4.19 Write a short recursive Python function that rearranges a sequence of in-

teger values so that all the even values appear before all the odd values.

C-4.20 Given an unsorted sequence, S, of integers and an integer k, describe a

recursive algorithm for rearranging the elements in S so that all elements

less than or equal to k come before any elements larger than k. What is

the running time of your algorithm on a sequence of n values?

C-4.21 Suppose you are given an n-element sequence, S, containing distinct in-

tegers that are listed in increasing order. Given a number k, describe a

recursive algorithm to find two integers in S that sum to k, if such a pair

exists. What is the running time of your algorithm?

C-4.22 Develop a nonrecursive implementation of the version of power from

Code Fragment 4.12 that uses repeated squaring.

You might also like