Professional Documents
Culture Documents
Spring 2015
Final Exam
14 May 2015
Sample Run:
For example, for the given text files, when you run your program with
./diff
text1.txt
text2.txt
it should print the following:
4<The
fourth
line
of
text1.txt.
You may create your own text files to thoroughly test your program
[note: fscanf returns EOF, if the file pointer reaches the end of file].
Your program should not allocate any additional memory to store the nodes or the linked list and
use the following algorithm to solve the problem.
Algorithm
Compute the length of the linked list by implementing the function linked_list_len:
Reverse the last half of the linked list in place without allocating any additional
memory by implementing and appropriately calling the reverse_list function:
node
*reverse_half_list(node
*
head,
int
len,
int
k)
This function takes as input a pointer (head) to the head of the linked list, the length of
the list (len), and an integer k. It reverses the last k nodes in the list in place, and returns
the pointer to the first of the last k nodes in the modified list. So the modified list will be (a
b a b Null) and a pointer to the second node containing a will be returned.
If the length of the linked list is odd, only reverse the last (n-1)/2 nodes.
Implement the is_palindrome function which traverse the modified linked list to
compare the first half and the second half of the link list, node by node. If the first half is
same as the second half it returns 1, otherwise, returns 0. is_palindrome takes as
arguments two pointers, namely, the head of the whole list (first) and the head of the
second half of the list (second); node * second is the pointer that returned by the
reverse_half_list function:
Sample Run:
Please
enter
a
sequence:
abccba
linked
list:
a->b->c->c->b->a
length
is
6
reversed
second
half:
a->b->c->a->b->c
The
given
string
is
a
palindrome!
Please
enter
a
sequence:
abcba
linked
list:
a->b->c->b->a
length
is
5
reversed
second
half:
a->b->c->a->b
The
given
string
is
a
palindrome!
Please
enter
a
sequence:
abcd
linked
list:
a->b->c->d
length
is
4
reversed
second
half:
a->b->d->c
The
given
string
is
not
a
palindrome!
Instructions
Please reverse the linked list by changing the pointers only. Modifying the data stored in
the nodes is not permitted.
The function to build the linked list from the input string is provided as build_list().
Another function print_list() is given to print out the linked list.
To run your code: ./a.out and then type in the string without any space.
Please use the given algorithm to solve the problem, otherwise pointes will be deducted.
Given code
The data type node_t defines a tree node. It contains an integer variable number that stores
the number of the node and two node pointers left and right that establish the tree links
between nodes.
We provide three benchmarks to test your program. These benchmarks are in the
directory ./tests under the folder of this problem.
The default compilation enables the debugger mode (i.e., with option -g). You should be able to
use the GDB on the compiled binary.