• 沒有找到結果。

4.3ExtendableArray 4.2StackandQueue 4.1MoreaboutLinkedList Thishomeworksetcomeswith200points.Ingeneral,everyhomeworksetofourswouldcomewithafullcreditof200points. Homework#4

N/A
N/A
Protected

Academic year: 2022

Share "4.3ExtendableArray 4.2StackandQueue 4.1MoreaboutLinkedList Thishomeworksetcomeswith200points.Ingeneral,everyhomeworksetofourswouldcomewithafullcreditof200points. Homework#4"

Copied!
3
0
0

加載中.... (立即查看全文)

全文

(1)

Data Structures and Algorithms (NTU, Spring 2012) instructor: Hsuan-Tien Lin

Homework #4

TA in charge: Yu-Cheng Chou, Ya-Hsuan Chang and Wei-Yuan Shen RELEASE DATE: 04/13/2012

DUE DATE: 04/20/2012, 17:00

As directed below, you need to submit your code to the designated place on the course website.

Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts.

Discussions on course materials and homework solutions are encouraged. But you should write the final solutions alone and understand them fully. Books, notes, and Internet resources can be consulted, but not copied from.

Since everyone needs to write the final solutions alone, there is absolutely no need to lend your homework solutions and/or source codes to your classmates at any time. In order to maximize the level of fairness in this class, lending and borrowing homework solutions are both regarded as dishonest behaviors and will be punished according to the honesty policy.

Both English and Traditional Chinese are allowed for writing any part of your homework (if the compiler recognizes Traditional Chinese, of course). We do not accept any other languages. As for coding, either C or C++ or a mixture of them is allowed.

This homework set comes with 200 points. In general, every homework set of ours would come with a full credit of 200 points.

4.1 More about Linked List

(1) (15%) Suppose we have a pointer to a node in a singly linked list that is not the last node of the list, and we do not have pointers to any other nodes (not even the head node). Use any pseudo-code to describe an O(1) algorithm that equivalently removes the content of the node from the list.

(2) (15%) If a singly linked list is wrongly constructed, there would be a cycle within the linked list. Use any search engine or consult any friend to find an O(N ) time and O(1) space algorithm that detects whether there is a cycle in a size-N linked list (hint: usually called Floyd’s Cycle-Finding Algorithm). Learn and explain the algorithm clearly to the grading TA in your own words. Also, cite the website (or the person) that you learn the algorithm from.

4.2 Stack and Queue

(1) (15%) Use any pseudo code to write down an algorithm that uses one queue (with enqueue and dequeue operations) and at most O(1) of additional memory to simulate one stack.

(2) (15%) Use any pseudo code to write down an algorithm that uses two stacks (with push and pop operations) to simulate one deque.

(3) (20%) Do Project P-5.2 of the textbook. You need to describe your design clearly for the TAs.

4.3 Extendable Array

(1) (20%) Do Exercise C-6.7 of the textbook.

1 of 3

(2)

Data Structures and Algorithms (NTU, Spring 2012) instructor: Hsuan-Tien Lin

4.4 Run-length Encoding

Run-length encoding (RLE) is a very simple form of data compression in which runs of data (that is, sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run. For example, consider a raw vector

x = (1, 1, 1, 1, 1, 4, 4, 4, 2, 2, 2, 2, 8, 8) It’s run-length encoding is a list of pairs like

[(5, 1), (3, 4), (4, 2), (2, 8)]

which means there is five “1”,followed by three “4”, followed by four “2”, followed by two “8.”

(1) (100%) You are asked to develope a container that stores an integer vector in its run-length encod- ing. You can choose any underlying container for storing the encoding. Your container should be

“compatible” with std::vector in the sense that the following member functions are supported:

• (constructor): The class must named RLE and takes a raw integer vector as the parameter. That is, RLE(vector<int> x).

• void insert(int pos, int value): insert value at pos-th position of the equivalent raw vector.

• void erase(int pos): delete element from the equivalent raw vector at pos-th position.

• int operator[](int pos): return the element at pos-th position in the equivalent raw vector;

return MAXINT if any error happens

• operator<<: output the run-length encoded vector, like 5 1 3 4 4 2 2 8, to an ostream object.

If the position is invalid (see below), please output a new line with message “ERROR.\n” (including the period and the change-of-line).

(1) insert(int pos,int value): pos less than 0 or greater than (raw vector size) is invalid.

(2) erase(int pos): pos less than 0 or greater than (raw vector size - 1) is invalid.

(3) operator[] (int pos): pos less than 0 or greater than (raw vector size - 1) is invalid.

Submission Rules

In this problem set, you need to submit three files, RLE.h, RLE.cpp and the makefile. You should declare the member functions in RLE.h and implement them in RLE.cpp. The TAs will use some demo programs named main.cpp and the command make and make run to test your program. Therefore you should compile RLE.cpp with a demo program named main.cpp and redirect standard output to a file name output in your makefile.

Warning: Please make sure that your makefile works fine before you sumbit your home- work, TAs may not fix it for you this time.

Toy Program/Output

To be announced on the course website.

2 of 3

(3)

Data Structures and Algorithms (NTU, Spring 2012) instructor: Hsuan-Tien Lin

Submission File

Please upload your program as a single ZIP compressed file to CEIBA before the deadline in the Friday afternoon(04/20/2012). The zip file should be like b86506054.zip, where the file name should be changed to your own school ID. The ZIP file should contain the following items:

• RLE.cpp, RLE.h and makefile

• an optional README, anything you want the TAs to read before grading your code

3 of 3

參考文獻

相關文件

Data Structures and Algorithms (NTU, Class 01, Spring 2020) instructor: Hsuan-Tien Lin..

Data Structures and Algorithms (NTU, Spring 2013) instructor: Hsuan-Tien Lin..

Given a VM host as you created in class, do something so that you can connect your VM to the network with Linux bridge without GUI tool.. Please write down what additional things you

Network Administration/System Administration (NTU CSIE, Spring 2018) Homework #1 Solution1. Homework

Data Structures and Algorithms (NTU, Class 01, Spring 2020) instructor: Hsuan-Tien Lin.. Correctness

[r]

Refer to class notes for notions used in this homework1. The metric on H is given by

The length of the middle part is 3 ε , and the remaining two parts have equal length. At the second stage, we subdivide each of the remaining two parts at the first stage into