Tag: 4th sem dsa
DSA 2019 Makeup
Group B Q.No 11.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package folder2019; import java.util.Scanner; class Stack { private java.util.LinkedList list = new java.util.LinkedList(); public Stack() { } public void clear() { list.clear(); } public boolean isEmpty() { return list.isEmpty(); } public void push(Object el) { list.addLast(el); } public Object pop() { if (isEmpty()) { System.out.println("stack is empty"); } return list.removeLast(); } public String toString() { return list.toString(); } } public class makeup2019no11 { public static void main(String[] args) { Stack ob = new Stack(); int data; Scanner sc = new Scanner(System.in); ob.clear(); System.out.println("the stack is empty:" + ob.isEmpty()); System.out.println("enter a number to push in any stack"); for (int i = 0; i < 3; i++) { data = sc.nextInt(); ob.push(data); } ob.pop(); } } |
Q.no 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package folder2019; import java.util.Scanner; class Node { public int info; public Node next; public Node() { next = null; } public Node(int el) { info = el; next = null; } public Node(int el, Node ptr) { info =el; next = ptr; } } class List{ Node head,tail; public void deleteAtAny(int n) { Node previous = head; int count=1, position=n; while(count < position - 1) { previous = previous.next; count++; } Node current = previous.next; previous.next = current.next; current.next = null; } } public class makeup2019no12{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Singly linked list created"); List mylist = new List(); System.out.println("Enter the position"); int pos=sc.nextInt(); mylist.deleteAtAny(pos); } } |
Q.n0 13 Q.no 14 Q.no 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Scanner; public class SelectionSorting { public static void selectionSort(int[] data){ int i,j,least,temp; for(i=0;i<data.length-1;i++){ for(j=i+1,least=i; j<data.length;j++){ if(data[j]<data[least]) least=j; } if(i!=least){ temp=data[least]; data[least]=data[i]; data[i]=temp; } } } static void result(int[] data){ System.out.println("The array after selection sorting is: "); for(int i=0;i<data.length;i++) System.out.print(data[i]+ " "); } public static void main(String args[]){ SelectionSorting obj1 =new SelectionSorting (); Scanner sc=new Scanner(System.in); System.out.println("enter the size of array for sorting"); int size=sc.nextInt(); System.out.println("Enter the element for array"); int data[]=new int[size]; for(int i=0;i<size;i++){ data[i]=sc.nextInt(); } selectionSort(data); result(data); } } |