Created
December 6, 2023 20:25
-
-
Save whizyrel/9ceab6074b604006749c3bea889e2db9 to your computer and use it in GitHub Desktop.
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
| package org.evryword; | |
| import java.util.Objects; | |
| public class Cell { | |
| public char content = ' '; | |
| public Cell next; | |
| public Cell prev; | |
| @Override | |
| public boolean equals(Object o) { | |
| if (this == o) return true; | |
| if (o == null || getClass() != o.getClass()) return false; | |
| Cell cell = (Cell) o; | |
| return content == cell.content && Objects.equals(next, cell.next) && Objects.equals(prev, cell.prev); | |
| } | |
| @Override | |
| public int hashCode() { | |
| return Objects.hash(content, next, prev); | |
| } | |
| } |
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
| package org.evryword; | |
| public class Main { | |
| public static void main(String[] args) { | |
| // INFO START A TAPE | |
| Tape myTape = new Tape(); | |
| myTape.setContent('a'); | |
| myTape.moveLeft(); | |
| myTape.setContent('b'); | |
| myTape.moveLeft(); | |
| myTape.setContent('c'); | |
| myTape.moveLeft(); | |
| myTape.setContent('d'); | |
| myTape.moveRight(); | |
| myTape.setContent('9'); | |
| myTape.moveRight(); | |
| myTape.setContent('8'); | |
| myTape.moveRight(); | |
| myTape.setContent('7'); | |
| System.out.println("[getTail] " + myTape.getTail(myTape.getCurrentCell()).content); | |
| System.out.println("[getHead] " + myTape.getHead(myTape.getCurrentCell()).content); | |
| String tapeContents = myTape.getTapeContents(); | |
| System.out.println("[tapeContents] " + tapeContents); | |
| } | |
| } |
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
| package org.evryword; | |
| public class Tape { | |
| public Cell currentCellPointer; | |
| public Tape() { | |
| final Cell newCell = new Cell(); | |
| currentCellPointer = newCell; | |
| } | |
| public Cell getCurrentCell() { | |
| return currentCellPointer; | |
| } | |
| public char getContent() { | |
| return currentCellPointer.content; | |
| } | |
| public void setContent(char ch) { | |
| currentCellPointer.content = ch; | |
| } | |
| public void moveLeft() { | |
| // INFO Traverse to get the size of the list | |
| final Cell cell = new Cell(); | |
| final Cell tail = getTail(currentCellPointer); | |
| currentCellPointer.next = cell; | |
| cell.prev = tail; | |
| currentCellPointer = cell; | |
| } | |
| public Cell getTail(Cell cell) { | |
| return cell.next == null ? cell : getTail(cell.next); | |
| } | |
| public Cell getHead(Cell cell) { | |
| return cell.prev == null ? cell : getHead(cell.prev); | |
| } | |
| public void moveRight() { | |
| // INFO If cell is rightmost, create new cell and added to tape at the right of the tape | |
| // INFO Find last cell | |
| final Cell cell = new Cell(); | |
| final Cell head = getHead(currentCellPointer); | |
| currentCellPointer.prev = cell; | |
| cell.next = head; | |
| currentCellPointer = cell; | |
| } | |
| public String getTapeContents() { | |
| Cell currentCell = getHead(currentCellPointer); | |
| // INFO Traverse list to get all contents | |
| return readTapeContents(currentCell); | |
| } | |
| // INFO a -> b -> c -> d -> e | |
| public String readTapeContents(Cell cell) { | |
| if (cell == null) return ""; | |
| return cell.content + readTapeContents(cell.next); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment