Recommendation: Take the CS50 Course from Harvard University, or the Free C# Course by BIMTeam on Stepik (by PIK), to build a solid foundation.
True and False (true/false)
In programs we often check conditions. For example: “if the pipe is longer than 10 m — make a joint.” A condition can only be either true or false.
True and False (0 and 1)
Computers don’t understand the words true/false — everything is stored as numbers. So true = 1, and false = 0.
Binary system (base‑2)
All information in a computer is stored as combinations of zeros and ones. For us it’s 0 and 1; for the processor it’s the presence of current (1) or the absence of current (0).
In the end: a programmer writes true/false, but the computer internally works only with 0 and 1.
| |
How binary notation works
In a computer, every number is stored as bits — 0s and 1s. Each position is a power of 2.
Example: the binary number 101
| |
Calculation:
- first 1 = 2^2 = 4
- 0 = skip
- last 1 = 2^0 = 1
Total: 4 + 1 = 5
—
• Each 0 or 1 is a bit.
• 8 bits = 1 byte.
• Memory is addressed in bytes.
ASCII Tables bring meaning to what various bytes represent:
ASCII maps integers to characters so raw 0s and 1s can represent letters, digits, and punctuation.
For example, the number 48 in the ASCII table corresponds to the character ‘0’:
| |
ASCII Table

Pointers and Structs (from C)
Pointersare hexadecimal based values that identify an address in computer memory.- If you write in
C, you need to managepointersand check memory in and out as you work. C#bypasses this using agarbage collector (GC).Pointersandstructsallow us to connect bytes into lists. We can also array objects adjacently.- In
C#, we typically work withclasses/structsandreferences; theCLR (Common Language Runtime)manages memory for us.
What is a reference?
- A reference is not the data itself, but a pointer to where the data lives in memory.
- Think of it like an address: you hold a note with the address, not the apartment itself.
Example in C#:
| |
📊 Breakdown
Struct 1 (First Node):
Object: Data (e.g., 10)Null: No previous node — this is the start of the list.Pointer to next: Points to Struct 2
Struct 2 (Middle Node):
Object: Data (e.g., 20)Pointer to previous: Points back to Struct 1Pointer to next: Points forward to Struct 3
Struct 3 (Last Node):
Object: Data (e.g., 30)Pointer to previous: Points back to Struct 2Null: No next node — this is the end of the list.
🔁 Why use this?
This doubly linked list structure allows you to:
- Move forward and backward through the list,
- Insert or remove elements in the middle more easily than with arrays.
ASCII diagram
| |
A bit more with arrows:
| |
In short: pointers are like arrows between elements. In
Cyou draw and move them yourself; inC#theCLRmanages those references for you.
Code: first C, then C#
To cement the ideas:
- In
Cyou work directly with pointers and manage memory manually (malloc/free). - In
C#you use references and classes; the CLR + GC manages memory.
C
| |
C#
| |
🧠 Insertion and Removal in a Linked List
In a linked list (especially a doubly linked list like the one we just looked at), the two most common operations are. Unlike arrays, you don’t shift or copy elements — you just rewire the arrows (references/pointers).
➕ Insertion
Adding a new node between existing nodes.
For example, inserting a new value between first and second:
Before:
| |
After inserting new node (e.g., 15):
| |
To do this in code:
- Create the new node.
- Update the
nextof the previous node. - Update the
prevof the next node. - Link the new node’s
prevandnext.
➖ Removal
Deleting a node from the list.
For example, removing second:
Before:
| |
After removal:
| |
To do this in code:
- Set
first.next = third. - Set
third.prev = first. - (In
C)Freethe memory ofsecond.
✅ GOAL:
We’ll insert a node with value 15 between nodes first (10) and second (20), and later remove it.
🧱 C Code
🔧 Insertion in C:
| |
❌ Removal in C:
| |
🧱 C# Code
🔧 Insertion in C#:
| |
❌ Removal in C#:
| |
✅ Summary
| Operation | C | C# |
|---|---|---|
| Insert | malloc + update 4 pointers | new + update 4 references |
| Remove | update 2 pointers + free() | update 2 references; GC frees memory |
Homework
Build a small console app to practice bits/bytes, ASCII, and linked lists:
- Binary → Decimal → ASCII
- Write
BinaryToDecimal(string bits)that converts a binary string (e.g.,"110000") to anint. - Write
AsciiFromBinary(string bits)that returns the corresponding ASCIIchar. - Given an input like
"01001000 01101001", decode it to"Hi".
- Doubly Linked List basics (C#)
- Implement a minimal
DoublyLinkedList<T>with innerNodehavingPrev,Next, andValue. - Support
AddLast,InsertAfter(node, value), andRemove(node). - Demonstrate: build
10 <-> 20 <-> 30, insert15between 10 and 20, print forward and backward, then remove15and print again.
- Explain in 2–3 sentences
- When arrays are a better fit than linked lists, and vice versa.
- What the CLR/GC handles for you vs. what C requires you to do manually.
Solution
Show/Hide Code
Project Solution
| |
Program.cs
| |
Notes
- Arrays are great for indexed random access and compact memory; linked lists are better for frequent inserts/removals in the middle without shifting elements.
- In C, you must
malloc/freeand wire pointers; in C#, the CLR manages memory and references via the GC.
Homework — breakdown and explanation
Below is an explanation of why each part of the task matters.
1) Working with bits and ASCII
Methods
BinaryToDecimal(string bits) Converts a binary string to a decimal number using shifting:
1value = (value << 1) + (c - '0');This reinforces the idea that each new digit in base‑2 = multiply by 2 and add the current bit.
AsciiFromBinary(string bits) Casts the decimal value to a char:
(char)BinaryToDecimal(bits). Here we connect back to the ASCII table — a byte maps to a letter or symbol.DecodeBinaryMessage(“01001000 01101001”) → “Hi” Splits the string into bytes, converts each, and assembles text — showing how binary groups directly represent characters.
Why this matters
It cements the core idea: computers store letters as numbers, and numbers as combinations of bits.
2) Doubly linked list (DoublyLinkedList<T>)
Structure
- Inner Node holds:
- Value — the data
- Prev — link to previous node
- Next — link to next node
- head and tail give fast access to the start and end.
Methods
- AddLast — adds a node to the end. If empty, both head and tail point to the new node.
- InsertAfter(existing, value) — inserts after a given node: rewires links on existing, newNode, and next.
- Remove(node) — removes a node: adjusts neighbors, updates head/tail, and nulls the removed node’s links (helpful for GC).
- Forward()/Backward() — iterate in both directions; this is a key benefit of a doubly linked list.
Why this matters
It reinforces “references/pointers” in practice. In C#, we work with references while the CLR and GC manage memory.
3) Arrays vs lists and C vs C#
- Arrays: O(1) random access and compact memory, but inserts/removals in the middle require shifting.
- Lists: great for frequent inserts/removals; index access is O(n).
- C vs C#: In C you manually manage memory (malloc/free) and raw pointers; in C# you use references and the CLR/GC manages memory.
Conclusion
- The progression from bits → ASCII → linked lists builds intuition step by step.
- Each code piece highlights the theory with a practical example.
- The assignment ties it together: representation, data structure operations, and the memory model.
