SlideShare una empresa de Scribd logo
1 de 22
Unit V
Stack
In the field of computing we have formulated many ways to handle data efficiently.
So far we have seen how to use variables to store data. However variables are not feasible when
handling a huge amount of data.
Also we need an organized medium of storage to handle any correlated information. Thus the
concept of data structure is introduced.
Data structure is a collection of organized data that are related to each other.
Data structures are primarily of two types they are
1. Primitive data structures
- Integers
- Real
- Char
2. Non-Primitive Data Structures
- Linear Data structures
- stack
-Queue
- List
- Array
- Non-Linear Data Structures
- Trees
- Graphs.
Operations performed on data structure:
The most common operations on the Data Structure
Insertion: Adding a new element from the Data Structure
Deletion: Deleting an element from the Data Structure.
Traversing: Each element of data is accessed on a process is known as traversing.
Searching: To find the position of the element in the given Data Structure.
Merging : Combining the elements of two similar data structures into one.
Stack:
A Stack is a linear data structure in which a data item is inserted and deleted at one end.
- A stack is called a “Last in First Out(LIFO)” Structure because the data item that is inserted last into the
stack is the first data item to be deleted from the stack.
- Stacks are extensively used in computer applications.
- Their most notable use is in system software.
- When one function calls another function and passes some parameters. These
parameters are passed using stack. In fact even many compilers store the local
variables inside a function on the stack.
- Inserting a value to the stack is called as “push” operation, Where as reading a
value from it is called as a “pop” operation.
Deleting: - The pop operation in the case of a stack is destructive i.e once an item is popped
from the stack, it is no longer available
Operation Content of stack after operation
Push(A) A
Push(B) A B
Pop A
Push( c) A C
Push(D) A C D
Pop A C
POP A
Pop Empty
Stack Implementation:
- Stack implementation can be achieved using arrays. It is a very simple method
but has few limitations.
- Once the size of an array is declared it can not be altered during program
execution.
- The program itself decides the amount of memory needed and it informs
accordingly to the compiler.
- In this method the memory requirement is determined before compilation and
compiler provides the required memory.
- It is an in efficient memory allocation technique.
- This is because in case we intend to store less arguments than declared
memory is wasted and if we desire to store more elements than declared array
could not expand.
- It is suitable only when we exactly know the number of elements to be stored.
Dynamic implementation:
- Pointers can also be used for implementation of a stack.
- The dynamic implementation is achieved using pointers.
- The limitations noticed in static implementation can be removed using
dynamic implementation.
- Using pointer implementation at run time. There is no restriction for number of
elements the stack may be expandable.
- It is efficient in memory allocation because the program informs the compiler
its memory requirement at run time.
- Memory is allocated only after an element is pushed
- Pointer implementation of a stack is carried similar to array implementation.
- Like an array elements can be stored in successive memoryocations using a
pointer.
WAp to perform the stack operations such as push() and pop() functions using arrays.
#include<stdio.h>
#include<conio.h>
#define max 100;
int stack[max]=n;
int top=-1;
void main()
{
int option,x;
char ch=’y’;
clrscr();
while(ch==’y’)
{
Printf(“1.pushn2.popn3.displayn”);
Printf(“enter choice”);
Scanf(“ %d”,&option);
Switch(option)
{
Case 1:
{
Printf(“enter elements to be pushedn”);
Scanf(“%d”,&n);
Push(stack,n);
Break;
}
Case 2:
{
Pop(stack)
Break;
}
Case 3:
{
Printf(“the elements are n”);
Display(stack);
Break;
Case 3:
{
Printf(“the elements are”);
Display(stack);
Break;
}
}
Printf(“do u want to continue(y/n) n”);
Ch=getche();
}
Getch();
Push(int a[],int item)
{
If(top>=max-1)
{
Printf(“stack if full n”);
}
Else
{
Top++;
A[top]=item;
Printf(“pushed”);
}
Return;
}
Pop(int a[])
{
Int p;
If(top==-1)
Printf(“stack is empty”);
Else
{
P=a[top];
Top--;
Printf(“poped”);
}
Return;
}
Display(int c[])
{
Int i;
If(top>=0)
For(i=top;i>=0;i--)
{
Printf(“%d”,c[i]);
}
Else
Printf(“no element”);
Return;
}
o/p:
1.push
2.pop
3.display
Enter choice
1
Enter elements to be pushed
80
Pushed
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter choice
1
Enter element to be pushed
90
Pushed
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter ur choice
3
The elements are
90
80
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter ur choice 2
Poped
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter choice
2
Poped
Do u want to continue(y/n) y
1.push
2.pop
3.display
Enter choice 2
Stack is empty
Do u want to continue(y/n) n
A stack is a ordered collection of data items into which new items may be inserted and
from which data items may be deleted at one end.
- A stack is also called push down lists.
Eg: A coin stacker etc.
- A stack is most commonly used as a place to store local variables,parameters
and return addresses when a function is called.
Applications of stack:
1.Situations where useful information must be held temporarily during the course of a program.
2. compiler in evaluation of an expression by recursion.
3. Memory management in operating system.
4. Evaluation of an arithmetic expression.
5. Nesting of functions.
6. Storing of register contents in process swapping.
Basic terminology associated with stacks.
1.Stack pointer(top) : Keeps track of the current position on the stack.
2.Overflow: occurs when we try to insert more information on a stack than it can hold.
Underflow:occurs when we try to delete an item of stack which is empty.
Algorithm for inserting an item into stack:
Procedure push(s,max,top,item)
s-stack
max- stack size
top- stack pointer
item- value in a cell
step1: {check for stack overflow}
if top>=max then
print stack overflow
return
step 2: {Increment pointer top}
toptop+1
step 3:{Insert item at top of the stack}
s[top]item
return
Algorithm for deleting an item from the stack:
Function pop(s top)
S array
Top stack pointer
Step 1: {check for stack underflow}
If top=0 then
Print “stack underflow”
Return
Step 2: {return former top element of stack}
Items[top]
Step 3:{decrement pointer top}
Toptop-1
Return
Applications of stack:
A stack has various real life applications
1. Stack is very useful to maintain the sequence of processing
2. Stacks are used to convert bases of numbers.
3. In evaluation of expressions stacks are used.
Unit V
Stack
In the field of computing we have formulated many ways to handle data efficiently.
So far we have seen how to use variables to store data. However variables are not feasible when
handling a huge amount of data.
Also we need an organized medium of storage to handle any correlated information. Thus the
concept of data structure is introduced.
Data structure is a collection of organized data that are related to each other.
Data structures are primarily of two types they are
3. Primitive data structures
- Integers
- Real
- Char
4. Non-Primitive Data Structures
- Linear Data structures
- stack
-Queue
- List
- Array
- Non-Linear Data Structures
- Trees
- Graphs.
Operations performed on data structure:
The most common operations on the Data Structure
Insertion: Adding a new element from the Data Structure
Deletion: Deleting an element from the Data Structure.
Traversing: Each element of data is accessed on a process is known as traversing.
Searching: To find the position of the element in the given Data Structure.
Merging : Combining the elements of two similar data structures into one.
Stack:
A Stack is a linear data structure in which a data item is inserted and deleted at one end.
- A stack is called a “Last in First Out(LIFO)” Structure because the data item that is inserted last into the
stack is the first data item to be deleted from the stack.
- Stacks are extensively used in computer applications.
- Their most notable use is in system software.
- When one function calls another function and passes some parameters.These
parameters are passed using stack. In fact even many compilers store the local
variables inside a function on the stack.
- Inserting a value to the stack is called as “push” operation, Where as reading a
value from it is called as a “pop” operation.
Deleting: - The pop operation in the case of a stack is destructive i.e once an item is popped
from the stack, it is no longer available
Operation Content of stack after operation
Push(A) A
Push(B) A B
Pop A
Push( c) A C
Push(D) A C D
Pop A C
POP A
Pop Empty
Stack Implementation:
- Stack implementation can be achieved using arrays. It is a very simple method
but has few limitations.
- Once the size of an array is declared it can not be altered during program
execution.
- The program itself decides the amount of memory needed and it informs
accordingly to the compiler.
- In this method the memory requirement is determined before compilation and
compiler provides the required memory.
- It is an in efficient memory allocation technique.
- This is because in case we intend to store less arguments than declared
memory is wasted and if we desire to store more elements than declared array
could not expand.
- It is suitable only when we exactly know the number of elements to be stored.
Dynamic implementation:
- Pointers can also be used for implementation of a stack.
- The dynamic implementation is achieved using pointers.
- The limitations noticed in static implementation can be removed using
dynamic implementation.
- Using pointer implementation at run time. There is no restriction for number of
elements the stack may be expandable.
- It is efficient in memory allocation because the program informs the compiler
its memory requirement at run time.
- Memory is allocated only after an element is pushed
- Pointer implementation of a stack is carried similar to array implementation.
- Like an array elements can be stored in successive memory locations using a
pointer.
WAp to perform the stack operations such as push() and pop() functions using arrays.
#include<stdio.h>
#include<conio.h>
#define max 100;
int stack[max]=n;
int top=-1;
void main()
{
int option,x;
char ch=’y’;
clrscr();
while(ch==’y’)
{
Printf(“1.pushn2.popn3.displayn”);
Printf(“enter choice”);
Scanf(“ %d”,&option);
Switch(option)
{
Case 1:
{
Printf(“enter elements to be pushedn”);
Scanf(“%d”,&n);
Push(stack,n);
Break;
}
Case 2:
{
Pop(stack)
Break;
}
Case 3:
{
Printf(“the elements are n”);
Display(stack);
Break;
Case 3:
{
Printf(“the elements are”);
Display(stack);
Break;
}
}
Printf(“do u want to continue(y/n) n”);
Ch=getche();
}
Getch();
Push(int a[],int item)
{
If(top>=max-1)
{
Printf(“stack if full n”);
}
Else
{
Top++;
A[top]=item;
Printf(“pushed”);
}
Return;
}
Pop(int a[])
{
Int p;
If(top==-1)
Printf(“stack is empty”);
Else
{
P=a[top];
Top--;
Printf(“poped”);
}
Return;
}
Display(int c[])
{
Int i;
If(top>=0)
For(i=top;i>=0;i--)
{
Printf(“%d”,c[i]);
}
Else
Printf(“no element”);
Return;
}
o/p:
1.push
2.pop
3.display
Enter choice
1
Enter elements to be pushed
80
Pushed
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter choice
1
Enter element to be pushed
90
Pushed
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter ur choice
3
The elements are
90
80
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter ur choice 2
Poped
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter choice
2
Poped
Do u want to continue(y/n) y
1.push
2.pop
3.display
Enter choice 2
Stack is empty
Do u want to continue(y/n) n
A stack is a ordered collection of data items into which new items may be inserted and
from which data items may be deleted at one end.
- A stack is also called push down lists.
Eg: A coin stacker etc.
- A stack is most commonly used as a place to store local variables,parameters
and return addresses when a function is called.
Applications of stack:
1.Situations where useful information must be held temporarily during the course of a program.
2. compiler in evaluation of an expression by recursion.
3. Memory management in operating system.
4. Evaluation of an arithmetic expression.
5. Nesting of functions.
6. Storing of register contents in process swapping.
Basic terminology associated with stacks.
3.Stack pointer(top) : Keeps track of the current position on the stack.
4.Overflow: occurs when we try to insert more information on a stack than it can hold.
Underflow:occurs when we try to delete an item of stack which is empty.
Algorithm for inserting an item into stack:
Procedure push(s,max,top,item)
s-stack
max- stack size
top- stack pointer
item- value in a cell
step1: {check for stack overflow}
if top>=max then
print stack overflow
return
step 2: {Increment pointer top}
toptop+1
step 3:{Insert item at top of the stack}
s[top]item
return
Algorithm for deleting an item from the stack:
Function pop(s top)
S array
Top stack pointer
Step 1: {check for stack underflow}
If top=0 then
Print “stack underflow”
Return
Step 2: {return former top element of stack}
Items[top]
Step 3:{decrement pointer top}
Toptop-1
Return
Applications of stack:
A stack has various real life applications
4. Stack is very useful to maintain the sequence of processing
5. Stacks are used to convert bases of numbers.
6. In evaluation of expressions stacks are used.
Queues
-A queue is a linear sequential list of items that are accessed in the order First In a Out(FIFO)
- A queue is special type of data structure in which insertions take place from one end called
“rear” end and deletions take place from another end called “front” end i.e insertions and
deletions take place from different ends.
- Queue works on the basis of first in first out[FIFO]. Since the first element entered in a queue
will be the first element to be deleted.
A queue can be represented using sequential allocation.
Queue type of data structures is used in time sharing system where many user jobs will be
waiting in the system queue for processing. These jobs may request the service of CPU,Main
memory or external devices such as printer.
Eg: people waiting in a line at a bank from a queue, where the first person in line is the first
person to be waiting and so on.

Más contenido relacionado

La actualidad más candente

Albert Bifet – Apache Samoa: Mining Big Data Streams with Apache Flink
Albert Bifet – Apache Samoa: Mining Big Data Streams with Apache FlinkAlbert Bifet – Apache Samoa: Mining Big Data Streams with Apache Flink
Albert Bifet – Apache Samoa: Mining Big Data Streams with Apache Flink
Flink Forward
 
Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...
Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...
Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...
Accumulo Summit
 
Data Structures for Statistical Computing in Python
Data Structures for Statistical Computing in PythonData Structures for Statistical Computing in Python
Data Structures for Statistical Computing in Python
Wes McKinney
 

La actualidad más candente (20)

Stack Data structure
Stack Data structureStack Data structure
Stack Data structure
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
Hadoop map reduce concepts
Hadoop map reduce conceptsHadoop map reduce concepts
Hadoop map reduce concepts
 
Applying stratosphere for big data analytics
Applying stratosphere for big data analyticsApplying stratosphere for big data analytics
Applying stratosphere for big data analytics
 
Albert Bifet – Apache Samoa: Mining Big Data Streams with Apache Flink
Albert Bifet – Apache Samoa: Mining Big Data Streams with Apache FlinkAlbert Bifet – Apache Samoa: Mining Big Data Streams with Apache Flink
Albert Bifet – Apache Samoa: Mining Big Data Streams with Apache Flink
 
Stratosphere with big_data_analytics
Stratosphere with big_data_analyticsStratosphere with big_data_analytics
Stratosphere with big_data_analytics
 
Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...
Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...
Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...
 
Queues
QueuesQueues
Queues
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Data Mining:  Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...Data Mining:  Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
 
Mining big data streams with APACHE SAMOA by Albert Bifet
Mining big data streams with APACHE SAMOA by Albert BifetMining big data streams with APACHE SAMOA by Albert Bifet
Mining big data streams with APACHE SAMOA by Albert Bifet
 
Apriori algorithm
Apriori algorithmApriori algorithm
Apriori algorithm
 
Relational Algebra and MapReduce
Relational Algebra and MapReduceRelational Algebra and MapReduce
Relational Algebra and MapReduce
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
 
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHM
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHMA PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHM
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHM
 
Stacks in data structure
Stacks  in data structureStacks  in data structure
Stacks in data structure
 
Data Structures for Statistical Computing in Python
Data Structures for Statistical Computing in PythonData Structures for Statistical Computing in Python
Data Structures for Statistical Computing in Python
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structure
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 

Similar a Stacks

QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
Mehedi Hasan
 
DS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxDS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptx
prakashvs7
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
HusnainNaqvi2
 

Similar a Stacks (20)

Ds
DsDs
Ds
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Data structure
Data structureData structure
Data structure
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Data structures
Data structuresData structures
Data structures
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
Data Structures
Data StructuresData Structures
Data Structures
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
DS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxDS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptx
 
Data Structures_Introduction
Data Structures_IntroductionData Structures_Introduction
Data Structures_Introduction
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 

Más de Acad

Más de Acad (20)

routing alg.pptx
routing alg.pptxrouting alg.pptx
routing alg.pptx
 
Network Layer design Issues.pptx
Network Layer design Issues.pptxNetwork Layer design Issues.pptx
Network Layer design Issues.pptx
 
Computer Science basics
Computer Science basics Computer Science basics
Computer Science basics
 
Union
UnionUnion
Union
 
Str
StrStr
Str
 
Functions
FunctionsFunctions
Functions
 
File
FileFile
File
 
Dma
DmaDma
Dma
 
Botnet detection by Imitation method
Botnet detection  by Imitation methodBotnet detection  by Imitation method
Botnet detection by Imitation method
 
Bot net detection by using ssl encryption
Bot net detection by using ssl encryptionBot net detection by using ssl encryption
Bot net detection by using ssl encryption
 
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
 
Literature survey on peer to peer botnets
Literature survey on peer to peer botnetsLiterature survey on peer to peer botnets
Literature survey on peer to peer botnets
 
Tiny os
Tiny osTiny os
Tiny os
 
Data retrieval in sensor networks
Data retrieval in sensor networksData retrieval in sensor networks
Data retrieval in sensor networks
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
 
Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data Strutures
 
Cluster analysis
Cluster analysisCluster analysis
Cluster analysis
 
Classification and prediction
Classification and predictionClassification and prediction
Classification and prediction
 
Association rule mining
Association rule miningAssociation rule mining
Association rule mining
 
Memory Organization
Memory OrganizationMemory Organization
Memory Organization
 

Último

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Último (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Stacks

  • 1. Unit V Stack In the field of computing we have formulated many ways to handle data efficiently. So far we have seen how to use variables to store data. However variables are not feasible when handling a huge amount of data. Also we need an organized medium of storage to handle any correlated information. Thus the concept of data structure is introduced. Data structure is a collection of organized data that are related to each other. Data structures are primarily of two types they are 1. Primitive data structures - Integers - Real - Char 2. Non-Primitive Data Structures - Linear Data structures - stack -Queue - List - Array - Non-Linear Data Structures - Trees - Graphs. Operations performed on data structure: The most common operations on the Data Structure Insertion: Adding a new element from the Data Structure Deletion: Deleting an element from the Data Structure. Traversing: Each element of data is accessed on a process is known as traversing. Searching: To find the position of the element in the given Data Structure.
  • 2. Merging : Combining the elements of two similar data structures into one. Stack: A Stack is a linear data structure in which a data item is inserted and deleted at one end. - A stack is called a “Last in First Out(LIFO)” Structure because the data item that is inserted last into the stack is the first data item to be deleted from the stack. - Stacks are extensively used in computer applications. - Their most notable use is in system software. - When one function calls another function and passes some parameters. These parameters are passed using stack. In fact even many compilers store the local variables inside a function on the stack. - Inserting a value to the stack is called as “push” operation, Where as reading a value from it is called as a “pop” operation. Deleting: - The pop operation in the case of a stack is destructive i.e once an item is popped from the stack, it is no longer available Operation Content of stack after operation Push(A) A Push(B) A B Pop A Push( c) A C Push(D) A C D Pop A C POP A Pop Empty Stack Implementation: - Stack implementation can be achieved using arrays. It is a very simple method but has few limitations. - Once the size of an array is declared it can not be altered during program execution.
  • 3. - The program itself decides the amount of memory needed and it informs accordingly to the compiler. - In this method the memory requirement is determined before compilation and compiler provides the required memory. - It is an in efficient memory allocation technique. - This is because in case we intend to store less arguments than declared memory is wasted and if we desire to store more elements than declared array could not expand. - It is suitable only when we exactly know the number of elements to be stored. Dynamic implementation: - Pointers can also be used for implementation of a stack. - The dynamic implementation is achieved using pointers. - The limitations noticed in static implementation can be removed using dynamic implementation. - Using pointer implementation at run time. There is no restriction for number of elements the stack may be expandable. - It is efficient in memory allocation because the program informs the compiler its memory requirement at run time. - Memory is allocated only after an element is pushed - Pointer implementation of a stack is carried similar to array implementation. - Like an array elements can be stored in successive memoryocations using a pointer. WAp to perform the stack operations such as push() and pop() functions using arrays. #include<stdio.h> #include<conio.h> #define max 100; int stack[max]=n; int top=-1; void main() { int option,x;
  • 4. char ch=’y’; clrscr(); while(ch==’y’) { Printf(“1.pushn2.popn3.displayn”); Printf(“enter choice”); Scanf(“ %d”,&option); Switch(option) { Case 1: { Printf(“enter elements to be pushedn”); Scanf(“%d”,&n); Push(stack,n); Break; } Case 2: { Pop(stack) Break; } Case 3: { Printf(“the elements are n”); Display(stack);
  • 5. Break; Case 3: { Printf(“the elements are”); Display(stack); Break; } } Printf(“do u want to continue(y/n) n”); Ch=getche(); } Getch(); Push(int a[],int item) { If(top>=max-1) { Printf(“stack if full n”); } Else { Top++; A[top]=item; Printf(“pushed”); }
  • 6. Return; } Pop(int a[]) { Int p; If(top==-1) Printf(“stack is empty”); Else { P=a[top]; Top--; Printf(“poped”); } Return; } Display(int c[]) { Int i; If(top>=0) For(i=top;i>=0;i--) { Printf(“%d”,c[i]); } Else Printf(“no element”);
  • 7. Return; } o/p: 1.push 2.pop 3.display Enter choice 1 Enter elements to be pushed 80 Pushed Do u want to continue(y/n) Y 1.push 2.pop 3.display Enter choice 1 Enter element to be pushed 90 Pushed Do u want to continue(y/n) Y 1.push
  • 8. 2.pop 3.display Enter ur choice 3 The elements are 90 80 Do u want to continue(y/n) Y 1.push 2.pop 3.display Enter ur choice 2 Poped Do u want to continue(y/n) Y 1.push 2.pop 3.display Enter choice 2 Poped Do u want to continue(y/n) y 1.push 2.pop
  • 9. 3.display Enter choice 2 Stack is empty Do u want to continue(y/n) n A stack is a ordered collection of data items into which new items may be inserted and from which data items may be deleted at one end. - A stack is also called push down lists. Eg: A coin stacker etc. - A stack is most commonly used as a place to store local variables,parameters and return addresses when a function is called. Applications of stack: 1.Situations where useful information must be held temporarily during the course of a program. 2. compiler in evaluation of an expression by recursion. 3. Memory management in operating system. 4. Evaluation of an arithmetic expression. 5. Nesting of functions. 6. Storing of register contents in process swapping. Basic terminology associated with stacks. 1.Stack pointer(top) : Keeps track of the current position on the stack. 2.Overflow: occurs when we try to insert more information on a stack than it can hold. Underflow:occurs when we try to delete an item of stack which is empty. Algorithm for inserting an item into stack:
  • 10. Procedure push(s,max,top,item) s-stack max- stack size top- stack pointer item- value in a cell step1: {check for stack overflow} if top>=max then print stack overflow return step 2: {Increment pointer top} toptop+1 step 3:{Insert item at top of the stack} s[top]item return Algorithm for deleting an item from the stack: Function pop(s top) S array Top stack pointer Step 1: {check for stack underflow} If top=0 then Print “stack underflow” Return Step 2: {return former top element of stack}
  • 11. Items[top] Step 3:{decrement pointer top} Toptop-1 Return Applications of stack: A stack has various real life applications 1. Stack is very useful to maintain the sequence of processing 2. Stacks are used to convert bases of numbers. 3. In evaluation of expressions stacks are used. Unit V Stack In the field of computing we have formulated many ways to handle data efficiently. So far we have seen how to use variables to store data. However variables are not feasible when handling a huge amount of data. Also we need an organized medium of storage to handle any correlated information. Thus the concept of data structure is introduced. Data structure is a collection of organized data that are related to each other. Data structures are primarily of two types they are 3. Primitive data structures - Integers - Real - Char 4. Non-Primitive Data Structures - Linear Data structures - stack -Queue - List
  • 12. - Array - Non-Linear Data Structures - Trees - Graphs. Operations performed on data structure: The most common operations on the Data Structure Insertion: Adding a new element from the Data Structure Deletion: Deleting an element from the Data Structure. Traversing: Each element of data is accessed on a process is known as traversing. Searching: To find the position of the element in the given Data Structure. Merging : Combining the elements of two similar data structures into one. Stack: A Stack is a linear data structure in which a data item is inserted and deleted at one end. - A stack is called a “Last in First Out(LIFO)” Structure because the data item that is inserted last into the stack is the first data item to be deleted from the stack. - Stacks are extensively used in computer applications. - Their most notable use is in system software. - When one function calls another function and passes some parameters.These parameters are passed using stack. In fact even many compilers store the local variables inside a function on the stack. - Inserting a value to the stack is called as “push” operation, Where as reading a value from it is called as a “pop” operation. Deleting: - The pop operation in the case of a stack is destructive i.e once an item is popped from the stack, it is no longer available Operation Content of stack after operation Push(A) A Push(B) A B Pop A
  • 13. Push( c) A C Push(D) A C D Pop A C POP A Pop Empty Stack Implementation: - Stack implementation can be achieved using arrays. It is a very simple method but has few limitations. - Once the size of an array is declared it can not be altered during program execution. - The program itself decides the amount of memory needed and it informs accordingly to the compiler. - In this method the memory requirement is determined before compilation and compiler provides the required memory. - It is an in efficient memory allocation technique. - This is because in case we intend to store less arguments than declared memory is wasted and if we desire to store more elements than declared array could not expand. - It is suitable only when we exactly know the number of elements to be stored. Dynamic implementation: - Pointers can also be used for implementation of a stack. - The dynamic implementation is achieved using pointers. - The limitations noticed in static implementation can be removed using dynamic implementation. - Using pointer implementation at run time. There is no restriction for number of elements the stack may be expandable. - It is efficient in memory allocation because the program informs the compiler its memory requirement at run time. - Memory is allocated only after an element is pushed - Pointer implementation of a stack is carried similar to array implementation. - Like an array elements can be stored in successive memory locations using a pointer. WAp to perform the stack operations such as push() and pop() functions using arrays.
  • 14. #include<stdio.h> #include<conio.h> #define max 100; int stack[max]=n; int top=-1; void main() { int option,x; char ch=’y’; clrscr(); while(ch==’y’) { Printf(“1.pushn2.popn3.displayn”); Printf(“enter choice”); Scanf(“ %d”,&option); Switch(option) { Case 1: { Printf(“enter elements to be pushedn”); Scanf(“%d”,&n); Push(stack,n); Break; }
  • 15. Case 2: { Pop(stack) Break; } Case 3: { Printf(“the elements are n”); Display(stack); Break; Case 3: { Printf(“the elements are”); Display(stack); Break; } } Printf(“do u want to continue(y/n) n”); Ch=getche(); } Getch(); Push(int a[],int item) { If(top>=max-1)
  • 16. { Printf(“stack if full n”); } Else { Top++; A[top]=item; Printf(“pushed”); } Return; } Pop(int a[]) { Int p; If(top==-1) Printf(“stack is empty”); Else { P=a[top]; Top--; Printf(“poped”); } Return; } Display(int c[])
  • 18. 3.display Enter choice 1 Enter element to be pushed 90 Pushed Do u want to continue(y/n) Y 1.push 2.pop 3.display Enter ur choice 3 The elements are 90 80 Do u want to continue(y/n) Y 1.push 2.pop 3.display Enter ur choice 2 Poped Do u want to continue(y/n) Y
  • 19. 1.push 2.pop 3.display Enter choice 2 Poped Do u want to continue(y/n) y 1.push 2.pop 3.display Enter choice 2 Stack is empty Do u want to continue(y/n) n A stack is a ordered collection of data items into which new items may be inserted and from which data items may be deleted at one end. - A stack is also called push down lists. Eg: A coin stacker etc. - A stack is most commonly used as a place to store local variables,parameters and return addresses when a function is called. Applications of stack: 1.Situations where useful information must be held temporarily during the course of a program. 2. compiler in evaluation of an expression by recursion. 3. Memory management in operating system. 4. Evaluation of an arithmetic expression. 5. Nesting of functions.
  • 20. 6. Storing of register contents in process swapping. Basic terminology associated with stacks. 3.Stack pointer(top) : Keeps track of the current position on the stack. 4.Overflow: occurs when we try to insert more information on a stack than it can hold. Underflow:occurs when we try to delete an item of stack which is empty. Algorithm for inserting an item into stack: Procedure push(s,max,top,item) s-stack max- stack size top- stack pointer item- value in a cell step1: {check for stack overflow} if top>=max then print stack overflow return step 2: {Increment pointer top} toptop+1 step 3:{Insert item at top of the stack} s[top]item return Algorithm for deleting an item from the stack:
  • 21. Function pop(s top) S array Top stack pointer Step 1: {check for stack underflow} If top=0 then Print “stack underflow” Return Step 2: {return former top element of stack} Items[top] Step 3:{decrement pointer top} Toptop-1 Return Applications of stack: A stack has various real life applications 4. Stack is very useful to maintain the sequence of processing 5. Stacks are used to convert bases of numbers. 6. In evaluation of expressions stacks are used. Queues -A queue is a linear sequential list of items that are accessed in the order First In a Out(FIFO) - A queue is special type of data structure in which insertions take place from one end called “rear” end and deletions take place from another end called “front” end i.e insertions and deletions take place from different ends.
  • 22. - Queue works on the basis of first in first out[FIFO]. Since the first element entered in a queue will be the first element to be deleted. A queue can be represented using sequential allocation. Queue type of data structures is used in time sharing system where many user jobs will be waiting in the system queue for processing. These jobs may request the service of CPU,Main memory or external devices such as printer. Eg: people waiting in a line at a bank from a queue, where the first person in line is the first person to be waiting and so on.