1. String Operations
File Name: string.cpp
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30],str3[30];
int length1,length2,length3;
clrscr();
cout<<" \n Enter the String First :: \n";
cin>>str1;
cout<<" \n Enter the String Second :: \n";
cin>>str2;
if(strcmp(str1,str2)!=0)
{
cout<<"\n String are not Equal. \n";
strcat(str1,str2);
}
else
cout<<"\n String are equal.\n";
strcpy(str3,str1);
length1=strlen(str1);
length2=strlen(str2);
length3=strlen(str3);
cout<<" \n The The Concatenation Of Sting is "<< str1<< " and the length of String is "<<length1;
cout<<" \n The Normal String is "<< str2<< " and the length of String is "<<length2;
cout<<" \n The Copy Of the String is"<< str3<< " and the length of String is "<<length3;
getch();
}
File Name: string.cpp
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30],str3[30];
int length1,length2,length3;
clrscr();
cout<<" \n Enter the String First :: \n";
cin>>str1;
cout<<" \n Enter the String Second :: \n";
cin>>str2;
if(strcmp(str1,str2)!=0)
{
cout<<"\n String are not Equal. \n";
strcat(str1,str2);
}
else
cout<<"\n String are equal.\n";
strcpy(str3,str1);
length1=strlen(str1);
length2=strlen(str2);
length3=strlen(str3);
cout<<" \n The The Concatenation Of Sting is "<< str1<< " and the length of String is "<<length1;
cout<<" \n The Normal String is "<< str2<< " and the length of String is "<<length2;
cout<<" \n The Copy Of the String is"<< str3<< " and the length of String is "<<length3;
getch();
}
/*----Program to implement Binary Search Algorithm---*/
/* Theory :In binary search, we first compare the key with the item in the middle position of the array. If there's a match, we can return immediately. If the key is less than the middle key, then the item sought must lie in the lower half of the array; if it's greater then the item sought must lie in the upper half of the array. So we repeat the procedure on the lower (or upper) half of the array. */
import java.io.*;
class BinarySearch
{
public static void main(String args[ ])
{
int i,n = 0,KEY, flag=0;
String ans="y";
int x[] = new int[25];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("\n\nEnter how many numbers to be stored : ");
n = Integer.parseInt(in.readLine());
System.out.println("\nEnter the number in INCREASING ORDER PLEASE.");
System.out.println("Enter "+n+" numbers ....");
for(i=1;i<=n;i++)
{
System.out.print("\t\tElement ["+i+"]=");
x[i] = Integer.parseInt(in.readLine());
}
do
{
System.out.print("\nEnter the number to be searched : ");
KEY = Integer.parseInt(in.readLine());
flag = Binary_Search(x,n,KEY); // call to binary search function
if (flag == -1)
System.out.println(" Number Not present in the given array");
else
System.out.println(" Number "+KEY+" found at "+flag+" location in the array");
System.out.print(" Want to search another number ?");
ans=in.readLine();
}while((ans.equals("y"))||(ans.equals("Y")));
}
catch(Exception e) { System.out.println("I/O Error"); }
}
//Binary Search Function
static int Binary_Search(int K[ ],int n,int KEY)
{
int low=1;
int high=n;
int mid;
mid=(low+high)/2; /* find the mid position */
while (high>=low)
{
if (K[mid]==KEY)
return(mid);
else
{
if(KEY>K[mid])
low=mid+1; /* number may be in upper half */
else
high=mid-1; /* Number may be present in lower half */
mid=(low+high)/2;
}
}
return(-1);
}
/*----Program to implement Insertion sort Algorithm---*/
/* Theory :Insertion sort is an elementary sorting algorithm. It has time complexity of (n2), thus being slower than heapsort, merge sort and also shellsort. Insertion sort is well suited for sorting small data sets or for the insertion of new elements into a sorted sequence. */
import java.io.*;
class InsertionSort
{
public static void main(String args[ ])
{ System.out.println(“\n\t\t **********INSERTION SORT***********”);
int i,n=0;
int x[]=new int[25];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("\n------------INSERTION SORT------------");
System.out.print("\nEnter how many numbers to be sorted : ");
n = Integer.parseInt(in.readLine());
System.out.println("\nEnter "+n+" numbers in any order :");
for(i=0;i<n;i++)
{
System.out.print("\t\t\tElement x["+(i+1)+"]=");
x[i] = Integer.parseInt(in.readLine());
}
}
catch(Exception e)
{
System.out.println("I/O Error");
}
insertion(x,n); // Call to insertion function
System.out.println("\nSorted Elements are :");
for(i=0;i<n;i++)
System.out.println("\t\t\tElement x["+(i+1)+"]="+x[i]);
}
//Insertion Sort Function
static void insertion(int x[],int n)
{
int i,k,y;
/*initially x[0] may be thought of as a sorted file of
one element.After each repetetion of the following
loop, the elements x[0] through x[k] are in order*/
for(k=1;k<n;k++)
{
/*Insert x[k] into the sorted file*/
y=x[k];
/*Move down 1 position all elements greater than y*/
for(i=k-1;i>=0&&y<x[i];i--)
x[i+1]=x[i];
/*Insert y at proper position*/
x[i+1]=y;
}/*end for*/
}/*end insertion function*/
}
/*----Program to implement Merge sort Algorithm---*/
/* Theory :Mergesort is a divide and conquer algorithm. The sorting elements are stored in a collection. This collection is divided into two collections and these are again sorted via mergesort. Once the two collections are sorted then the result is combined.*/
import java.io.*;
class MergeSort
{
public static void main(String args[ ])
{
int i,n=0;
int x[]=new int[25];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("\n-------------MERGE SORT-------------");
System.out.print("\nEnter how many numbers to be sorted : ");
n = Integer.parseInt(in.readLine());
System.out.println("\nEnter "+n+" numbers in any order :");
for(i=0;i<n;i++)
{
System.out.print("\t\t\t x["+(i)+"]=");
//System.out.print(x[i]+"");
x[i] = Integer.parseInt(in.readLine());
}
}
catch(Exception e)
{
System.out.println("I/O Error"); }
merge(x,n); // Call to Merge Sort
System.out.println("\nSorted Elements are :");
for(i=0;i<n;i++)
System.out.println("\t\t\t x["+(i)+"]="+x[i]);
}
//Merge Sort Function
static void merge(int x[],int n)
{
int sub[] = new int[25];
int i,j,k,l1,l2,u1,u2,size;
size=1; //Merge files of size 1
while(size<n)
{
l1=0; // Initialize lower bounds of first file
k=0; // k is index for auxiliary array
while((l1+size)<n) // Check to see if there are two files to merge
{
l2=l1+size; // Compute remaining indices
u1=l2-1;
u2=((l2+size-1)<n)?(l2+size-1):(n-1);
// Proceed through the two subfiles
for(i=l1,j=l2;i<=u1 && j<=u2;k++)
if(x[i]<=x[j])
sub[k]=x[i++];
else
sub[k]=x[j++];
/*At this point, one of the subfiles has been exhausted. Insert any remaining portions of the other file*/
for(;i<=u1;k++)
sub[k]=x[i++];
for(;j<=u2;k++)
sub[k]=x[j++];
// Advance l1 to the start of the next pair of files.
l1=u2+1;
} // end of while
//Copy any remaining single file
for(i=l1;k<n;i++)
sub[k++]=x[i];
//Copy aux into x and adjust size
for(i=0;i<n;i++)
x[i]=sub[i];
size*=2;
} // end of while
} // end of merge sort function
}
/*----Program to implement QuickSort sort Algorithm---*/
/* Theory :The basic version of quick sort algorithm was invented by C. A. R. Hoare in 1960 and formally introduced quick sort in 1962. It is used on the principle of divide-and-conquer. Quick sort is an algorithm of choice in many situations because it is not difficult to implement, it is a good "general purpose" sort and it consumes relatively fewer resources during execution. */
import java.io.*; // to load DataInputStream class
class QuickSort
{
public static void main(String args[ ])
{
int i,n=0;
int x[]=new int[55];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("\n-------------QUICK SORT-------------");
System.out.print("\nEnter how many numbers to be sorted : ");
n = Integer.parseInt(in.readLine());
System.out.println("\nEnter "+n+" numbers in any order....");
for(i=0;i<n;i++)
{
System.out.print("\t\tElement x["+(i+1)+"]=");
x[i] = Integer.parseInt(in.readLine());
}
}
catch(Exception e)
{
System.out.println("I/O Error"); }
quicksort(x,0,n-1,n); // Call to recursive quick sort
System.out.println("\nSorted Elements are :");
System.out.println("-----------------------------------------------------------");
display(x,n);
System.out.println("------------------------------------------------------------");
}
//Recursive quick Sort Function
static void quicksort(int x[], int low, int high, int n)
{
int j=0;
if(low>=high)
return;
j=partition(x, low, high, j);
System.out.print("After partitioning array from index "+(low+1)+" to "+(high+1)+ " :\n ");
display(x,n);
quicksort(x, low, j-1, n);
quicksort(x, j+1, high, n);
} // end of recursive quick sort function
static int partition(int x[], int low, int high, int pj)
{
int a, left, temp, right;
a=x[low]; // a is pivot element
right=high;
left=low;
while(left<right)
{
while(x[left]<=a && left<right)
left=left+1; //move right the array
while(x[right]>a)
right=right-1; //move left the array
if(left<right)
{
temp=x[left]; //interchange x[left]with x[right]
x[left]=x[right];
x[right]=temp;
}/*end if*/
}/* end while*/
x[low]=x[right];
x[right]=a;
pj=right;
return (pj);
} // end partition
static void display(int x[], int n)
{
int i;
System.out.println(" ");
for(i=0;i<n;i++)
System.out.print("\t"+x[i]+ " ");
System.out.println(" ");
} // end of display function
}
/*----Program to implement Selection sort Algorithm---*/
/* Theory :This type of sorting is called "Selection Sort" because it works by repeatedly element. It works as follows: first find the smallest in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.*/
import java.io.*;
class SelectionSort
{
public static void main(String args[ ])
{
int i,n=0;
//int increments[]={5,3,1};
int x[]=new int[25];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("\nEnter how many numbers to be sorted : ");
n = Integer.parseInt(in.readLine());
System.out.println("\nEnter "+n+" numbers in any order....");
for(i=0;i<n;i++)
{
System.out.print("\t\t\tElement x["+(i+1)+"]=");
x[i] = Integer.parseInt(in.readLine());
}
}
catch(Exception e)
{
System.out.println("I/O Error"); }
selection(x,n); // Call to Selection Sort
System.out.println("\nSorted Elements are :");
for(i=0;i<n;i++)
System.out.println("\t\t\tElement x["+(i+1)+"]="+x[i]);
}
//Selection Sort Function
static void selection(int x[],int n)
{
int i,indx,j,large;
System.out.println("--------------------------------------------------------------");
System.out.println("\tSteps by step sorting of elements are as follows");
System.out.println("-----------------------------------------------------------------");
for(i=n-1;i>0;i--)
{
/*place the largest number ofx[0] through
x[i] into large and its index into indx*/
large=x[0];
indx=0;
for(j=1;j<=i;j++)
if(x[j]>large)
{
large=x[j];
indx=j;
}/*end for...if*/
x[indx]=x[i];
x[i]=large;
for(int v=0;v<n;v++)
System.out.print("\t"+x[v]);
System.out.println();
}/*end for*/
}/*end select sort*/
}
/*----Program to Convert Infix Expression into PostFix Expression---*/
/* Theory :In conversion of infix expression to postfix expression infix expression a+b in this expression a & b are operand and + is a opertor while converting a will be copied to new expression callsed as postfix and after that + will be copied to the stack the b will be copied to postfix expression and as infix expression has no more elements all the element of stack will copied to postfix expression*/
import java.io.*;
class MyStack
{
char []a;
int top;
MyStack()
{
a=new char[5];
top=-1;
}
MyStack(int size)
{
new char[size];
p=-1;
}
boolean empty()
{
return (top==-1)?true:false;
}
void push(char ele)
{
if(top<a.length-1)
a[++top]=ele;
else
System.out.println("\n\t\t Stack OverFlow");
}
char pop()
{
if(!empty())
return a[top--];
else
return (char)-1;
}
}
class In2PostExample
{
String exp;
In2PostExample()
{
exp=new String("a+b");
}
In2PostExample(String s)
{
exp=new String (s);
}
int precedence(char a) //Start of precedent method
{
switch(a)
{
case '+':
return 1;
case '-':
return 1;
case '*':
return 2;
case '/':
return 2;
}
return 0;
} //End of precedent method
// Start of convert method
String convert()
{
char[] pexpr=new char[exp.length()+1];
char c,sc='\0';
int i,j;
MyStack st=new MyStack(exp.length());
for(i=0,j=0;i<exp.length();i++)
{
c=exp.charAt(i);
switch(c)
{
case '+':
case '-':
case '*':
case '/':
/*Checking the precedence of operator in stack and the operator that has been taken from infix expression*/
while(!st.empty() && precedence(sc=st.pop())>=precedence(c))
pexpr[j++]=sc;
st.push(sc);
st.push(c);
break;
case '(':
st.push(c);
break;
case ')':
while((sc=st.pop())!='(')
pexpr[j++]=sc;
break;
default:
pexpr[j++]=c;
}
}
while(!st.empty())
if((sc=st.pop())!='(')
pexpr[j++]=sc;
return (new String(pexpr));
}
public static void main(String args[])throws IOException //Start of main method
{
System.out.print("\n\t\t Enter the Infix Expression:: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
In2Post infix=new In2Post(str);
String p=infix.convert();
System.out.println("\n\t\t The Postfix String is:: "+p); //Printing Postfix string
} //End of main method
} //End of main class
STACK
import java.util.Scanner;
import java.io.*;
class MyStack
{
int []a;
int top;
MyStack() //initialize stack in default manner
{
a=new int[5];
top=-1;
}
MyStack(int size)
{
a=new int[size];
top=-1;
}
boolean empty()
{
if(top==-1)
return true;
else
return false;
}
void push(int ele)
{
if(top<a.length-1){
System.out.println("\n\t\t Pushed Element is::"+ele);
a[++top]=ele;
System.out.println("\n\t\tStack-->");
for(int i=top;i>=0;i--)
System.out.println("\n\t\t\t\t"+a[i]);
}
//System.out.println("\n\t\t Pushed Element is::"+ele);
else
System.out.println("\n\t\t Stack overflow");
}
void pop()
{
if(!empty())
System.out.println("\n\t\tPopped ELEMENT:"+a[top--]);
else
System.out.println("\n\t\tStack is empty");
} void display()
{
System.out.println("\n\t\tStack-->");
for(int i=top;i>=0;i--)
System.out.println("\n\t\t\t\t"+a[i]);
}
public static void main(String []arg)throws IOException
{
int ch,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//Scanner sc=new Scanner(System.in);
MyStack st = new MyStack();
while(true)
{
System.out.println("\n\t\t\t\t STACK");
System.out.println("\t\t1.Initialize");
System.out.println("\t\t2.Push");
System.out.println("\t\t3.Pop");
System.out.println("\t\t4.Display");
System.out.println("\t\t5.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
{
System.out.print("\n\t\tEnter the Size of Stack:");
int a=Integer.parseInt(br.readLine());
st=new MyStack(a);
}
break;
case 2:System.out.print("\n\t\tEnter Number : ");
n=Integer.parseInt(br.readLine());
st.push(n);
break;
case 3:
st.pop();
st.display();
break;
case 4:
st.display();
break;
case 5:return;
}
}
}
}
/*Program to implement Circular Queue
Theory:-A Queue is a Linear DataStructure in which Insertion is done from rear end and Deletion is done from front end .
Different Operation that can be performed on Circular Queue are as follow:-
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit */
import java.util.Scanner;
import java.io.*;
class MyQueue //Start of Queue class
{
int a[];
int i,front=0,rear=0,count=0;
MyQueue() //Initialize Queue in Default Manner
{
a=new int[5];
front=rear=-1;
}
MyQueue(int size)
{
a=new int[size];
front=rear=-1;
}
boolean empty() // Start of Empty Method
{
return(front==-1 || rear==-1) ?true:false;
} //End of Empty Method
void insert(int ele) //Start of insert Method
{
if(front==(rear+1)%a.length)
System.out.println("\n\t\t Queue Overflow.");
else
{
rear=(rear+1)%a.length;
a[rear]=ele;
System.out.println("\n\t\t Queue-->");
for(i=rear;i>=0;i--)
System.out.println("\n\t\t\t\t"+a[i]);
}
if(front==-1)
front++;
} //End of insert Method
String remove() // Start of remove Method
{
if(!empty()) {// Checking Queue for empty
String s ="\n\t\t Removed ELEMENT:: "+a[front];
if(front==rear)
front=rear=-1;
else{
front=(front+1)%a.length;
return s;
}
rear --;
return "\n\t\tQueue is Empty.";
}
else {
return "\n\t\tQueue is Empty.";
}
} //End of remove Method
void display()
{
if(!empty()) {// Checking Queue for empty
System.out.println("\n\t\t Queue-->");
int b=front;
do {
System.out.println("\t\t\t\t"+a[b]);
b=(b+1)%a.length;
} while(b!=rear);
//System.out.println("\t\t\t\t"+a[b]);
}
else{
System.out.println("\n\t\tQueue is Empty.");
}
}
public static void main(String []arg) throws IOException //Start of main method
{
int ch,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//Scanner sc=new Scanner(System.in);
MyQueue st = new MyQueue();
while(true)
{
System.out.println("\n\t\t\t\t Queue");
System.out.println("\t\t1.Initialize");
System.out.println("\t\t2.Insert");
System.out.println("\t\t3.Remove");
System.out.println("\t\t4.Display");
System.out.println("\t\t5.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
{
System.out.print("\n\t\tEnter the Size of Queue :: ");
int a=Integer.parseInt(br.readLine());
st=new MyQueue(a);
}
break;
case 2:
System.out.print("\n\t\tEnter Number : ");
n=Integer.parseInt(br.readLine());
st.insert(n);
break;
case 3:
System.out.print(st.remove());
break;
case 4:
st.display();
break;
case 5:
return;
default:
System.out.println("\n \t \t Please Select From 1 To 5 ");
}
}
} //End of Main Method
}//End of Class CD
/* Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 1
Enter the Size of Queue :: 3
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 2
Enter Number : 23
Queue-->
23
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 2
Enter Number : 3
Queue-->
3
23
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 2
Enter Number : 34
Queue-->
34
3
23
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 3
Removed Element:: 23
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 3
Removed Element:: 3
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 4
Queue-->
34
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 5
*/
/* Program to Implement different Operation on Linear Queue*/
/* Theory:-A Queue is a Linear DataStructure in which Insertion is done from rear end and Deletion is done from front end .
Different Operation that can be performed on Circular Queue are as follow:-
1.Empty
2.Insert
3.Remove */
import java.util.Scanner;
import java.io.*;
class MyQueue
{
int []a;
int front,rear;
MyQueue() //Initialize stack in default manner
{
a=new int[5];
front=-1;
rear=-1;
}
MyQueue(int size)
{
a=new int[size];
front=rear=-1;
}
boolean empty() //Start of Empty method
{
return (front==-1||front>rear)?true:false;
} //End of empty Method
void insert(int ele) //Start of Insert Method
{
if(rear<a.length-1)
{
System.out.println("\n\t\t Inserted Element is::"+ele);
a[++rear]=ele;
System.out.println("\n\t\t Inserted Element is::"+ele);
System.out.println("\n\t\tQueue-->");
for(int i=rear;i>=0;i--)
System.out.println("\n\t\t\t\t"+a[i]);
}
else
System.out.println("\n\t\t Queue overflow");
if(front==-1)front++;
} //End of Insert Method
void remove() //Start Of Remove Method
{
if(!empty())
System.out.println("\n\t\tRemoved Element is:"+a[front++]);
else
System.out.println("\n\t\tQueue is empty");
} //End of Remove Method
void display() //Start of Display Method
{
System.out.println("\n\t\tQueue-->");
for(int i=rear;i>=front;i--)
System.out.println("\n\t\t\t\t"+a[i]);
} //End Of Display Method
public static void main(String []arg)throws IOException
{
int ch,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
MyQueue st = new MyQueue();
while(true)
{
System.out.println("\n\t\t\t\t Queue");
System.out.println("\t\t1.Initialize");
System.out.println("\t\t2.Insert");
System.out.println("\t\t3.Remove");
System.out.println("\t\t4.Display");
System.out.println("\t\t5.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
{
System.out.print("\n\t\tEnter the Size of Queue:");
int a=Integer.parseInt(br.readLine());
st=new MyQueue(a);
}
break;
case 2:System.out.print("\n\t\tEnter Number : ");
n=Integer.parseInt(br.readLine());
st.insert(n); //Calling Insert Method
break;
case 3:
st.remove(); //Calling Remove Method
st.display();//Calling Display Method
break;
case 4:
st.display();
break;
case 5:return;
}
}
}
}
/*
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 1
Enter the Size of Queue:2
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 2
Enter Number : 32
Inserted Element is::32
Inserted Element is::32
Queue-->
32
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 2
Enter Number : 32
Inserted Element is::32
Inserted Element is::32
Queue-->
32
32
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 2
Enter Number : 32
Queue overflow
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 3
Removed Element is:32
Queue-->
32
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 4
Queue-->
32
Queue
1.Initialize
2.Insert
3.Remove
4.Display
5.Exit
Enter your choice : 5
*/
/* Program to Implement Linked List
Theory:-A linked list is a Linear DataStructure which is a collection of nodes that are linked one another.Linked list is dynamic
1.Insert
2.Delete */
import java.io.BufferedReader;
import java.util.*;
class Node //Start of class node
{
int info;
Node next,prev;
Node()
{
prev=next=null;
}
void insert(int d) //Start of Insert Method
{
Node p=new Node();
p.info=d;
if(this.next==null)
this.next=p;
else
{
Node tmp;
tmp=this.next;
while(tmp.next!=null) //First Travesing and then Inserting a new Node at last postion
tmp=tmp.next;
tmp.next=p;
p.prev=tmp;
}
} //End of Insert Method
void delete() //Start of Delete Method
{
Node tmp,p=null;
tmp=this.next;
if(tmp==null) //Checking Whether Ldoubly LinkedList is Empty
System.out.println("\n\t\t DOUBLY LINKED LIST IS EMPTY.");
while(tmp.next!=null)//Traversing to the last Node
{
tmp=tmp.next;
}
if(tmp==this.next){
this.next=null;
tmp=null;
}
else{
int d=tmp.info;
System.out.println("\n\t\t Deleted Node is::"+d);//Displaying Deleted Node
p=tmp.prev;
p.next=null;
tmp.next=prev.next=null;
tmp=null;
}
}
void display()//Start of Display Method
{
Node tmp;
tmp=this.next;
if(tmp!=null){
System.out.println("\n\t\t DOUBLY LINKED LIST");
while(tmp!=null)
{
//Displaying Data
System.out.println("\n\t\t"+tmp.info);
tmp=tmp.next;
}
}
else
System.out.println("\n\t\t DOUBLY LINKED LIST IS EMPTY");
}//End of Display Method
}//End of Node Class
//Start of Main Class
class DLLinked {
//Start of main method
public static void main(String args[])
{
Node ll=new Node(); //Creating object of class Node
Scanner sc=new Scanner(System.in);//Creating object of class Scanner
int ch;
while(true)
{
//Displaying Menu
System.out.println("\n\t\t\t\t DOUBLY Linked List");
System.out.println("\t\t1.Insert");
System.out.println("\t\t2.Delete");
System.out.println("\t\t3.Display");
System.out.println("\t\t4.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("\n\t\tEnter Number : ");
int n1=sc.nextInt(); //Reading data
ll.insert(n1);
break;
case 2:
ll.delete();
break;
case 3:
ll.display();
break;
case 4:
return;
default:
System.out.println("\n \t \t Please Select From 1 To 5 ");
}
}
}//End of Main Method
}//End of Main Class
/*
**********************************************OUTPUT*****************************************************
DOUBLY Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 12
DOUBLY Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 32
DOUBLY Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 3
DOUBLY Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 3
DOUBLY LINKED LIST
12
32
3
DOUBLY Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Deleted Node is::3
DOUBLY Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 4 */
/* Program to Implement Linked List
Theory:-A linked list is a Linear DataStructure which is a collection of nodes that are linled one another.Linked list is dynamic
1.Insert
2.Delete */
import java.io.BufferedReader;
import java.util.*;
class Node
{
int info;
Node next;
Node()
{
next=null;
}
void insert(int d)
{
Node p=new Node();
p.info=d;
if(this.next==null)
this.next=p;
else
{
Node tmp;
tmp=this.next;
while(tmp.next!=null)
tmp=tmp.next;
tmp.next=p;
}
}
void delete()
{
Node tmp,prev;
tmp=this.next;
prev=tmp;
while(tmp.next!=null)
{
prev=tmp;
tmp=tmp.next;
}
int d=tmp.info;
System.out.println("\n\t\t Deleted Node is::"+d);
if(tmp!=this.next)
prev.next=null;
else
this.next=null;
tmp=null;
}
void display()
{
Node tmp;
tmp=this.next;
if(tmp!=null){
while(tmp!=null)
{
System.out.println("\n\t\t LINKED LIST");
System.out.println("\n\t\t"+tmp.info);
tmp=tmp.next;
}
}
else
System.out.println("\n\t\t LINKED LIST IS EMPTY");
}
}
class LLinked {
public static void main(String args[])
{
Node ll=new Node();
Scanner sc=new Scanner(System.in);
int ch;
while(true)
{
System.out.println("\n\t\t\t\t Linked List");
System.out.println("\t\t1.Insert");
System.out.println("\t\t2.Delete");
System.out.println("\t\t3.Display");
System.out.println("\t\t4.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("\n\t\tEnter Number : ");
int n1=sc.nextInt();
ll.insert(n1);
break;
case 2:
ll.delete();
break;
case 3:
ll.display();
break;
case 4:
return;
default:
System.out.println("\n \t \t Please Select From 1 To 5 ");
}
}
}
}/*
*************************************************OUTPUT*****************************************************
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 12
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 32
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 3
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 3
LINKED LIST
12
32
3
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Deleted Node is::3
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 4 */
/* PROGRAM TO IMPLEMENT BINARY TREE
Theory:-A Binary tree is tree in which Every node at most as 2 children called the Left child and Right child */
import java.util.Scanner;
class Node
{
int info;
Node left,right;
Node()
{
left=right=null;
}
//Start of insertion Method
void insert(int d)
{
Node p = new Node(); //Creating new Node
p.info=d;
if(d<this.info)//Checking whether the element for insertion is less than root
{
if(this.left!=null)//Checking Whether root node is having any child
this.left.insert(d);
else
this.left=p;
}
else
{
if(this.right!=null)//Checking Whether root node is having any child
this.right.insert(d);
else
this.right=p;
}
}//End of insertion Method
void makeRoot(int d) //Start of Makeroot Method
{
this.info=d;
} //End of Makeroot Method
void inOrder(Node root) //Start Of InOrder Method
{
if(root!=null)
{
inOrder(root.left);
System.out.println("\t\t"+root.info);
inOrder(root.right);
}
}//End of InOrder Method
void preOrder(Node root)//Start Of PreOrder Method
{
if(root!=null)
{
System.out.println("\t\t"+root.info);
preOrder(root.left);
preOrder(root.right);
}
}//End of PreOrder Method
void postOrder(Node root)//Start Of PostOrder Method
{
if(root!=null)
{
postOrder(root.left);
postOrder(root.right);
System.out.println("\t\t"+root.info);
}
}//End of PostOrder Method
}
class BTree
{
public static void main(String []arg)
{
Node bt=new Node();
bt.makeRoot(10);//Calling MakeRoot Method For object bt
Scanner sc=new Scanner(System.in);//Creating Scanner Object
int ch;
while(true)
{//Displaying Menu
System.out.println("\n\t\t\t\t Tree");
System.out.println("\n\t\t1.Insert");
System.out.println("\n\t\t2.InOrder");
System.out.println("\n\t\t3.PostOrder");
System.out.println("\n\t\t4.PreOrder");
System.out.println("\n\t\t5.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("\n\t\tEnter the No to Insert in Tree:");
int a=sc.nextInt();
bt.insert(a);
break;
case 2:
System.out.println("\t\tInorder Traversals :");
bt.inOrder(bt);
break;
case 3:
System.out.println("\t\tPostorder Traversals :");
bt.postOrder(bt);
break;
case 4:
System.out.println("\t\tPreorder Traversals :");
bt.preOrder(bt);
break;
case 5:return;
}
}
}
}
/*
:::::::OUTPUT::::::
Tree
1.Insert
2.InOrder
3.PostOrder
4.PreOrder
5.Exit
Enter your choice : 1
Enter the Number to be Inserted in Tree:32
Tree
1.Insert
2.InOrder
3.PostOrder
4.PreOrder
5.Exit
Enter your choice : 1
Enter the Number to be Inserted in Tree:323
Tree
1.Insert
2.InOrder
3.PostOrder
4.PreOrder
5.Exit
Enter your choice : 2
Inorder Traversals :
10
32
323
Tree
1.Insert
2.InOrder
3.PostOrder
4.PreOrder
5.Exit
Enter your choice : 3
Postorder Traversals :
323
32
10
Tree
1.Insert
2.InOrder
3.PostOrder
4.PreOrder
5.Exit
Enter your choice : 4
Preorder Traversals :
10
32
323
Tree
1.Insert
2.InOrder
3.PostOrder
4.PreOrder
5.Exit
Enter your choice : 5
*/
Comments
Post a Comment