STACKS & QUEUES for CLASS XII ( C++).

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks And Queues Chapter 18.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
CHP-3 STACKS.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Linear Data Structures
Computer Engineering Rabie A. Ramadan Lecture 6.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
18 Chapter Stacks and Queues
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Introduction Of Stack.
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Chapter 15 Lists Objectives
MEMORY REPRESENTATION OF STACKS
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Dr. Bernard Chen Ph.D. University of Central Arkansas
Homework 4 questions???.
Program to search an element of array using linear search.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Data Structures Interview / VIVA Questions and Answers
Stack and Queue APURBO DATTA.
STACKS.
Algorithms and Data Structures
CSCE 210 Data Structures and Algorithms
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Stacks, Queues, and Deques
Stack and Queues Stack implementation using Array
UNIT-I Topics to be covere d 1.Introduction to data structures.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues Prof. Michael Tsai 2017/02/21.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
DATA STRUCTURES IN PYTHON
Presentation transcript:

STACKS & QUEUES for CLASS XII ( C++)

STACKS AND QUEUES

Objective Introduction to Data Structure Array and Link list Stack Array implementation of Stack Linked Implementation of Stack Queue Array implementation of Queue Linked Implementation of Queue

DATA STRUCTURE: INTRODUCTION A mathematical and logical model of data is known as Data Structure.   Primitive data structure: The data structure, which is available in the compiler, is known as a primitive data structure. Non-primitive data structure: The data structure, which is not available in the compiler, is known as non-primitive data structure.  

DATA STRUCTURE: INTRODUCTION   Linear Data Structure: The data structure in which each element has access to maximum of one predecessor element and maximum of one successor element is known as linear data structure. Example: Stack, Queue, etc. Non-linear Data Structure: The data structure in which each element can access any number of predecessor elements and any number of successor elements is known as Non-linear data structure. Example: Tree, Graphs, etc.  

TYPES OF DATA STRUCTURE Static Data Structure: The data structure in which the number of elements is fixed, is known as Static Data Structure. Example: Arrays Dynamic Data Structure: The data structure in which the number of elements is not fixed, is known as Dynamic Data Structure. Example: Linked List.

ARRAY It is a static data structure. It is a homogeneous collection of data. The elements in the array are stored on consecutive memory locations. Array is also known as a subscripted variable, e.g., A[i] is ith element of the array A.

STACK It is a non-primitive linear data structure in which insertion and deletion of elements takes place from only one end, known as top. QUEUE It is a non-primitive linear data structure in which insertion and deletion of elements takes place from two opposite ends rear and front respectively.

STACKS Stacks is LIFO (Last In First Out) structure and physically can be implemented as an array or as a linked list. Stack, when implemented as an array is functionally same as any other array except that here, adding an element and deletion is done from the same direction just like a pile of books.

Implementation of STACK in computers Inserting an element in an array is known as PUSH. Deleting an element from an array is known as POP. Implementation of STACK in computers When functions are called. To convert a infix expression to postfix. To evaluate a postfix expression.

Representation of STACK A stack is a list in which insertion and deletion takes place only at one end called top. Thus, called LIFO. Representation of STACK data1 data2 data3 data4 TOP TOP data4 data3 data2 data1 data1 data2 data3 data4 TOP TOP data4 data3 data2 data1 Each one of the above has one open and one close end and data movement takes place from open end.

Basic operation and implementation of stacks Creation of stack Check for empty stack Check for full stack Add element in stack Delete element in stack Print stack

STACKS The fundamental operations that can be performed on stack are PUSH and POP. When element is added on the stack top is called PUSH. And When Data is removed from the stack top, the operation is called POP.

STACK The stack operation can be explained as follows: Stack operation Content of array Push(a) a Push (a) a Push(b) ba Push(b) b a Push( c) cba Pop() ba Push( c) C b a Pop() a Pop( c) b a Pop(b) a

STACKS A stack is a list, any list implementation can be used to implement stack. We can implement stack by the following data structures: Array called Linear Stack Linked List called Linked Stack

Linear Stack Stack array int TOP; int S[5]; To hold address of location where data is inserted or deleted int TOP; When PUSH is selected, TOP is incremented, And data is added at that subscript location When POP is selected, TOP is decremented, And data is removed from that subscript location

Lets see working of Linear Stack 8 9 10 7 8 9 10 20 7 8 9 10 TOP TOP OVERFLOW ARRAY IS FULL TOP Push 7 Push 20 Push 14 Top is incremented TOP++

CONTINUED…. 20 7 8 9 10 7 8 9 10 TOP 8 9 10 Top UNDERFLOW OCCURS WHEN STACK IS EMPTY Top Pop 20 TOP is decremented TOP -- Pop 7

Lets see this using a program Click here to execute program Program Code for the Same is Click here to see program code

POINTER Pointer P holds address of amt A variable which holds an address of a memory location of another variable is known as a Pointer Variable (or only pointer). Example int amt, *p; Requires 2 bytes   900 amt 0x8ffebab4 Requires 2 bytes   0x8ffebab4 *P Pointer P holds address of amt

DYNAMIC ALLOCATION NEW operator in C++ returns the address of a block of unallocated bytes (depending on data type a pointer pointing to). DELETE operator in C++ reverses the process of new operator, by releasing the memory location from a pointer. It de allocates memory assigned by NEW.

LINK LIST To allocate dynamic allocation and store address in point g A pointer, which stores the address of struct type data, is known as Pointer to structure. struct abc { int X,Y; }; struct *g=new abc; Holds address of dynamic object of struct abc To allocate dynamic allocation and store address in point g     G   0x8ff134ab G->X G->X G->Y 0x8ff134ab

LINK STACK To hold address of First node of the list struct STACK // structure for stack { int data; STACK *link; }; struct *TOP; To hold address of First node of the list TOP pointer to holds address of dynamic objects of link stack. As we push a node TOP element get shifted and new node becomes first node. LIFO implementation every new node becomes first node. When we pop Top node is deleted and next node becomes first node.

Lets see working of Linked stack Push operation Lets see working of Linked stack Temp holds address of new location * TOP * Temp NULL Initially top is assigned NULL 0x8ffab2e6 A new memory is allocated and address is stored in temp Top = Temp Top will hold address of new location data link * TOP X NULL 0x8ffab2e6 0x8ffab2e6 Thus, TOP will have this address.

Cont….. *TOP Another new memory is allocated to an object 0x8ffab2e6 * Temp 0x8ffab2e8 TOP will get shifted Y becomes first node X becomes second node temp-> link = Top Top=temp * TOP 0x8ffab2e8 data link Now TOP is Y 0x8ffab2e6 data link 0x8ffab2e8 X NULL 0x8ffab2e6

An object is deleted from top delete temp (to release memory) POP operation Cont….. An object is deleted from top * TOP * Temp TOP will get shifted X becomes first node Y will be released 0x8ffab2e8 0x8ffab2e8 Temp=TOP TOP=TOP->link Thus Top will be * TOP delete temp (to release memory) 0x8ffab2e6 data link Y 0x8ffab2e6 data link 0x8ffab2e8 X NULL 0x8ffab2e6

Program Code for the Same is Lets see this using a program Click here to execute program Program Code for the Same is Click here to see program code

Queues Queue is FIFO (First In First Out) structure and physically can be implemented as an array or as a linked list. Queue, when implemented as an array is functionally same as any other array except that here, adding an element and deletion is done from the one direction and deletion from other just like any queue of peoples.

Implementation of queue in computers Queues Inserting an element in an array is known as insert. Deleting an element from an array is known as delete But this is done with the help of two parameters rear and front. Implementation of queue in computers When program is executed.

Representation of Queue A Queue is a data structure in which insertion is done at the end and deletion is done from the front of queue. It is FIFO . Representation of Queue Rear data2 data3 data4 data4 data3 data2 Front data2 data3 data4 Front Rear data4 data3 data2 Rear Front Each one of the above has two open end Front and Rear. Insertion is done from Rear and deletion form Front Front Rear

Basic operation and implementation of QUEUE Creation of Queue Check for empty Queue Check for full Queue Add element in Queue Delete element in Queue Print Queue

QUEUE The fundamental operations that can be performed on Queue are Insert and Delete. When element is added on the Queue Front is called Insert. And When Data is removed from the Queue Rear, the operation is called Delete.

Though first two cells are empty QUEUE The Queue operation can be explained as follows: Queue operation Content of array Insert(a) Front=0 Rear=0 a Insert(b) Front=0 Rear=1 Insert( c) Front=0 Rear=2 a b Delete() Front=1 Rear=2 Delete() Front=2 Rear=2 a b c If we try to insert Overflow occurs Though first two cells are empty b c c

Linear Queue Queue array int Front, Rear; int Q[5]; To hold address of location where data is inserted or deleted int Front, Rear; When INSERT is selected, Rear is incremented, And data is added at that subscript location When DELETE is selected, Front is decremented, And data is removed from that subscript location

QUEUE A Queue is a list, any list implementation can be used to implement Queue. We can implement Queue by the following data structures: Array called Linear Queue Linked List called Linked Queue

Lets see working of LINEAR QUEUE 8 9 10 7 8 9 10 20 7 8 9 10 rear rear OVERFLOW QUEUE is full rear Front Front Front Insert 20 Insert 7 Insert 14 Rear is incremented Rear++

Lets see working of Queue as an array 20 7 8 9 10 Rear 20 7 8 9 Rear 20 7 8 Rear Underflow occurs when QUEUE is empty Front Front Front Delete Delete Front is incremented Front++

Lets see this using a program Click here to execute program Program Code for the Same is Click here to see program code

First and Last node of the list LINKED QUEUE struct QUEUE // structure for QUEU { int data; QUEUE *link; }; struct *Front,*Rear; To hold address of First and Last node of the list Front and Rear pointer to holds address of dynamic objects of link stack. As we insert a node Rear element get shifted and new node becomes next node. FIFO implementation every new node added at end. When we Delete Front node is deleted and next node becomes first node.

Lets see working of Linked Queue Insert operation Lets see working of Linked Queue Temp holds address of new location * Front * Rear * Temp NULL NULL Initially Front and Rear is assigned NULL 0x8ffab2e6 A new memory is allocated and address is stored in temp Front=Rear = Temp Front and Rear will hold address of First location data link * Front * Rear X NULL 0x8ffab2e6 0x8ffab2e6 0x8ffab2e6 Thus, Front and Rear will have this address.

Cont….. Another new memory is allocated to an object *Front * Rear 0x8ffab2e6 0x8ffab2e6 * Temp 0x8ffab2e8 Rear will get shifted Y becomes Last node temp-> link = Rear Rear=temp * Rear 0x8ffab2e8 data link Now Rear is X 0x8ffab2e8 data link 0x8ffab2e6 Y NULL 0x8ffab2e8

An object is deleted from Front delete temp (to release memory) Delete operation Cont….. An object is deleted from Front * Front * Rear * Temp 0x8ffab2e6 0x8ffab2e8 0x8ffab2e6 Front will get shifted Y becomes first node X will be released Temp=Front Front=Front->link Thus Front will be * Front delete temp (to release memory) 0x8ffab2e8 data link X 0x8ffab2e8 data link 0x8ffab2e6 Y NULL 0x8ffab2e8

Program Code for the Same is Lets see this using a program Click here to execute program Program Code for the Same is Click here to see program code

CIRCULAR QUEUE The fundamental operations that can be performed on Circular Queue are Insert and Delete. When overflow occurs though the free cells are available, Rear reaches ends Circular Queue is implemented to avoid this drawback. In Circular Queue as soon as Rear reaches maximum it should reset to 0.

QUEUE The Queue operation can be explained as follows: Queue operation Content of array Insert(a) Front=0 Rear=0 a Insert(b) Front=0 Rear=1 Insert( c) Front=0 Rear=2 a b Delete() Front=1 Rear=2 Insert (d) Front=2 Rear=0 a b c Overflow occurs only when Array is FULL. Rear moves to 0 if array is empty b c d c

Program Code for the Same is Lets see this using a program Click here to execute program Program Code for the Same is Click here to see program code

Do you have any ? QUESTIONS

TEST YOUR KNOWLEDGE What is difference between Stack & Queue? What is Dynamic Allocation? What is the significance of Top? What is the significance of Front & Rear? What is Overflow? What is Underflow? Where Stack is Implemented?

THANK YOU