0% found this document useful (0 votes)
26 views2 pages

Stacks

Uploaded by

Adityan Nair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Stacks

Uploaded by

Adityan Nair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

DATA STRUCTURES

Introduction:
A data structure is an organized way of storing data, so that it can be used
efficiently.
Data structure is of two types:
i) Linear
ii) Non–linear

Linear
A linear data structure is one in which the elements are arranged in a sequential
manner. For e.g. arrays, stacks, queues, linked lists.

Non–Linear
A data structure is said to be non–linear if its elements do not form a sequence
i.e. arranged in a random manner. For e.g. trees, graphs.

stacks:
A stack is an ordered data structure which allows insertions and deletions at one
end only, called the top of the stack.
tack Operations
i) Push – element added to a stack.
ii) Pop – element removed from a stack.
iii) Empty – determines whether stack is empty or not.
iv) Peek – determines the last added element of the stack.

A stack is also called a LIFO (Last in–first out), list since the last inserted
element is the first to get removed.
Operation: Push
Application of stack
1. infix to postfix conversion
2. Reversing an Array
3. Recursive function
4. matching of braces

class Stack {
int a[ ], n, top ;
Stack(int size) {
top = -1 ; // initialize empty stack
n = size ;
a = new int [n] ;
}
void push(int x)
{
if(top == n-1)
System.out.println("Stack overflow") ;
else
a[++top ] = x ;
}
int pop( )
{
if(top == -1)
{
System.out.println("Stack underflow") ;
return -999 ;
}
int x = a[ top-- ] ; // get the last added element and adjust top
return x ;
}
void display( ) {
if(top == -1)
System.out.println("Stack empty") ;
else
for(int i = top ; i>=0 ; i--)
System.out.println(a[ i ]) ;
}
} // end of class

You might also like