There are two different kinds of lists: unsorted and sorted. Last week, we focused on unsorted lists--where there is no particular order to the data. This week we focus on sorted lists-- where data are maintained in ascending (or descending order).
Regardless of whether the list is sorted or unsorted, there are a set of general purpose operations.
The main difference between the unsorted and sorted lists comes in the implementations of:
Unsorted insert a, b, r, p | Sorted insert a, b, r, p |
---|---|
The following diagram shows the difference between what happens in the replace statements for the unsorted and sorted lists (given the initial list below).
Initial List | Unsorted replace x | Sorted replace x |
---|---|---|
If the list is sorted, however, we can make use of a binary search, which is typically a faster search algorithm. Consider the following example.
If we are looking for 'x' at the end of the list, a binary search will do 4 comparisons, while a sequential search (such as in an unsorted list) will do 8 comparisons.
Note: binary searches can only be done on sorted lists. This is why unsorted lists do not make use of the binary search.
Something has been omitted in the above discussion. That is, sorted lists are typically sorted by some key. In the above examples, the lists have have been shown with one item (a letter). In fact, sorted lists, contain items which are structures.
Let's demonstrate this pictorially (below) using a list of student records. Student records contain at least the student name and number (two components). We can sort this list by either name or number. In the first case, the key is the student name and in the second case, the key is the student number.
Key is Student Name | Key is Student Number |
---|---|
Summary of List Operations *(indicates different implementation) |
---|
isEmpty() |
isFull() |
insert(DataType)* |
retrieve(char, DataType)* |
replace(DataType)* |
clear() |
showStructure() |
gotoBeginning() |
gotoEnd() |
gotoNext() |
gotoPrior() |
getCursor() |
We recycle this code using inheritance. We will derive the Sorted List from the Unsorted List. The derived class (the Sorted List) will inherit the member functions and data members of the existing base class (the Unsorted List). In additon, the derived class may have its own member functions and data members. The following declaration from ordlist.h derives a class called OrdList from the List class.
class OrdList : public List { public: // Constructor OrdList ( int maxNumber = defMaxListSize ); // Modified (or new) list manipulation operations virtual void insert ( const DataType &newDataItem ) throw ( logic_error ); virtual void replace ( const DataType &newDataItem ) throw ( logic_error ); bool retrieve ( char searchKey, DataType &searchDataItem ); // Output the list structure -- used in testing/debugging virtual void showStructure () const; private: // Locates an element (or where it should be) based on its key bool binarySearch ( char searchKey, int &index ); };Note that the declaration
class OrdList : public ListIndicates that Ordlist is derived from List. The keyword "public" specifies that this is a public inheritance. Therefore, OrdList will have access to List's public member functions, but not to its private data members (or private member functions).
However, because we want the member functions in OrdList to have access to List's private data members, we must change the data members in the List class declaration from private to protected as follows:
class List { . . . protected: // Data members int maxSize, // Maximum number of elements in the list size, // Actual number of elements in the list cursor; // Cursor array index DataType *dataItems; // Array containing the list elements };Before this change, List's data members were private and therefore could only be accessed by List's member functions. Now, with protected data members, the derived (OrdList) class has access (through its member functions) to List's data members.
A few other things can be noted about this derived OrdList class:
class List { public: ... // List manipulation operations virtual void insert ( const DataType &newDataItem ) // Insert throw ( logic_error ); virtual void replace ( const DataType &newDataItem ) // Replace throw ( logic_error ); ... protected: // Data members int maxSize, // Maximum number of elements in the list size, // Actual number of elements in the list cursor; // Cursor array index DataType *dataItems; // Array containing the list elements };
When a message is sent through a packet-switching network, the message is NOT sent as a continuous stream of data. Instead, it is divided into pieces called packets. These packets are sent through the network to a receiving site, which reassembles the message.
Each packet may come by a different path, and, therefore, arrive in a different order then they were intended. In order for the receiving site to reassemble the message, each packet must contain its order relative to the sequence of packets.
For example, if we want to send the message "HELLO! CS 210 IS FUN" as packets of five characters long. Each packet will also contain a number indicating its position in the message. The result will be the following set of packets:
1 HELLO
2 ! CS
3 210 I
4 S FUN
No matter in what order these packets arrive, a receiving site can correctly reassemble the message by placing the packets in ascending order based on their position numbers.
Your job is to create a program that reassembles the packets contained in a text file (message.dat). The contents of this file are the following:
13 ions! 4 sfull 11 ongra 8 the 5 y rec 9 messa 3 ucces 2 ave s 10 ge. C 7 ucted 1 You h 12 tulat 6 onstr 14 1!!2!Your program will make use of the Sorted List to reassemble the packets in this message. Assume that each packet in the message file contains a position number and five characters from the message.
Your program will have the following declarations:
const int packetSize = 6; // Number of characters in a packet // including null ('\0') terminator, // but excluding the key (char 0). struct DataType { int position; // (Key) Packet's position w/in message char body[packetSize]; // Characters in the packet int getKey () const { return position; } // Returns the key field };Notice that the position is the key, and the body contains the five characters of the message.
Click here To get the zipped files for this exercise
Your primary tasks for this exercise are to edit the packet.cpp
file so that the program does the following:
Steps include:
messageFile.ignore(); messageFile.getline(currPacket.body, packetSize, '\n');
Message: You have successfully reconstructed the message. Congratulations!1!!2! Press any key to continue
Test Plan for Message Processing Program
Test Case | Checked |
---|---|
Message in the file message.dat | |
If you would like to, make your own message files and test them with this program.
Friday, 21-Aug-2020 15:22:14 CST |
|
|
|
|