DSA 2018
Group A Group B Q.no 11
Java
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 48 49 50 51 52 53 54 55 |
package folder2019; import java.util.Scanner; public class no11 { private int STACKSIZE; private Integer stack[]; no11(int STACKSIZE) { this.STACKSIZE = STACKSIZE; stack = new Integer[this.STACKSIZE]; } int TOP = -1; public void push(int data) { if (TOP == STACKSIZE - 1) { System.out.println("Stack overflow"); } else { TOP++; stack[TOP] = data; } } public int pop() { if (TOP == -1) { System.out.println("Stack underflow"); } else { System.out.println("Item deleted" + stack[TOP]); TOP--; } return 0; } public void view(){ if(TOP==-1){ System.out.println("Stack empty"); } else{ for(int i=TOP;i>=0;i--){ System.out.print(stack[i]+" "); } System.out.println(""); } } } class StackUsingArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("enter the size of stack"); int STACKSIZE = sc.nextInt(); no11 ob = new no11(STACKSIZE); int data; System.out.println("enter a number to push"); for (int i = 0; i < STACKSIZE; i++) { data = sc.nextInt(); ob.push(data); } ob.pop(); ob.view(); } } |
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 |
//node structure class Node { int data; Node next; Node prev; }; class LinkedList { Node head; LinkedList(){ head = null; } //Inserts a new element at the given position void push_at(int newElement, int position) { Node newNode = new Node(); newNode.data = newElement; newNode.next = null; newNode.prev = null; if(position < 1) { System.out.print("\nposition should be >= 1."); } else if (position == 1) { newNode.next = head; head.prev = newNode; head = newNode; } else { Node temp = new Node(); temp = head; for(int i = 1; i < position-1; i++) { if(temp != null) { temp = temp.next; } } if(temp != null) { newNode.next = temp.next; newNode.prev = temp; temp.next = newNode; if(newNode.next != null) newNode.next.prev = newNode; } else { System.out.print("\nThe previous node is null."); } } } |
Q.no 13 Q.no 14
Java
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 48 49 50 |
import java.util.Scanner; public class Hashing { int tablesize; Integer[] arr; public Hashing(int tablesize) { this.tablesize=tablesize; arr=new Integer[tablesize]; } public int hashfunction(int key) { return key%this.tablesize; } public boolean collision(int index) { return (arr[index]!=null); } public void inserthash(int key) { int index=hashfunction(key); int i=1; while(collision(index)) { index=(hashfunction(key)+i)%this.tablesize; i++; } arr[index]=key; } public void printAll() { for (Integer var:arr ) { System.out.println(var); } } } class Hashdemo { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int arr[]={24,20,37,84,50,47,67,74,52,53}; int size=arr.length; Hashing h=new Hashing(size); for(int i=0;i<size;i++){ h.inserthash(arr[i]); } h.printAll(); } } |