0% found this document useful (0 votes)
27 views12 pages

Lab 3

Uploaded by

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

Lab 3

Uploaded by

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

LAB TASK 3 – Array based Stack implementation

and Display Function()


Code:

package stacklab;

public class StackLab {

public static void main(String[] args) {

Stack s = new Stack(10);

s.push(10);

s.push(20);

s.push(30);

s.display();

System.out.println(s.pop() + " Popped from stack");

s.display();

s.peek();

///////////////////////////////////////////////////////////////////////////////////////////////////////////

package stacklab;

public class Stack {

static int MaxSize = 1000 ;


int top;

int sta[] = new int[MaxSize];

Stack(int MaxSize){

top = -1;

boolean isEmpty(){

return(top<0);

void push(int x){

if(top >=(MaxSize-1)){

System.out.println("Stack Overflow!");

else{

sta[++top] =x;

int pop(){

if(top<0){

System.out.println("Stack Underflow!");

return 0;

else{

int x= sta[top--];

return x;

void peek(){
if(top<0){

System.out.println("Stack Underflow!");

else{

System.out.println("Top of stack is :" + sta[top]);

void display(){

System.out.println("Stack has :");

for(int i = top; i>=0; i--){

System.out.print(sta[i]+" ");

System.out.println();

Output:
Bracket Matching using Stack
Code:

package stacklab;

public class Stack {

private int maxSize;

private char[] stackArray;

private int top;

public Stack(int s)

maxSize = s;

stackArray = new char[maxSize];

top = -1;

public void push(char j)

stackArray[++top] = j;

public char pop()

return stackArray[top--];

}
public char peek()

return stackArray[top];

public boolean isEmpty()

return (top == -1);

////////////////////////////////////////////////////////////////////////////////////////////

package stacklab;

public class BracketChecker {

private final String input;

public BracketChecker(String in) {

input = in;

public void check() {

Stack stack = new Stack(input.length());

for (int i = 0; i < input.length(); i++) {

char ch = input.charAt(i);

switch (ch) {

case '{':
case '[':

case '(':

stack.push(ch);

break;

case '}':

case ']':

case ')':

checkClosingBracket(stack, ch, i);

break;

default:

break;

if (!stack.isEmpty()) {

System.out.println("Error: closing bracket missing for: " + stack.peek());

private void checkClosingBracket(Stack stack, char closing, int index) {

if (!stack.isEmpty()) {

char opening = stack.pop();


if ((closing == '}' && opening != '{') || (closing == ']' && opening != '[') ||(closing == ')' &&
opening != '(')) {

System.out.println("Error: unmatched closing bracket '" + closing + "' at index " + index);

} else {

System.out.println("Error: unmatched closing bracket '" + closing + "' at index " + index);

////////////////////////////////////Main class/////////////////////////////////////

package stacklab;

import java.util.Scanner;

public class StackLab {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Bracking Matching Program!");

boolean run =true;

while(run){

System.out.print("(Press e to exit) \n Enter an Expression : ");

String expression = scan.nextLine().toLowerCase();

if(expression.charAt(0) == 'e'){

break;

BracketChecker theChecker = new BracketChecker(expression);

theChecker.check();

}
}

Output:

Palindrome Checker using stack


Code:

package palindrome;

public class Stack {

private int maxSize;

private char[] stackArray;

private int top;


public Stack(int s)

maxSize = s;

stackArray = new char[maxSize];

top = -1;

public void push(char j)

stackArray[++top] = j;

public char pop()

return stackArray[top--];

public char peek()

return stackArray[top];

public boolean isEmpty()

return (top == -1);

////////////////////////////////////////////////////////////////////////////////////////////
package palindrome;

public class PalindromeChecker {

public static boolean isPalindrome(String str) {

String cleanedStr = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

int len = cleanedStr.length();

Stack stack = new Stack(len / 2 + len % 2);

for (int i = 0; i < len / 2; i++) {

stack.push(cleanedStr.charAt(i));

int startIndex = (len % 2 == 0) ? len / 2 : len / 2 + 1;

for (int i = startIndex; i < len; i++) {

if (cleanedStr.charAt(i) != stack.pop()) {

return false;

return true;

}
}

//////////////////////////////////////////////////////////////////////////

package palindrome;

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

System.out.println("Enter a Word :");

Scanner scan = new Scanner(System.in);

String testString = scan.nextLine();

PalindromeChecker palindromech = new PalindromeChecker();

if (palindromech.isPalindrome(testString)) {

System.out.println("\"" + testString + "\" is a palindrome.");

} else {

System.out.println("\"" + testString + "\" is not a palindrome.");

Output:

You might also like