-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cs
153 lines (132 loc) · 4.63 KB
/
LinkedList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Text;
namespace Scrabble.Game_Logic {
class LinkedList {
public Node Head { get; set; }
public LinkedList() {
Head = new Node();
}
//functions needed
// insert order
// clear
/// <summary>
///
/// Insert Order
///
/// Inserts tiles in order by which they are played on the board
/// IE: from left to right, top to bottom
///
/// </summary>
/// <param name="tile"> Tile of play </param>
/// <param name="row"> Row of Play </param>
/// <param name="col"> Col of Play </param>
/// <returns>
///
/// True if the tile is allowed to be inserted
/// False if it is not within row or col
///
/// </returns>
public bool insertOrder(Tile tile, int row, int col) {
Node newNode = new Node(tile, row, col);
// if head.next is null then the list is empty
if(Head.next == null) {
Head.next = newNode;
newNode.prev = Head;
return true;
}
else {
// temp used to iterate
for(Node temp = Head; temp.next != null; temp = temp.next) {
if(row < temp.next.row && col == temp.next.col || row == temp.next.row && col < temp.next.col) {
newNode.next = temp.next;
newNode.prev = temp.next.prev;
newNode.next.prev = newNode;
newNode.prev.next = newNode;
return true;
}
}
return insertLast(tile, row, col);
}
}
/// <summary>
///
/// Insert Last
///
/// Called by Insert Order
/// Only called when tile is inserted at the end of list
///
/// IE: furthest right or down cell on grid
///
/// </summary>
///
///
/// <param name="tile"> Tile Data </param>
/// <param name="row"> Row of play </param>
/// <param name="col"> Col of play </param>
/// <returns>
///
/// True if the tile is allowed to be inserted
/// False if it is not within row or col
///
/// </returns>
public bool insertLast(Tile tile, int row, int col) {
Node newNode = new Node(tile, row, col);
// if head.next is null then the list is empty
if (Head.next == null) {
Head.next = newNode;
newNode.prev = Head;
return true;
}
else {
Node current = new Node();
current.next = Head.next;
while(current.next.next != null) {
current = current.next;
}
if(current.next.row < row && current.next.col == col || current.next.row == row && current.next.col < col) {
current.next.next = newNode;
newNode.prev = current.next;
newNode.next = null;
return true;
}
return false;
}
}
public string toWord() {
string LL = "";
for (Node test = Head.next; test != null; test = test.next) {
LL += test.data;
}
return LL;
}
// Helper obj for LL
// LL is comprised of Nodes
public class Node {
public Node next { get; set; }
public Node prev { get; set; }
public Tile data { get; set; }
public int row { get; set; }
public int col { get; set; }
public Node() {
next = null;
prev = null;
row = -1;
col = -1;
}
public Node(Tile data, int row, int col) {
this.data = data;
this.row = row;
this.col = col;
}
}
// Testing purposes, shows LL
public override string ToString() {
string LL = "head -> ";
for(Node test = Head.next; test != null; test = test.next) {
LL += test.data + " -> ";
}
return LL;
}
}
}