SlideShare une entreprise Scribd logo
1  sur  49
Let’s learn
TACKS & UEUES
for CLASS XII( C++)
SENIOR SECONDARY GROUP
Presented By : NITI ARORA
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
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.
 
 
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.
 
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.
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.
It is a non-primitive linear data structure in
which insertion and deletion of elements
takes place from only one end, known as top.
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.
STACK
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.
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)
b
a
Push( c)
C
b
a
Pop( c)
b
a
Pop(b)
a
Push(b) ba
Push( c) cba
Pop() ba
Pop() 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
int S[5];
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
Stack array
int TOP;
To hold address of
location where data
is inserted or deleted
Lets see working of Linear Stack
8
9
10
Push 7
7
8
9
10
Push 20
20
7
8
9
10
Push 14
OVERFLOW
ARRAY IS FULLTOP
TOP
TOP
Top is incremented
TOP++
CONTINUED….
20
7
8
9
10
Pop 20
7
8
9
10
Pop 7
8
9
10
UNDERFLOW
OCCURS WHEN
STACK IS EMPTY
TOP
Top
Top
TOP is decremented
TOP --
Lets see this using a program
Program Code for the Same is
Click here to execute program
Click here to see program code
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;
amt
 
 
900
Requires 2
bytes
0x8ffebab4
 
 
0x8ffebab4*P
Requires 2
bytes
Pointer P holds
address of amt
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.
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
 
G
 
 
 
0x8ff134ab
G->X
0x8ff134ab
G->X G->Y
To allocate dynamic allocation
and
store address in point g
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
* TOP* TOP * Temp
NULL 0x8ffab2e6
A new memory is
allocated and address
is stored in temp
X NULL
data link
0x8ffab2e6
Top = Temp
Top will hold address
of new location
* TOP* TOP
0x8ffab2e6
Thus, TOP will have
this address.
Push operation
Initially top is
assigned NULL
Temp holds
address of new
location
Cont…..
*TOP*TOP
* Temp0x8ffab2e6
0x8ffab2e8
Another new memory is
allocated to an object
Y
data link
0x8ffab2e8
* TOP* TOP
0x8ffab2e8
X NULL
data link
0x8ffab2e6
temp-> link = Top
Top=temp
0x8ffab2e6
Now TOP is
TOP will get shifted
Y becomes first node
X becomes second node
Cont…..
* TOP* TOP * Temp
0x8ffab2e8
0x8ffab2e8
An object is deleted
from top
Y
data link
0x8ffab2e8
Thus Top will be
* TOP* TOP
0x8ffab2e6
X NULL
data link
Temp=TOP
TOP=TOP->link
0x8ffab2e6
delete temp (to release
memory)
0x8ffab2e6
TOP will get shifted
X becomes first node
Y will be released
POP operation
Lets see this using a program
Program Code for the Same is
Click here to execute program
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.
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.
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
data2
data3
data4
data4
data3
data2
data2 data3 data4
data4 data3 data2
Each one of the above has two open end
Front and Rear. Insertion is done from
Rear and deletion form Front
Rear
Front
Front
Rear
Front Rear
FrontRear
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.
QUEUE
The Queue operation can be explained as follows:
Queue operation Content of array
Insert(a) Front=0 Rear=0
Insert(b) Front=0 Rear=1
Insert( c) Front=0 Rear=2
Delete() Front=1 Rear=2
Delete() Front=2 Rear=2
a
a b
a b c
b c
c
If we try to insert
Overflow occurs
Though first two cells are empty
Linear Queue
int Q[5];
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 array
int Front, Rear;
To hold address of
location where data
is inserted or deleted
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
Insert 7
7
8
9
10
Insert 20
20
7
8
9
10
Insert 14
OVERFLOW
QUEUE is full
Front
rear
rear
rear
Front Front
Rear is incremented
Rear++
Lets see working of Queue as an array
20
7
8
9
10
Delete
20
7
8
9
Delete
20
7
8
Underflow occurs
when QUEUE is
empty
Rear
Front
Rear
Rear
Front
Front
Front is incremented
Front++
Lets see this using a program
Program Code for the Same is
Click here to execute program
Click here to see program code
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
* Front * Rear* Front * Rear * Temp
NULL NULL 0x8ffab2e6
A new memory is
allocated and address
is stored in temp
X NULL
data link
0x8ffab2e6
Front=Rear = Temp
Front and Rear will
hold address of First
location
* Front * Rear* Front * Rear
0x8ffab2e6 0x8ffab2e6
Thus, Front and Rear will
have this address.
Insert operation
Initially Front and
Rear is assigned
NULL
Temp holds
address of new
location
Cont…..
*Front * Rear*Front * Rear
* Temp
0x8ffab2e6 0x8ffab2e6
0x8ffab2e8
Another new memory is
allocated to an object
X
data link
0x8ffab2e6
* Rear* Rear
0x8ffab2e8
Y NULL
data link
0x8ffab2e8
temp-> link = Rear
Rear=temp
0x8ffab2e8
Now Rear is
Rear will get shifted
Y becomes Last node
Cont…..
* Front * Rear* Front * Rear * Temp
0x8ffab2e6 0x8ffab2e8
0x8ffab2e6
An object is deleted
from Front
X
data link
0x8ffab2e6
Thus Front will be
* Front* Front
0x8ffab2e8
Y NULL
data link
Temp=Front
Front=Front->link
0x8ffab2e8
delete temp (to release
memory)
0x8ffab2e8
Front will get shifted
Y becomes first node
X will be released
Delete operation
Lets see this using a program
Program Code for the Same is
Click here to execute program
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
Insert(b) Front=0 Rear=1
Insert( c) Front=0 Rear=2
Delete() Front=1 Rear=2
Insert (d) Front=2 Rear=0
a
a b
a b c
b c
d c
Overflow occurs only when
Array is FULL.
Rear moves to 0 if array is empty
Lets see this using a program
Program Code for the Same is
Click here to execute program
Click here to see program code
Do you have any
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?
Stacks queues-1220971554378778-9

Contenu connexe

Tendances

Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queuesurya pandian
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)swajahatr
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queueSrajan Shukla
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structurepcnmtutorials
 

Tendances (17)

stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Stack project
Stack projectStack project
Stack project
 
linked list
linked list linked list
linked list
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
stack & queue
stack & queuestack & queue
stack & queue
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
 

En vedette

Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Getachew Ganfur
 
Lect02 120929183452-phpapp02
Lect02 120929183452-phpapp02Lect02 120929183452-phpapp02
Lect02 120929183452-phpapp02Getachew Ganfur
 
Storyboard for Love and Lust
Storyboard for Love and LustStoryboard for Love and Lust
Storyboard for Love and Lustawebsterg09
 
The Purpose of Film Openings
The Purpose of Film OpeningsThe Purpose of Film Openings
The Purpose of Film Openingsawebsterg09
 
Labmanualc2ndedition2 2-121115034959-phpapp02
Labmanualc2ndedition2 2-121115034959-phpapp02Labmanualc2ndedition2 2-121115034959-phpapp02
Labmanualc2ndedition2 2-121115034959-phpapp02Getachew Ganfur
 
Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Getachew Ganfur
 
Sound in Trailers Analysis
Sound in Trailers AnalysisSound in Trailers Analysis
Sound in Trailers Analysisawebsterg09
 
Advanced computer architect lesson 3 and 4
Advanced computer architect lesson 3 and 4Advanced computer architect lesson 3 and 4
Advanced computer architect lesson 3 and 4Ismail Mukiibi
 
Addressing mode of 8051
Addressing mode of 8051Addressing mode of 8051
Addressing mode of 8051Nitin Ahire
 
Sangramiento digestivo alto
Sangramiento digestivo altoSangramiento digestivo alto
Sangramiento digestivo altoRoxana Menesses
 
Character Profiles
Character ProfilesCharacter Profiles
Character Profilesawebsterg09
 
Instruction pipelining
Instruction pipeliningInstruction pipelining
Instruction pipeliningTech_MX
 

En vedette (14)

Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01
 
Lect02 120929183452-phpapp02
Lect02 120929183452-phpapp02Lect02 120929183452-phpapp02
Lect02 120929183452-phpapp02
 
Storyboard for Love and Lust
Storyboard for Love and LustStoryboard for Love and Lust
Storyboard for Love and Lust
 
The Purpose of Film Openings
The Purpose of Film OpeningsThe Purpose of Film Openings
The Purpose of Film Openings
 
Labmanualc2ndedition2 2-121115034959-phpapp02
Labmanualc2ndedition2 2-121115034959-phpapp02Labmanualc2ndedition2 2-121115034959-phpapp02
Labmanualc2ndedition2 2-121115034959-phpapp02
 
Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01
 
Sound in Trailers Analysis
Sound in Trailers AnalysisSound in Trailers Analysis
Sound in Trailers Analysis
 
Document(1)
Document(1)Document(1)
Document(1)
 
Cache memory
Cache memoryCache memory
Cache memory
 
Advanced computer architect lesson 3 and 4
Advanced computer architect lesson 3 and 4Advanced computer architect lesson 3 and 4
Advanced computer architect lesson 3 and 4
 
Addressing mode of 8051
Addressing mode of 8051Addressing mode of 8051
Addressing mode of 8051
 
Sangramiento digestivo alto
Sangramiento digestivo altoSangramiento digestivo alto
Sangramiento digestivo alto
 
Character Profiles
Character ProfilesCharacter Profiles
Character Profiles
 
Instruction pipelining
Instruction pipeliningInstruction pipelining
Instruction pipelining
 

Similaire à Stacks queues-1220971554378778-9

STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUEDev Chauhan
 
chapter three ppt.pptx
chapter three ppt.pptxchapter three ppt.pptx
chapter three ppt.pptxselemonGamo
 
Data Structure
Data Structure Data Structure
Data Structure Ibrahim MH
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptxline24arts
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5ecomputernotes
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problemecomputernotes
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queuePulkitmodi1998
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queueRai University
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 

Similaire à Stacks queues-1220971554378778-9 (20)

STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUE
 
chapter three ppt.pptx
chapter three ppt.pptxchapter three ppt.pptx
chapter three ppt.pptx
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Data Structure
Data Structure Data Structure
Data Structure
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
04 stacks
04 stacks04 stacks
04 stacks
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
12888239 (2).ppt
12888239 (2).ppt12888239 (2).ppt
12888239 (2).ppt
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 

Plus de Getachew Ganfur

Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Getachew Ganfur
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01Getachew Ganfur
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Getachew Ganfur
 
Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Getachew Ganfur
 
Datastructureanditstypes 110410094332-phpapp02
Datastructureanditstypes 110410094332-phpapp02Datastructureanditstypes 110410094332-phpapp02
Datastructureanditstypes 110410094332-phpapp02Getachew Ganfur
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Getachew Ganfur
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 

Plus de Getachew Ganfur (8)

Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01
 
Datastructureanditstypes 110410094332-phpapp02
Datastructureanditstypes 110410094332-phpapp02Datastructureanditstypes 110410094332-phpapp02
Datastructureanditstypes 110410094332-phpapp02
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
1to5 4th
1to5 4th1to5 4th
1to5 4th
 

Stacks queues-1220971554378778-9

  • 1. Let’s learn TACKS & UEUES for CLASS XII( C++) SENIOR SECONDARY GROUP Presented By : NITI ARORA
  • 2.
  • 3. 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
  • 4. 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.  
  • 5.   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.  
  • 6. 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.
  • 7. 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.
  • 8. It is a non-primitive linear data structure in which insertion and deletion of elements takes place from only one end, known as top. 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.
  • 9. 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.
  • 10. STACK 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.
  • 11. 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.
  • 12. 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
  • 13. 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.
  • 14. STACK The stack operation can be explained as follows: Stack operation Content of array Push(a) a Push (a) a Push(b) b a Push( c) C b a Pop( c) b a Pop(b) a Push(b) ba Push( c) cba Pop() ba Pop() a
  • 15. 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
  • 16. Linear Stack int S[5]; 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 Stack array int TOP; To hold address of location where data is inserted or deleted
  • 17. Lets see working of Linear Stack 8 9 10 Push 7 7 8 9 10 Push 20 20 7 8 9 10 Push 14 OVERFLOW ARRAY IS FULLTOP TOP TOP Top is incremented TOP++
  • 18. CONTINUED…. 20 7 8 9 10 Pop 20 7 8 9 10 Pop 7 8 9 10 UNDERFLOW OCCURS WHEN STACK IS EMPTY TOP Top Top TOP is decremented TOP --
  • 19. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
  • 20. 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; amt     900 Requires 2 bytes 0x8ffebab4     0x8ffebab4*P Requires 2 bytes Pointer P holds address of amt
  • 21. 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.
  • 22. 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   G       0x8ff134ab G->X 0x8ff134ab G->X G->Y To allocate dynamic allocation and store address in point g
  • 23. 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.
  • 24. Lets see working of Linked stack * TOP* TOP * Temp NULL 0x8ffab2e6 A new memory is allocated and address is stored in temp X NULL data link 0x8ffab2e6 Top = Temp Top will hold address of new location * TOP* TOP 0x8ffab2e6 Thus, TOP will have this address. Push operation Initially top is assigned NULL Temp holds address of new location
  • 25. Cont….. *TOP*TOP * Temp0x8ffab2e6 0x8ffab2e8 Another new memory is allocated to an object Y data link 0x8ffab2e8 * TOP* TOP 0x8ffab2e8 X NULL data link 0x8ffab2e6 temp-> link = Top Top=temp 0x8ffab2e6 Now TOP is TOP will get shifted Y becomes first node X becomes second node
  • 26. Cont….. * TOP* TOP * Temp 0x8ffab2e8 0x8ffab2e8 An object is deleted from top Y data link 0x8ffab2e8 Thus Top will be * TOP* TOP 0x8ffab2e6 X NULL data link Temp=TOP TOP=TOP->link 0x8ffab2e6 delete temp (to release memory) 0x8ffab2e6 TOP will get shifted X becomes first node Y will be released POP operation
  • 27. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
  • 28. 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.
  • 29. 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.
  • 30. 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 data2 data3 data4 data4 data3 data2 data2 data3 data4 data4 data3 data2 Each one of the above has two open end Front and Rear. Insertion is done from Rear and deletion form Front Rear Front Front Rear Front Rear FrontRear
  • 31. 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
  • 32. 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.
  • 33. QUEUE The Queue operation can be explained as follows: Queue operation Content of array Insert(a) Front=0 Rear=0 Insert(b) Front=0 Rear=1 Insert( c) Front=0 Rear=2 Delete() Front=1 Rear=2 Delete() Front=2 Rear=2 a a b a b c b c c If we try to insert Overflow occurs Though first two cells are empty
  • 34. Linear Queue int Q[5]; 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 array int Front, Rear; To hold address of location where data is inserted or deleted
  • 35. 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
  • 36. Lets see working of LINEAR QUEUE 8 9 10 Insert 7 7 8 9 10 Insert 20 20 7 8 9 10 Insert 14 OVERFLOW QUEUE is full Front rear rear rear Front Front Rear is incremented Rear++
  • 37. Lets see working of Queue as an array 20 7 8 9 10 Delete 20 7 8 9 Delete 20 7 8 Underflow occurs when QUEUE is empty Rear Front Rear Rear Front Front Front is incremented Front++
  • 38. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
  • 39. 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.
  • 40. Lets see working of Linked Queue * Front * Rear* Front * Rear * Temp NULL NULL 0x8ffab2e6 A new memory is allocated and address is stored in temp X NULL data link 0x8ffab2e6 Front=Rear = Temp Front and Rear will hold address of First location * Front * Rear* Front * Rear 0x8ffab2e6 0x8ffab2e6 Thus, Front and Rear will have this address. Insert operation Initially Front and Rear is assigned NULL Temp holds address of new location
  • 41. Cont….. *Front * Rear*Front * Rear * Temp 0x8ffab2e6 0x8ffab2e6 0x8ffab2e8 Another new memory is allocated to an object X data link 0x8ffab2e6 * Rear* Rear 0x8ffab2e8 Y NULL data link 0x8ffab2e8 temp-> link = Rear Rear=temp 0x8ffab2e8 Now Rear is Rear will get shifted Y becomes Last node
  • 42. Cont….. * Front * Rear* Front * Rear * Temp 0x8ffab2e6 0x8ffab2e8 0x8ffab2e6 An object is deleted from Front X data link 0x8ffab2e6 Thus Front will be * Front* Front 0x8ffab2e8 Y NULL data link Temp=Front Front=Front->link 0x8ffab2e8 delete temp (to release memory) 0x8ffab2e8 Front will get shifted Y becomes first node X will be released Delete operation
  • 43. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
  • 44. 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.
  • 45. QUEUE The Queue operation can be explained as follows: Queue operation Content of array Insert(a) Front=0 Rear=0 Insert(b) Front=0 Rear=1 Insert( c) Front=0 Rear=2 Delete() Front=1 Rear=2 Insert (d) Front=2 Rear=0 a a b a b c b c d c Overflow occurs only when Array is FULL. Rear moves to 0 if array is empty
  • 46. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
  • 47. Do you have any
  • 48. 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?