Esempio n. 1
0
 public void pop() { // pops the data on the top of the stack //topand pop
   if (theList.isEmpty()) {
     System.out.println("sorry but the stack is empty");
   } else {
     theList.deleteFirst();
   }
 }
 // to generate adjacent moves of the black pieces
 public LinkList generateMove_Black(String board, LinkList LL1) {
   String b;
   int j;
   LL1.first = null;
   int[] n = null;
   for (int i = 0; i < board.length(); i++) {
     if (board.charAt(i) == 'B') {
       n = neighbors(i);
       for (int k = 0; k < n.length; k++) {
         j = n[k];
         if (board.charAt(j) == 'x') {
           b = board;
           b = b.substring(0, i) + 'x' + b.substring(i + 1);
           b = b.substring(0, j) + 'B' + b.substring(j + 1);
           if (closeMill(j, b)) {
             LL1 = generateRemove_Black(b, LL1);
           } else {
             LL1.insert(b);
           }
         }
       }
     }
   }
   return LL1;
 }
Esempio n. 3
0
  private void route(JsonObject message, int fromNode) {
    String text = message.getString("text", null);
    int from = message.getInt("from", -1);
    int to = message.getInt("to", -1);

    if (fromNode == from && to >= 0 && text != null) {
      SwingUtilities.invokeLater(
          () -> {
            messages.addPacket(from, to, text, getByNode(from).getNetwork());
          });
      if (to == 0) {
        for (int i = 0; i < connections.size(); i++) {
          Connection c = connections.get(i);
          if (c != null && links.isNeighbour(fromNode, c.getNode())) {
            send(c, text, from, to);
          }
        }
      } else {
        Connection c = getByNode(to);
        if (c != null && links.isNeighbour(from, to)) {
          send(c, text, from, to);
        }
      }
    }
  }
Esempio n. 4
0
  @Override
  /**
   * ******************************************************************* Method that gets mixed
   * message and the file to unmix the message
   *
   * @param filename file that contains the undo commands to revert message to original
   * @param userMessage mixed message that is returned from the mix program
   * @return unmix original message before mixing it up
   *     ******************************************************************
   */
  public String UnMixUsingFile(String filename, String userMessage) {
    if (userMessage.trim().equals("")) {
      throw new IllegalArgumentException();
    }

    String str;
    String unmix = userMessage;
    Mix mix = new Mix();

    // calls the mix command and then sets the unmix message
    // provided as the usermessage on mix class -> pretty much did
    // the same thing so I guessed I could use this for easier process.
    mix.setInitialMessage(userMessage);

    // sets the filename output extension if it was not provided
    if (!filename.contains(".txt")) {
      filename = filename + ".txt";
    }

    try {
      // open the data file
      Scanner fileReader = new Scanner(new File(filename));
      LinkList<String> commands = new LinkList<String>();

      // goes through the file and adds it to the linked if it hasNextLine()
      while (fileReader.hasNextLine()) {
        str = fileReader.nextLine();
        commands.addfirst(str);
      }

      // goes through the linked list and process each
      // command to unmix the commands
      for (int i = 0; i < commands.count(); i++) {
        String s = commands.readList(i).getData();
        unmix = mix.processCommand(s);
      }

      return unmix;
    }
    // could not find file
    catch (FileNotFoundException error) {
      throw new IllegalArgumentException();
    }

    // problem reading the file
    catch (Exception error) {
      throw error;
    }
  }
Esempio n. 5
0
 private void addLink(int from, int to, String network) {
   int nodeA = connections.get(from).getNode();
   int nodeB = connections.get(to).getNode();
   System.out.println("Create link between " + nodeA + " and " + nodeB);
   if (!links.isNeighbour(nodeA, nodeB)) {
     links.addElement(new Link(nodeA, nodeB, network));
   }
 }
  public static void main(String args[]) {
    LinkList ll = new LinkList();
    Node head = null;
    head = ll.addNode(9, head);
    head = ll.addNode(4, head);

    Node head1 = null;
    head1 = ll.addNode(3, head1);
    head1 = ll.addNode(1, head1);
    head1 = ll.addNode(2, head1);

    AddNumberRepresentedByLinkList anr = new AddNumberRepresentedByLinkList();
    Node result = anr.add(head, head1);
    ll.printList(result);
  }
 // to remove white pieces from the board
 private LinkList generateRemove_Black(String board, LinkList LL2) {
   String b;
   int count = 0;
   for (int k = 0; k < board.length(); k++) {
     if (board.charAt(k) == 'W') {
       if (!closeMill(k, board)) {
         count++;
         b = board;
         b = b.substring(0, k) + 'x' + b.substring(k + 1);
         LL2.insert(b);
       }
     }
   }
   if (count == 0) LL2.insert(board);
   return LL2;
 }
  private void generateMidgameEndgame(String in_file, String out_file) {
    // TODO Auto-generated method stub
    // code to read the file
    try {
      // Open the file that is the first
      // command line parameter
      FileInputStream fstream = new FileInputStream(in_file);
      // Get the object of DataInputStream
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String board;
      // Read the first Line
      board = br.readLine();
      L.insert(board);
      // Print the content on the console
      System.out.println("The input board position is:");
      System.out.println(board);
      // Close the input stream
      in.close();
      fstream.close();

    } catch (Exception e) { // Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
Esempio n. 9
0
 private void send(Connection c, String message, int fromNode, int toNode) {
   int drop = rand.nextInt(100);
   if (drop >= links.getDropRate() || links.getDropRate() == 0) {
     String content = message;
     // TODO Change to only allowed matched responses.
     boolean shouldSend =
         (links.getOffset() == 0)
             || (!links.isCheckwhois())
             || ((content.length() > 12)
                 && (content.substring(0, 12).equals("WHOIS(Query,")
                     || content.subSequence(0, 13).equals("WHOIS(Answer,")));
     if (shouldSend) {
       // Corruption
       int corr = rand.nextInt(100);
       if (corr < links.getCorruptionRate()) {
         content = Texts.corrupt(content);
       }
       // Network delay
       int delay = (links.getDelay() > 0) ? rand.nextInt(links.getDelay()) : 0;
       synchronized (queue) {
         queue.add(new PacketMessage(c, delay, fromNode, toNode, content));
       }
     }
   }
 }
Esempio n. 10
0
  public static void main(String[] args) {
    LinkList theList = new LinkList();

    theList.insertFirst(22, 2.99);
    theList.insertFirst(44, 4.99);
    theList.insertFirst(66, 6.99);
    theList.insertFirst(88, 8.99);

    theList.displayList();

    Link f = theList.find(88);
    if (f != null) System.out.println("Found link with key " + f.iData);
    else System.out.println("Can't find link");

    Link d = theList.delete(88);

    if (d != null) System.out.println("Deleted link with key " + d.iData);
    else System.out.println("Can't delete link");

    d = theList.delete(22);

    if (d != null) System.out.println("Deleted link with key " + d.iData);
    else System.out.println("Can't delete link");

    d = theList.delete(44);

    if (d != null) System.out.println("Deleted link with key " + d.iData);
    else System.out.println("Can't delete link");

    d = theList.delete(66);

    if (d != null) System.out.println("Deleted link with key " + d.iData);
    else System.out.println("Can't delete link");

    // while(!theList.isEmpty()) {
    // 	Link aLink = theList.deleteFirst();
    // 	System.out.print("Deleted ");
    // 	aLink.displayLink();
    // 	System.out.println("");
    // }

    theList.displayList();
  }
 // to generate hopping moves for black pieces
 public LinkList generateHopping_Black(String board, LinkList LL1) {
   String b;
   LL1.first = null;
   for (int i = 0; i < board.length(); i++) {
     if (board.charAt(i) == 'B') {
       for (int j = 0; j < board.length(); j++) {
         if (board.charAt(j) == 'x') {
           b = board;
           b = b.substring(0, i) + 'x' + b.substring(i + 1);
           b = b.substring(0, j) + 'B' + b.substring(j + 1);
           if (closeMill(j, b)) {
             LL1 = generateRemove_Black(b, LL1);
           } else {
             LL1.insert(b);
           }
         }
       }
     }
   }
   return LL1;
 }
 public Node add(Node head1, Node head2) {
   if (head1 == null || head2 == null) {
     throw new IllegalArgumentException();
   }
   LinkList ll = new LinkList();
   int size1 = ll.size(head1);
   int size2 = ll.size(head2);
   Node larger = null;
   Node smaller = null;
   if (size1 >= size2) {
     larger = head1;
     smaller = head2;
   } else {
     larger = head2;
     smaller = head1;
   }
   int diff = Math.abs(size1 - size2);
   Node largerStart = larger;
   while (diff > 0) {
     largerStart = largerStart.next;
     diff--;
   }
   Node result = addWithCarry(largerStart, smaller);
   Node result1 = addRemaining(larger, largerStart);
   if (carry != 0) {
     Node top = Node.newNode(carry);
     result1 = ll.addAtFront(top, result1);
   }
   if (result1 != null) {
     Node tail = result1;
     while (tail.next != null) {
       tail = tail.next;
     }
     tail.next = result;
     return result1;
   }
   return result;
 }
  public static void main(String args[]) {
    LinkList ll = new LinkList();
    Node head = null;
    head = ll.addNode(1, head);
    head = ll.addNode(2, head);
    head = ll.addNode(3, head);
    head = ll.addNode(7, head);
    head = ll.addNode(5, head);
    head = ll.addNode(6, head);
    head = ll.addNode(13, head);
    head = ll.addNode(11, head);
    head = ll.addNode(12, head);

    SortNearlySortedList sns = new SortNearlySortedList();
    head = sns.sort(head);
    ll.printList(head);
  }
 public static void main(String args[]) {
   DeleteNodeWithGreaterValueOnRight dng = new DeleteNodeWithGreaterValueOnRight();
   LinkList ll = new LinkList();
   Node head = null;
   head = ll.addNode(12, head);
   head = ll.addNode(15, head);
   head = ll.addNode(10, head);
   head = ll.addNode(11, head);
   head = ll.addNode(5, head);
   head = ll.addNode(6, head);
   head = ll.addNode(2, head);
   head = ll.addNode(3, head);
   head = dng.deleteNodes(head);
   ll.printList(head);
 }
Esempio n. 15
0
  // This method can only be executed in the EDT so is safe from Connections
  // updates
  public void updateStatus() {
    // The first update triggers network building.
    if (!started) {
      started = true;
      int maxNets = Math.max(1, connections.size() / MIN_PER_NET);
      int numNets = Math.min((connections.size() + MAX_PER_NET - 1) / MAX_PER_NET, maxNets);
      createCycles(numNets);
      // Begin to check for messages.
      synchronized (queue) {
        this.start();
      }
    }
    int l = connections.size();
    List<String> texts = null;
    if (links.getOffset() != 0 && !links.isCheckwhois()) {
      texts = new ArrayList<String>();
      Texts.choose_messages(texts, l, links.getCorruptionRate() > 0);
    }
    // mark any current messages out of date
    startTime = System.currentTimeMillis();

    synchronized (queue) {
      for (int i = 0; i < l; i++) {
        Connection c = connections.get(i);
        if (c != null) {
          if (links.getOffset() == 0) {
            queue.add(new TaskMessage(c, -1, links));
          } else {
            int recipient = cycles.get(c.getNetwork()).offsetNode(c.getNode(), links.getOffset());
            System.out.println("Node:" + c.getNode() + "sending to: " + recipient);
            if (links.isCheckwhois()) {
              String unknown = connections.get(nodeToIndex(recipient)).getHostname();
              queue.add(new TaskMessage(c, -1, links, unknown));
            } else {
              queue.add(new TaskMessage(c, -1, links, recipient, texts.get(i)));
            }
          }
        }
      }
    }
  }
Esempio n. 16
0
 public T pop() {
   return linkList.removeFirst();
 }
Esempio n. 17
0
 // --------------------------------------------------------------
 public void displayStack() {
   System.out.print("Stack (top-->bottom): ");
   theList.displayList();
 }
Esempio n. 18
0
 // --------------------------------------------------------------
 public boolean isEmpty() // true if stack is empty
     {
   return (theList.isEmpty());
 }
Esempio n. 19
0
 // --------------------------------------------------------------
 public long pop() // take item from top of stack
     {
   return theList.deleteFirst();
 }
Esempio n. 20
0
 // --------------------------------------------------------------
 public void push(long j) // put item on top of stack
     {
   theList.insertFirst(j);
 }
 void addNode(LinkList l, int data) {
   while (l.next != null) {
     l = l.next;
   }
   l.next = new LinkList(data);
 }
Esempio n. 22
0
 public String peek() {
   return theList.peek(); // checks the top of the list
 }
 public boolean isEmpty() {
   return (theList.isEmpty());
 }
Esempio n. 24
0
 public boolean isEmpty() { // checks to see of the list is empty
   return theList.isEmpty();
 }
Esempio n. 25
0
 public boolean isEmpty() {
   return linkList.isEmpty();
 }
Esempio n. 26
0
 public void push(String thisPage) {
   theList.insertHead(thisPage); // inserts the newdata at the top of the stack
 }
Esempio n. 27
0
 @Override
 public String toString() {
   return linkList.toString();
 }
Esempio n. 28
0
 public String toString() { // converts the list into all strings
   return theList.toString();
 }
Esempio n. 29
0
 public void push(T data) {
   linkList.addFirst(data);
 }