Q.no 11
1 2 3 4 5 6 7 8 9 10 11 |
public void push(int element) { // Check if stack is full. if (top == (MAX_SIZE - 1)) { System.out.println("Stack Overflow"); } else { top = top + 1; stack[top] = element; System.out.println(element + " pushed into stack"); } } |
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 |
public class LinkedList { // head of list Node head; /* Linked list Node*/ class Node { int data; Node next; Node(int d) { data = d; next = null; } } /* Inserts a new Node at a specific position. */ public void insertAtPosition(int data, int position) { Node new_node = new Node(data); if (head == null) return; Node temp = head; if (position == 0) { new_node.next = head; head = new_node; return; } for (int i = 0; temp != null && i < position - 1; i++) temp = temp.next; if (temp == null || temp.next == null) return; |
Q.no 13
Q.no 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class TowerOfHanoi { public static void main(String[] args) { int numOfDisks = 3; // A, B, C are names of rods towerOfHanoi(numOfDisks, 'A', 'C', 'B'); } // Java recursive function to solve tower of hanoi puzzle static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { System.out.println("Move disk 1 from rod " + from_rod + " to rod " + to_rod); return; } towerOfHanoi(n-1, from_rod, aux_rod, to_rod); System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + to_rod); towerOfHanoi(n-1, aux_rod, to_rod, from_rod); } } |