SlideShare a Scribd company logo
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.

More Related Content

ODP
Java - Collections
PPT
Elag 2012 - Under the hood of 3TU.Datacentrum.
PPTX
Data Analysis packages
PDF
Enterprise Scale Topological Data Analysis Using Spark
PDF
Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern...
PPTX
Stack Data Structure
PPTX
Analysis of algorithms
PPTX
Ppt presentation of queues
Java - Collections
Elag 2012 - Under the hood of 3TU.Datacentrum.
Data Analysis packages
Enterprise Scale Topological Data Analysis Using Spark
Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern...
Stack Data Structure
Analysis of algorithms
Ppt presentation of queues

What's hot (20)

PPTX
Stack Data structure
PPTX
Heap Management
PDF
Hadoop map reduce concepts
PDF
Applying stratosphere for big data analytics
PDF
Albert Bifet – Apache Samoa: Mining Big Data Streams with Apache Flink
PPTX
Stratosphere with big_data_analytics
PDF
Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...
PPTX
Ist year Msc,2nd sem module1
PPT
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
PDF
Mining big data streams with APACHE SAMOA by Albert Bifet
PPTX
Apriori algorithm
PDF
Relational Algebra and MapReduce
PPT
Difference between stack and queue
PDF
Mapreduce Algorithms
PDF
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHM
DOCX
Stacks in data structure
PDF
Data Structures for Statistical Computing in Python
PPTX
2 introduction to data structure
PPT
Queue Data Structure
Stack Data structure
Heap Management
Hadoop map reduce concepts
Applying stratosphere for big data analytics
Albert Bifet – Apache Samoa: Mining Big Data Streams with Apache Flink
Stratosphere with big_data_analytics
Accumulo Summit 2016: GeoMesa: Using Accumulo for Optimized Spatio-Temporal P...
Ist year Msc,2nd sem module1
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Mining big data streams with APACHE SAMOA by Albert Bifet
Apriori algorithm
Relational Algebra and MapReduce
Difference between stack and queue
Mapreduce Algorithms
A PREFIXED-ITEMSET-BASED IMPROVEMENT FOR APRIORI ALGORITHM
Stacks in data structure
Data Structures for Statistical Computing in Python
2 introduction to data structure
Queue Data Structure
Ad

Similar to Stacks (20)

DOCX
Ds
PPTX
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
PDF
Chapter 4 stack
PPTX
STACK.pptx
PPTX
TSAT Presentation1.pptx
PPTX
01-Introduction of DSA-1.pptx
PPTX
Stack in Sata Structure
PPT
The Stack in data structures .ppt
PPTX
stack.pptx
PDF
04 stacks
PPT
Lecture 2c stacks
PPTX
هياكلبيانات
PPT
Stacks & Queues
PPT
Stacks & Queues By Ms. Niti Arora
PPSX
Stacks fundamentals
PPTX
Stack and its applications
PPTX
Data structure
PDF
Chapter 5 Stack and Queue.pdf
PPTX
My lecture stack_queue_operation
PPTX
data structures with algorithms vtu 2023 notes.pptx
Ds
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
Chapter 4 stack
STACK.pptx
TSAT Presentation1.pptx
01-Introduction of DSA-1.pptx
Stack in Sata Structure
The Stack in data structures .ppt
stack.pptx
04 stacks
Lecture 2c stacks
هياكلبيانات
Stacks & Queues
Stacks & Queues By Ms. Niti Arora
Stacks fundamentals
Stack and its applications
Data structure
Chapter 5 Stack and Queue.pdf
My lecture stack_queue_operation
data structures with algorithms vtu 2023 notes.pptx
Ad

More from Acad (20)

PPTX
routing alg.pptx
PPTX
Network Layer design Issues.pptx
PPTX
Computer Science basics
DOCX
Union
DOCX
Str
DOC
Functions
DOCX
File
DOCX
Dma
PDF
Botnet detection by Imitation method
PDF
Bot net detection by using ssl encryption
PDF
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
PDF
Literature survey on peer to peer botnets
PPT
Tiny os
PPT
Data retrieval in sensor networks
DOCX
Structure and Typedef
DOCX
Union from C and Data Strutures
PPT
Cluster analysis
PPT
Classification and prediction
PPT
Association rule mining
PPT
Memory Organization
routing alg.pptx
Network Layer design Issues.pptx
Computer Science basics
Union
Str
Functions
File
Dma
Botnet detection by Imitation method
Bot net detection by using ssl encryption
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
Literature survey on peer to peer botnets
Tiny os
Data retrieval in sensor networks
Structure and Typedef
Union from C and Data Strutures
Cluster analysis
Classification and prediction
Association rule mining
Memory Organization

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Pre independence Education in Inndia.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
master seminar digital applications in india
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Business Ethics Teaching Materials for college
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Types and Its function , kingdom of life
PDF
Classroom Observation Tools for Teachers
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Structure & Organelles in detailed.
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
102 student loan defaulters named and shamed – Is someone you know on the list?
Pre independence Education in Inndia.pdf
RMMM.pdf make it easy to upload and study
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O7-L3 Supply Chain Operations - ICLT Program
master seminar digital applications in india
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Business Ethics Teaching Materials for college
human mycosis Human fungal infections are called human mycosis..pptx
PPH.pptx obstetrics and gynecology in nursing
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
TR - Agricultural Crops Production NC III.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Basic Mud Logging Guide for educational purpose
Complications of Minimal Access Surgery at WLH
Cell Types and Its function , kingdom of life
Classroom Observation Tools for Teachers

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.