Documente Academic
Documente Profesional
Documente Cultură
SYNTAX
class Strange
{
int ele[];
int capacity,top;
Strange(int cap)
{
ele=new int[20];
capacity=cap;
top=-1;
}
void pushItem(int value)
{
if(top+1==capacity)
System.out.println("Strange is full,cannot push item");
else
{
ele[++top]=value;
System.out.println("Pushed item= "+value);
}
}
int popItem()
{
if(top==-1)
{
System.out.println("Strange is empty, returning -9999");
return -9999;
}
else
{
System.out.println("Popped item = "+ele[top--]);
return ele[top+1];
}
}
}
VARIABLE DESCRIPTION
SL. NO. NAME TYPE METHOD DESCRIPTION
01. ele int[] - Array storing the integers
02. capacity int - Stores length of array
03. top int - Points index of topmost element
04. cap int Strange() Passes the value of capacity
05. value int pushItem() Stores the element to be added
PROGRAM-22
To implement a deqUEue and manipulate data
ALGORITHM
STEP 1 - START
STEP 2 -
SYNTAX
class Notice
{
int DQ[];
int c,MaxSize,front,rear;
Notice(int size)
{
DQ=new int[20];
MaxSize=size;
c=0;
front=0;
rear=-1;
}
void JoinRear(int ele)
{
if(c==MaxSize)
System.out.println("Line is overflow");
else
{
rear++;
c++;
System.out.println("Element added to rear = "+ele);
DQ[rear]=ele;
}
}
void JoinFront(int ele)
{
if(c==MaxSize)
System.out.println("The line is overflow");
else
{
for(int i=rear;i>=front;i--)
DQ[i+1]=DQ[i];
c++;
System.out.println("Element added to front = "+ele);
DQ[front]=ele;
rear++;
}
}
void FrontDelete()
{
if(c==0)
System.out.println("The line is underflow");
else
{
System.out.println("Element removed from front = "+DQ[front]);
for(int i=front;i<=rear;i++)
DQ[i]=DQ[i+1];
c--;
rear--;
}
}
void RearDelete()
{
if(c==0)
System.out.println("Line is underflow");
else
{
System.out.println("Element removed from rear = "+DQ[rear]);
c--;
rear--;
}
}
void PrintLine()
{
if(c!=0)
for(int i=front;i<=rear;i++)
System.out.println("-> "+DQ[i]);
else
System.out.println("The line is empty");
}
}
VARIABLE DESCRIPTION
SL. NO. NAME TYPE METHOD DESCRIPTION
01. DQ int[] - Array to store elements
02. c int - Counter
03. MaxSize int - Stores maximum number elements
04. front int - Stores position of first element
05. rear int - Stores position of last element
06. size int Notice() Constructor parameter
07. ele int JoinRear() Method parameter
08. ele int JoinFront() Method parameter
09. i int PrintLine() Loop variable
PROGRAM-23
To implement a stack
ALGORITHM
STEP 1 - START
STEP 2 -
SYNTAX
class Stack
{
String st[];
int size,top,ctr;
Stack()
{
size=0;
top=0;
st=null;
}
Stack(int cap)
{
st=new String [cap];
size=cap;
top=-1;
}
void PushName(String n)
{
if(top+1==size)
System.out.println("Overflow");
else
{
st[++top]=n;
System.out.println("Pushed name = "+n);
ctr++;
}
}
String popname()
{
if(top==-1)
{
System.out.println("Underflow");
return null;
}
else
{
System.out.println("Popped name = "+st[top--]);
ctr--;
return st[top+1];
}
}
void display()
{
for(int i=0;i<=top;i++)
System.out.println("-> "+st[i]);
}
}
VARIABLE DESCRIPTION
SL. NO. NAME TYPE METHOD DESCRIPTION
01. st String[] - Array to store elements
02. size int - Stores size
03. top int - Stores position of top
04. ctr int - Counter
05. cap int Stack() Constructor parameter
06. n String PushName Method parameter
07. i int display() Loop variable
PROGRAM-24
To check for equality of numbers using nodes
ALGORITHM
STEP 1 - START
STEP 2 -
SYNTAX
import java.io.*;
class Node
{
int data;
Node link;
Node(int d)
{
data=d;
link=null;
}
}
class linkedList
{
Node head,rear,ptr;
linkedList()
{
head=null;
rear=null;
}
boolean isEmpty()
{
if (head==null && rear==null)
return true;
else
return false;
}
void pushhead(int a)
{
if(head==null)
{
Node n1=new Node(a);
head=n1;
}
else
{
ptr=head;
while(ptr!=null)
{
ptr=ptr.link;
}
Node n1=new Node(a);
ptr.link=n1;
}
}
void pushRear(int a)
{
if(rear==null)
{
Node n1=new Node(a);
rear=n1;
}
else
{
ptr=rear;
while(ptr!=null)
{
ptr=ptr.link;
}
Node n1=new Node(a);
ptr.link=n1;
}
}
int pop()
{
ptr=head;
int r;
if(head.link==null)
{
r=head.data;
head=null;
}
else
{
while(ptr.link.link!=null)
{
ptr=ptr.link;
}
r=ptr.link.data;
ptr.link=null;
}
return r;
}
}
import java.io.*;
class check
{
public static void main(String args[])throws IOException
{
linkedList l1=new linkedList();
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Give the first number :");
int n=Integer.parseInt(obj.readLine());
l1.pushhead(n);
System.out.print("Give the second number : ");
int m=Integer.parseInt(obj.readLine());
l1.pushhead(m);
int f=0;
int num1=l1.pop();
int num2=l1.pop();
String num3=Integer.toString(num1);
String num4=Integer.toString(num2);
while((num3!="")||(num4!=""))
{
int len1=num3.length();
int len2=num4.length();
System.out.print("First number : ");
for(int i=0;i<=(len1-1);len1++)
{
char c=num3.charAt(i);
if(i<(len1-1))
System.out.print(c+",");
else
System.out.print(c);
}
System.out.print("Second number : ");
for(int j=0;j<=(len2-1);len2++)
{
char c2=num4.charAt(j);
if(j<(len2-1))
System.out.print(c2+",");
else
System.out.print(c2);
}
for(int i=0;i<=(len1-1);len1++)
{
char c=num3.charAt(i);
for(int j=0;j<=(len2-1);len2++)
{
char c2=num4.charAt(j);
if(c==c2)
continue;
else
{
f=1;
break;
}
}
}
}
if(f==1)
System.out.println("Unequal");
else
System.out.println("Equal");
}
}VARIABLE DESCRIPTION
SYNTAX
class Repeat
{
int st[];
int cap,f,r;
Repeat(int m)
{
st=new int[100];
cap=m;
f=r=0;
}
void pushvalue(int v)
{
if(r==cap)
System.out.println("Overflow");
else
{
st[r++]=v;
System.out.println("Pushed value = "+v);
}
}
int popvalue()
{
if(r==0)
{
System.out.println("Underflow");
return -99999;
}
else
{
int x=st[f];
System.out.println("Popped value = "+x);
for(int i=0;i<(r-1);i++)
st[i]=st[i+1];
r--;
return x;
}
}
void display()
{
for(int i=0;i<r;i++)
System.out.println("-> "+st[i]);
}
}
VARIABLE DESCRIPTION
SL. NO. NAME TYPE METHOD DESCRIPTION
01. st int[] - Array to store elements
02. cap int - Stores capacity of st
03. f int - Stores position of first element
04. r int - Stores position of last element
05. m int Repeat() Constructor parameter
06. v int pushvalue() Method parameter
07. x int popvalue() Temporary storage
08. i int popvalue(),display() Loop variable