3.introduction toDS - Algorithms - Day3 PDF
3.introduction toDS - Algorithms - Day3 PDF
– The first item placed on the queue is the first item retrieved. The
second item put in is the second item retrieved, and so on.
– Example:
• Queues are very common in everyday life. For example, lines at a bank or
a fast-food restaurant are queues.
• used in many types of programming situations such as simulations, event
or appointment scheduling (such as in a PERT or Gant chart), and I/O
buffering.
4 4
3 3 3
2 2 2 2
1 1 1 1 1
5 5 5 5 5
4 4 4 4
3 3 3
2 2
class Queue{
• // constructor © constructor and = operator to void bitwise copying problem
int *q;
int size;
int spos;
int rpos;
public:
Queue ( ) {
spos= rpos = 0;
size = 10;
q= new int[size]; // default value
}
Queue (int s ) {
spos= rpos = 0;
size = s;
q= new int[size];
}
~Queue ( ) {
delete [] q;
}
// We need only some methods as the other methods are against the concept of the
queue.
class Queue : private LinkedList {
// No need for size,,spos, rpos and Employee as the size is dynamic
public:
Queue ( ): LinkedList() {
} //No need for the constructor which take size
~Queue ( ) {
}
void qStore (Employee *e){
addList(e); // or LinkedList :: addList(e);
}
– Example:
• Imagine a stack of plates. The bottom plate in the stack is the last to be
used, and the top plate (the last plate placed on the stack) is the first to be
used.
• Stacks are used a great deal in system software including compilers and
interpreters. In fact, C uses the computer's stack when passing arguments
in functions.
4 4
3 3 3
2 2 2 2
1 1 1 1 1
4 4
3 3 3
2 2 2 2
1 1 1 1 1
2 2 2 2 2
Top 0
if(tos==SIZE)
cout << “full”;
else
st[tos++]=n;
}
int pop(){
int n=0;
if(tos==0)
cout << “empty”;
else{=
n=st[--tos];
return n;
}
Stack(){
delete[] st;
}
};
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 18
Stack
// We need only some methods as the other methods are against the concept of the
Stack.
class Stack : private LinkedList {
// No need for size,tos and Employee as the size is dynamic
public:
Stack ( ): LinkedList() {
} //No need for the constructor which take size
~ Stack ( ) {
}
void push (Employee *e){ // also add to the end of the stack and the list
addList(e); // or LinkedList :: addList(e);
}
• 1st Assignment :
1. Implement Stack and Queue with Linked list.