Sei sulla pagina 1di 2

KIE1008 (Programming Tutorial) Session 2016/2017 (Semester 2)

Tutorial 3: Linked Lists


1. Design and implement a class whose objects represent polynomials.
The polynomial anxn+an-1xn-1 + …+a0 is implemented as a linked list.
Each node will contain an int value for the power of x and an int value for the corresponding
coefficient.
The class operations should include addition, subtraction, multiplication, and evaluation of a
polynomial. Overload the operator +, -, * for addition, subtraction and multiplication.
Evaluation of a polynomial is implemented as a member function with one argument of type int.
The evaluation member function returns the value obtained by plugging in its argument for x and
performing the indicated operations.
Include four constructors: a default constructor, a copy constructor, a constructor with a single
argument of type int that produces the polynomial that has only one constant term that is equal
to the constructor arguments (a0), and a constructor with two arguments of type int that
produces the one-term polynomial whose coefficient and exponent are given by the two
arguments (anxn).
Include a simple destructor, and member functions to input and output polynomials.
When the user inputs a polynomial, the user types in the following:
anx^n + an-1x^n-1 + … + a0
However, if a coefficient is zero, the user may omit the term. For example, 3x4+7x2+5 can be input
as 3x^4+7x^2+5 or 3x^4+0x^3+7x^2+0x^1+5.
If a coefficient is negative, a minus sign is used in place of a plus sign, such as 3x^5-7x^3+2x^1-8
or -7x^4+5x^2+9.
A minus sign at the front of the polynomial, applied only to the first coefficient, but not the entire
polynomial.
Polynomials are output in the same format, except the zero coefficients are not output.
To simplify input, you can assume the polynomials are always entered one per line and always
end with a constant or zero (if a0 is 0).

2. Create a library system that stores the books available as a doubly linked list.
Create a class Book, which contains at least two data members: title and author. (Assume
only one author)
Create two types of linked lists: the first one will sort according to title, and the second one
will sort according to author.
Write a main function to demonstrate the linked list.
Note: create the necessary function to insert and remove book.
KIE1008 (Programming Tutorial) Session 2016/2017 (Semester 2)

3. In a game, all the players are lined up after the other and assigned a number: the first person is 1,
the second is 2 and so on until N.
Starting from the first player, every 4th person in the line will be eliminated.
When the counting reach the end of the line, it is then continued from the beginning again. The
last player survive the elimination is the winner of the games.
For example, if there are 6 players:
123456 -> 3 is first eliminated, followed by 6.
1245 -> 4 is eliminated
125 -> 2 is eliminated
15-> 5 is eliminated
1 is the winner.
Write a program that creates a circular linked list of nodes to determine which position will win if
there are N players in the game (take N from 2 to 50).
Your program should simulate the elimination process by showing the elimination sequence for
each case. Consider also the possibility of deleting the head node in the list. Example output:
For N = 6, elimination sequence: 3, 6, 4, 2, 5, 1 (winner)
Note: modify the code to eliminate every x-th person, and display a table for N from 2 to 50, and
x from 2 to 10, that shows the winner for each case.
--END--

Potrebbero piacerti anche