Lab3 - Data - Staructures and Algorithms
Lab3 - Data - Staructures and Algorithms
LAB 3
Theory
A stack is a linear data structure that follows the LIFO (Last–In, First–Out) principle. That
means the objects can be inserted or removed only at one end of it, also called a top.
The stack supports the following operations:
Push: inserts an item at the top of the stack (i.e., above its current top element).
Pop: removes the object at the top of the stack and returns that object from the function. The
stack Size: will be decremented by one.
isEmpty tests if the stack is empty or not.
isFull tests if the stack is full or not.
peek returns the object at the top of the stack without removing it from the stack or modifying
the stack in any way.
size returns the total number of elements present in the stack.
return -1;
}
class Main
{
public static void main (String[] args)
{
Stack stack = new Stack(3);
class Main
{
public static void main(String[] args)
{
Stack<String> stack = new Stack<String>();
privateint top;
privateint[] storage;
Stack(int capacity) {
if (capacity <= 0)
top = -1;
if (top == storage.length)
top++;
storage[top] = value;
int peek() {
if (top == -1)
void pop() {
if (top == -1)
top--;
booleanisEmpty() {
}
public class StackException extends RuntimeException {
publicStackException(String message) {
super(message);
}
}
}
Reverse a String Using the Stack Data Structure
Approach:
Insert each character one at a time into the datatype character stack.
Pop each character from the stack one at a time until the stack is empty.
Increase the character array by one popped element.
Create a string from a character array.
Provide the reversed string.
// using stack data structure a Java Program to Reverse a String
import java.io.*;
importjava.util.*;
classReverseStringUsingStack {
public static String ReverseString(String s)
{
char[] rS = new char[s.length()];
// Declaring an st of Character type
Stack<Character>st = new Stack<Character>();
Computer systems engineering department, sukkur iba university 6
// Traversing the String and pushing the characters of the string
// into the St one by one
for (int j = 0; j <s.length(); j++) {
// pushing the characters into the St
st.push(s.charAt(j));
}
// Now Poping the Characters from the st until
// the st becomes empty
int j = 0;
while (!st.isEmpty()) { // popping elements until from st
// st becomes empty
// getting the character from the top of the st
rS[j++] = st.pop();
}
// returning the string object
return new String(rS);
}
// Main code
public static void main(String args[])
{
String s1 = " SukkurIBA ";
// calling the method
System.out.println(s1 + " <- Reverse -> "
+ ReverseString(s1));
String s2 = " Welcome to SukurIBA ";
// calling the method
System.out.println(s2 + " <- Reverse -> "
+ ReverseString(s2));
}
}
Output:
JavaTpoint<- Reverse ->ABIrukkuS
Welcome to JavaTpoint<- Reverse ->ABIrukkuSotemocleW
Java program to reverse the number Using Stack
importjava.util.Stack;
while (!stack.isEmpty()) {
reverseString = reverseString+stack.pop();
}
if (inputString.equals(reverseString))
System.out.println("The input String is a palindrome.");
else
System.out.println("The input String is not a palindrome.");
}
}
Output:
Enter any string:abccba
The input String is a palindrome.
Exercise:Do as directed
Task No:01
Write a java program using stack to store the information given in table below so that each
column is stored in a different stack. Later on the program displays the information as it is
givenin the table. Note that the program also helps the user to remove or alter any information
from the table as well.
class Lab3Task1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (!exit) {
System.out.println("\n1. Add Data");
System.out.println("2. Display Table");
System.out.println("3. Remove Data");
System.out.println("4. Alter Data");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
switch (choice) {
case 1:
addData(idStack,nameStack, classStack, markStack,
genderStack, scanner);
break;
case 2:
displayTable(idStack,nameStack, classStack, markStack,
genderStack);
System.out.println("Program terminated.");
scanner.close();
}
class Lab3Task2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Program terminated.");
scanner.close();
}