CSS 342 DATA S TR U CTURES ALGO

  • Slides: 32
Download presentation
CSS 342 DATA S TR U CTURES, ALGO RITHMS , AND DISCRETE MATHEMATICS I

CSS 342 DATA S TR U CTURES, ALGO RITHMS , AND DISCRETE MATHEMATICS I LECTU RE 15. 150304. CHAPT ER 13, 14.

Agenda • Code example from last time • STL • Topic: Queues • Peer

Agenda • Code example from last time • STL • Topic: Queues • Peer Review of Lab 5 Design • Announcement: Please bring computers on Monday

Write a member function, Move. To. End which takes in an item by value,

Write a member function, Move. To. End which takes in an item by value, finds the first occurrence and moves it to the end of the list. Return false if item could not be found; otherwise true.

bool My. List: : Move. To. End(Item it) { Node *current = head; Node

bool My. List: : Move. To. End(Item it) { Node *current = head; Node *p. Found = NULL; if (current == NULL) { return false; } while ((current != NULL) && (p. Found == NULL)) { if (*(current->p. Item) == it) { p. Found = current; } else { current = current->next; } } if (p. Found == NULL) { return false; } while (current->next != NULL) { current = current->next; } Item *temp = current->p. Item; current->p. Item = p. Found->p. Item; p. Found->p. Item = temp; }

STL WHY REINVENT THE WHEEL?

STL WHY REINVENT THE WHEEL?

STL Sequence Containers: The Big 3 • Vector • • Flexibly sized array Access

STL Sequence Containers: The Big 3 • Vector • • Flexibly sized array Access any element in constant time (index into array) Add/Remove from the end of array Data kept contiguous in memory • Deque • • Double ended queue Can add/look from front or back Access any element in constant time Not guaranteed to be contiguous in memory • List • Linked list • Need iterator to traverse • Can add anywhere in list in constant time

Recall the stack • Last In First Out (LIFO) • We implemented with following

Recall the stack • Last In First Out (LIFO) • We implemented with following structures: push • Array • Linked List • (a. Stack. push(new. Item)). pop() is equal to a. Stack • STL has a stack implementation as a Container Adapter • Container adapter on vector, deque, or list • Default is deque • Functions: empty, size, push, pop, top pop

int main( ) { stack<int> a. Stack; stack<int, vector<int>> b. Stack; for (int i

int main( ) { stack<int> a. Stack; stack<int, vector<int>> b. Stack; for (int i = 0; i < 3; i++) { b. Stack. push(i); } cout << "stack size is: " << b. Stack. size() << endl; cout << "top element is: " << b. Stack. top() << endl; b. Stack. pop(); cout << "Popped! " << endl; cout << "top element is: " << b. Stack. top() << endl; return 0; }

Queues

Queues

Queue • First in First Out (FIFO) • Can be implemented with Time =

Queue • First in First Out (FIFO) • Can be implemented with Time = 0 • Array • Linked List • (a. Q. push(new. Item)). pop() != a. Q Time = 12 • Uses • Many: powerful data structures for impls. • Often used for short lived (in-memory) or persistent applications (written to disk) • Modeling: Rich field on queueing theory/modeling CSS 342: QUEUES Time = 20 Time = 38 10

Queue Specification • STL has a stack implementation as a Container Adapter • Container

Queue Specification • STL has a stack implementation as a Container Adapter • Container adapter on vector, deque, or list • Default is deque • Functions: empty, size, push, pop, back, front pop( ): remove and get the front item push() to the back(): get the back item but do not remove it Front(): get the front item but do not remove it

int main( ) { queue<int> a. Q; for (int i = 0; i <

int main( ) { queue<int> a. Q; for (int i = 0; i < 5; i++) { a. Q. push(i); } cout << "Queue size is: " << a. Q. size() << endl; cout << "Front element is: " << a. Q. front() << endl; cout << "Back element is: " << a. Q. back() << endl; a. Q. pop(); cout << "Popped! " << endl; cout << "Front element is: " << a. Q. front() << endl; cout << "Back element is: " << a. Q. back() << endl; return 0; }

Comparison of Stack and Queue Operations • size, empty, push, pop, =, comparators are

Comparison of Stack and Queue Operations • size, empty, push, pop, =, comparators are all common syntax • Both are adapters which can be built on containter classes • Differences: • stack: top • queue: front, back • Semantics: LIFO, FIFO

Queue implementation • Array Based • Pointer based • Can we make a queue

Queue implementation • Array Based • Pointer based • Can we make a queue from lists? • How can we make a queue Persistent?

Computer Scientist of the week Gary Kildall Born in Seattle UW Graduate, Ph. D

Computer Scientist of the week Gary Kildall Born in Seattle UW Graduate, Ph. D in 1972 Created CP/M Operating System Created BIOS: Basic Input Output System Allow microprocessor to communicate with disk drive! DRI (Kildall) and IBM negotiated about usage of CP/M but did not reach a deal • PC-DOS soon followed • General controversy on whether any infringements occurred • • •

An Array-Based Implementation 0 k front back 47 2 4 1 0 1 2

An Array-Based Implementation 0 k front back 47 2 4 1 0 1 2 49 front back …. . 1 2 …. . k …. . 0 7 the. Array. size( )-1 4 10 7 47 48 49 the. Array. size( )-1 1) Shift left on each removal? 2) Shift left when end of queue is reached? 3) Never Shift but maintain moving front and back pointers? 4) Allocate new array when full?

Circular-array implementation front 0 the. Array. size( )-1 2 4 1 7 1 2

Circular-array implementation front 0 the. Array. size( )-1 2 4 1 7 1 2 3 back

MAX_Q-1 (1) 0 front back (2) 7 count = 0 back 2 count =

MAX_Q-1 (1) 0 front back (2) 7 count = 0 back 2 count = 2 3 3 MAX_Q-1 0 (4) 1 4 2 9 1 6 Enqueue(7) Enqueue(6) 2 Enqueue(3) back Enqueue(8) Enqueue(9) Dequeue() Enqueue(2) Dequeue(4) Dequeue() count = 5 front 0 1 Initial state (3) MAX_Q-1 8 3 3 2 front MAX_Q-1 0 while (the. Q. size()) back { front Dequeue() } 1 2 count = 0 3

Contract (. h) class My. Queue { public: My. Queue(); ~My. Queue(); bool is.

Contract (. h) class My. Queue { public: My. Queue(); ~My. Queue(); bool is. Empty() const; int get. Count() const; int Dequeue() throw (exception); void Enqueue(const int &val); private: vector<int> arr; int front; int back; int count; void double. Queue. Size(); };

My. Queue: : My. Queue() { arr. resize(ARRAY_START_SIZE); front = 0; back = arr.

My. Queue: : My. Queue() { arr. resize(ARRAY_START_SIZE); front = 0; back = arr. size() - 1; count = 0; } void My. Queue: : double. Queue. Size() { } int My. Queue: : get. Count() const { return count; } bool My. Queue: : is. Empty() const { if (count == 0) { return true; } else { return false; } }

void My. Queue: : Enqueue(const int &val) { if (count == arr. size()) {

void My. Queue: : Enqueue(const int &val) { if (count == arr. size()) { double. Queue. Size(); } back = (back + 1) % arr. size(); arr[back] = val; count++; } int My. Queue: : Dequeue() throw (exception) { int ret. Val; if (count > 0) { ret. Val = arr[front]; front = (front + 1) % arr. size(); count--; return ret. Val; } else { throw exception("Queue Empty"); } }

Peer design review of lab 5…

Peer design review of lab 5…

A Pointer-Based Implementation 2 front 4 1 7 back NULL

A Pointer-Based Implementation 2 front 4 1 7 back NULL

class My. Queue { public: My. Queue(); ~My. Queue(); bool is. Empty() const; int

class My. Queue { public: My. Queue(); ~My. Queue(); bool is. Empty() const; int get. Count() const; int Dequeue() throw (exception); void Enqueue(const int &val); private: struct Node { int val; Node *next = NULL; }; Node *front; Node *back; int count; };

My. Queue: : My. Queue() { front = NULL; back = NULL; count =

My. Queue: : My. Queue() { front = NULL; back = NULL; count = 0; } My. Queue: : ~My. Queue() { while (get. Count() > 0) { Dequeue(); } } bool My. Queue: : is. Empty() const { if (front == NULL) { return true; } else { return false; } } int My. Queue: : get. Count() const { return count; }

void My. Queue: : Enqueue(const int &value) { Node *ins. Node; ins. Node =

void My. Queue: : Enqueue(const int &value) { Node *ins. Node; ins. Node = new Node; ins. Node->val = value; if (count == 0) { back = ins. Node; front = ins. Node; } else { back->next = ins. Node; back = ins. Node; } count++; } int My. Queue: : Dequeue() throw (exception) { if (count == 0) { throw exception("Queue is empty"); } Node *temp = front; int ret. Val = temp->val; front = front->next; delete temp; if (count == 1) { back == NULL; } count--; return ret. Val; }

Are we done? • Do we need to overload =, copy constructor? • Why

Are we done? • Do we need to overload =, copy constructor? • Why or why not? • Any other overloads?

Queue usage in OS, Network, Services • Message queues • Scheduling queue • TCP

Queue usage in OS, Network, Services • Message queues • Scheduling queue • TCP Flow control • Payment processing

Unix Message Queues struct mymesg { long mytype; char mtext[512]; } message_body; int main(

Unix Message Queues struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); strcpy( message_body. mtext, “hello worldn” ); msgsnd( msgid, &message_body, 512, 0 ); } int main( void ) { int msgid = msgget( 100, IPC_CREAT ); msgrcv( msgid, &message_body, 512, 0, 0 ); cout << message_body. mtext << endl; } Message queue (id = msgid) 0 1 2 Some other process can enqueue and dequeue a message

Multilevel Queue Scheduling • Each queue has its own scheduling algorithm, – foreground (interactive)

Multilevel Queue Scheduling • Each queue has its own scheduling algorithm, – foreground (interactive) – RR, 80% CPU time – background (batch) – FCFS, 20% CPU time

Multilevel Feedback-Queue Scheduling • • A new job enters queue Q 0 which is

Multilevel Feedback-Queue Scheduling • • A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2.

Flow Control Sending application Receiving application packet Send Socket Buffer Receive Socket Buffer send

Flow Control Sending application Receiving application packet Send Socket Buffer Receive Socket Buffer send ack read blocked X packet retransmitted packet dropped packet retransmitted packet dropped X write blocked X X Application is slow to read.