Вы находитесь на странице: 1из 31

1

B.Sc (Computer Sc ience) Object Oriente d Programming with Java and Data Structures Lab Programs- Data Structures

1. Program to impleme nt Bubble Sort. import java.io.*; class Prog37 { public static void main(String[] args) throws IOException { System.out.println("How many values do u want:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int arr[]=new int[n]; System.out.println("Enter " + n + " numbers:"); int temp; for(int i=0;i<n;i++) arr[i]=Integer.parseInt(br.readLine()); for(int j=n-1;j>1;j--) for(int k=0;k<j;k++) if(arr[k]>arr[k+1]) { temp=arr[k]; arr[k]=arr[k+1]; arr[k+1]=temp; } System.out.println("Bubble Sorted List:"); for(int i=0;i<n;i++) System.out.println(arr[i]); } } 2. Program to impleme nt Selection Sort. import java.io.*; class Prog36 { public static void main(String[] args) throws IOException { System.out.println("How many values do u want:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int arr[]=new int[n]; System.out.println("Enter " + n + " numbers:"); for(int i=0;i<n;i++)

Prasanth Kumar K (Head-Dept of Computers, IIMC)

2
arr[i]=Integer.parseInt(br.readLine()); int min; int temp; for(int j=0;j<n-1;j++) { min=j; for(int k=j+1;k<n;k++) if(arr[k]<arr[min]) min=k; temp=arr[j]; arr[j]=arr[min]; arr[min]=temp; } System.out.println("Selection Sorted List:"); for(int i=0;i<n;i++) System.out.println(arr[i]); } } 3. Program to impleme nt Insertion Sort. import java.io.*; class Prog26 { public static void main(String[] args) throws IOException { System.out.println("How many values do u want:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int arr[]=new int[n]; System.out.println("Enter " + n + " numbers:"); for(int i=0;i<n;i++) arr[i]=Integer.parseInt(br.readLine()); int temp,k; for(int j=1;j<n;j++) { temp=arr[j]; k=j; while(k>0 && arr[k-1]>=temp) { arr[k]=arr[k-1]; --k; } arr[k]=temp; } System.out.println("Selection Sorted List:"); for(int i=0;i<n;i++) System.out.println(arr[i]); }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

3
4. Program to impleme nt Quic k Sort. import java.io.*; class Prog39 { public static void main(String[] args) throws IOException { System.out.println("How many values do u want:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int arr[]=new int[n]; System.out.println("Enter " + n + " numbers:"); for(int i=0;i<n;i++) arr[i]=Integer.parseInt(br.readLine()); quickSort(arr,0,arr.length-1); System.out.println("Quick Sorted List:"); for(int i=0;i<n;i++) System.out.println(arr[i]); } public static void quickSort(int arr[], int left, int right) { int index = partition(arr, left, right); if (left < index - 1) quickSort(arr, left, index - 1); if (index < right) quickSort(arr, index, right); } public static int partition(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = t mp; i++; j--; } } return i; } } }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

5. Program to impleme nt PUSH a nd POP ope rations on Stac k using array method. import java.io.*; class stack { int [ ]a; int top; public stack(int n) { a=new int[n]; top=-1; }

public void push(int val) { if(top==a.length-1) { System.out.println("Stack Overf low "); } else { top++; a[top]=val; } }

public void pop() { if(top==-1) { System.out.println("Stack Underf low "); } else { System.out.println("Popped element is "+a[top]); top--; } }

public void display() { if(top==-1) { System.out.println("Stack is Empty ");

Prasanth Kumar K (Head-Dept of Computers, IIMC)

5
} else { for(int i=top;i>=0;i--) { System.out.println("stack element:"+a[i]); } } } } public class Prog27 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); stack s; System.out.println("enter the size of stack" ); int n=Integer.parseInt(br.readLine()); s=new stack(n); int choice; do { System.out.print("--------\nenter your choice\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT:\n--------\n"); choice=Integer.parseInt(br.readLine()); switch(choice) { case 1: System.out.println("enter element to push:"); int value=Integer.parseInt(br.readLine()); s.push(value); break; case 2: s.pop(); break; case 3: s.display(); break; case 4: break; default:System.out.println("Invalid Choice"); } }while(choice!=4); } }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

6. Program to impleme nt inse rt a nd delete ope rations on Queue using array method.

import java.io.*; class queue { int maxSize; int [ ]a; int front; int rear; int nItems; public queue(int n) { maxSize = n; a = new int[maxSize]; front = -1; rear = -1; nItems = 0; } public void insert(int val) { if(rear == maxSize-1) { System.out.println("Queue is Full"); } else { a[++rear] = val; front =0; } } public void remove() { if(front ==- 1 && rear==-1) { System.out.println("Queue is Empty "); } else if(front ==rear) { front = -1; rear=-1; } else front ++; } public void display()

Prasanth Kumar K (Head-Dept of Computers, IIMC)

7
{ if(front ==- 1 || rear==-1) { System.out.println("Queue is Empty "); } else { for(int i=f ront;i<=rear;i++) { if(front!=-1 && rear!=-1) System.out.println("Queue Element:"+ a[i]); } } } } public class Prog29 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); queue q; System.out.println("enter the size of Queue" ); int n=Integer.parseInt(br.readLine()); q=new queue(n); int choice; do { System.out.print("--------\nenter your choice\n1.Insert\n2.Delete\n3.DISPLAY\n4.EXIT:\n--------\n"); choice=Integer.parseInt(br.readLine()); switch(choice) { case 1: System.out.println("enter element to Insert:"); int value=Integer.parseInt(br.readLine()); q.insert(value); break; case 2: q.remove(); break; case 3: q.display(); break; case 4: break; default:System.out.println("Invalid Choice"); }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

8
}while(choice!=4); } }

7. Program to create, inse rt, delete a nd display operations on Single Linked List. import java.io.*; class Link { int data; Link link; } class Prog21 { int count=0; Link start,temp,pre; public void insertFirst()throws Exception { Link newLink = new Link(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a value:"); newLink.data=Integer.parseInt(br.readLine()); newLink.link = start; start = new Link; count++; } public void deleteLast()throws Exception { if(start==null) { System.out.println("List is Empty"); } else if(start.link==null) { System.out.println(start.data+" deleted from the list"); start=null; } else { temp=start; for(int i=1;i<count-1;i++) temp=temp.link; pre=temp.link; System.out.println(pre.data+" deleted from the list"); temp.link=null; count--;

Prasanth Kumar K (Head-Dept of Computers, IIMC)

9
} } public void showList() { if(start==null) System.out.println("List is Empty"); else { temp=start; System.out.println("Linked List is"); for(;temp!=null;temp=temp.link) System.out.print(temp.data+"- >"); System.out.println("Null"); } } public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Prog21 s=new Prog21(); do { System.out.print("-------Enter your choice\n1.Insert\n2.Show\n3.Delete\n4.Exit:"); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: s.insertFirst(); break; case 2: s.showList(); break; case 3: s.deleteLast(); break; case 4: System.exit(0); break; default: System.out.println("Invalid Choice"); } }while(ch!=4); } }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

10

8. Program to impleme nt PUSH a nd POP ope rations on Stac k using Linke d List method. import java.io.*; class Link { int data; Link link; } class stack1 { int top,size; Link start,temp,pre; public stack1(int n) { size=n; top=-1; } public void push(int val) { if(top==size-1) { System.out.println("Stack Overf low "); } else { Link newLink = new Link(); top++; newLink.data=val; newLink.link = start; start = newLink; } }

public void pop() { if(top==-1) { System.out.println("Stack Underf low "); } else { temp=start;

Prasanth Kumar K (Head-Dept of Computers, IIMC)

11
System.out.println(temp.data+" deleted from the list"); start=temp.link; top--; } }

public void display() { if(top==-1) { System.out.println("Stack is Empty "); } else { System.out.println("Linked List is"); for(temp=start;temp!=null;temp=temp.link) System.out.print(temp.data+"- >"); System.out.println("Null"); } } } public class Prog28 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); stack1 s; System.out.println("enter the size of stack" ); int n=Integer.parseInt(br.readLine()); s=new stack1(n); int choice; do { System.out.print("--------\nenter your choice\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT:\n--------\n"); choice=Integer.parseInt(br.readLine()); switch(choice) { case 1: System.out.println("enter element to push:"); int value=Integer.parseInt(br.readLine()); s.push(value); break; case 2: s.pop(); break; case 3: s.display(); break;

Prasanth Kumar K (Head-Dept of Computers, IIMC)

12
case 4: break; default:System.out.println("Invalid Choice"); } }while(choice!=4); }}

9. Program to impleme nt inse rt a nd delete ope rations on Queue using Linked List method.

import java.io.*; class Link { int data; Link link; } class queue1 { int front,rear,size,count=0; Link start,temp,pre; public queue1(int n) { size=n; front =-1; rear=- 1; } public void insert(int val) { if(rear==size-1) { System.out.println("Queue is Full"); } else { Link newLink = new Link(); ++rear; newLink.data=val; newLink.link = start; start = newLink; front =0; count++; } } public void delete() { if(front ==- 1 && rear==-1) { System.out.println("Queue is Empty "); } else if(front ==rear)

Prasanth Kumar K (Head-Dept of Computers, IIMC)

13
{ System.out.println(start.data+" deleted from the list "); start=null; front =-1; rear=- 1; count--; } else { temp=start; for(int i=1;i<count-1;i++) temp=temp.link; pre=temp.link; System.out.println(pre.data+" deleted from the list"); temp.link=null; front ++; count--; } } public void display() { if(front ==- 1 && rear==-1) { System.out.println("Queue is Empty "); } else { System.out.println("Queue in terms of Linked List is"); for(temp=start;temp!=null;temp=temp.link) System.out.print(temp.data+"- >"); System.out.println("Null"); } } } public class Prog30 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); queue1 q; System.out.println("enter the size of Queue" ); int n=Integer.parseInt(br.readLine()); q=new queue1(n); int choice; do { System.out.print("--------\nenter your choice\n1.Insert\n2.Delete\n3.DISPLAY\n4.EXIT:\n--------\n"); choice=Integer.parseInt(br.readLine()); switch(choice) { case 1: System.out.println("enter element to push:");

Prasanth Kumar K (Head-Dept of Computers, IIMC)

14
int value=Integer.parseInt(br.readLine()); q.insert(value); break; case 2: q.delete(); break; case 3: q.display(); break; case 4: break; default:System.out.println("Invalid Choice"); } }while(choice!=4); } }

10. Program to reverse a single linked list.

import java.io.*; class Link { int data; Link link; } class RevList { int count=0; Link start,temp,pre; int a[]=new int[50]; public void insertFirst()throws Exception { Link newLink = new Link(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a value:"); newLink.data=Integer.parseInt(br.readLine()); newLink.link = start; start = newLink; count++; } public void reverseList()throws Exception { if(start==null) System.out.println("List is Empty ");

Prasanth Kumar K (Head-Dept of Computers, IIMC)

15
else { int i=0; for(temp=start;temp!=null;temp=temp.link) { a[i++]=temp.data; } int j; System.out.println("Reversed List:"); for(temp=start,j=i-1;temp!=null;temp=temp.link,j--) temp.data=a[j]; } } public void showList() { if(start==null) System.out.println("List is Empty"); else { System.out.println("Linked List is"); for(temp=start;temp!=null;temp=temp.link) System.out.print(temp.data+"- >"); System.out.println("Null"); } } } class Prog25a { public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); RevList s=new RevList(); do { System.out.print("-------Enter your choice\n1.Insert\n2.Show\n3.Reverse\n4.Exit:"); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: s.insertFirst(); break; case 2: s.showList(); break; case 3: s.reverseList(); break; case 4: System.exit(0); break; default: System.out.println("Invalid Choice"); } }while(ch!=4);

Prasanth Kumar K (Head-Dept of Computers, IIMC)

16
} }

11. Program to split a single linked list.

import java.io.*; class Link { int data; Link link; } class SplitList { int count=0; Link start,temp,pre; int a[]=new int[50]; public void insertFirst()throws Exception { Link newLink = new Link(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a value:"); newLink.data=Integer.parseInt(br.readLine()); newLink.link = start; start = newLink; count++; } public void split() { if(start==null) System.out.println("List is Empty "); else { Link l1=new Link(); Link l2=new Link(); int k=count/2; int i=0;

Prasanth Kumar K (Head-Dept of Computers, IIMC)

17
for(temp=start;i<k;temp=temp.link,i++) { l1.data=temp.data; System.out.print(l1.data + "- >"); } System.out.println("Null"); for(;temp!=null;temp=temp.link) { l2.data=temp.data; System.out.print(l2.data + "- >"); } System.out.println("Null"); } } public void showList() { if(start==null) System.out.println("List is Empty"); else { System.out.println("Linked List is"); for(temp=start;temp!=null;temp=temp.link) System.out.print(temp.data+"- >"); System.out.println("Null"); } } public void deleteLast()throws Exception { if(start==null) { System.out.println("List is Empty"); } else if(start.link==null) { System.out.println(start.data+" deleted from the list"); start=null; } else { temp=start; for(int i=1;i<count-1;i++) temp=temp.link; pre=temp.link; System.out.println(pre.data+" deleted from the list"); temp.link=null; count--; } }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

18

} class Prog24 { public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); SplitList s=new SplitList(); do { System.out.print("-------Enter your choice\n1.Insert\n2.Show\n3.Split\n4.Delete\n5.Exit:"); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: s.insertFirst(); break; case 2: s.showList(); break; case 3: s.split(); break; case 4: s.deleteLast(); break; case 5: System.exit(0); break; default: System.out.println("Invalid Choice"); } }while(ch!=5); } }

12. Program to c reate, insert, delete and display ope rations on circular single linked list. import java.io.*; class Link { int data; Link link; } class CList { int count=0; Link start,temp,pre;

Prasanth Kumar K (Head-Dept of Computers, IIMC)

19
public void insertFirst()throws Exception { Link newLink = new Link(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a value:"); newLink.data=Integer.parseInt(br.readLine()); newLink.link = start; start = new Link; if(newLink.link==null) newLink.link=start; count++; } public void deleteLast()throws Exception { if(start==null) { System.out.println("List is Empty"); } else if(start.link==null) { System.out.println(start.data+" deleted from the list"); start=null; } else { temp=start; for(int i=1;i<count-1;i++) temp=temp.link; pre=temp.link; System.out.println(pre.data+" deleted from the list"); temp.link=start; count--; } } public void showList() { if(start==null) System.out.println("List is Empty"); else { int c=0; System.out.println("Linked List is"); for(temp=start;c<count;temp=temp.link,c ++) System.out.print(temp.data+"- >"); System.out.println("F irstElement(" + start.data + ")"); } } } class Prog23 {

Prasanth Kumar K (Head-Dept of Computers, IIMC)

20
public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); CList c=new CList(); do { System.out.print("-------Enter your choice\n1.Insert\n2.Show\n3.Delete\n4.Exit:"); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: c.insertFirst(); break; case 2: c.showList(); break; case 3: c.deleteLast(); break; case 4: System.exit(0); break; default: System.out.println("Invalid Choice"); } }while(ch!=4); } }

13. Program to c reate, insert, delete and display ope rations on double linked list. import java.io.*; class Link { int data; Link next; Link previous; public Link(int d) { data = d; } public void displayLink(){ System.out.print(data + "- >"); }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

21
} class DLList { Link first; Link last; public DLList() { first = null; last = null; } public boolean isEmpty() { return first == null; } public void insertFirst(int val){ Link new Link = new Link(val); if (isEmpty()) last = newLink; else first.previous = newLink; newLink.next = f irst; first = newLink; } public void deleteLast()throws Exception { if(first==null) { System.out.println("List is Empty"); } else if(first.next ==null) { System.out.println(f irst.data+" deleted from the list"); first=null; } else { System.out.println(last.data + " is deleted"); last.previous.next = null; last = last.previous; } } public void showList() { if(first==null) System.out.println("List is Empty"); else { displayForward();

Prasanth Kumar K (Head-Dept of Computers, IIMC)

22
display Backward(); } } public void displayForward() { System.out.print("List(first to last): "); Link current = first; while (current != null) { current.display Link(); current = current.next; } System.out.println("Null"); } public void display Backward() { System.out.print("List (last to first) : "); Link current = last; while (current != null){ current.display Link(); current = current.prev ious; } System.out.println("Null"); }

} class Prog22 { public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); DLList c=new DLList(); do { System.out.print("-------Enter your choice\n1.Insert\n2.Show\n3.Delete\n4.Exit:"); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: System.out.println("Enter a value:"); int val=Integer.parseInt(br.readLine()); c.insertFirst(val); break; case 2: c.showList(); break; case 3: c.deleteLast(); break; case 4: System.exit(0); break;

Prasanth Kumar K (Head-Dept of Computers, IIMC)

23
default: System.out.println("Invalid Choice"); } }while(ch!=4); } }

14. Program to implement insert and delete on Priority Queue.

import java.io.*; class queue { int maxSize; int [ ]a; int front; int rear; int nval; public queue(int n) { maxSize = n; a = new int[maxSize]; front = -1; rear = -1; nval = 0; } public void insert(int val) { if(rear == a.length-1) { System.out.println("Queue is Full"); } else if(nval==0) a[nval++]=val; else { int j; for(j=nval-1;j>=0;j--) { if(val>a[j]) a[j+1]=a[j]; else break; } a[j+1]=val; nval++; } ++rear; front =0;

Prasanth Kumar K (Head-Dept of Computers, IIMC)

24
} public void remove() { if(front ==- 1 && rear==-1) { System.out.println("Queue is Empty "); } else if(front ==rear) { front = -1; rear=-1; nval--; System.out.println(a[nval] + "is deleted"); } else { nval--; System.out.println(a[nval] + "is deleted"); front ++; } } public void display() { if(front ==- 1 && rear==-1) { System.out.println("Queue is Empty "); } else { System.out.print("Queue Elements are:"); for(int i=0;i<nval;i++) { if(front!=-1 && rear!=-1) System.out.print(a[i] + " "); } } } } public class Prog31 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); queue q; System.out.println("enter the size of Queue" ); int n=Integer.parseInt(br.readLine()); q=new queue(n); int choice; do {

Prasanth Kumar K (Head-Dept of Computers, IIMC)

25
System.out.print("--------\nenter your choice\n1.Insert\n2.Delete\n3.DISPLAY\n4.EXIT:\n--------\n"); choice=Integer.parseInt(br.readLine()); switch(choice) { case 1: System.out.println("Enter element to Insert:"); int value=Integer.parseInt(br.readLine()); q.insert(value); break; case 2: q.remove(); break; case 3: q.display(); break; case 4: break; default:System.out.println("Invalid Choice"); } }while(choice!=4); } }

15. Program to implement insert and delete Double Ended Queue.

import java.io.*; class queue { int maxSize; int [ ]a; int front; int rear; public queue(int n) { maxSize = n; a = new int[maxSize]; front = -1; rear = -1; }

public void insertL(int val) { if(rear == maxSize-1) { System.out.println("Queue is Full");

Prasanth Kumar K (Head-Dept of Computers, IIMC)

26
} else if(rear==-1 && front ==- 1) { front =0; rear=0; a[front]=val; } else { int temp; for(int i=rear;i>=0;i--) a[i+1]=a[i]; rear++; a[front]=val; } } public void insert R(int val) { if(rear == maxSize-1) { System.out.println("Queue is Full"); } else { a[++rear] = val; front =0; } }

public void removeL() { if(front ==- 1 && rear==-1) { System.out.println("Queue is Empty "); } else if(front ==rear) { System.out.println(a[front] + " is deleted"); front = -1; rear=-1; } else { System.out.println(a[0] + " is deleted"); for(int i=0;i<rear;i++) a[i]=a[i+1]; rear--; } }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

27
public void removeR() { if(front ==- 1 && rear==-1) { System.out.println("Queue is Empty "); } else if(front ==rear) { System.out.println(a[rear] + " is deleted"); front = -1; rear=-1; } else { System.out.println(a[rear] + " is deleted"); rear--; } } public void display() { if(front ==- 1 || rear==-1) { System.out.println("Queue is Empty "); } else { System.out.print("Queue Elements:"); for(int i=f ront;i<=rear;i++) { if(front!=-1 && rear!=-1) System.out.print(a[i] + " "); } } } } public class Prog32 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); queue q; System.out.println("enter the size of Queue" ); int n=Integer.parseInt(br.readLine()); q=new queue(n); int choice,value; do { System.out.print("--------\nenter your choice\n1.InsertLeft\n2.InsertRight\n3.DeleteLeft\n4.DeleteRight\n5.DISPLAY\n6.EX IT:\n--------\n"); choice=Integer.parseInt(br.readLine()); switch(choice) {

Prasanth Kumar K (Head-Dept of Computers, IIMC)

28
case 1: System.out.println("enter element to Insert:"); value=Integer.parseInt(br.readLine()); q.insertL(value); break; case 2: System.out.println("enter element to Insert:"); value=Integer.parseInt(br.readLine()); q.insertR(value); break; case 3: q.removeL(); break; case 4: q.removeR(); break; case 5: q.display(); break; case 6: break; default:System.out.println("Invalid Choice"); } }while(choice!=6); 16. Program to evaluate postfix expression by using stac k. } } import java.io.*; class stack { int [ ]a; int top; public stack(int n) { a=new int[n]; top=-1; }

public void push(int val) { a[++top]=val; }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

29
public int pop() { return a[top--]; } }

class Postfix { stack s; String input; public Postfix(String str) { input =str; } public int doParse() { s=new stack(20); char ch; int j; int num1,num2,ans; for(j=0;j<input.length();j++) { ch=input.charAt(j); if(ch>='0' && ch<='9') s.push((int) (ch-'0')); else { num2=s.pop(); num1=s.pop(); switch(ch) { case '+': ans=num1+num2; break; case '-': ans=num1-num2; break; case '*': ans=num1* num2; break; case '/': ans=num1/num2; break; default: ans=0; } s.push(ans); } } ans=s.pop(); return ans; } }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

30
public class Prog33 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the Postfix expression" ); String s=br.readLine(); Postfix p=new Postfix(s); int res=p.doParse(); System.out.println("Evaluates to " + res); } }

17. Program to construct Binary Searc h Tree a nd implement tree traversing techniques.

import java.io.*; public class Prog34 { public static void main(String[] args) throws IOException { Prog34 p=new Prog34(); p.run(); } static class Node { Node left; Node right; int value; public Node(int value) { this.value = value; } } public void run() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the root element"); int n=Integer.parseInt(br.readLine()); Node root = new Node(n); System.out.println("Building tree with root value " + root.value); int choice; int val; do { System.out.println("Enter the element to insert "); val=Integer.parseInt(br.readLine()); insert(root,val);

Prasanth Kumar K (Head-Dept of Computers, IIMC)

31
System.out.println("To stop Enter 0"); choice=Integer.parseInt(br.readLine()); }while (choice!=0); System.out.println("Traversing tree(PREORDER)"); printPreOrder(root); System.out.println("Traversing tree(INORDER)"); printInOrder(root); System.out.println("Traversing tree(POSTORDER)"); printPostOrder(root); } public void insert(Node node, int value) { if (value <= node.value) { if (node.left != null) { insert(node.left, value); } else { System.out.println(" Inserted " + value + " to left of " + node.value); node.left = new Node(value); } } else if (value >= node.value) { if (node.right != null) { insert(node.right, value); } else { System.out.println(" Inserted " + value + " to right of " + node.value); node.right = new Node(value); } } } public void printPostOrder(Node node) { if (node != null) { printInOrder(node.left); printInOrder(node.right); System.out.println(" Traversed " + node.value); } } public void printPreOrder(Node node) { if (node != null) { System.out.println(" Traversed " + node.value); printInOrder(node.left); printInOrder(node.right); } }

public void printInOrder(Node node) { if (node != null) { printInOrder(node.left); System.out.println(" Traversed " + node.value); printInOrder(node.right); } } }

Prasanth Kumar K (Head-Dept of Computers, IIMC)

Вам также может понравиться