-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson3 hw #3
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexrm84
wants to merge
5
commits into
Lesson2HW
Choose a base branch
from
Lesson3HW
base: Lesson2HW
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lesson3 hw #3
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package queue; | ||
|
|
||
| public class DequeImplement<E extends Object > extends QueueImplement<E> { | ||
|
|
||
| public static final int DEFAULT_REAR = -1; | ||
| public static final int DEFAULT_FRONT = 0; | ||
|
|
||
| private int rear; | ||
| private int front; | ||
|
|
||
| protected int size; | ||
| protected E[] data; | ||
|
|
||
| public DequeImplement(int maxSize) { | ||
| super(maxSize); | ||
| this.size=0; | ||
| this.data=(E[]) new Object[maxSize]; | ||
| this.front = DEFAULT_FRONT; | ||
| this.rear = DEFAULT_REAR; | ||
| } | ||
|
|
||
| public void insertLeft(E value) { | ||
| if (front==0) | ||
| front=data.length; | ||
| data[--front]=value; | ||
| size++; | ||
| } | ||
|
|
||
| public void insertRight(E value) { | ||
| if (rear==data.length-1) | ||
| rear=DEFAULT_REAR; | ||
| data[++rear]=value; | ||
| size++; | ||
| } | ||
|
|
||
| public E removeLeft() { | ||
| E value = data[front++]; | ||
| if (front==data.length) | ||
| front=DEFAULT_FRONT; | ||
| size--; | ||
| return value; | ||
| } | ||
|
|
||
| public E removeRight() { | ||
| E value = data[rear--]; | ||
| if (rear==DEFAULT_REAR) | ||
| rear=data.length-1; | ||
| size--; | ||
| return value; | ||
| } | ||
|
|
||
| public E peekLeft() { | ||
| return data[front]; | ||
| } | ||
|
|
||
| public E peekRight() { | ||
| return data[rear]; | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return size==0; | ||
| } | ||
|
|
||
| public boolean isFull() { | ||
| return size==data.length; | ||
| } | ||
|
|
||
| public int size() { | ||
| return size; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package queue; | ||
|
|
||
| import java.io.DataOutputStream; | ||
|
|
||
| public class MainApp { | ||
| public static void main(String[] args) { | ||
| // Queue<Integer> queue = new QueuePriority<Integer>(5); | ||
| // insert(queue, 2); | ||
| // insert(queue, 1); | ||
| // insert(queue, 4); | ||
| // insert(queue, 5); | ||
| // insert(queue, 3); | ||
| // insert(queue, 222); | ||
| // | ||
| // System.out.println("Size: " + queue.size()); | ||
| // System.out.println("Peek: " + queue.peek()); | ||
| // | ||
| // while (!queue.isEmpty()) { | ||
| // System.out.println(remove(queue)); | ||
| // } | ||
|
|
||
|
|
||
| DequeImplement<Integer> dec = new DequeImplement<Integer>(5); | ||
|
|
||
| insertLeft(dec,1); | ||
| insertLeft(dec,2); | ||
| insertLeft(dec,3); | ||
| insertRight(dec,4); | ||
| insertRight(dec,5); | ||
| while (!dec.isEmpty()) { | ||
| System.out.println(removeRight(dec)); | ||
| } | ||
|
|
||
| System.out.println(""); | ||
|
|
||
| insertLeft(dec,1); | ||
| insertLeft(dec,2); | ||
| insertLeft(dec,3); | ||
| insertRight(dec,4); | ||
| insertRight(dec,5); | ||
| while (!dec.isEmpty()) { | ||
| System.out.println(removeLeft(dec)); | ||
| } | ||
| } | ||
|
|
||
| private static <E> E remove(Queue<E> queue) { | ||
| if (!queue.isEmpty()) | ||
| return queue.remove(); | ||
| return null; | ||
| } | ||
|
|
||
| private static <E> void insert(Queue<E> queue, E value) { | ||
| if (!queue.isFull()) | ||
| queue.insert(value); | ||
| } | ||
|
|
||
| private static <E> E removeRight(DequeImplement<E> dec) { | ||
| if (!dec.isEmpty()) | ||
| return dec.removeRight(); | ||
| return null; | ||
| } | ||
|
|
||
| private static <E> void insertRight(DequeImplement<E> dec, E value) { | ||
| if (!dec.isFull()) | ||
| dec.insertRight(value); | ||
| } | ||
|
|
||
| private static <E> E removeLeft(DequeImplement<E> dec) { | ||
| if (!dec.isEmpty()) | ||
| return dec.removeLeft(); | ||
| return null; | ||
| } | ||
|
|
||
| private static <E> void insertLeft(DequeImplement<E> dec, E value) { | ||
| if (!dec.isFull()) | ||
| dec.insertLeft(value); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package queue; | ||
|
|
||
| public interface Queue<E> { | ||
|
|
||
| void insert(E value); | ||
| E remove(); | ||
| E peek(); | ||
|
|
||
| boolean isEmpty(); | ||
| boolean isFull(); | ||
| int size(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package queue; | ||
|
|
||
| public class QueueImplement<E> implements Queue<E> { | ||
|
|
||
| public static final int DEFAULT_REAR = -1; | ||
| public static final int DEFAULT_FRONT = 0; | ||
|
|
||
| private int rear; | ||
| private int front; | ||
|
|
||
| protected int size; | ||
| protected E[] data; | ||
|
|
||
| public QueueImplement(int maxSize) { | ||
| this.size=0; | ||
| this.data=(E[]) new Object[maxSize]; | ||
| this.front = DEFAULT_FRONT; | ||
| this.rear = DEFAULT_REAR; | ||
| } | ||
|
|
||
| @Override | ||
| public void insert(E value) { | ||
| if (rear==data.length-1) | ||
| rear=DEFAULT_REAR; | ||
| size++; | ||
| data[++rear]=value; | ||
| } | ||
|
|
||
| @Override | ||
| public E remove() { | ||
| E value = data[front++]; | ||
| if (front==data.length) | ||
| front=DEFAULT_FRONT; | ||
| size--; | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public E peek() { | ||
| return data[front]; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
| return size==0; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isFull() { | ||
| return size==data.length; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return size; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package queue; | ||
|
|
||
| public class QueuePriority<E extends Object & Comparable<? super E>> extends QueueImplement<E> { | ||
|
|
||
| public QueuePriority(int maxSize) { | ||
| super(maxSize); | ||
| this.data = (E[]) new Object[maxSize]; | ||
| } | ||
|
|
||
| @Override | ||
| public void insert(E value) { | ||
| if (isEmpty()){ | ||
| data[size++]=value; | ||
| return; | ||
| } | ||
| int index; | ||
| for (index=size-1;index>=0;index--){ | ||
| if (value.compareTo(data[index])>0) | ||
| data[index+1]=data[index]; | ||
| else | ||
| break;; | ||
| } | ||
| data[index+1]=value; | ||
| size++; | ||
| } | ||
|
|
||
| @Override | ||
| public E remove() { | ||
| return data[--size]; | ||
| } | ||
|
|
||
| @Override | ||
| public E peek() { | ||
| return data[size-1]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package stack; | ||
|
|
||
| public class MainApp { | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| String str = "public static void main(String[] args)"; | ||
| Shifter shifter = new Shifter(); | ||
| System.out.println(str); | ||
| System.out.println(shifter.shift(str)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package stack; | ||
|
|
||
| public class Shifter { | ||
|
|
||
| public String shift(String inputStr){ | ||
| Stack<Character> stack = new StackImplement<Character>(inputStr.length()); | ||
| for (int i = 0; i < inputStr.length(); i++) { | ||
| stack.push(inputStr.charAt(i)); | ||
| } | ||
| StringBuilder outputStr = new StringBuilder(); | ||
| while (!stack.isEmpty()) | ||
| outputStr.append(stack.pop()); | ||
| return outputStr.toString(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package stack; | ||
|
|
||
| public interface Stack<E> { | ||
|
|
||
| void push(E value); | ||
| E pop(); | ||
| E peek(); | ||
|
|
||
| boolean isEmpty(); | ||
| boolean isFool(); | ||
| int size(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package stack; | ||
|
|
||
| public class StackImplement<E> implements Stack<E> { | ||
| private E[] data; | ||
| private int size; | ||
|
|
||
| public StackImplement(int maxSize) { | ||
| this.size=0; | ||
| this.data = (E[]) new Object[maxSize]; | ||
| } | ||
|
|
||
| @Override | ||
| public void push(E value) { | ||
| data[size++]=value; | ||
| } | ||
|
|
||
| @Override | ||
| public E pop() { | ||
| return data[--size]; | ||
| } | ||
|
|
||
| @Override | ||
| public E peek() { | ||
| return data[size-1]; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
| return size==0; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isFool() { | ||
| return size==data.length; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return size; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
у вас идет дублирование констант и полей из QueueImplement, поэтому методы isEmpty и т.д. методы не работали корректно. Нужно переиспользовать все поля из родительского класса.