0% found this document useful (0 votes)
65 views

3.introduction toDS - Algorithms - Day3 PDF

Uploaded by

Riham Abdallah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

3.introduction toDS - Algorithms - Day3 PDF

Uploaded by

Riham Abdallah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

JavaTM Education & Technology Services

Data Structures and Algorithms

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 1
Queue

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 2
Queue

– A Queue is a linear list of information that is accessed in First-In, First-


Out (FIFO) order.

– 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.

– a queue does not allow random access of any specific item.

– In addition, a queue needs a fixed size region of memory to use as the


queue.

– 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.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 3
Queue

– To visualize how a queue works, consider-two-functions:


• qstore() function places an item onto the end of the queue

Empty Queue Consecutive Store Operations Full Queue


1,2,3,4,5

4 4

3 3 3

2 2 2 2

1 1 1 1 1

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 4
Queue

– To visualize how a queue works, consider-two-functions:


• qretrieve( ) function removes the first item from the queue and returns its
value, if the value is not stored elsewhere, destroys it.

Full Queue Consecutive retrieve Operations Empty Queue


1,2,3,4,5

5 5 5 5 5

4 4 4 4

3 3 3

2 2

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 5
Queue
• spos: pointer to the next empty place to store new item at it.
• rpos: pointer to the next place to retrieve from it.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 6
Queue

– Queue implementation as an array of fixed length:


• // this queue will carry integers. It can carry also Employees Employee *q;

class Queue{
• // constructor &copy 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;
}

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 7
Queue

– Queue implementation as an array of fixed length:


void qStore (int i){ // void qStore ( Employee *i)
if (spos==size )
cout<<"Queue is full.";
else
q[spos++]=i; // q[spos++] = *i;
}

int qretrieve (){ // Employee * qretrieve (){


int ret=-1; // Employee *ret;
if (rpos==spos) // or if(rpos == 0)
cout<<"Queue is Empty.";
else
ret = q[rpos++]; // ret = & q[rpos++];
return ret;
}
};

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 8
Queue

– Queue implementation as a linked list for dynamic length:


1. Analyze the Linked list class first;
class LinkedList
{
protected:
Employee *pStart;
Employee *pEnd;
public:
LinkedList() {pStart=pEnd=NULL;}
~LinkedList() { freeList();}
// Setters and getters for pStart and pEnd;
void addList(Employee *pItem); // Needed : add to the end as the queue
void InsertList(Employee *pItem); // Not needed here
Employee* searchList(int Code); // Needed
int DeleteList(int Code); // Needed :delete element from queue
void freeList(); // Needed
void displayAll(); // Needed
~LinkedList() { freeList();}
};
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 9
Queue

– Queue implementation as a linked list for dynamic length:


2. Class Queue will inherits private from Linked List

// 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);
}

Employee* qretrieve (){


Employee *pNode;
pNode = pStart;
if(pStart){
if(pStart == pEnd){ // there is only node
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 10
Queue

– Queue implementation as a linked list for dynamic length:


pStart = pEnd =NULL;
}
else { // first elment
pStart = pStart -> pNext;
pStart -> pPrevious = NULL;
}
}
return pNode;
}

Employee* searchQueue(int Code){


return searchList(Code);
}

int DeleteQueue(int Code){


return DeleteList(Code);
}
void freeQueue(){
freeList();
}
void displayAll(){
displayAll();
}
};

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 11
Stack

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 12
Stack

– A Stack is the opposite of a Queue because it uses Last-In, First-Out


(LIFO) Accessing.
• This means elements are removed from the stack in the reverse order to the order of
their addition: therefore, the lower elements are typically those that have been in the
list the longest.
• Insert: PUSH
• Remove: POP
• The accessible element is called TOP.
• Like the queue, the retrieval function takes a value off the list and, if the value is not
stored elsewhere, destroys it.

– 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.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 13
Stack

Empty Stack Consecutive Push Operations Full Stack


1,2,3,4,5

4 4

3 3 3

2 2 2 2

1 1 1 1 1

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 14
Stack

– To visualize how a Stack works, consider-two-functions:


• Pop (Retrieve) Operation : Pop operation retrieves a value from the stack.

Full Stack Consecutive Pop Operations Empty Stack


5,4,3,2,1

4 4

3 3 3

2 2 2 2

1 1 1 1 1

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 15
Stack
Top n • Push (2)
Top 1 • Push (5)
• Push (7)
Top 7 7 • Push (1)
Top 5 5 5 • Push (3) // stack is full 3 not added
• Pop() 1
Top 2 2 2 2 • Push (21)
• Pop()21
Top • Pop()7
Top Top • Pop()5
21
• Pop()2
7 7 7 Top • Pop() // stack is empty
5 5 5 5 Top

2 2 2 2 2

Top 0

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 16
Stack

– Stack implementation as an array of fixed length:


class Stack
{
int *st;
int size;
int tos;
public:
Stack( ){ // default
size=10;
st= new int[size];
tos=o;
}

Stack(int s){ // as user wishes


size=s;
st= new int[size];
tos=o;
}
void push(int n){
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 17
Stack

– Stack implementation as an array of fixed length:

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

– Stack implementation as a linked list for dynamic length:


1. Analyze the Linked list class first;
class LinkedList
{
protected:
Employee *pStart;
Employee *pEnd;
public:
LinkedList() {pStart=pEnd=NULL;}
~LinkedList() { freeList();}
// Setters and getters for pStart and pEnd;
void addList(Employee *pItem); // Needed : add to the end as the Stack
void InsertList(Employee *pItem); // Not needed here
Employee* searchList(int Code); // Needed
int deleteList(int Code); // Needed :delete element from Stack
void freeList(); // Needed
void displayAll(); // Needed
~LinkedList() { freeList();}
};
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 19
Stack

– Stack implementation as a linked list for dynamic length:


2. Class Stack will inherits private from Linked List

// 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);
}

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 20
Stack

– Stack implementation as a linked list for dynamic length:


2. Class Stack will inherits private from Linked List
Employee* pop (){ // 1) go to the End 2) go step back 3) remove it.
Employee *pNode;
pNode = pEnd;
if(pEnd){
if(pStart == pEnd){ // there is only node
pStart = pEnd =NULL;
}
else { // last element
pEnd = pEnd -> pPrevious;
pStart -> pNext = NULL;
}
}
return pNode;
}

Employee* searchStack(int Code){


return searchList(Code);
}
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 21
Stack

– Stack implementation as a linked list for dynamic length:


2. Class Stack will inherits private from Linked List

int deleteStack(int Code){


return deleteList(Code);
}
void freeStack(){
freeList();
}
void displayAll(){
displayAll();
}
};

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 22
Lab Exercise

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 23
Lab Exercise

• 1st Assignment :
1. Implement Stack and Queue with Linked list.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 24

You might also like