Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I

Similar presentations


Presentation on theme: "CSS 342 Data Structures, Algorithms, and Discrete Mathematics I"— Presentation transcript:

1 CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Lecture CHAPTER 13, 14.

2 Agenda STL Queues Lab 5 Intro and peer design requirements

3 STL Sequence Containers: the Big 3 (recap)
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

4 Recall the stack pop push Last In First Out (LIFO)
We implemented with following structures: Array Linked List (aStack.push(newItem)).pop() is equal to aStack 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 push

5 #include <vector>
#include <stack> #include <deque> int main( ) { stack<int> aStack; stack<int, vector<int>> bStack; for (int i = 0; i < 3; i++) bStack.push(i); } cout << "stack size is: " << bStack.size() << endl; cout << "top element is: " << bStack.top() << endl; bStack.pop(); cout << "Popped! " << endl; return 0;

6 Big-O Challenge for (int i = 1; i <= n; i++) { int j = n;
while (i * i < j) j--; MyBigTask(); }

7 Queues

8 Queue First in First Out (FIFO) Can be implemented with
Array Linked List (aQ.push(newItem)).pop() != newItem 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 Time = 0 Time = 12 Time = 20 Time = 38 CSS342: Queues

9 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 back(): get the back item but do not remove it Front(): get the front item but do not remove it

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

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

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

13 In-Class Code Create a queue using an array as an underlying data structure

14 An Array-Based Implementation
k 2 4 1 ….. 7 ….. front back 1 2 k theArray.size( )-1 47 49 ….. 4 10 7 front back 1 2 47 48 49 theArray.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?

15 Circular-array implementation
2 4 1 7 3 theArray.size( )-1 front back

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

17 Contract (.h) class MyQueue { public: MyQueue(); ~MyQueue();
bool isEmpty() const; int getCount() const; int Dequeue() throw (exception); void Enqueue(const int &val); private: vector<int> arr; int front; int back; int count; void doubleQueueSize(); };

18 int MyQueue::getCount() const
{ return count; } bool MyQueue::isEmpty() const if (count == 0) return true; else return false; MyQueue::MyQueue() { arr.resize(ARRAY_START_SIZE); front = 0; back = arr.size() - 1; count = 0; } void MyQueue::doubleQueueSize() { }

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

20 Lab5 Intro

21 Lab 5 The Jolly Banker Purpose: Two phase project Learn Queues
Learn Binary Search Trees Practice class design Two phase project Design Review: Thursday, 12/1 Lab Due: 12/9.

22 Computer Scientist of the Week
Margaret Hamilton Developed on-board flight software for Apollo space program Director of MIT Instruments Lab Just awarded Presidential Medal of Freedom Contributions to async software, priority scheduling and priority Display Foundations for ultra-reliable software design Apollo 11 Landing: design helped to successfully land ship amidst Overloading

23 Computer Scientists who were awarded Presidential Medal of Freedom this week
Grace Hopper Margaret Hamilton Robert DeNiro

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

25 Bell Rang

26 class MyQueue { public: MyQueue(); ~MyQueue(); bool isEmpty() const; int getCount() const; int Dequeue() throw (exception); void Enqueue(const int &val); private: struct Node int val; Node *next = NULL; }; Node *front; Node *back; int count;

27 bool MyQueue::isEmpty() const
{ if (front == NULL) return true; } else return false; int MyQueue::getCount() const return count; MyQueue::MyQueue() { front = NULL; back = NULL; count = 0; } MyQueue::~MyQueue() { while (getCount() > 0) Dequeue(); }

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

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

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

31 Unix Message Queues 1 2 Message queue (id = msgid)
struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); strcpy( message_body.mtext, “hello world\n” ); msgsnd( msgid, &message_body, 512, 0 ); } struct mymesg { long mytype; char mtext[512]; } message_body; 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) 1 2 Some other process can enqueue and dequeue a message

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

33 Multilevel Feedback-Queue Scheduling
A new job enters queue Q0 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 Q1. At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

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


Download ppt "CSS 342 Data Structures, Algorithms, and Discrete Mathematics I"

Similar presentations


Ads by Google