Professional Documents
Culture Documents
Aim:
Algorithm:
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:
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
{
}
}
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:
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
{
}
}
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.
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:
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.
Program:
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();
tail.prev=second;
second.prev=head;
head.prev=null;
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;
}
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:
Aim:
Algorithm:
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