Advertisement
Guest User

node.cpp

a guest
Mar 29th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. //
  2. //  node.cpp
  3. //  EightPuzzle
  4. //
  5. //  Created by 李依蓉 on 2020/3/19.
  6. //  Copyright © 2020 李依蓉. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <iostream>
  11. #include "node.h"
  12.  
  13. using namespace std;
  14.  
  15. void Node::displayNodeStatus()
  16. {
  17.     for(int i = 0; i < 9; i++)
  18.     {
  19.         cout << this->status[i] << "," ;
  20.     }
  21.     cout << endl;
  22. }
  23.  
  24. void Fringe::InsertNode(Node x)
  25. {
  26.     int status_temp[9];
  27.     for(int i = 0; i < 9; i++)
  28.         status_temp[i] = x.status[i];
  29.     Node *newNode = new Node(status_temp);   // 配置新的記憶體
  30.  
  31.     if (first == NULL) {                      // 若list沒有node, 令newNode為first
  32.         first = newNode;
  33.         return;
  34.     }
  35.  
  36.     Node *current = first;
  37.     while (current->child != 0) {           // Traversal
  38.         current = current->child;
  39.     }
  40.     current->child = newNode;               // 將newNode接在list的尾巴
  41. }
  42.  
  43. void Fringe::PrintFringe()
  44. {
  45.     if (first == NULL) {                      // 如果first node指向NULL, 表示list沒有資料
  46.         cout << "List is empty.\n";
  47.         return;
  48.     }
  49.  
  50.     Node *current = first;             // 用pointer *current在list中移動
  51.     while (current != NULL) {                 // Traversal
  52.         for(int i = 0; i < 9; i++)
  53.             cout << current->status[i] << ",";
  54.         cout << endl;
  55.         current = current->child;
  56.     }
  57.     cout << endl;
  58. }
  59.  
  60. void Node::getStatus(int (&arr)[9])
  61. {
  62.     copy(begin(this->status), end(this->status), begin(arr));
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement