-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListOfStrings.cs
150 lines (135 loc) · 4.37 KB
/
ListOfStrings.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
*
* April 1, 2013
*
* Without using any of the .NET Collection or Linq libraries (i.e. without using List<string>),
* implement a ListOfStrings object that contains a set of strings.
* This object has the following methods:
* AddAt(int) - inserts a string at a given index
* RemoveAt(int) - given an index, removes the string item at that index
* GetItemAt(int) - given an index, retrieves the string item at that index
* Length - Number of items in the list
*
*/
namespace ListOfStrings
{
class ListOfStrings
{
private Node headNode;
public bool AddAt(string newEntry, int position)
{
Node tmp = headNode;
if (position >= 0)
{
Node newNode = new Node(newEntry);
Node tmp2 = new Node();
if (isEmpty() || position == 0) // adding Node at the begining of the List
{
newNode.setNextNode(tmp);
headNode = newNode;
return true;
}
else if (position == Length()) // adding Node at the end of the List
{
for (int i = 0; i < (position - 1); i++)
{
tmp = tmp.getNextNode();
}
tmp.setNextNode(newNode);
return true;
}
else // inserting node between first and last node
{
for (int i = -1; i < (position - 1) & tmp != null; i++)
{
if (i == (position - 2))
{
tmp2 = tmp;
}
tmp = tmp.getNextNode();
}
if (tmp == null) // or if (position > Length())
{
return false;
}
newNode.setNextNode(tmp);
tmp2.setNextNode(newNode);
return true;
}
}
else
{
return false;
}
}
public bool AddAt(string newEntry)
{
return AddAt(newEntry, Length() + 1);
}
public bool isEmpty()
{
return (headNode == null);
}
public string RemoveAt(int position)
{
string tmpObject = null;
if (isEmpty() || position < 0)
{
return null;
}
Node currentNode = headNode;
if (position == 0)
{
tmpObject = currentNode.getData();
headNode = currentNode.getNextNode();
}
else
{
for (int i = -1; i < position - 2 && currentNode != null; i++)
{
currentNode = currentNode.getNextNode();
}
if (currentNode == null) // or if (position > Length())
{
return null;
}
tmpObject = currentNode.getNextNode().getData();
currentNode.setNextNode(currentNode.getNextNode().getNextNode());
}
return tmpObject;
}
public string GetItemAt(int position)
{
if (isEmpty() || position < 0)
{
return null;
}
Node currentNode = headNode;
string temObject = currentNode.getData();
for (int i = -1; i < position - 1 && currentNode != null; i++)
{
currentNode = currentNode.getNextNode();
}
if (currentNode == null)
{
return null;
}
return currentNode.getData();
}
public int Length()
{
Node currentNode = headNode;
int count = 0;
while (currentNode != null)
{
count++;
currentNode = currentNode.getNextNode();
}
return count;
}
}
}