You are on page 1of 23

1232_Vishal_Jagadale

Data Structure Assignment

IACSD
e-DAC
1. Implementbracket opening and closing operation by
using Stack and array, e.g., case 1: inputstring
“{([])}” output“Balanced”, case 2: input“({[})]]”
output”Not Balanced”

Stack class
package char_using_stack;

public class Stack {


private int top;
private char[] arr;

public Stack(int size) {


top = -1;
arr = new char[size];
}

public void push(char ch) {


arr[++top] = ch;
}

public char pop() {


return arr[top--];
}

public char peek() {


return arr[top];
}

public boolean isFull() {


return (top == arr.length - 1);
}

public boolean isEmpty() {


return (top == -1);
}
}
Implementation class

package char_using_stack;

public class Implementation {


public void check(String str) {
Stack stack = new Stack(100);
boolean flag = true;
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '{' || str.charAt(i) == '}' ||
str.charAt(i) == '[' || str.charAt(i) == ']'
|| str.charAt(i) == '(' || str.charAt(i) == ')')
{
if (str.charAt(i) == '{' || str.charAt(i) == '[' ||
str.charAt(i) == '(') {
stack.push(str.charAt(i));
count++;
} else if (!stack.isEmpty() && (str.charAt(i) == ']' &&
stack.peek() == '[')
|| (str.charAt(i) == ')' && stack.peek()
== '(')
|| (str.charAt(i) == '}' && stack.peek()
== '{')) {
stack.pop();
count--;
} else if (str.charAt(i) == '}' || str.charAt(i) == ']'
|| str.charAt(i) == ')') {
flag = false;
break;
}
} else {
System.out.println("No valid input");
flag = false;
break;
}
}
if (count != 0) {
flag = false;
}

if (flag == true) {
System.out.println("\"Balanced\"");
} else {
System.out.println("\"Not balanced\"");
}
}
}
Test class

package char_using_stack;
import java.util.Scanner;

public class Test {

public static void main(String[] args) {


Implementation impl=new Implementation();
Scanner ip = new Scanner(System.in);
System.out.println("Enter input");
impl.check(ip.next());
ip.close();

Outputs
2. Implement stack using linked list

Node class
package stcak_using_linkedlist;

public class Node {


private int info;
private Node next;

public Node() {
this.info = 0;
this.next = null;
}

public int getInfo() {


return info;
}

public void setInfo(int info) {


this.info = info;
}

public Node getNext() {


return next;
}

public void setNext(Node next) {


this.next = next;
}
}

Stack class

package stcak_using_linkedlist;

public class Stack {


private Node top;

public Stack() {
top = null;
}

public boolean push(int element) {


Node temp = new Node();
temp.setInfo(element);
if (top == null) {
top = temp;
return true;
} else {
temp.setNext(top);
top = temp;
return true;
}
}

public int pop() throws StackEmptyException {


if (top == null) {
throw new StackEmptyException("\"Stack is empty
cannot pop\"");
} else {
Node current = top;
top = top.getNext();
return current.getInfo();
}
}

public int peek() throws StackEmptyException {


if (top == null) {
throw new StackEmptyException("\"Stack is empty
cannot peek\"");
} else {
return top.getInfo();
}
}
}

StackEmptyException class

package stcak_using_linkedlist;

@SuppressWarnings("serial")
public class StackEmptyException extends Exception {

public StackEmptyException(String message) {


super(message);
}
}

TestStack class

package stcak_using_linkedlist;

public class TestStack {

public static void main(String[] args) {


try {
Stack stack = new Stack();
System.out.println(stack.push(10));
System.out.println(stack.push(20));
System.out.println(stack.push(30));
System.out.println(stack.push(40));
System.out.println("poped " + stack.pop());
System.out.println("poped " + stack.pop());
System.out.println("Peeking " + stack.peek());
System.out.println("poped " + stack.pop());
System.out.println("poped " + stack.pop());
System.out.println("poped " + stack.pop());
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
}

Outputs

3. Implement circular queue using linked list

Node class

package com.app;

public class Node {


private int info;
private Node next;

public Node() {
this.info = 0;
this.next = null;
}

public int getInfo() {


return info;
}

public void setInfo(int info) {


this.info = info;
}

public Node getNext() {


return next;
}

public void setNext(Node next) {


this.next = next;
}
}

Queue class

package com.app;

import exception.QueueHandlingException;

public class Queue {

private Node tail;

public Queue() {
this.tail = null;
}

public boolean isEmpty() {


if (tail == null)
return true;
else
return false;
}

public boolean enqueue(int element) {


Node temp = new Node();
temp.setInfo(element);
if (tail == null) {
tail = temp;
tail.setNext(temp);
} else {
temp.setNext(tail.getNext());
tail.setNext(temp);
tail = temp;
}
return true;
}

public int dequeue() throws QueueHandlingException {


Node curr;
if (isEmpty()) {
throw new QueueHandlingException("Queue is empty
cannot dequeue");
} else if (tail != tail.getNext()) {
curr = tail.getNext();
tail.setNext(curr.getNext());
return curr.getInfo();
} else {
curr = tail;
tail = null;
return curr.getInfo();
}
}

QueueHandlingException class

package exception;

@SuppressWarnings("serial")
public class QueueHandlingException extends Exception {

public QueueHandlingException(String message) {


super(message);
}
}

Test class
package tester;

import com.app.Queue;

public class TestQueue {

public static void main(String[] args) {


try {
Queue q = new Queue();
// q.dequeue();
// System.out.println("Dequeued "+q.dequeue());
System.out.println(q.isEmpty());
System.out.println(q.enqueue(10));
System.out.println(q.enqueue(20));
System.out.println(q.isEmpty());
System.out.println("Dequeued " + q.dequeue());
System.out.println("Dequeued " + q.dequeue());
// System.out.println("Dequeued "+q.dequeue());
System.out.println(q.isEmpty());

} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

Outputs
4. Find mn with the help of recursion.

Recursion class
package recursion;

import java.util.Scanner;

public class Recursion {


public static void main(String[]args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter number and power");
System.out.println(Rec(sc.nextInt(), sc.nextInt()));
sc.close();
}
public static int Rec(int base, int power) {
if(power==0) {
return 1;
}
else {
return (base*Rec(base,power-1));
}
}
}

Outputs
5. Create a singly linked list of integers such that all
elements are inserted in ascending order.
Eg.
i/p 20, 5, 10, 30, 25
o/p 5,10, 20, 25, 30

Node Class

package com.app;

public class Node {


private int info;
private Node next;

public Node() {
info=0;
next=null;
}
public int getInfo() {
return info;
}
public void setInfo(int info) {
this.info = info;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}

}
SortedLL Class

package com.app;

public class SortedLL {


private Node head;

public SortedLL() {
head = null;
}

public boolean isEmpty() {


if (head == null)
return true;
else
return false;
}

public boolean insert(int element) {


Node temp = new Node();
temp.setInfo(element);
if (head == null) {
head = temp;
return true;
} else {
Node curr, helper;
curr = head;
if (element < head.getInfo()) {
temp.setNext(head);
head = temp;
return true;
}
while (curr.getNext() != null) {
helper = curr;
curr = curr.getNext();
if (element < curr.getInfo()) {
temp.setNext(helper.getNext());
helper.setNext(temp);
return true;
}
}
curr.setNext(temp);
return true;
}
}
public void display() {
if (head == null) {
System.out.println("List is Empty");
} else {
Node curr;
curr = head;
while (curr != null) {
System.out.println(curr.getInfo());
curr = curr.getNext();
}
}
}

TestSortedLL Class
package tester;

import com.app.SortedLL;

public class TestSortedLL {


public static void main(String[]args){
SortedLL s = new SortedLL();
System.out.println("When no element is inserted");
s.display();
System.out.println("When elements are inseted "
+ "radomly but stored in ascending order");
s.insert(20);
s.insert(5);
s.insert(10);
s.insert(30);
s.insert(25);
s.display();
}
}
Outputs

6. Write a recursive method to return nth term of Fibonacci


Series.
1st term = 0
2nd term = 1
3rd = 1
4th = 2

FibonacciSeries Class

import java.util.Scanner;

public class FibonacciSeries {

public static void main(String[] args) {


// FibonacciSeries 1st = 0,2nd = 1,3rd = 1,4th = 2,
// 5th = 3,6th = 5,7th = 8,8th = 13...
Scanner sc = new Scanner(System.in);
System.out.println("Enter the index of kth term you
want.");
System.out.println("(index starts from 1, FibonacciSeries
starts from 0)");
System.out.println(recursion(sc.nextInt()));
sc.close();
}
public static int recursion(int k) {
if (k == 0 || k == 1 || k == 2) {
return k - 1;
} else {
return (recursion(k - 1) + recursion(k - 2));
}
}
}

Outputs
7. Consider a singly linked list. Rotate the list from k
position from the last node.
Eg.
k = 2
10, 20, 30, 40, 50

After rotation
o/p
40, 50, 10, 20, 30

Node class

package com.app;

public class Node {


private int info;
private Node next;
public Node() {
this.info = 0;
this.next = null;
}
public int getInfo() {
return info;
}
public void setInfo(int info) {
this.info = info;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}

LinkedList class

package com.app;

public class LinkedList {


private Node head;

public LinkedList() {
this.head = null;
}

public boolean isEmpty() {


if (head == null)
return true;
else
return false;
}

public boolean insert(int element) {


Node temp = new Node();
temp.setInfo(element);
Node curr = null;
if (head == null) {
head = temp;
return true;
} else {
curr = head;
while (curr.getNext() != null) {
curr = curr.getNext();
}
curr.setNext(temp);
return true;
}
}

public void rotate(int k) {


Node curr, helper = null;
int count = 1;
curr = helper = head;
while (curr.getNext() != null) {
curr = curr.getNext();
count++;
}
if (k < count) {
for (int i = 1; i < count - k; i++) {
helper = helper.getNext();
}
curr.setNext(head);
head = helper.getNext();
helper.setNext(null);
} else {
int rem;
rem = k % count;
rotate(rem);
System.out.println("Index " + k + " is not part of
current LL.");
System.out.println("Still rotating");
}
}

public void display() {


Node curr;
curr = head;
while (curr != null) {
System.out.print(curr.getInfo() + ",");
curr = curr.getNext();
}
System.out.println();
}
}

TestRotation Class

package com.app;

import java.util.Scanner;

public class TestRotation {

public static void main(String[] args) {


LinkedList ll = new LinkedList();
Scanner sc = new Scanner(System.in);
ll.insert(10);
ll.insert(20);
ll.insert(30);
ll.insert(40);
ll.insert(50);
ll.insert(60);
ll.insert(70);
ll.display();
System.out.println("Enter the index from the last");
ll.rotate(sc.nextInt());
ll.display();
sc.close();
}
}
Outputs

You might also like