Skip to content

Commit 5c430aa

Browse files
committed
Start implementing Stack
1 parent c467165 commit 5c430aa

File tree

7 files changed

+139
-8
lines changed

7 files changed

+139
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# pure-data-structures-and-algorithms-csharp
22
Data structures and algorithms implementation in pure csharp
3+
4+
Data structures that have been implemented so far:
5+
- Arrays Introduction
6+
- Array List
7+
- List List
8+
- Singly Linked List
9+
- Doubly Linked List

TODO.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Here is Data Structures base on https://en.wikipedia.org/wiki/List_of_data_struc
1818
- [ ] Queue (example Priority queue)
1919
- [ ] Double-ended queue
2020
- [ ] Graph (example Tree, Heap)
21+
- [ ] Hash Table
22+
- [ ] Dictionary
2123

2224
### Linear
2325
- [ ] Array
@@ -42,13 +44,13 @@ Here is Data Structures base on https://en.wikipedia.org/wiki/List_of_data_struc
4244
- [ ] Variable-length array
4345
- [ ] List
4446
- [x] Singly Linked List
45-
- [ ] Doubly Linked List
47+
- [x] Doubly Linked List
4648
- [ ] Circular Linked List
4749
- [ ] Doubly Circular Linked List
4850
- [ ] header Linked List
4951
- [ ] Doubly linked list
5052
- [x] Array list
51-
- [ ] Linked list
53+
- [x] Linked list
5254
- [ ] Association list
5355
- [ ] Self-organizing list
5456
- [ ] Skip list

src/ConsoleApp/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//ArrayUsage();
55
//ArrayListUsage();
66
//ListUsage();
7-
//SinglyLinkedListUsage();
7+
SinglyLinkedListUsage();
88
DoublyLinkedListUsage();
99

1010
void ArrayUsage()
@@ -49,6 +49,7 @@ void SinglyLinkedListUsage()
4949
Console.WriteLine(i);
5050
}
5151

52+
var last = ll[2];
5253
ll.Remove(1);
5354
}
5455

src/DataStructures/AbstractDataTypes/Lists/NikDoublyLinkedList.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ public void Remove(int index)
131131

132132
_count--;
133133
}
134+
135+
public T this[int index]
136+
{
137+
get
138+
{
139+
if (_head is null || index < 0 || index > _count)
140+
throw new ArgumentOutOfRangeException(nameof(index), "Out of range");
141+
142+
var i = 0;
143+
var current = _head;
144+
while (i != index && current is not null)
145+
{
146+
current = current.Next;
147+
148+
i++;
149+
}
150+
151+
return current!.Value;
152+
}
153+
}
134154

135155
public bool RemoveValue(T value)
136156
{

src/DataStructures/AbstractDataTypes/Lists/NikList.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ public class NikList<T> : IEnumerable<T>
1111
private int _capacity;
1212
private readonly T[] _emptyArray = Array.Empty<T>();
1313

14-
public void CopyTo(Array array, int index)
15-
{
16-
throw new NotImplementedException();
17-
}
18-
1914
public int Count => _size;
2015

2116
public int Capacity => _capacity;

src/DataStructures/AbstractDataTypes/Lists/NikSinglyLinkedList.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ public void Remove(int index)
8888

8989
_count--;
9090
}
91+
92+
public T this[int index]
93+
{
94+
get
95+
{
96+
if (_head is null || index < 0 || index > _count)
97+
throw new ArgumentOutOfRangeException(nameof(index), "Out of range");
98+
99+
var i = 0;
100+
var current = _head;
101+
while (i != index && current is not null)
102+
{
103+
current = current.Next;
104+
105+
i++;
106+
}
107+
108+
return current!.Value;
109+
}
110+
}
91111

92112
public bool RemoveValue(T value)
93113
{
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace DataStructures.AbstractDataTypes.Stacks;
2+
3+
public class NikStack<T>
4+
{
5+
private T[] _items;
6+
private int _top;
7+
private int _capacity;
8+
private readonly T[] _emptyArray = Array.Empty<T>();
9+
10+
public int Count => _size;
11+
12+
public int Capacity => _capacity;
13+
14+
public NikStack()
15+
{
16+
_items = _emptyArray;
17+
_capacity = 0;
18+
}
19+
20+
public NikStack(int capacity)
21+
{
22+
if (capacity < 0)
23+
throw new ArgumentOutOfRangeException(nameof(capacity), "Should be non negative");
24+
25+
_items = capacity == 0 ? _emptyArray : new T[capacity];
26+
27+
_capacity = capacity;
28+
}
29+
30+
private void CheckCapacity(int size)
31+
{
32+
if (_items.Length >= size)
33+
return;
34+
int newCapacity = _items.Length == 0 ? 4 : _items.Length * 2;
35+
if ((uint) newCapacity > Array.MaxLength) newCapacity = Array.MaxLength;
36+
if (newCapacity < size) newCapacity = size;
37+
38+
ChangeCapacity(newCapacity);
39+
}
40+
41+
private void ChangeCapacity(int newCapacity)
42+
{
43+
if (newCapacity < -_size)
44+
throw new ArgumentOutOfRangeException(nameof(newCapacity), "Out of range");
45+
46+
if (newCapacity == _capacity)
47+
return;
48+
49+
if (newCapacity > 0)
50+
{
51+
var newItems = new T[newCapacity];
52+
53+
if (_size > 0)
54+
{
55+
Array.Copy(_items, 0, newItems, 0, _size);
56+
}
57+
58+
_items = newItems;
59+
}
60+
else
61+
{
62+
_items = _emptyArray;
63+
}
64+
65+
_capacity = newCapacity;
66+
}
67+
68+
public void Push(T value)
69+
{
70+
if (_size == _items.Length) CheckCapacity(_size + 1);
71+
72+
_top++;
73+
_size++;
74+
_items[_top] = value;
75+
}
76+
77+
public T Pop()
78+
{
79+
80+
}
81+
82+
public T Peek()
83+
{
84+
85+
}
86+
}

0 commit comments

Comments
 (0)