Maths PY 2023
Maths PY 2023
Maths PY 2023
Queue(int n)
{
size=n;
que = new int[size];
front=-1;
rear=-1;
}
void insert(int x)
{
if(rear == size-1)
System.out.println("Overflow");
else if(front==-1 && rear==-1)
{
front++;
rear++;
que[rear]=x;
}
else
{
rear++;
que[rear]=x;
}
}
int delete()
{
if(front==-1 && rear==-1)
{
System.out.println("Underflow");
return -9999;
}
else if(front==rear)
{
int temp=que[front];
front=-1;
rear=-1;
return temp;
}
else
{
int temp=que[front];
front++;
return temp;
}
}
else
{
System.out.println("The queue is…");
for(int i=front;i<=rear;i++)
System.out.print(que[i]+" | ");
}
System.out.println("\n");
}
do
{
System.out.println("1. Add");
System.out.println("2. Remove");
System.out.println("3. Display");
System.out.println("4. Quit");
System.out.println("Enter choice ?");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the new element to be added at rear");
element=Integer.parseInt(br.readLine());
obj.insert(element);
break;
case 2:
t=obj.delete();
if(t!=-9999)
System.out.println("Deleted "+t);
break;
case 3:
obj.display();
break;
case 4:
System.exit(0);
default:
System.out.println("Wrong choice… Re enter your choice");
}
} while(k==1);
}
}
Stack
class Stack
{
int st[];
int size;
int top;
Stack(int n)
{
size = n;
st = new int[size];
top = -1;
}
void push(int n)
{
if(top == size-1)
{
System.out.println("OVERFLOW");
}
else
{
top = top + 1;
st[top] = n;
}
}
int pop()
{
if(top == -1)
{
System.out.println("UNDERFLOW");
return -999;
}
else
{
int val = st[top];
top = top - 1;
return val;
}
}
void display()
{
if(top == -1)
{
System.out.println("The stack is empty");
}
else
{
System.out.println("The elements in the stack are : ");
for(int i = top; i>=0; i--)
{
System.out.println(st[i]);
}
}
}
void tell()
{
if(point==-1)
System.out.println("SHELF EMPTY");
else
System.out.println(name[point]);
}
void add(String v)
{
if(point==max-1)
System.out.println("SHELF FULL");
else
name[++point]=v;
}
void display()
{
for(int i=point;i>=0;i--)
{
System.out.println(name[i]);
}
}
}
public class Stack1
{
char cha[];
int size,top;
for(int i=top;i>=0;i--)
{
System.out.println(cha[i]);
}
}
}
/*A stack is a linear data structure which enables the user to add and remove
integers from one end only,using the concept of LIFO(LastInFirstOut).An array
containing the marks of 50 students in ascending order is to be pushed into the
stack.
Define a class Array_to_Stack with the following details:
Classname:Array_to_Stack
Data members / instance variables:
m[ ]:to store the marks
Array_to_Stack(int n)
{
cap=n;
top=-1;
st=new int[cap];
m=new int[cap];
}
void input_marks()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter "+cap+" elements in ascending order");
for(int i=0;i< cap;i++)
{
m[i]=sc.nextInt();
pushmarks(m[i]);
}
}
void pushmarks(int v)
{
if(top==cap-1)
System.out.println("not possible stack is full");
else
st[++top]=v;
}
int popmarks()
{
if(top==-1)
{
System.out.println("UNDERFLOW");
return -999;
}
else
{
int temp=st[top];
top=top-1;
return temp;
}
}
void display()
{
for(int i=top;i>=0;i--)
{
System.out.println(st[i]);
}
}
}
/*Register is an entity which can hold a maximum of 100 names. The register
enables the user to add and remove names from the top most end only.
Define a class Register with the following details:
Class name:Register
Data members / instance variables:
stud[ ]:array to store the names of the students
cap:stores the maximum capacity of the array
top: to point the index of the top end
Member functions:
Register (int max):constructor to initialize the data member cap = max, top = -1
and create the string array
void push(String n):to add names in the register at the top location if possible,
otherwise display the message “OVERFLOW”
String pop():removes and returns the names from the top most location of the
register if any, else returns “$”
void display():displays all the names in the register
(a)Specify the class Register giving details of the functions void push(String) and
String pop( ). Assume that the other functions have been defined .
The main function and algorithm need NOT be written .*/
public class Register
{
String stud[];
int cap,top;
Register(int max)
{
cap=max;
top=-1;
stud=new String[cap];
}
void push(String n)
{
if (top==cap-1)
System.out.println("OVERFLOW");
else
stud[++top]=n;
}
String pop()
{
if (top==-1)
{
System.out.println("UNDERFLOW");
return " $$";
}
else
{
String temp=stud[top];
top=top-1;
return temp;
}
}
}
/*WordPile is an entity which can hold maximum of 20 characters .The restriction
is that a character can be added or removed from one end only.Some of the
members of classes are given below:
Classname:WordPile
Data members/instance variables:
ch[ ]:character array to hold the character elements
capacity:integer variable to store the maximum capacity
top:to point to the index of the top most element
Methods / Member functions:
WordPile( int cap):constructor to initialise the data member capacity=cap , top=-1
and create the WordPile
void pushChar(char v):adds the character to the top of WordPile if
possible,otherwise output a message ”WordPile
is full”
char popChar():returns the deleted character from the top of the WordPile if
possible,otherwise it returns ‘\\’
(a)Specify the class WordPile giving the details of the constructor, void
pushChar(char) and char popChar().
THE MAIN() FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.*/
public class WordPile
{
char ch[]=new char[20];
int capacity,top;
WordPile(int cap)
{
capacity=cap;
top=-1;
ch=new char[capacity];
}
void pushChar(char v)
{
if(top==capacity-1)
System.out.println("WordPile is full");
else
ch[++top]=v;
}
char popChar()
{
if(top==-1)
return'\\';
else
{
char temp=ch[top];
top=top-1;
return temp;
}
}
}
/*Stack is a kind of data structure which can store elements with the restriction
that an element can be added or removed from the top only.
The details of the class Stack is given below:
Classname:Stack
Data Members/ instance variables:
st[]: the array to hold the names
size:the maximum capacity of the string array
top: the index of the top most element of the stack
ctr: to count the number of elements of the stack
Member functions:
}
public String popname()
{
String temp;
if(top==-1)
{
System.out.println("UNDERFLOW");
return " ";
}
else
{
temp=st[top];
top=top-1;
return temp;
}
}
public void display()
{
for(int i=top;i>=0;i--)
{
System.out.println(st[i]);
}
}
}
Classname:Repeat
Member functions:
void pushvalue(int v):to add integers from the rear index if possible else display
the message(“OVERFLOW”)
int popvalue():to remove and return element from the front.If array is empty then
return -9999
public Repeat(int m)
{
cap=m;
f=0;
r=0;
st=new int[cap];
}
int temp;
if(f==0 && r==0)
{
System.out.println("Underflow");
return -9999;
}
else if(f==r-1)
{
temp=st[f];
f=0;
r=0;
return temp;
}
else
{
temp =st[f];
f=f+1;
return temp;
}
(a) Specify the class Link giving details of the constructor(int) void addlink(int), int
dellink() and void display().
THE MAIN() FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.*/
public class Link
{
int lnk[]=new int[100];
int max,begin,end;
Link(int mm)
{
max=mm;
begin=0;
end=0;
lnk=new int[max];
}
void addlink(int v)
{
if(end==max-1)
System.out.println("OUT OF SIZE...");
else
lnk[end++]=v;
}
int dellink( )
{
int temp;
if(begin==0 && end==0)
{
System.out.println("EMPTY...");
return -99;
}
else if(begin==end-1)
{
temp=lnk[begin];
begin=0;
end=0;
return temp;
}
else
{
temp=lnk[begin];
begin=begin+1;
return temp;
}
}
void display()
{
if(begin==0 && end==0)
System.out.println("Empty queue");
else
{
System.out.println("The queue is…");
for(int i=begin;i<end;i++)
{
System.out.print(lnk[i]+" | ");
}
}
}
}
/*Queue is an entity which can hold a maximum of 100 integers . The queue
enables the user to add integers from the rear and remove integers from the
front.
Define a class Queue with the following details:
Class name Queue
Data Members / instance variables:
Que[] :array to hold the integer elements
size : stores the size of the array
front:to point the index of the front
rear:to point the index of the rear
Member functions:
Queue (int mm):constructor to initialize the data size = mm, front = 0, rear = 0
void addele(int v ):to add integer from the rear if possible else display the
message “Overflow”
int delele( ):returns elements from front if present, otherwise displays the
message “Underflow ” and return -9999
void display ( ):displays the array elements
Specify the class Queue giving details of ONLY the functions void addele(int) and
int delele( ). Assume that the other functions have been defined.
The main function and algorithm need NOT be written.*/
public class Queue1
{
int Que[];
int size;
int front,rear;
Queue1(int mm)
{
size=mm;
front=0;
rear=0;
Que=new int[size];
}
void addele(int v)
{
if(rear==size-1)
System.out.println(" Overflow");
else
Que[rear++]=v;
}
int delele()
{ int temp;
if(front==0 && rear==0)
{
System.out.println("UnderFlow");
return -9999;
}
if(front==rear-1)
{
temp=Que[front];
front=0;
rear=0;
return temp;
}
else
{
temp=Que[front];
front++;
return temp;
}
}
void display()
{
if(front==0 && rear==0)
System.out.println("Empty queue");
else
{
System.out.println("The queue is…");
for(int i=front;i<rear;i++)
{
System.out.println(Que[i]+"|");
}
}
}
}
/*A linear data structure enables the user to add address from rear end and
remove address from front. Define a class Diary with the following details:
Class name:Diary
Data members / instance variables:
Q[ ]:array to store the addresses
size:stores the maximum capacity of the array
start: to point the index of the front end
end:to point the index of the rear end
Member functions:
Diary (int max):constructor to initialize the data member size=max, start=0 and
end=0
void pushadd(String n):to add address in the diary from the rear end if possible,
otherwise display the message “NO
SPACE”
String popadd( ):removes and returns the address from the front end of the diary
if any, else returns “?????”
void show( ):displays all the addresses in the diary
(a)Specify the class Diary giving details of the functions void pushadd(String) and
String popadd( ).Assume that the other functions have been defined.
The main function and algorithm need NOT be written.*/
class Diary
{
String Q[];
int size, start, end;
void pushadd(String n)
{
if(end==size)
System.out.println(" NO SPACE");
else
{
Q[end]=n;
end=end+1;
}
}
String popadd()
{
String temp;
if(start==0 && end==0)
return "?????";
else if(start==end-1)
{
temp=Q[start];
end=0;
start=0;
return temp;
}
else
{
temp=Q[start];
start++;
return temp;
}
}
void show()
{
if(start==0 && end==0)
System.out.println("Empty queue");
else
{
System.out.println("The queue is…");
for(int i=start;i<end;i++)
{
System.out.print(Q[i]+" | ");
}
}
}
}