Skip to content

A simple, well-commented implementation of a singly linked list in C++ — built for learning data structures and object-oriented design. Includes basic operations like insertion, deletion, traversal, and memory-safe cleanup using destructors.

Notifications You must be signed in to change notification settings

habibma/cpp-singly-linked-list

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔗 Singly Linked List (C++)

This project implements a Singly Linked List in C++ as part of my Learning Journey.
It’s written with detailed comments and educational explanations — for learners to follow easily, and for recruiters to see my ability to write clean, well-documented, and modular C++ code.


🧠 What Is a Linked List?

A linked list is a linear data structure where each element, called a node, contains:

  • data — the stored value
  • next — a pointer to the next node

🧩 Visualization

	[HEAD] → [10 | *] → [20 | *] → [30 | NULL]

Each arrow () represents a pointer connecting one node to the next.

A list with two elements can be visualized like this:

	 Node*      Node         Node
	+------+   +-----+      +-----+
	| head |-->|value|  +-->|value|
	+------+   +-----+  |   +-----+
	           |next |--+   |next |-->nullptr
	           +-----+      +-----+

The Pointer "head" here points to the fisrts element of linked list. because we don't want to loose the head our chain in the memory!

💡 Why Linked Lists Are Important

Concept Description
Dynamic Memory Linked lists grow and shrink at runtime — no fixed size like arrays.
Efficient Insertion/Deletion Inserting or deleting nodes doesn’t require shifting elements.
Foundation for Other Structures Stacks, Queues, Graphs, and Hash Tables often use linked lists internally.

Stacks, Queues, Graphs, etc... like vectors are containers to save data in in a progamming languages like C++.

⚙️ Features Implemented

This SinglyLinkedList class supports the following operations:

Operation Description Time Complexity
push_front(value) Insert a node at the beginning O(1)
push_back(value) Insert a node at the end O(n)
at(index) Return the value of the element located in a a specific position
insert_at(index, value) Insert a node at a specific position O(n)
pop_front() Delete the first node O(1)
pop_back() Delete the last node O(n)
remove_at(index) Delete a node by index O(n)
size() Return the number of nodes O(n)

🧱 File Overview

File Description
singly_linked_list.hpp Header file with class definition
singly_linked_list.cpp Implementation with detailed comments
main.cpp Demonstration program

🧰 How to Compile and Run

You can build the demo with any C++ compiler:

g++ main.cpp singly_linked_list.cpp -o linked_list
./linked_list

📘 Learning Notes

  • This implementation focuses on:

  • Understanding pointer logic

  • Practicing memory management (new / delete)

  • Writing clear, documented code

  • Following object-oriented design principles


🔮 Future Improvements

  • Add these methods:

    • search(value)
    • print()
    • clear()
    • reverse()
  • Implement a DoublyLinkedList class

  • Visualize the list structure with ASCII diagrams

  • Create unit tests for each method

  • Convert Node class to Template class

  • Error handling and using informational errors


👨‍💻 Author

Habib Mote Student at 42 Berlin

About

A simple, well-commented implementation of a singly linked list in C++ — built for learning data structures and object-oriented design. Includes basic operations like insertion, deletion, traversal, and memory-safe cleanup using destructors.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages