Skip to content

Commit 0cdd5d4

Browse files
ClémentClément
authored andcommitted
Added notes on queue and refined notes on stacks.
1 parent e37e150 commit 0cdd5d4

4 files changed

Lines changed: 147 additions & 4 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System; // This is required for the exception.
2+
3+
class CQueue<T>
4+
{
5+
private int front,
6+
end,
7+
size;
8+
private T[] mArray;
9+
10+
public CQueue(int capacity = 10)
11+
{
12+
mArray = new T[capacity];
13+
// Note that front, end and size
14+
// are set to 0.
15+
}
16+
17+
public void Clear()
18+
{
19+
front = 0;
20+
end = 0;
21+
size = 0;
22+
}
23+
24+
public bool IsEmpty()
25+
{
26+
return size == 0;
27+
}
28+
29+
public bool IsFull()
30+
{
31+
return size == mArray.Length;
32+
}
33+
34+
public void Enqueue(T newItem)
35+
{
36+
if (!IsFull())
37+
{
38+
mArray[end] = newItem;
39+
Increment(ref end);
40+
size++;
41+
}
42+
else
43+
Resize();
44+
}
45+
46+
public T Dequeue()
47+
{
48+
size--;
49+
T data = mArray[front];
50+
Increment(ref front);
51+
return data;
52+
}
53+
54+
public void Resize()
55+
{
56+
throw new Exception(
57+
"Queue Resize is not implemented - you could implement it"
58+
);
59+
}
60+
61+
// Increment must be done carefully:
62+
// what if we reached the "end of mArray"
63+
// but there is room available at the
64+
// beginning of the queue? Then the value
65+
// needs to become 0.
66+
private void Increment(ref int value)
67+
{
68+
value++;
69+
if (value == mArray.Length)
70+
{
71+
value = 0;
72+
}
73+
}
74+
75+
public int Capacity
76+
{
77+
get { return mArray.Length - size; }
78+
}
79+
80+
public override string ToString()
81+
{
82+
string returned = "";
83+
returned +=
84+
$"Front : {front}, end : {end}, size : {size}, capacity: {Capacity}\n";
85+
for (int i = front; i < size + front; i++)
86+
{
87+
returned += mArray[i % mArray.Length] + ":";
88+
}
89+
return returned;
90+
}
91+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
/* First example. */
8+
CQueue<int> myQueue1 = new CQueue<int>();
9+
Console.WriteLine(myQueue1);
10+
myQueue1.Enqueue(1);
11+
myQueue1.Enqueue(2);
12+
myQueue1.Enqueue(3);
13+
myQueue1.Enqueue(4);
14+
myQueue1.Enqueue(5);
15+
Console.WriteLine(myQueue1);
16+
Console.WriteLine(
17+
"Value dequeued: " + myQueue1.Dequeue()
18+
);
19+
Console.WriteLine(myQueue1);
20+
21+
myQueue1.Enqueue(6);
22+
myQueue1.Enqueue(7);
23+
myQueue1.Enqueue(8);
24+
myQueue1.Enqueue(9);
25+
myQueue1.Enqueue(10);
26+
Console.WriteLine(myQueue1);
27+
28+
Console.WriteLine(
29+
"Value dequeued: " + myQueue1.Dequeue()
30+
);
31+
Console.WriteLine(myQueue1);
32+
Console.WriteLine(
33+
"Value dequeued: " + myQueue1.Dequeue()
34+
);
35+
Console.WriteLine(myQueue1);
36+
myQueue1.Enqueue(11);
37+
myQueue1.Enqueue(12);
38+
myQueue1.Enqueue(13);
39+
40+
try
41+
{
42+
myQueue1.Enqueue(14);
43+
}
44+
catch (Exception ex)
45+
{
46+
Console.WriteLine(ex.Message);
47+
}
48+
Console.WriteLine(myQueue1);
49+
}
50+
}

source/lectures/data/stack.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ The fact that it may contain the same element multiple times makes it different
1919

2020
Generally, it has operations to…
2121

22-
- … create an empty list,
22+
- … create an empty stack,
2323
- … test for emptiness,
24-
- … obtain the value of the element at "the top" of the stak ("peek"),
24+
- … obtain the value of the element at "the top" of the stack ("peek"),
2525
- … add an element at "the top" of the stack ("push"),
2626
- … remove an element at "the top" of the stack ("pop").
2727

@@ -30,15 +30,15 @@ Exactly like a stack of plates, stacks implement a "last in, first out" (LIFO) p
3030

3131
### Difference with array
3232

33-
Like lists, the `Stack` class serves a similar purpose than arrays, but with a few notable differences:
33+
Like lists, stacks serve a similar purpose to arrays, but with a few notable differences:
3434

3535
- Stacks do not need to have a number of elements fixed ahead of time,
3636
- Stacks automatically expand when elements are added,
3737
- Stacks automatically shrink when elements are removed.
3838

3939
### Difference with lists
4040

41-
However, the `Stack` class is also different from lists:
41+
However, stacks have difference with lists:
4242

4343
- Only the top element's value can be read (accessed),
4444
- Adding and removing can only be done "on the right side", that is, at the top of the stack.

source/order

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
./lectures/data/CList.md
6363
./lectures/data/CList_as_ICollection.md
6464
./lectures/data/DLList.md
65+
./lectures/data/stack.md
66+
./lectures/data/queue.md
6567
./lectures/misc/
6668
./lectures/misc/over_under_flow.md
6769
./lectures/misc/random.md

0 commit comments

Comments
 (0)