Question
Question: How can I solve it in "Java program"?, i need test code also. Define Pi...
Question: How can I solve it in "Java program"?, i need test code also.
Define Pi to be a parking lane i in a parking lot and carj to be the jth car; where the number of lanes in a parking lot is 4 and there are 7 cars to park. 1<= i<=4, and Cj: 1<=j<=7. Assume that all lanes are of the same length, and all cars are also of the same length, hence, the same number of cars could be lined up in each lane. Only 5 cars can be lined up in each lane. However, different lanes have different lining up rules according to their data structures. The first parking lot lane is an array, the second is a single listed, and the third is a stack.
Parking lot line - Structure
P1 - Array
P2 - Single Linked List
P3 - Stack
When a car arrives to the parking lot, the valet parking lot staff insert the car in one of lanes. However, customers may also ask the staff to get their cars back later. Keep in mind that any (inserting/ removing) operation (corresponding to moving any car into or out of any lanes) cost 1 KD.
Test the program on each data structure using the following Insert operations.
lane operation - Car Number
Insert - C1
Insert - C4
Remove - C1
Insert - C5
Insert - C7
Insert - C3
Insert - C2
Remove - C2
Insert - C1
Remove - C7
Insert - C6
Remove - C1
Answers
So basically here we want a class to have three structures array,single LinkedList and a stack.we want insert and remove operations on each.lets concentrate on this part:
importjava.util.*;
class Stack{
private :
int arr[];
int top;
int capacity;
Stack(int size)
{
art=new int[size];
capacity=size;
top=-1;
}
public void push(int x)
{
if(isFull()){
System.out.println("Parking full); System.exit(1);}
System.out.println("Inserting"+x);
arr[++top]=x;
}
public int pop(){
if(isEmpty()) System.out.println("Empty');System.exit(1);
System.out.orintln("Removing")+perk());
return arr[top--];}
public int peek(){
if(!isEmpty()) return art[top]; else System.exit(1);return -1}
public int size(){return top+1;}
public Boolean isEmpty(){return top=-1;}
public Boolean isFull(){return top=capacity-1;}
}
class SinglyLInkList
{.
int data;
Node next;
Node(int d){data=d;next=null;}
public void pushkal(int new_data){
Node newnode=new Node(new_data)
newnode.next=head;head=newnode;}
void deleteNode(int pos){if(head==null) return; Node temp=head; if(pos==0){head=temp.next; return; }for(int i=0;temp!=null && i<pos-1;i++) temp=temp.next; Node next =temp.next.next;temp.next=next;
}
class Array{
static int ind( int ar[],int n,int key){.
for(int I=0;I<n; I++) if(art[I]==key) return i; return -1}
static int insert(int ar[],int key,int capacity,int n){
if(n>capacity) return n;
ar[n]=key; return n+1;
}
}
class Main{
public static void main (String[] args){
//Implement all the three classes in this for Inserting and Removing elements using condition al loops.
}
}
Hope this helps.Kindly compile for typing error
add Your code in Main .
Regards.