Download - Stack and Queue

Transcript
Page 1: Stack and Queue

Stack and Queue APURBO DATTA

Page 2: Stack and Queue

Overview what is stack ? Stack in Programming. Basic operations of stack(Pushing, popping etc.) Implementation of Stacks Queue Definition Basic operations of queue

Enqueuing, dequeuing etc. Implementation of queue

Page 3: Stack and Queue

Stack Overview

Stack Definition what is stack ? Condition of stack. Stack in Programming. Basic operations of stack(Pushing, popping etc.) Implementation of Stacks

Page 4: Stack and Queue

Stack DefinitionWe know that the Stack is LIFO Structure i,e Last in First Out. It is very useful data structure in C Programming. Stack can be implemented using the Linked List or Array.

Page 5: Stack and Queue

1.Stack is LIFO Structure [ Last in First Out ]2.Stack is Ordered List of Elements of Same Type.3.Stack is Linear List4.In Stack all Operations such as Insertion and Deletion are permitted at only one end called Top

Condition of stack

Page 6: Stack and Queue

Stack in Programming

Similar is the idea of stack in Programming

1. 1. You can add an element onto the stack.

● 2. You can remove the top most element.

● 3. You can see the top most element.

Page 7: Stack and Queue

Push: Equivalent to an insertPop: Deletes the most recently inserted elementTop: Examines the most recently inserted element

Fundamental operations

Page 8: Stack and Queue

Push and PopPush

Add an element to the top of the stackPop

Remove the element at the top of the stack

empty stack

toptop

push an element

A A A

B

push another pop

top

top

Page 9: Stack and Queue

Implementation of Stacks

Any list implementation could be used to implement a stack Arrays (static: the size of stack is given initially) Linked lists (dynamic: never become full)

We will explore implementations based on array and linked list

Let’s see how to use an array to implement a stack first

Page 10: Stack and Queue

Array Implementation of StackJust like the array implementation of the List, we also

need the array of items where we are going to store the elements of the Stack

Aside from this, we also need an object that keeps track of where the last element is located From this point on, we are going to call it the top top is simply an integer (very much like the head in the

cursor implementation of the List)

Page 11: Stack and Queue

Our Stack class should look very much like this:const MAX = 100;class Stack{private:

int top, items[MAX];public:

Stack();bool push(int);bool pop();int peek(); //int top();bool isEmpty();bool isFull();void display();

};

Array Implementation of Stack

Page 12: Stack and Queue

The constructorStack::Stack(){

top = -1;}

The full checkbool Stack::isFull(){

if(top+1==MAX)return true;

return false;}

The empty checkbool Stack::isEmpty(){

if(top==-1)return true;

return false;}

Array Implementation of StackThe push

bool Stack::push(int x){if(isFull())

return false;items[++top] = x;return true;

}The pop

bool Stack::pop(){if(isEmpty())

return false;top--;return true;

}

Page 13: Stack and Queue

10 13 16 19 22

top = -1

top = 0 top =1 top =2 top =3 top =4

10 13 16 19 22top = 0 top =1 top =2 top =3 top =4

Array Implementation of Stack

Page 14: Stack and Queue

Linked-list Implementation of StackThis implementation is the linked-list implementation of the list except for the following operations

General insert and appendGeneral delete

44 97 23 17head: tail:

9

Page 15: Stack and Queue

Linked-list Implementation of Stack

44 97 23 17

9

PUSH

top:

top:

Page 16: Stack and Queue

44 97 23 17

top:

tmp tmp tmp tmp

del9

Linked-list Implementation of Stackpop

Page 17: Stack and Queue

Queue Overview

Queue Definition Basic operations of queue

Enqueuing, dequeuing etc. Implementation of queue

Array Linked list

Page 18: Stack and Queue

What is Queue

The Queue is like the List but with “limited” insertion and deletion.

Insertion can be done only at the end or rear Deletion can only be done in the front FIFO – first-in-first-out data structure Operations

enqueuedequeue

Page 19: Stack and Queue

Basic operations of queue Primary queue operations: Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear. Enqueue

◦ Insert an element at the rear of the queue Dequeue

◦ Remove an element from the front of the queue

Insert (Enqueue)Remove

(Dequeue) rearfront

Page 20: Stack and Queue

C Program

Algorithm

Enqueue Operation

Page 21: Stack and Queue

Algorithm

C Program

Dequeue Operation

Page 22: Stack and Queue

Storing a queue in a static data structure

This implementation stores the queue in an array.

The array indices at which the head and tail of the queue are currently stored must be maintained.

The head of the queue is not necessarily at index 0. The array can be a “circular array” – the queue “wraps round” if the last index of the array is reached.

Example – storing a queue in an array of length 5

Page 23: Stack and Queue

Storing a queue in a dynamic data structure

Each node in a dynamic data structure contains data AND a reference to the next node.

A queue also needs a reference to the head node AND a reference to the tail node.

The following diagram describes the storage of a queue called Queue. Each node consists of data (DataItem) and a reference (NextNode).

The first node is accessed using the name Queue.Head. Its data is accessed using Queue.Head.DataItem The second node is accessed using Queue.Head.NextNode The last node is accessed using Queue.Tail

Page 24: Stack and Queue

Adding a node (Add) in a dynamic data structure

The new node is to be added at the tail of the queue. The reference Queue.Tail should point to the new node, and the NextNode reference of the node previously at the tail of the queue should point to the DataItem of the new node.

Page 25: Stack and Queue

Removing a node (Remove) in a dynamic data structure The value of Queue.Head.DataItem is returned. A temporary reference Temp is declared and set to point

to head node in the queue (Temp = Queue.Head). Queue.Head is then set to point to the second node instead of the top node.

The only reference to the original head node is now Temp and the memory used by this node can then be freed.

Page 26: Stack and Queue

Implementation of Queue

Just as stacks can be implemented as arrays or linked lists, so with queues.

Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks

Page 27: Stack and Queue

Array Implementation of Queue

10 13 16 19 22

size=5size=1 size=2 size=3 size=4

10 13 16 29 22

size=1 size=2 size=3 size=4 size=5

Page 28: Stack and Queue

/*Queue - Linked List implementation*/

#include<stdio.h>#include<stdlib.h>

struct Node {

int data;struct Node* next;

};// Two l variables to store address of front and rear nodes. struct Node* front = NULL;struct Node* rear = NULL;

// To Enqueue an integervoid Enqueue(int x) {

struct Node* temp = (struct

Node*)malloc(sizeof(struct Node));temp->data =x; temp->next = NULL;if(front == NULL && rear == NULL){

front = rear = temp;return;

}rear->next = temp;rear = temp;

}

queue using linked list in c // To Dequeue an integer.void Dequeue() {

struct Node* temp = front;if(front == NULL) {

printf("Queue is Empty\n");

return;}if(front == rear) {

front = rear = NULL;}else {

front = front->next;}free(temp);

}

int Front() {if(front == NULL) {

printf("Queue is empty\n");

return;}return front->data;

}

void Print() {struct Node* temp = front;while(temp != NULL) {

printf("%d ",temp->data);

temp = temp->next;}printf("\n");

}

int main(){/* Drive code to test the

implementation. */// Printing elements in

Queue after each Enqueue or Dequeue

Enqueue(2); Print(); Enqueue(4); Print();Enqueue(6); Print();Dequeue(); Print();Enqueue(8); Print();

Implement queue using linked list in c

Page 29: Stack and Queue

Comparing queue implementations • Memory requirements

– Array-based implementation • Assume a queue (size: 100) of strings (80 bytes

each) • Assume indices take 2 bytes • Total memory: (80 bytes x 101 slots) + (2 bytes x 2

indexes) = 8084 bytes – Linked-list-based implementation

• Assume pointers take 4 bytes • Total memory per node: 80 bytes + 4 bytes = 84

bytes

Page 30: Stack and Queue

Comparing queue implementations(cont.)

Page 31: Stack and Queue

Comparing queue implementationsBig-O Comparison of Queue OperationsOperation Array

ImplementationLinked

Implementation

Class constructor O(1) O(1)MakeEmpty O(1) O(N)IsFull O(1) O(1)IsEmpty O(1) O(1)Enqueue O(1) O(1)Dequeue O(1) O(1)Destructor O(1) O(N)

Page 32: Stack and Queue

Top Related