Vous êtes sur la page 1sur 6

ECE 220: Computer Systems and Programming

Spring 2015
Final Exam
14 May 2015

This is a closed book exam except for 1 sheet of notes..


According to our trial test takers problem 3 took less time than problems 1
and 2. Use your time judiciously
There will be some partial credit for partial solutions
Absolutely no interaction between students is allowed
Dont panic!

Problem 1 (35 points): Text File Comparing


Write a program that compares two text files. The program should take two text files from the
command line and print the first line where the files differ. When the two files are identical, or
when one file is completely included in the first part of the other file, the program shouldnt print
anything. You may assume that each line in a text will not have more than 999 characters
including the newline character (\n). The first line of a text file is line number 1.
You may not use any functions from string.h library.
Three text files and a main.c file are provided. The given main function includes code to read
files from command line argument. Write your code in main.c. You may create functions if you
need any.

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.

4>The fourth line of text2.txt.



The content that comes after the line number and the bracket, < or >, is the text on line 4 from
each text file.
If you run your program with
./diff text1.txt text3.txt
the program should print nothing.
Instructions:
You may NOT use string.h.
To compile your code, type:
gcc -g -Wall main.c -o diff

To run your program, type:


./diff text1.txt text2.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].

Problem 2 (30 Points): Palindrome in linked-list


Write a program to check if a given singly linked list of characters is a palindrome. Each node in
the list contains a character and a node pointer that points to the next node.

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:

int linked_list_len(node * head)



This function takes as input a pointer (head) to the head node of the linked list and
returns the length of the linked list.

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:

int is_palindrome(node *first, node *second)


(more in the next page)

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

Do not modify main function

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 compile your code:

gcc Wall g linkedlist.c

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.

Problem 3 (35 Points): Tree Recovery


Consider a type of binary tree which contains non-negative (0) numbers as leaf nodes and
negative (<0) number as internal nodes. Each node has either 0 or 2 children. Every tree of this
type can be represented uniquely by the sequence obtained through a post-order traversal of
the tree. We call it the postfix sequence. For example, the tree below corresponds to the
unique postfix sequence {1, 0, 2, -3, -2, 3, -1}.
In this problem, you are given a valid postfix sequence and you
have to write a C program that constructs the unique
corresponding tree. The following algorithm uses a stack to
construct the tree from an input postfix sequence. The stack is used
to push pointers to nodes in the tree. You are given a stack
implementation to used (see details below). You may want to
mentally run this algorithm on the given sequence to understand
how it works.

Input: integer pointer sequence to the postfix sequence, sequence size N


Output: pointer to the tree root of constructed tree
Initialize a stack S of node pointers
For i = 0 to N-1
If sequence[i] is negative
NodePtr create a node with number from sequence[i];
RightChildPtr pop out the top node pointer from the stack S;
Establish the tree links between RightChildPtr and NodePtr;
LeftChildPtr pop out the top node pointer from the stack S;
Establish the tree links between LeftChildPtr and NodePtr;
Push NodePtr into the stack S;
Else
NodePtr create a node with number from sequence[i];
Push NodePtr into the stack S;
Endif
EndFor
RootNodePtr pop out the top node pointer from the stack S;
Return RootNodePtr;

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.

typedef struct node {


int number;
struct node* left;
struct node* right;
} node_t;
We have provided you the implementation of stack by push_node, pop_node, and stack_size.
Sample usage of these functions can be found in function recover_tree.
Based on these predefined structures, the stack, and the algorithm given above, implement the
function recover_tree(int *, int) that takes two input arguments, 1) an integer pointer to the
postfix sequence and 2) an integer value of the sequence size, and returns the node pointer to
the tree root constructed by your program. A golden checker that compares your tree with the
correct tree in terms of their unique postfix sequences is also given. The comparison result will
be displayed on the console after running your program.
Instructions

Once you finish the program, you can type :



make sequence1, make sequence2, or make sequence3.
The makefile will automatically build your code to a binary tree and run it on the
specified input sequence.

We provide three benchmarks to test your program. These benchmarks are in the
directory ./tests under the folder of this problem.

A correct message example for sequence1 is shown as follows:


*************************** TREE ***************************
Input sequence: 4 3 -1 2 -1 1 -2 0 3
Your sequence: 4 3 -1 2 -1 1 -2 0 3
Congratulation! Your recover_tree is correct on this input:)
************************ END TREE **************************

The default compilation enables the debugger mode (i.e., with option -g). You should be able to
use the GDB on the compiled binary.

Vous aimerez peut-être aussi