Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA programming Implement insertion (or push) and deletion (or pop) for a stack

ID: 3756869 • Letter: J

Question

JAVA programming

Implement insertion (or push) and deletion (or pop) for a stack and a circular queue with length of n keys as defined in class.

-Input: list of pairs of an integer and an operation, e.g.,

10 1.in 3.in 5.in del 2.in ….. for the queue

10 1.push 2.push 5.push pop 2.push ….. for the stack

/* note that the forefront “10” in each input file indicates the size of the stack or queue. */

              /* Note that input is to be read in from a file in a batch. */

              /* In case of overflow, print “overflow” and halt your program */

-Output: the list before operation and the list after operation displayed on the screen standard outout (not into a file).

A command line user interface has to be provided as follows:

              “Enter your input file name:”

              “Display the input before:” /* display the input integers following a statement */

              “Display the output after the operation:” /* display the resulting output on the screen */

Total 2 programs (one for stack and one for queue)

Explanation / Answer

implementation of insertion and deletion in stack

#include<iostream>
#define SIZE 10

using namespace std;


int STACK[MAX],TOP;

//stack initialization is done here
void initializeStack()
{
TOP=-1;
}
//check it is empty or not
//if empty operation pop cannot be performed
int isEmpty()
{
if(TOP==-1)
return 1;
else
return 0;
}

//check stack is full or not
//if full push operation cannot be performed
int isFull(){
if(TOP==MAX-1)
return 1;
else
return 0;
}

void push(int num){
if(isFull()){
cout<<"STACK is FULL."<<endl;
return;
}
++TOP;//TOP is incremented and number is pushed into the stack
STACK[TOP]=num;
cout<<num<<" has been inserted."<<endl;
}

void display(){
int i;
if(isEmpty()){
cout<<"STACK is EMPTY."<<endl;
return;
}
for(i=TOP;i>=0;i--){
cout<<STACK[i]<<" ";
}
cout<<endl;
}

//pop - to remove item
void pop(){
int temp;
if(isEmpty()){
cout<<"STACK is EMPTY."<<endl;
return;
}

temp=STACK[TOP];
TOP--;
cout<<temp<<" has been deleted."<<endl;
}
int main(){
int num;
initStack();
char ch;
do{
int a;
cout<<"Choose 1.push "<<"2.pop "<<"3.display ";
cout<<"Please enter your choice: ";
cin>>a;
switch(a)
{
case 1:
cout<<"Enter an Integer Number: ";
cin>>num;
push(num);
break;

case 2:
pop();
break;

case 3:
display();
break;

default :
cout<<"enter the correct choice!!! ";


}
cout<<"Do you want to continue ? ";
cin>>ch;
}while(ch=='Y'||ch=='y');
return 0;
}

Implementation of insertion and deletion in a circular queue

#include <iostream>

#define MAX 10

using namespace std;

class Circular_Queue

{

private:

int *cqueue_arr;

int front, rear;

public:

Circular_Queue()

{

cqueue_arr = new int [MAX];

rear = front = -1;

}

void insert(int item)

{

if ((front == 0 && rear == MAX-1) || (front == rear+1))

{

cout<<"Overflow ";

return;

}

if (front == -1)

{

front = 0;

rear = 0;

}

else

{

if (rear == MAX - 1)

rear = 0;

else

rear = rear + 1;

}

cqueue_arr[rear] = item ;

}

void del()

{

if (front == -1)

{

cout<<"Underflow ";

return ;

}

cout<<"Element deleted from queue is : "<<cqueue_arr[front]<<endl;

if (front == rear)

{

front = -1;

rear = -1;

}

else

{

if (front == MAX - 1)

front = 0;

else

front = front + 1;

}

}

void display()

{

int front_pos = front, rear_pos = rear;

if (front == -1)

{

cout<<"Queue is empty ";

return;

}

cout<<"Queue elements : ";

if (front_pos <= rear_pos)

{

while (front_pos <= rear_pos)

{

cout<<cqueue_arr[front_pos]<<" ";

front_pos++;

}

}

else

{

while (front_pos <= MAX - 1)

{

cout<<cqueue_arr[front_pos]<<" ";

front_pos++;

}

front_pos = 0;

while (front_pos <= rear_pos)

{

cout<<cqueue_arr[front_pos]<<" ";

front_pos++;

}

}

cout<<endl;

}

};

int main()

{

int choice, item;

Circular_Queue cq;

do

{

cout<<"1.Insert ";

cout<<"2.Delete ";

cout<<"3.Display ";

cout<<"4.Quit ";

cout<<"Enter your choice : ";

cin>>choice;

switch(choice)

{

case 1:

cout<<"Input the element u want to insert : ";

cin>>item;

cq.insert(item);

break;

case 2:

cq.del();

break;

case 3:

cq.display();

break;

case 4:

break;

default:

cout<<"Wrong choice ";

}

}

while(choice != 4);

return 0;

}