You are on page 1of 53

Data Structures - CSC2077

Lecture No. 07

Stack Applications

Infix to postfix conversion Infix to prefix conversion Evaluation of prefix and postfix expressions Decimal to Binary Conversion Checking Balanced Symbols

Stack Applications

Infix to postfix conversion Infix to prefix conversion Evaluation of prefix and postfix expressions Decimal to Binary Conversion Checking Balanced Symbols

Balance Sequence

Balance Sequence
{[]} {((()))}

Un-Balanced Sequence
{[} ([[))

Algorithm for Checking Balance Sequence

Push on the Stack if character is Opening symbol If it is closing symbol)


If stack is empty report error Pop symbol from the stack. Report error if it is not corresponding opening symbol (

At the end if stack is not empty report error

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

Stack

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

5+(7*9)(9-5)
5 + ( 7 * 9 ) ( 9 5 )

Operation is Successful

5+)7*9)(9-5)
5 + ) 7 * 9 ) ( 9 5 )

5+)7*9)(9-5)
5 + ) 7 * 9 ) ( 9 5 )

5+)7*9)(9-5)
5 + ) 7 * 9 ) ( 9 5 )

Queue Data Structure

Queue Data Structure


A Stack is LIFO (Last-In, First-Out) structure In contrast, a Queue is a FIFO (First-In, First-Out ) structure A Queue is a linear structure for which items can be only inserted at one end and removed at another end

Queue Data Structure

Queue Operations

Enqueue(X)

Place X at the Rear of the Queue Remove the Front element and return it Return front element without removing it

Dequeue()

Front()

IsEmpty()

Return TRUE if queue is empty, FALSE otherwise


Return TRUE if queue is full, FALSE otherwise

IsFull()

Implementation of Queue

Linked List Array

Linked List Implementation of Queue

Linked List Implementation

front 2 8 6

rear 3

front 2 8 6

rear 3

Linked List Implementation


Dequeue()

front

rear

front

rear

Linked List Implementation


Dequeue()

front 8 6

rear 3

front

rear

Linked List Implementation


enqueue(5)

front 8 6

rear 3

front

rear

Linked List Implementation


enqueue(5)

front 8 6 3

rear 5

front

rear

Linked List Implementation


class LinkedQueue { private: Node* Front; Node* Rear;
public: LinkedQueue(); void Enqueue(int); void Dequeue(); void PrintQueue(); bool IsEmpty(); int GetFront(); };

Constructor of the class


LinkedQueue::LinkedQueue() { Front = NULL; Rear = NULL; }

IsEmpty Member Function


bool LinkedQueue::IsEmpty() { if(Front == NULL) return true;
else return false; }

Enqueue Member Function


void LinkedQueue::Enqueue(int data) { Node* newNode = new Node(); newNode->SetData(data); newNode->SetNext(NULL);
//If Queue is Empty if(IsEmpty() == true) { Front = newNode; } else Rear->SetNext(newNode); Rear = newNode; }

Dequeue Member Function


void LinkedQueue::Dequeue() { Node* temp = Front;
Front = Front->GetNext(); delete temp; }

GetFront Member Function


int LinkedQueue::GetFront() { return Front->GetData(); }

PrintQueue Member Function


void LinkedQueue::PrintQueue() { Node* temp = Front;
if(IsEmpty() == true) { cout << "The Queue is Empty!\n\n"; return; }

PrintQueue Member Function


else {
while(temp != NULL) { cout << temp->GetData(); cout << " --> "; temp = temp->GetNext(); } cout << "NULL\n\n"; } }

Array Implementation

PrintQueue Member Function


class QueueArray { private: int Queue[QUEUE_SIZE]; int Front; int Rear;
public: QueueArray(); void Enqueue(int); int Size(); void Dequeue(); bool IsFull(); bool IsEmpty(); int GetFront(); void PrintQueue();

};

Constructor
QueueArray::QueueArray() { Front = -1; Rear = -1; }

Size Member Function


int QueueArray::Size() { return (Rear - Front + 1); }

IsEmpty Member Function


bool QueueArray::IsEmpty() { if(Front == -1) return true;
else return false; }

IsFull Member Function


bool QueueArray::IsFull() { if(Rear == QUEUE_SIZE) return true;
else return false; }

Enqueue Member Function


void QueueArray::Enqueue(int item) { if(IsFull() == true) { cout << "Can't Insert because the Queue is Full!\n\n"; } else { Queue[++Rear] = item; } }

Dequeue Member Function


void QueueArray::Dequeue() { if(IsEmpty() == true) { cout << "Can't Delete because the Queue is Empty!\n\n"; } else Queue[Front++]; }

PrintQueue Member Function


void QueueArray::PrintQueue() { if(IsEmpty() == true) { cout << "The Queue is Empty!\n\n"; } else { for(int i=Front; i<=Rear; i++) { cout << Queue[i] << endl; } } }

Questions

You might also like