You are on page 1of 28

Exercise:1

ADT list using Array


Program Code:
import java.util.*;
public class arraylist
{
public static void main(String args[])
{
ArrayList<String>list=new ArrayList<String>();
list.add("COMPUTER APPLICATION");
list.add("COMMERCE");
list.add("COMPUTER SCIENCE");
list.add("VISUAL COMMUNICATION");
System.out.println("\n\n"+list);
}
}
Output:

.
EXERCISE-2
ADT LIST USING LINKED LIST
Program Code:
import java.util.*;
public class Linkedlist
{
public static void main(String args[])
{
LinkedList<String>al=new
LinkedList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ramu");
al.add("Ajay");
Iterator<String>itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Output:
Exercise -3
STACK ADT USING SINGLY LINKED LIST
Program Code:
Import java.util.*;
class LNode
{
int data;
LNode next;
LNode(int d)
{
data=d;
}
class Stack
{
LNode push(int d,LNode head)
{
LNode tmp1=new LNode(int d);
If(head==null)
head=tmp1;
else
{
tmp1.next=head;
head=tnp1;
}
return head;
}
LNode pop(LNode head)
{
if(head==null)
System.out.println(“underflow”);
else
head=head.next;
return head;
}
void display(LNode head)
{
System.out.println(“\n list is:”);
if(head==null);
{
System.out.println(“no LNodes”);
return;
}
LNode tmp=head;
While(tmp!=null)
{
System.out.println(tmp.data+””);
tmp=tmp.next;
}
}
boolean isEmpty(LNode head)
{
if(head==null)
return true;
else
return false;
}
int peek(LNode head)
{
if(head==null)
return-1;
return head.data;
}
}
public class StacklinkDemo
{
public static void main(String [] args)
{
Stack s=new Stack();
LNode head=null;
Scanner in=new Scanner(System.in);
do
{
System.out.println(“\n*******MENU*******”);
System.out.println(“\n1.PUSH”);
System.out.println(“\n2.POP”);
System.out.println(“\n3.PEEK”);
System.out.println(“\n4.IS EMPTY”);
System.out.println(“\n5.DISPLAY”);
System.out.println(“\n6.EXIT”);
System.out.println(“\n enter your choice :”);
switch(in.nextInt())
{
case 1:
System.out.println(“enter the value :”);
head=s.push(in.nextInt(),head);
break:
case 2:
head=s.pop(head);
break;
case 3:
System.out.println(“\n top element :”+ s.peek(head));
break;
case 4:
System.out.println(“\n is empty :”+ s.isEmpty(head));
break;
case 5:
s.display(head);
break:
case 6:
System.exit(0);
break;
default:
System.out.println(“\n wrong choice!”);
break;
}
System.out.println(“\n do you want to continue? press ‘0”);
}
While(in.nextInt()==0);
}
}
Output:
*******MENU*******

1.PUSH

2.POP

3.PEEK

4.IS EMPTY

5.DISPLAY

6.EXIT

enter your choice:4

is empty : true

do want to continue? press’0’

enter your choice:5

list is:no LNodes

enter your choice:1

enter the value

22

enter your choice:1

enter the value

33

enter your choice:3

top element:33

enter your choice:5

list is:33 22 11
enter your choice:5

list is:22 11

enter your choice:4

is empty:false

do you want to continue? press’0’

enter your choice:

6
Exercise-4
QUEUE ADT USING SINGLY LINKED LIST
Program Code:

Class LNode

int data;

LNode next;

LNode(int d)

data=d;

class Queue

LNode enqueue (LNode head,int a)

if (head==null)

head=tmp;

else

{
LNode tmp1=head;

while(tmp1.next!=null)

tmp1=tmp1.next;

tmp1.next=tmp;

return head;

LNode dequeue(LNode head)

if(head==null)

System.out.println(“underflow”);

else

head=head.next;

return head;

void display(LNode head)

System.out.println(“\n list is:”);

if(head==null)

{
System.out.println(“no LNodes”);

return;

LNode tmp=head;

while(tmp!=null)

System.out.print(tmp.data+””);

tmp=tmp.next;

public class QueueDemo

public static void main(String [] args)

Queue ob=new Queue();

LNode head=null;

head=ob.enqueue(head,1);

head=ob.enqueue(head,2);

head=ob.enqueue(head,3);
head=ob.enqueue(head,4);

head=ob.enqueue(head,5);

ob.display(head);

}
Output:
list is:
12345
list is:
2345
EXERCISE 5:

INFIX EXPRESSION TO CONVERT INTO POSTFIX

Program Code:
import java.util.Stack;

public class InfixToPostfix

static int precedence(char c)

switch(c)

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

return -1;

static String infixToPostfix(String expression)


{

String result="";

Stack<Character> stack=new Stack<>();

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

char c=expression.charAt(i);

if(precedence(c)>0)

while(stack.isEmpty()==false && precedence(stack.peek())>=precedence(c))

result +=stack.pop();

stack.push(c);

else if(c==')')

char x=stack.pop();

while(x!='(')

result +=x;

x=stack.pop();

}
else if(c=='(')

stack.push(c);

else

result += c;

for(int i=0;i<=stack.size();i++)

result += stack.pop();

return result;

public static void main(String[]args)

String exp="(A+B)*C/A+(D-B)";

System.out.println("Infix Expression:"+exp);

System.out.println("Postfix Expression:"+infixToPostfix(exp));

}
Output:
EXERCISE:6
INFIX EXPRESSION TO CONVERT INTO POSTFIX
USING STACK ADT
Program code:

import java.util.*;

public class EvaluatePostfix

static int evaluateExpression(String expression)

Stack<Integer>stack=new Stack<>();

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

char c=expression.charAt(i);

if(Character.isDigit(c))

stack.push(c-'0');

else

int value1=stack.pop();

int value2=stack.pop();

switch(c)

{
case'+':

stack.push(value2+value1);
break;

case'-':

stack.push(value2-value1);
break;

case'/':

stack.push(value2/value1);
break;

case'*':

stack.push(value2*value1);
break;

} }

return stack.pop();

public static void main(String[]args) {

Scanner obj=new Scanner(System.in);

System.out.println("enter the postfix expression(Operands are integer of width 1):");

String exp=obj.nextLine();

System.out.println("postfix Evaluation Result: "+evaluateExpression(exp));

}
Output:
Exercise:7

Priority Queue

Program Code:
import java.util.*;

class Main

public static void main(String args[])

PriorityQueue<String> cities_queue=new PriorityQueue<String>();

cities_queue.add("Sydney");

cities_queue.add("Venice");

cities_queue.add("New York");

cities_queue.add("California");

cities_queue.add("Melbourne");

System.out.println("PriorityQueue Head:"+cities_queue.element());

System.out.println("\nPriorityQueue contents:");

Iterator iter=cities_queue.iterator();

while(iter.hasNext()) {

System.out.println(iter.next()+" ");

}
Output:
Exercise: 8
Insert an element from a binary search tree
Program Code:

public class bst_insert

public Node root;

bst_insert()

root=null;

void insertKey(int key)

root=insert(root,key);

Node insert(Node root,int key)

if(root==null)

root=new Node(key);

return root;

if(key<root.key)

root.left=insert(root.left,key);

}
if(key>root.key)

root.right=insert(root.right,key);

return root;

void inOrder()

inOrderTraverse(root);

void inOrderTraverse(Node root)

if(root!=null)

inOrderTraverse(root.left);

System.out.println(root.key);

inOrderTraverse(root.right);

public static void main(String[] args)

bst_insert bst=new bst_insert();

bst.insertKey(4);

bst.insertKey(35);

bst.insertKey(25);

bst.insertKey(38);

bst.insertKey(65);
bst.insertKey(55);

bst.insertKey(90);

bst.insertKey(42);

bst.insertKey(1);

bst.insertKey(16);

bst.inOrder();

class Node

int key;

Node left,right;

public Node(int element)

key=element;

left=right=null;

}
Output:

You might also like