You are on page 1of 23

1. Demonstrate recursive algorithms with examples.

Aim:

Algorithm:

Step 1: Create a Java class named FibonacciCalc.


Step 2: Inside the FibonacciCalc class, define a static method called
fibonacciRecursion(int n) to calculate the nth Fibonacci number using recursion.

 If n is 0, return 0 (base case).


 If n is 1 or 2, return 1 (base case).
 Otherwise, recursively calculate the Fibonacci numbers for n-2 and n-1 and
return their sum.
Step 3: In the main method:
 Initialize the maxNumber variable to 10, indicating that you want to
calculate and print the first 10 Fibonacci numbers.
Step 4: Print a message indicating that you are displaying the Fibonacci series for
maxNumber numbers.

Step 5: Use a for loop to iterate from 0 to maxNumber - 1 (inclusive):


 Inside the loop, calculate and print the ith Fibonacci number using
fibonacciRecursion(i) .

Program:
public class FibonacciCalc
{
public static int fibonacciRecursion(int n)
{
if(n == 0)
{
return 0;
}
if(n == 1 || n == 2)
{
return 1;
}
return fibonacciRecursion(n-2) + fibonacciRecursion(n-1);
}
public static void main(String args[])
{
int maxNumber = 10;
System.out.print("Fibonacci Series of "+maxNumber+" numbers: ");
for(int i = 0; i < maxNumber; i++)
{
System.out.print(fibonacciRecursion(i) +" ");
}
}
}

Output:

Fibonacci Series of 10 numbers: 0 1 1 2 3 5 8 13 21 34


2. Develop a program to perform operations of a Stack and Queue using
arrays.

(i) Stack Implementation using Array

Aim:

Algorithm:
Step 1: Create a Java class named stack.
Step 2: Inside the stack class, declare class-level variables:
 int top to track the top element in the stack.
 int maxsize to represent the maximum size of the stack.
 int arr[] to hold the elements of the stack.
 Create a constructor to initialize top to -1.
Step 3: Implement a push(Scanner in) method inside the stack class to insert an
element into the stack.
 Check if the stack is full (i.e., top is equal to maxsize - 1).
 If it's not full, prompt the user to enter a value, read it with a Scanner,
increment top, and store the value in the arr array.
 Print a message indicating that an item has been pushed onto the stack.
Step 4: Implement a pop() method inside the stack class to remove an element
from the stack.
 Check if the stack is empty (i.e., top is equal to -1).
 If it's not empty, decrement top.
 Print a message indicating that an element has been popped from the stack.
Step 5: Implement a display() method inside the stack class to display the elements
of the stack.
 Loop through the elements in the arr array from index 0 to top.
 Print each element in the stack.
Step 6: Create a Java class named Main to contain the main method.
Step 7: In the main method:
 Create a Scanner object to read user input.
 Create an instance of the stack class named s.
 Use a while loop to repeatedly display a menu and process user choices
until the user chooses to exit (choice 4).
 Inside the loop, prompt the user for a choice and use a switch statement to
execute the corresponding operation: push, pop, display, or exit. Handle
invalid choices by providing an appropriate message.

Program:

import java.util.Scanner;
class stack
{
int top;
int maxsize=3;
int arr[]=new int[maxsize];
stack()
{
top=-1;
}
void push(Scanner in)
{
if(top==maxsize-1)
{
System.out.println("Over flow");

}
else
{

System.out.println("Enter the value:");


int val=in.nextInt();
top++;
arr[top]=val;
System.out.println("Item pushed");

}
}
void pop()
{
if(top==-1)
{
System.out.println("Under Flow");
}
else
{
top--;
System.out.println("Element Poped");
}
}
void display()
{

for(int i=0;i<=top;i++)
{
System.out.println("Element:"+arr[i]);
}
}
}
public class Main
{
public static void main(String args[])
{
int choice=0;
Scanner in=new Scanner(System.in);
stack s=new stack();
while(choice!=4)
{
System.out.println("1 push");
System.out.println("2 pop");
System.out.println("3 display");
System.out.println("4 exit");
System.out.println("Enter your choice:");
choice=in.nextInt();

switch(choice)
{
case 1:
s.push(in);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
System.out.println("Exit:");
break;
default:
System.out.println("Enter the valied choice:");
break;
}
}
}
}

Output:

1 push
2 pop
3 display
4 exit
Enter your choice:
1
Enter the value:
10
Item pushed
1 push
2 pop
3 display
4 exit
Enter your choice:
1
Enter the value:
20
Item pushed
1 push
2 pop
3 display
4 exit
Enter your choice:
3
element:10
element:20
1 push
2 pop
3 display
4 exit
Enter your choice:
2
Item Poped
1 push
2 pop
3 display
4 exit
Enter your choice:
3
element:10
1 push
2 pop
3 display
4 exit
Enter your choice:
3
element:10
1 push
2 pop
3 display
4 exit
Enter your choice:
4
Exit:

(ii) Queue Implementation Using Array


Aim:

Algorithm:
Step 1: Create a Java class named queue.
Step 2: Inside the queue class, declare class-level variables:
 int top to track the top element in the stack.
 int maxsize to represent the maximum size of the queue.
 int arr[] to hold the elements of the queue.
 Create a constructor to initialize top to -1.
Step 3: Implement a push(Scanner in) method inside the queue class to insert an
element into the queue.
 Check if the queue is full (i.e., top is equal to maxsize - 1).
 If it's not full, prompt the user to enter a value, read it with a Scanner,
increment top, and store the value in the arr array.
 Print a message indicating that an element has been pushed.
Step 4: Implement a pop() method inside the queue class to remove an element
from the queue.
 Check if the queue is empty (i.e., top is equal to -1).
 If it's not empty, shift all elements in the arr array one position to the left to
simulate dequeuing (i.e., removing the front element).
 Decrement top.
 Print a message indicating that an element has been popped.
Step 5: Implement a display() method inside the queue class to display the
elements of the queue.
 Loop through the elements in the arr array from index 0 to top.
 Print each element in the queue.
Step 6: Create a Java class named Main to contain the main method.
Step 7: In the main method:
 Create a Scanner object to read user input.
 Create an instance of the queue class named s.
 Use a while loop to repeatedly display a menu and process user choices
until the user chooses to exit (choice 4).
 Inside the loop, prompt the user for a choice and use a switch statement to
execute the corresponding operation: push, pop, display, or exit.
Program:

import java.util.Scanner;

class queue
{
int top;
int maxsize=3;
int arr[]=new int[maxsize];
queue()
{
top=-1;
}
void push(Scanner in)
{
if(top==maxsize-1)
{
System.out.println("Over flow");

}
else
{

System.out.println("Enter the value:");


int val=in.nextInt();
top++;
arr[top]=val;
System.out.println("Element pushed");

}
}
void pop()
{
if(top==-1)
{
System.out.println("Under Flow");
}
else
{
for(int i=0;i<maxsize-1;i++)
{
arr[i]=arr[i+1];
}
top--;
System.out.println("Element Poped");
}
}
void display()
{

for(int i=0;i<=top;i++)
{
System.out.println("element:"+arr[i]);
}
}
}
public class Main
{
public static void main(String args[])
{
int choice=0;
Scanner in=new Scanner(System.in);
queue s=new queue();
while(choice!=4)
{
System.out.println("1 push");
System.out.println("2 pop");
System.out.println("3 display");
System.out.println("4 exit");
System.out.println("Enter your choice:");
choice=in.nextInt();

switch(choice)
{
case 1:
s.push(in);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
default:
System.out.println("Exit:");
break;
}
}
}
}
Output:
1 push
2 pop
3 display
4 exit
Enter your choice:
1
Enter the value:
10
Element pushed
1 push
2 pop
3 display
4 exit
Enter your choice:
1
Enter the value:
20
Element pushed
1 push
2 pop
3 display
4 exit
Enter your choice:
1
Enter the value:
30
Element pushed
1 push
2 pop
3 display
4 exit
Enter your choice:
1
Over flow
1 push
2 pop
3 display
4 exit
Enter your choice:
3
element:10
element:20
element:30
1 push
2 pop
3 display
4 exit
Enter your choice:
2
Element Poped
1 push
2 pop
3 display
4 exit
Enter your choice:
3
element:20
element:30
1 push
2 pop
3 display
4 exit
Enter your choice:
4
Exit:
3. Implement and perform different operations on Single, Double and
Circular Linked Lists.

(i) Singly Linked List

Aim:

Algorithm:
Step 1: Define a Java class named single.
Step 2: Inside the single class, define a static nested class called Node with two
fields:
 int data: to store the data value of the node.
 Node next: to reference the next node in the list.
Step 3: Define a static method PrintList(Node n) that takes a reference to the head
node of a singly linked list as input and prints the elements of the linked list in a
loop.
 Initialize a loop that continues until the current node n is not null.
 Inside the loop, print the data value of the current node and move to the
next node by updating n to n.next.
Step 4: In the main method:
 Create five Node objects: head, second, third, fourth, and fifth, representing
nodes of the singly linked list.
 Assign integer values to the data field of each node.
 Link the nodes together to form a singly linked list by setting the next
references accordingly. The last node ( fifth) should have its next reference
set to null to indicate the end of the list.
Step 5: Call the PrintList method with head as the argument to print the elements
of the singly linked list from the head node to the end.

Program:

public class single


{
static class Node
{
int data;
Node next;
};
static void PrintList(Node n)
{
while(n!=null)
{
System.out.println(n.data+"");
n=n.next;
}
}
public static void main(String args[])
{
//creations of node:
Node head=new Node();
Node second=new Node();
Node third=new Node();
Node fourth=new Node();
Node fifth=new Node();
//assign the value
head.data=1;
second.data=2;
third.data=3;
fourth.data=4;
fifth.data=5;

//link the nodes


head.next=second;
second.next=third;
third.next=fourth;
fourth.next=fifth;
fifth.next=null;
PrintList(head);
}
}

Output:

1 2 3 4 5
(ii) Doubly Linked List

Aim:

Algorithm:

Step 1: Define a Node class with three fields: int data to store the data value, Node
next to reference the next node, and Node prev to reference the previous node.

Step 2: Create two static methods:


 Printnext(Node n): Prints the elements of the doubly linked list in forward
order.
 Printprev(Node n): Prints the elements of the doubly linked list in reverse
order.
Step 3: In the main method:
 Create three Node objects: head, second, and tail, representing the nodes of
the doubly linked list.
 Assign integer values to the data field of each node.
 Link the nodes together to form a doubly linked list, including setting next
and prev references as needed.
Step 4: Use the Printnext method to print the elements of the doubly linked list
from the head to the tail.
Step 5: Use the Printprev method to print the elements of the doubly linked list
from the tail to the head.

Program:

public class doubly {


static class Node
{
int data;
Node next;
Node prev;
}
static void Printnext(Node n)
{

while(n!=null)
{
System.out.println(n.data);
n=n.next;
}
}
static void Printprev(Node n)
{

while(n!=null)
{
System.out.println(n.data);
n=n.prev;
}
}
public static void main(String args[])
{
//Node Creations
Node head=new Node();
Node second=new Node();
Node tail=new Node();

//Assign the data


head.data=10;
second.data=20;
tail.data=30;

//Link the current Node


head.next=second;
second.next=tail;
tail.next=null;

tail.prev=second;
second.prev=head;
head.prev=null;

System.out.println("Head Node to Tail Node");


Printnext(head);
System.out.println("Tail Node to Head Node");
Printprev(tail);
}
}
Output:
Head Node to Tail Node
10
20
30
Tail Node to Head Node
30
20
10

(iii) Circular Linked List

Aim:
Algorithm:

Step 1: Define a Node class with fields for data and a reference to the next node.
Step 2: Create a CircularSinglyLinkedList class with methods to add elements to an
initially empty list, add elements at the beginning and end, and display the list.
Step 3: In the CircularSinglyLinkedList constructor, initialize the head and tail
pointers to null to represent an initially empty list.
Step 4: Implement methods for adding elements:
 addToEmptyList(int data): Add an element to an empty list, creating the
circular link.
 addAtBeginning(int data): Insert an element at the beginning, updating the
head and tail pointers accordingly.

 addAtEnd(int data): Insert an element at the end, updating the tail pointer.
Step 5: In the main method, create an instance of the CircularSinglyLinkedList
class, add elements to the list, and display the list after each insertion.

Program:

class Node
{
int data;
Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
class CircularSinglyLinkedList
{
Node head;
Node tail;

public CircularSinglyLinkedList()
{
this.head = null;
this.tail = null;
}

public void addToEmptyList(int data)


{
Node newNode = new Node(data);
if (head == null)
{
head = newNode;
tail = newNode;
newNode.next = head;
}
}

public void addAtBeginning(int data) {


Node newNode = new Node(data);
if (head == null) {
addToEmptyList(data);
} else {
newNode.next = head;
tail.next = newNode;
head = newNode;
}
}

public void addAtEnd(int data) {


Node newNode = new Node(data);
if (head == null) {
addToEmptyList(data);
} else {
newNode.next = head;
tail.next = newNode;
tail = newNode;
}
}
public void display() {
if (head == null) {
System.out.println("The list is empty.");
return;
}
Node n = head;
do {
System.out.print(n.data + " ");
n = n.next;
} while (n != head);
System.out.println();
}
}
public class circularinsertion {
public static void main(String[] args) {
CircularSinglyLinkedList list = new CircularSinglyLinkedList();

list.display();
// Add at beginning
list.addAtBeginning(10);
list.addAtBeginning(20);
list.display();
//Add at ending
list.addAtEnd(30);
list.addAtEnd(40);
list.display();
}
}

Output:

The list is empty.


20 10
20 10 30 40
1. Develop a program to perform operations of Stack and Queue using
Linked Lists

(i) Stack using linked list

Aim:

Algorithm:

Step 1: Define Node Class


 Create a Node class with fields data (to store integer values) and next (to
reference the next node).
 Create a constructor in the Node class to initialize data and set next to null
when a new node is created.
Step 2: Define StackUsingLinkedlist Class
 Create a StackUsingLinkedlist class to implement the stack using a linked
list.
 Initialize the top reference to null in the constructor, indicating an empty
stack.
Step 3: Push an Element onto the Stack (push)
 Implement a push method to add elements to the stack.
 Create a new Node called temp with the given data.
 Set the next of temp to point to the current top (if any).
 Update the top reference to point to temp, effectively pushing the new
element onto the stack.
Step 4: Pop an Element from the Stack (pop)
 Implement a pop method to remove the top element from the stack.
 Check if top is null. If it is, print "Stack Underflow" to indicate that there
are no elements to pop.
 If top is not null, update the top reference to point to the next element in
the list, effectively popping the top element from the stack.
 Print "Element popped" after successfully popping.
Step 5: Display Stack Elements (display)
 Implement a display method to print the elements of the stack from top to
bottom.
 Check if top is null.
 If top is null, print "stack isEmpty" to indicate that the stack is empty.
 If top is not null, initialize a Node n to top and print "Elements:" to
indicate the start of the list.
 Iterate through the linked list using a while loop.
 Print each node's data and move to the next node by updating n.
 Print a newline at the end of the display to separate it from other output.
Program:

class Node
{
int data;
Node next;
Node(int data)
{
this.data=data;
this.next=null;
}
}
class StackUsingLinkedlist
{
Node top;
StackUsingLinkedlist()
{
this.top = null;
}
public void push(int data)
{
Node temp = new Node(data);
temp.next = top;
top = temp;
}
public void peek()
{
if (top!=null)
{
System.out.println("peek element:"+top.data);
}
else
{
System.out.println("Stack is empty");
}
}
public void pop() // remove at the beginning
{
if (top == null)
{
System.out.println("Stack Underflow");

}
else
{
top = top.next;
System.out.println("Element poped");
}
}
public void display()
{
if (top == null)
{
System.out.println("stack isEmpty");
}
else
{
Node n = top;
System.out.println("Elemets:");
while (n != null)
{
System.out.println(n.data);
n = n.next;
}
}
}
}
public class Main
{
public static void main(String[] args)
{
StackUsingLinkedlist obj= new StackUsingLinkedlist();
obj.push(10);
obj.push(20);
obj.push(30);
obj.display();
obj.peek();
obj.pop();
obj.display();
obj.peek();
obj.pop();
obj.peek();
obj.pop();
obj.display();
}
}
Output:

Elemets:
30
20
10
peek element:30
Element poped
Elemets:
20
10
peek element:20
Element poped
peek element:10
Element poped
stack isEmpty

You might also like