OOPs & DS LAB Manual
OOPs & DS LAB Manual
COLLEGE (AUTONOMOUS),
TIRUNELVELI
LABORATORY RECORD
REGISTER NUMBER:
STUDENT NAME :
REG NO ………………………………
BONAFIDE CERTIFICATE
AIM:
To write a JAVA program to display the name and age of the person using Class.
ALGORITHM
PROGRAM:
import java.util.*;
class prg
{
public static void main()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter your name :");
String st=s.nextLine();
System.out.println("Enter your age :");
int age=s.nextInt();
System.out.println("Enter your year of birth :");
String yr=s.nextLine(); System.out.println("Name :"+st);
System.out.println("Age :"+age);
System.out.println("Year of birth :"+yr);
}
}
OUTPUT:
RESULT:
EX:01(b) Basics of JAVA
SWAPPING OF TWO NUMBERS
DATE:
AIM:
To write a Java program for swapping of two numbers.
ALGORITHM:
1.Include the header files.
2.Declare a function named ‘swap’.
3.Read two numbers a and b.
4. Call that function in the main program
5. For Call by value, pass the value when we call that function
6.For Call by reference, assign the address to the pointer
7.Print the swapped values
8.Compile and run the program
PROGRAM
//call by value
{ int a = 30;
int b = 45;
System.out.println("Before swapping, a = " + a + " and b = " + b);
// Invoke the swap method
swapFunction(a, b);
System.out.println("\n**Now, Before and After swapping values will be same here**:");
System.out.println("After swapping, a = " + a + " and b is " + b);
}
public static void swapFunction(int a, int b) {
System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}
}
//Call by reference
RESULT:
EX:01(c) Basics of JAVA
STATIC CLASS MEMBER
DATE:
AIM:
To write a Java program to demonstrate static class member.
ALGORITHM:
PROGRAM:
/ inner class
class Reptile {
public void displayInfo() {
System.out.println("I am a reptile.");
}
}
// static class
static class Mammal {
public void displayInfo() {
System.out.println("I am a mammal.");
}
}
}
class Main {
public static void main(String[] args) {
// object creation of the outer class
Animal animal = new Animal();
}
}
OUTPUT:
RESULT:
EX:02 Program to Define Inheritance and Show Method
Overriding.
DATE:
AIM
To develop java program using abstract class
ALGORITHM:
PROGRAM
OUTPUT:
RESULT:
EX:03 Programs to Demonstrate Exception Handling
DATE:
AIM
ALGORITHM
PROGRAM
RESULT:
EX:4 Programs to Demonstrate Multithreading
DATE:
AIM
To develop java program to perform multi threading
ALGORITHM:
PROGRAM
import java.util.*;
class even implements Runnable
{
public int x;
public even(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is EVEN and Square of " + x + " is: " + x * x);
}}
class odd implements Runnable
{
public int x;
public odd(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x);
}}
class A extends Thread
{
public void run()
{
int num = 0;
Random r = new Random();
try
{
for (int i = 0; i < 5; i++)
{
num = r.nextInt(100);
System.out.println("Main Thread and Generated Number is " + num);
if (num % 2 == 0)
{
Thread t1 = new Thread(new even(num));
t1.start();
} else {
Thread t2 = new Thread(new odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println("-------------------------------------");
}}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}}}
public class Multithread
{
public static void main(String[] args)
{
A a = new A();
a.start();
}}
OUTPUT:
RESULT:
EX:5 Array implementation of List ADT
DATE:
AIM:
To write a JAVA program to display the name and age of the person using Class.
ALGORITHM
PROGRAM:
AIM:
To write a program to implement singly linked list.
ALGORITHM:
1. For insert operation, allocate memory for the new node, add the number given as
input bythe user and add the node at the end of the list.
2. For the delete operation, delete the node with the value given as input by the user.
3. Display all the values present in the list.
4. Display the size which denotes the total number of elements in the list.
PROGRAM:
import java.util.*;
class LLNode{ int
data;
LLNode next;
LLNode(int data)
{
this.data=data;
this.next=null;
}
}
class Demo{
LLNode head;
LLNode insertInBeg(int key,LLNode head)
{
LLNode ttmp=new LLNode(key); if(head==null)
head=ttmp; else
{
ttmp.next=head;
head=ttmp;
}
return head;
}
LLNode insertInEnd(int key,LLNode head)
{
LLNode ttmp=new LLNode(key); LLNode
ttmp1=head; if(ttmp1==null)
head=ttmp; else
{
while(ttmp1.next!=null) ttmp1=ttmp1.next;
ttmp1.next=ttmp;
}
return head;
}
LLNode insertAtPos(int key,int pos,LLNode head)
{
LLNode ttmp=new LLNode(key); if(pos==1)
{
ttmp.next=head;
head=ttmp;
}
else
{
LLNode ttmp1=head;
for(int i=1;ttmp1!=null && i<pos;i++)
ttmp1=ttmp1.next; ttmp.next=ttmp1.next;
ttmp1.next=ttmp;
}
return head;
}
LLNode delete(int pos,LLNode head)
{
LLNode ttmp=head;
if(pos==1)
head=ttmp.next; else
{
for(int i=1;ttmp!=null && i<pos-1;i++)
ttmp=ttmp.next;
ttmp.next=ttmp.next.next;
}
return head;
}
int length(LLNode head)
{
LLNode ttmp=head; int
c=0; if(ttmp==null)
return 0;
else
{
while(ttmp!=null)
{ ttmp=ttmp.next; c+
+;
}
}
return c;
}
LLNode reverse(LLNode head)
{
LLNode prevLNode=null,curLNode=head,nextLNode=null;
while(curLNode!=null)
{
nextLNode=curLNode.next;
curLNode.next=prevLNode;
prevLNode=curLNode;
curLNode=nextLNode;
}
head=prevLNode;
return head;
}
void display(LLNode head)
{
LLNode ttmp=head;
while(ttmp!=null)
{System.out.print(ttmp.data+" "); ttmp=ttmp.next;
}
}
public static void main(String[] args)
{
LinkedListDemo l=new LinkedListDemo(); l.head=null;
Scanner in=new Scanner(System.in); do
{
OUTPUT:
RESULT:
EX:7(a) Stack ADT- Array Implementation of Stack
DATE:
AIM:
To write a program to perform array implementation of stack.
ALGORITHM:
1. Initialize the top pointer to be -1.
2. To perform push operation, get the input from the user and add the value to
the stack byincrementing the value of top.
3. To perform the pop operation, the value is deleted by decrementing the top pointer.
4. Display the values in the stack.
PROGRAM
import java.util.*;
class Stack
{
int[] a;
int top;
Stack()
{
a=new int[100];
top=-1;
}
void push(int x)
{
if(top==a.length-1)
System.out.println("overflow");
else
a[++top]=x;
}
int pop()
{
if(top==-1)
{System.out.println("underflow");
return -1;
}
else
return(a[top--]);
}
void display()
{
for(int i=0;i<=top;i++)
System.out.print(a[i]+" ");
System.out.println();
}
boolean isEmpty()
{
if(top==-1)
return true;
else
return false;
}
int peek()
{
if(top==-1)
return -1;
return (a[top]);
}
}
public class Demo
{
public static void main(String args[])
{
Stack s=new Stack();
Scanner in= new Scanner(System.in); do
{System.out.println("\n******** MENU *******");
System.out.println("\n1.PUSH");
System.out.println("\n2.POP");
System.out.println("\n3.PEEK");
System.out.println("\n4 IS EMPTY"); System.out.println("\
n5.EXIT");
System.out.println("\n enter ur choice : ");
switch(in.nextInt())
{
case 1:
System.out.println("\nenter the value ");
s.push(in.nextInt());
break;
case 2:
System.out.println("\n popped element : "+ s.pop());
break;
case 3:
System.out.println("\n top element : "+ s.peek());
break;
case 4:
System.out.println("\n is empty : "+ s.isEmpty());
break;
case 5:
System.exit(0);
break;
default:
System.out.println("\n Wrong Choice!");
break;
}
System.out.println("\n do u want to cont... ");
}while(in.nextInt()==1);
}
}
OUTPUT:
RESULT:
EX:7(b) Stack ADT- Linked List Implementation of Stack
DATE:
AIM:
To write a program to implement stack using linked list.
ALGORITHM:
1. Initially create the header node and assign its next pointer to null.
2. For push operation, allocate memory for creating a temporary node and add the
nodetothe next of header node.
3. For pop operation, delete the node next to the header and free the memory space.
4. Display the values present in the stack
PROGRAM:
import java.util.*;
class LNode
{
int data;
LNode next;
LNode(int d)
{
data=d;
}
}
class Stack
{
LNode push(int d,LNode head)
{
LNode tmp1 = new LNode(d);
if(head==null)
head=tmp1;
else
{
tmp1.next=head;
head=tmp1;
}
return head;
}
LNode pop(LNode head)
{
if(head==null)
System.out.println("underflow");
else
head=head.next;
return head;
}
void display(LNode head)
{
System.out.println("\n list is : ");
if(head==null){
System.out.println("no LNodes");
return;
}
LNode tmp=head;
while(tmp!=null){
System.out.print(tmp.data+" ");
tmp=tmp.next;
}
}
boolean isEmpty(LNode head)
{
if(head==null)
return true;
else
return false;
}
int peek(LNode head)
{
if(head==null)
return -1;
return head.data;
}
}
public class Demo{
public static void main(String[] args)
{
Stack s=new Stack();
LNode head=null;
Scanner in=new Scanner(System.in); do
{
System.out.println("\n******** MENU *******");
System.out.println("\n1.PUSH");
System.out.println("\n2.POP");
System.out.println("\n3.PEEK");
System.out.println("\n4 IS EMPTY");
System.out.println("\n5 DISPLAY");
System.out.println("\n6.EXIT");
System.out.println("\n enter ur choice : ");
switch(in.nextInt())
{
case 1:
System.out.println("\nenter the value ");
head=s.push(in.nextInt(),head);
break;
case 2:
head=s.pop(head);
break;
case 3:
System.out.println("\n top element : "+ s.peek(head));
break;
case 4:
System.out.println("\n is empty : "+ s.isEmpty(head));
break;
case 5:
s.display(head);
break;
case 6:
System.exit(0);
break;
default:
System.out.println("\n Wrong Choice!");
break;
}
System.out.println("\n do u want to cont... ");
}while(in.nextInt()==1);
}
}
OUTPUT:
RESULT:
EX:8 Evaluation of a Postfix Expression Using a Stack
DATE:
AIM:
ALGORITHM:
PROGRAM:
RESULT:
EX:9(a) Queue ADT- Array implementation of Queue
DATE:
Aim:
To write a C program for array implementation of queue.
Algorithm:
Program:
#include <stdio.h>
#include <conio.h>
#define max 5
static int
queue[max];int front = -
1;
int rear=-1; void
insert(intx)
{
queue[++rear] = x;
if (front == -1)
front = 0;
}
int rem()
{
int val;
val = queue[front];
if (front==rear && rear==max-
1) front = rear = -1;
else front+
+; return
(val);
}
void view()
{
int i;
if (front == -1)
printf("\n Queue Empty \
n"); else
{ printf("\n Front-->");
for(i=front; i<=rear; i++)
printf("%4d", queue[i]);
printf("<--Rear\n");
}
}
void main()
{
int d,ch= 0,val;clrscr();while(ch != 4)
{
printf("\n QUEUE OPERATION \
n"); printf("1.INSERT");
printf("2.DELETE");
printf("3.VIEW");
printf("4.QUIT\n");
printf("Enter Choice :
"); scanf("%d", &ch);
switch(ch)
{
case 1:
if(rear < max-1)
{
printf("\n Enter element to be inserted : ");
scanf("%d", &val);
insert(val);
}
else
printf("\n Queue Full \
n"); break;
case 2:
if(front == -1)
printf("\n Queue Empty \
n"); else
{
d=rem();
printf("\n Element deleted : %d \n",d);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}}}
OUTPUT:
RESULT:
EX:9(b) Queue ADT- Linked List Implementation of Queue
DATE:
AIM:
ALGORITHM:
1. Initially create the header node and assign its next pointer to null.
2. For insert operation, allocate memory for the new node and insert the node next to
the header.
3. For delete operation, delete the last node in the list and free the memory space.\
4. Display the values available in the queue.
PROGRAM:
#include <stdio.h>
#include<conio.h>
#include <process.h>
#include <alloc.h> struct
node
{
int label;
struct node *next;
};
main()
{
int ch=0;int k;
struct node *h, *temp, *head;
/* Head node construction */
head = (struct node*) malloc(sizeof(struct
node)); head->next = NULL;
while(1)
{
printf("\n Queue using Linked List \n");
printf("1->Insert ");
printf("2->Delete ");printf("3->View ");
printf("4->Exit \n");
printf("Enter your choice :
"); scanf("%d", &ch);
switch(ch)
{
/* Create a new node */
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : ");
scanf("%d", &temp->label);
/* Reorganize the links */
h = head;
while (h->next !=
NULL); h = h->next;
h->next = temp;
temp->next =
NULL; break;
case 2:
/* Delink the first node */h = head-
>next;
head->next = h->next;
printf("Node deleted \n");
free(h); break;
case 3: printf("\n\
nHEAD -> "); h=head;
while (h->next!=NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
case 4:
printf("NULL \n");break;
exit(0);
}
}
}
OUTPUT:
RESULT:
EX:10 Implementation of Binary Trees and Traversals
DATE:
AIM:
To write a program to implement binary trees and traversals.
ALGORITHM:
1. Get the input from the user and create a binary tree such that the root is greater
thantheleft child and less than the right child.
2. For inorder traversal
a) Traverse the left subtree, i.e., call Inorder(left-subtree)
b) Visit the root.
c) Traverse the right subtree, i.e., call Inorder(right-subtree)
3. For preorder traversal
a) Visit the root.
b) Traverse the left subtree, i.e., call Preorder(left-subtree)
c) Traverse the right subtree, i.e., call Preorder(right-subtree)
4. For postorder traversal
a) Traverse the left subtree, i.e., call Postorder(left-subtree)
b) Traverse the right subtree, i.e., call Postorder(right-subtree)
c) Visit the root.
5. Display the results
PROGRAM:
#include<stdio.h>
#include<conio.h> struct
node
{
int data;
struct node *right,*left;
}
*root,*p,*q;
struct node *make(int y)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct
node)); newnode->data=y;
newnode->right=newnode->left=NULL;return(newnode);
}
void left(struct node *r,int x)
{
if(r->left!=NULL)printf("\n invalid!");else
r->left=make(x);
}
void right(struct node *r,int x)
{
if(r->right!=NULL)printf("\n invalid!");else
r->right=make(x);
}
void inorder(struct node *r)
{if(r!=NULL)
{
inorder(r->left);printf("\t %d",r->data);
inorder(r->right);
}}
void preorder(struct node *r)
{ if(r!=NULL)
{
printf("\t%d",r->data);
preorder(r->left);
preorder(r->right);
}
}
void postorder(struct node *r)
{if(r!=NULL)
{
postorder(r->left);postorder(r->right);
printf("\t%d",r->data);
}
}
void main()
{
int no;
int choice;char ch;clrscr(); printf("\n
enter the root");
scanf("%d",&no);root=make(no);
p=root;do
{
printf("Do you want to
continue(y/n)"); scanf("%s",&ch);
printf("\n enter another
number:"); scanf("%d",&no);
if(no==-1)
break; p=root;
q=root;
while(no!=p->data && q!=NULL)
{
p=q;
if(no<p->data)q=p-
>left;
else
q=p->right;
}
if(no<p->data)
{
printf("\n leftbranch of %d is%d",p->data,no);
left(p,no);
}
else
{
right(p,no);
printf("\n rightbranch of %d is %d",p->data,no);
}
}while(ch=='y'||ch=='Y');do
{
printf("\n 1.inorder traversal \n 2.preorder traversal \n3.postorder \n 4.exit");
printf("\n enter the choice");
scanf("%d",&choice);switch(choice)
{
case 1:inorder(root);
break;
case 2:preorder(root);
break;
case 3:postorder(root);
break;
case 4:exit(0);
default: printf("error!invalid
choice"); break;
}
getch();
}
while(choice<=3);
OUTPUT:
RESULT:
EX:11 Implementation of Graph Traversals - BFS and DFS
DATE:
AIM:
To write a program to implement DFS and BFS graph traversal.
ALGORITHM:
DFS
1. Define a Stack of size total number of vertices in the graph.
2. Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
3. Visit any one of the adjacent vertex of the vertex which is at top of the stack which is not visited
and push it on to the stack.
4. Repeat step 3 until there are no new vertex to be visit from the vertex on top of the stack.
5. When there is no new vertex to be visit then use back tracking and pop one vertex from the stack.
6. Repeat steps 3, 4 and 5 until stack becomes Empty.
7. When stack becomes Empty, then produce final spanning tree by removing unused edges from
the graph
BFS
1. Define a Queue of size total number of vertices in the graph.
2. Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue.
3. Visit all the adjacent vertices of the verex which is at front of the Queue which is not visited and
insert them into the Queue.
4. When there is no new vertex to be visit from the vertex at front of the Queue then delete that
vertex from the Queue.
5. Repeat step 3 and 4 until queue becomes empty.
6. When queue becomes Empty, then produce final spanning tree by removing unused edges from
the graph
PROGRAM:
#include<stdio.h>
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();
void main()
{
int n,i,s,ch,j;
char c,dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("THE ADJACENCY MATRIX IS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
do
{
for(i=1;i<=n;i++)
vis[i]=0; printf("\
nMENU");
printf("\n1.B.F.S");
printf("\n2.D.F.S"); printf("\
nENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);
switch(ch)
{
case 1:bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? ");
scanf("%c",&dummy);
scanf("%c",&c);
}while((c=='y')||(c=='Y'));
}
OUTPUT:
70
RESULT:
70