Skip to content

Commit d85e48b

Browse files
ClémentClément
authored andcommitted
Adding notes on Stacks.
1 parent c236711 commit d85e48b

3 files changed

Lines changed: 192 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
class CStack<T>
8+
{
9+
10+
private class Cell
11+
{
12+
public T Data { get; set; }
13+
public Cell Next { get; set; }
14+
public Cell(T data = default(T), Cell node = null)
15+
{
16+
Data = data;
17+
Next = node;
18+
}
19+
}
20+
21+
private Cell top;
22+
23+
public CStack()
24+
{
25+
top = null;
26+
}
27+
28+
public void Clear()
29+
{
30+
top = null;
31+
}
32+
public bool IsEmpty()
33+
{
34+
return top == null;
35+
}
36+
37+
public void Push(T value)
38+
{
39+
top = new Cell(value, top);
40+
}
41+
42+
public T Pop()
43+
{
44+
if (IsEmpty())
45+
throw new ApplicationException("An empty stack cannot be popped.");
46+
T removedData = top.Data;
47+
top = top.Next;
48+
return removedData;
49+
}
50+
51+
public T Peek()
52+
{
53+
if (IsEmpty())
54+
throw new ApplicationException("An empty stack cannot be peeked.");
55+
return top.Data;
56+
}
57+
public int Count
58+
{
59+
get
60+
{
61+
int count = 0;
62+
Cell cCell = top;
63+
while (cCell != null)
64+
{
65+
count++;
66+
cCell = cCell.Next;
67+
}
68+
return count;
69+
}
70+
}
71+
72+
public override string ToString()
73+
{
74+
string returned = "";
75+
if (!IsEmpty())
76+
{
77+
Cell cCell = top;
78+
while (cCell != null)
79+
{
80+
if (returned.Length > 0)
81+
returned += ":";
82+
returned += cCell.Data;
83+
cCell = cCell.Next;
84+
}
85+
}
86+
return returned;
87+
}
88+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
/* First example. */
8+
CStack<int> myStack1 = new CStack<int>();
9+
Console.WriteLine(myStack1);
10+
myStack1.Push(1);
11+
myStack1.Push(5);
12+
myStack1.Push(2);
13+
myStack1.Push(2);
14+
myStack1.Push(1);
15+
myStack1.Push(1);
16+
myStack1.Push(3);
17+
myStack1.Push(4);
18+
myStack1.Push(5);
19+
myStack1.Push(5);
20+
myStack1.Push(5);
21+
myStack1.Push(2);
22+
myStack1.Push(2);
23+
Console.WriteLine(myStack1);
24+
25+
/* Second example. */
26+
CStack<char> myStack2 = new CStack<char>();
27+
try
28+
{
29+
myStack2.Pop();
30+
}
31+
catch(Exception ex)
32+
{
33+
Console.WriteLine(
34+
ex.Message
35+
);
36+
}
37+
try
38+
{
39+
myStack2.Peek();
40+
}
41+
catch (Exception ex)
42+
{
43+
Console.WriteLine(
44+
ex.Message
45+
);
46+
}
47+
48+
myStack2.Push('a');
49+
myStack2.Push('z');
50+
Console.WriteLine("Value at top of stack: " + myStack2.Peek());
51+
}
52+
}

source/lectures/data/stack.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
tags:
3+
- datatypes/collections
4+
---
5+
6+
# The Stack collections
7+
8+
## Introduction
9+
10+
### Abstract Data Type
11+
12+
Described [abstractly](./lectures/data/intro#abstract-data-types), [a stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) is
13+
14+
- a finite collection of elements,
15+
- in a particular order,
16+
- that may contain the same element multiple times.
17+
18+
The fact that it may contain the same element multiple times makes it different from a set, the fact that it is ordered makes it different from a [multiset](https://en.wikipedia.org/wiki/Multiset).
19+
20+
Generally, it has operations to…
21+
22+
- … create an empty list,
23+
- … test for emptiness,
24+
- … obtain the value of the element at "the top" of the stak ("peek"),
25+
- … add an element at "the top" of the stack ("push"),
26+
- … remove an element at "the top" of the stack ("pop").
27+
28+
The fact that only the "top element" can be accessed is the main difference with the list abstract data type.
29+
Exactly like a stack of plates, stacks implement a "last in, first out" (LIFO) principle.
30+
31+
### Difference with array
32+
33+
Like lists, the `Stack` class serves a similar purpose than arrays, but with a few notable differences:
34+
35+
- Stacks do not need to have a number of elements fixed ahead of time,
36+
- Stacks automatically expand when elements are added,
37+
- Stacks automatically shrink when elements are removed.
38+
39+
### Difference with lists
40+
41+
However, the `Stack` class is also different from lists:
42+
43+
- Only the top element can be read,
44+
- Adding and removing can only be done "on the right side", that is, at the top of the stack.
45+
46+
## Possible Implementation
47+
48+
Here is a possible implementation of stacks:
49+
50+
```{download="./code/projects/CStack.zip"}
51+
!include code/projects/CStack/CStack/CStack.cs
52+
```

0 commit comments

Comments
 (0)