• No results found

Data Structures and Algorithms

N/A
N/A
Protected

Academic year: 2021

Share "Data Structures and Algorithms"

Copied!
28
0
0

Loading.... (view fulltext now)

Full text

(1)

Data Structures and Algorithms

Alice E. Fischer

(2)

Outline Overview: L inea r Data Structures Queues C+ + T em p la te Cl a ss F u n ct io n s Ho m ew or k 1 Over vi ew: Li n ea r Da ta S tr u ctu res 2 Qu eu es Ci rc u la r Qu eu es Link ed Q ueue s 3 C + + T em p la te C la ss F u n ct ion s Q u eu e S yn ta x an d F u n ct ion s in C + + 4 H om ew or k Alice E. Fischer Data Str uctur es L6, Restr ic ted Linea r Data Str Stacks and Q ueues. ..

(3)

FIFO and LIFO

Data structures can be specialized versions of the general array, growing array, or linked list.

A Stack is a Last-In-First-Outdata structure: all insertions

and deletions happen at one end of the array or list.

A Queue is a First-In-First-Outdata structure with both a

front and aback. Data is added at the back of the queue

and removed at the front.

A Dequeue is a more general data structure with both a

front and aback. Data can be either added or removed at

both the front and the back of the dequeue. Stacks and queues have played an important role in the development of computer science.

(4)

Stack and Queue Implementations

Implementation can be an array, a growing array, or a linked list. Each implementation has its own and advantages / disadvantages.

Stack Queue Dequeue

Array simple, fast, fast, not

limited size. limited size. appropriate.

Growing fast, fast, used by STL

Array not complex, complex to program tricky to program

unlimited size unlimited size unlimited size

Linked easy not difficult not

List slower runtime slower runtime appropriate.

unlimited size unlimited size

(5)

Traditional Stack and Queue Functions

There is a long tradition behind the names of the functions used with stacks and queues.

Stack empty full top push pop Queue empty full front back enqueue dequeue

C++ provides template class implementations that honor some of these names, change some, add alternative names and provide

(6)

Queues Circular Queues

Linked Queues

(7)

Queues are Familiar

The first item inserted in a queue is the first to be processed, and every item is processed in order.

We “queue up” for service in banks, grocery stores, airports, etc.

(8)

Queues in Computing

Queues are central in computer simulations. Events that happen in the simulation are modeled by objects placed in a queue. When they get to the front of the queue, the

computer “executes” the imaginary event.

Queues are used in GUI interfaces to manage events.

Within a running computer, there are multiple processes and threads that all need to share one (or a few) processing units. The operating system handles these processes by putting them in queues, waiting for a turn at executing on a CPU. Every I/O request ends one turn. When the I/O is finished, the process goes back into the queue, waiting for another turn.

(9)

A Circular Queue - Empty

A queue implemented in an array is called acircular queuebecause

the part of the array that is filled with date moves toward the end, then circles back to the beginning of the array.

Here, we push 4 items onto the queue.

? 0 count 0 front data ? ? ? [0] [1] [2] [3] [4] [5] [6] [7] Queue q; ? ? ? ? 0 back 8 max

(10)

A Circular Queue - 1

Here, we push 4 items onto the queue.

a 4 count 0 front data z m p [0] [1] [2] [3] [4] [5] [6] [7]

q.push('a'); q.push('z'); q.push('m'); q.push('p');

? ? ? ? 4 back 8 max

(11)

A Circular Queue - 2

Now we pop two and push two.

q.pop(); q.push('b'); q.push('c'); q.pop();

? 4 count 2 front data ? m p [0] [1] [2] [3] [4] [5] [6] [7] ? ? c b 6 back 8 max

(12)

A Circular Queue - 3

Here we see the back position wrap around.

q.push('d'); q.push('e'); q.push('f');

f 7 count 2 front data ? m p [0] [1] [2] [3] [4] [5] [6] [7] e d c b 1 back 8 max

(13)

A Circular Queue - 4

Here we see the front position wrap around.

? 2 count 1 front data g h ? [0] [1] [2] [3] 3 back 8 max [4] [5] [6] [7] ? ? ? ?

q.pop(); q.pop(); q.pop(); q.pop(); q.pop(); q.push('g'); q.push('h'); q.pop(); q.pop();

(14)

A Linked Queue – Empty

An empty linked queue has a dummy header cell.

This eliminates special cases and makes programming easier. The dummy cell gives a place to anchor front and back pointers in an empty list.

nullptr ~ 0 count front back Queue q;

(15)

A Linked Queue - 1

A linked queue after four pushes.

a 4

count front

q.push('a'); q.push('z'); q.push('m'); q.push('p');

z m nullptr p back ~

(16)

Pop One and Push One.

nullptr a

q.pop();

pop() must free this cell: q.push('b'); z 4 count front m p nullptr b back ~

(17)

A Linked Queue - 3

The queue grows and shrinks easily by relinking cells.

2 count

front

q.pop(); q.pop(); q.pop();

back q.push('c'); nullptr c b ~

(18)

STL Classes Overview of STL STL Queue Implementations

(19)

Stack and Queue Implementations

When using a data structure, there are two approaches: Build your own.

Use one of the template classes (STL) provided by C++. The second choice is normally better. STL containers are debugged, ready to use, and very adaptable.

However, because this is a data structures class, I will sometimes ask you to build your own. In this way, you will better understand how data structures work.

(20)

Stack and Queue Functions in C++

C++ template classes exist for many data structures, including Stack and Queue. These functions are defined:

Stack (constructor) empty size top push emplace pop swap Queue (constructor) empty size front back push emplace pop swap

(21)

queue<BT>

queue is an adapter class; it is an interface wrapped around another STL class.

The underlying class can be a vector<BT>, a list<BT> or a deque<BT>.

A deque (double-ended queue) is the default. (First form of the constructor, below).

To make a queue with a di↵erent underlying container, use the second form of the constructor.

queue<int> qu1; // Empty queue using deque queue<int, list<int>> qu2; // Empty queue using list

(22)

queue<BT> Functions

Examples of use are given here. Prototypes are at cplusplus.com

while (!qu2.empty()) ... // Result is type bool

unsigned n = qu2.size(); // # of elements in container

BT temp = qu2.front( ); // Refers to oldest item in qu2

BT temp = qu2.back( ); // Refers to newest item in qu2

qu2.push( item ); // Insert item after back

qu2.emplace( BT( ) ); // Construct and push an item

qu2.pop(); // Remove item at front. Return void

(23)

Program 4: Bucket Sort.

Due March 1

A bucket sort1 is the fastest sort for a vary large number of

data items2. It is an O(n) sort that works by using groups of

bits from the sort keys rather than by comparing key values to each other.

The bucket sort described here is applicable to any large data set, but the presentation is specific to the one you will be using.

In this assignment, you will implement an array of queues. You will build your own queue class, not use the C++ STL queue.

(24)

The Data to Sort

We will be sorting objects with two data fields: (1) the time (type time t), a 4-byte unsigned integer. (2) the temperature, a 4-byte float.

The data file will consist of a very large number of

time/temperature measurements, recorded in January at an apartment in Tuckahoe, New York, arranged by time. There will be three files, one from each of three sensors on di↵erent sides of the building. You only need to process one of the files, but di↵erent people should choose di↵erent files. Handle file opening and EOF properly.

First, all of the data must be read from a file, and each data item installed in a new cell. The cells must each be linked onto the end of a queue called the master queue.

(25)

Sorting by Temperature.

Data cells will be distributed intobuckets.

An array of 256 buckets must be created. Each bucket is a linked queue, initially empty.

When the data and buckets are ready, sorting begins. It will work in four passes (numbered 0, 1, 2, and 3), where each pass consists of a distribute operation (next slide) and a collect operation (slide 26) .

After distributing all the data into the buckets. the queues from the buckets must be collected and joined back into a single master queue.

At the end of four passes, the data will again be in the master queue, and will be sorted. Output the sorted data to a file.

(26)

Distributing Data into Buckets

Distribute each cell into a bucket as follows: Remove it from the front of the master queue.

Access one byte of the temperature and use the result as a subscript to select a bucket. Isolate that byte using shifting and masking (next slide).

In pass k, use the kth byte from the right.

(27)

Isolating one byte.

Define mask = 0xff; to isolate one byte (8 bits).

Start with the temperature value from the cell you are distributing.

Compute subscript = mask & (temp >> n)

where n is 8⇤ k, and k is the pass number.

The computed subscript is the number of the bucket to use for this data item.

Detach the cell from the master queue and push it onto the selected bucket queue.

(28)

Collecting the Data into a Master List

At the end of each distribution pass, your master queue is empty. Now collect all the cells, as follows:

Find the first non-empty bucket, (k), in the bucket array. Start at bucket k and process buckets, in order, through number 255.

If the bucket is not empty,

Attach the last cell of the master queue to the first non-dummy cell in the bucket.

Set the back pointer of the master list equal to the back pointer of the bucket.

All the cells have now been removed from the bucket, so reset it to empty.

References

Related documents

Polycystic ovary syndrome (PCOS) is a very common endocrine disorder, affecting approximately 10% of females in the re- productive age worldwide. PCOS is not only associated

diplomacy ) hanyalah sebagai usaha untuk mencari kesepakatan damai ditingkat akar rumput sehingga membantu masyarakat untuk mempererat hubungan antar kelompok serta

experiences. However, I recognize that practitioners, especially those in student affairs, want to better serve their students and peers and sometimes need tangible and

Therefore dynamic SCPC delivers the same dynamic bandwidth allocation functionality of the iDirect Shared Service, while dramatically reducing required return link

Journal of Applied and Natural Science 11(1): 23- 34 (2019) ISSN : 0974-9411 (Print), 2231-5209 (Online) journals.ansfoundation.org Composition, richness and floristic

While this study [38] has generated important lessons on the relationship of both neighbourhood form and SES with PA in children, and highlighted the significance of considering

Concept of Structured data: Data structure definition, Different types and classification of data structures, Operations on Data structures, Arrays – representation of array

Specialization: Digital design(System design,RTL and Verification), Verilog Main business: Design, Research, Offshore projects. Description: ARF-Design works in two domains of