Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Stack ADT Java

A stack is a linear data structure that follows the LIFO (Last In First Out) principle. Elements can only be inserted and removed from one end called the top. There are two types of stacks - static implemented with arrays with a fixed size, and dynamic implemented with linked lists that can grow in size. Common stack operations include push to add an element, pop to remove an element, peek to access the top element without removing it, and isEmpty to check if the stack is empty. Stacks have many real-world applications like representing undo operations, function call stacks, and parsing expressions.

Uploaded by

onlineearningfor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Stack ADT Java

A stack is a linear data structure that follows the LIFO (Last In First Out) principle. Elements can only be inserted and removed from one end called the top. There are two types of stacks - static implemented with arrays with a fixed size, and dynamic implemented with linked lists that can grow in size. Common stack operations include push to add an element, pop to remove an element, peek to access the top element without removing it, and isEmpty to check if the stack is empty. Stacks have many real-world applications like representing undo operations, function call stacks, and parsing expressions.

Uploaded by

onlineearningfor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

(JAVA)

Hira Awais
Lecturer (DCS)
DDSA (FOC)
 “A Stack is a special kind of list in which all insertions and
deletions take place at one end, called the Top”

 Other Names
 Pushdown List
 Last In First Out (LIFO)
Examples:

 Books on a floor

 Dishes on a shelf
 There are two kinds of stack data structure -

 a) static, i.e. they have a fixed size, and are implemented as arrays.

 b) dynamic, i.e. they grow in size as needed, and implemented as


linked lists
Method Modifier and Type Method Description
empty() boolean The method checks the stack is empty or not.
push(E item) E The method pushes (insert) an element onto the top of the
stack.
pop() E The method removes an element from the top of the stack
and returns the same element as the value of that
function.
peek() E The method looks at the top element of the stack without
removing it.
search(Object o) int The method searches the specified object and returns the
position of the object.
public class MyStack { public boolean isFull() {
private int maxSize; return (top == maxSize - 1);
private long[] stackArray; }
private int top;
public static void main(String[] args) {
public MyStack(int s) { MyStack theStack = new MyStack(10);
maxSize = s; theStack.push(10);
stackArray = new long[maxSize]; theStack.push(20);
top = -1; theStack.push(30);
} theStack.push(40);
theStack.push(50);
public void push(long j) {
stackArray[++top] = j; Output:
while (!theStack.isEmpty()) { 50
}
public long pop() { long value = theStack.pop(); 40
return stackArray[top--]; System.out.println(value); 30
} } 20
public long peek() { } 10
return stackArray[top]; }
}
public boolean isEmpty() {
return (top == -1);
}
import java.util.Stack; Position of Horse:
3
class StactDemo { // Search an element
public static void main(String[] args) {
int position = animals.search("Horse");
Stack<String> animals= new Stack<>();
System.out.println("Position of Horse: " + position);
// Add elements to Stack Is the stack empty?
animals.push("Dog"); // Check if stack is empty false
Initial Stack: [Dog,
animals.push("Horse"); Horse, Cat, Lion, Panda]
boolean result = animals.empty();
animals.push("Cat"); System.out.println("Is the stack empty? " + result);
animals.push("Lion");
animals.push("Panda"); //Removing all elements in stack
System.out.println("Initial Stack: " + animals);
while(animals.empty() != true){
// Remove element from Stack Removed Element: animals.pop();
String element = animals.pop(); Panda }
System.out.println("Removed Element: " + element); }
}
// Access element from the top Element at top:
String element1 = animals.peek(); Lion
System.out.println("Element at top: " + element1);

You might also like