Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update DynamicArrayQueue.java#1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
RanchCoder wants to merge1 commit intomaster
base:master
Choose a base branch
Loading
fromRanchCoder-patch-1
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletionssrc/chapter05queues/DynamicArrayQueue.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,8 @@ public DynamicArrayQueue (){
// Initializes the queue to use an array of given length.
public DynamicArrayQueue (int cap){
queueRep = new int [ cap];
//Resetting the CAPACITY to current user provided cap value.
CAPACITY = cap;
size = 0; front = 0; rear = 0;
}

Expand DownExpand Up@@ -80,13 +82,45 @@ private void expand(){
int length = size();
int[] newQueue = new int[length<<1]; // or 2* length

//copy items
for(int i = front; i <= rear; i ++)
newQueue[i-front] = queueRep[i%CAPACITY];
//This loop wont work for circular case when our rear will be pointing to the same location as that of front , i.e
// queue[5] = {1,2,3,4,5} , now our rear will be at (rear + 1) % CAPACITY
// i.e 0 and loop condition will run only once.
// for(int i = front; i <= rear; i ++)
// newQueue[i-front] = queueRep[i%CAPACITY];

// if front and rear pointer are pointing at the exact same location i.e 0 , when only enqueue is done and not dequeue uptil capacity
if(rear == 0){
for(int i = 0 ; i < queueRep.length ; i++){
newQueue[i] = queueRep[i];
}
}

// if front and rear pointer are pointing at the exact same location but not at 0 , i.e dequeue() is also performed in between.
else{

int j = 0, temp = front;
//System.out.println("temp value : " + temp + " and rear " + rear);

//we go only till one element before rear pointer , because there might be a issue of rear and front both pointer pointing at same location other than 0
while(temp != rear - 1){
newQueue[j] = queueRep[temp%CAPACITY];
j++;
temp = (temp+1) % CAPACITY;

}

newQueue[j] = queueRep[temp];

}


queueRep = newQueue;
front = 0;
rear = size-1;
//if we go uptil size - 1 then we will be pointing the latest element entered in our queue and not the latest free slot to enter
// the value
//rear = size-1;
//as size is incremented each time on enqueue and decremented each time at dequeue
rear = size;
CAPACITY *= 2;
}

Expand DownExpand Up@@ -117,4 +151,4 @@ public String toString(){
result += "]";
return result;
}
}
}

[8]ページ先頭

©2009-2025 Movatter.jp