• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 2 fuera de 10 páginas
Notas de lectura

Data Structures & Algorithms in C++ – Self-Referencing Classes, Dynamic Memory & Garbage Collection

Document preview thumbnail
Vista previa 2 fuera de 10 páginas

Complete C++ lecture notes covering Self-Referencing Classes, Dynamic Memory, and Garbage Collection. These notes explain important concepts in a clear and easy-to-understand way, making them useful for DSA students, programming revision, assignments, quizzes, and exam preparation.

Vista previa del contenido

Data Structures & Algorithms in C++
Self-Referencing Classes • Dynamic Memory • Garbage Collection

1. SELF-REFERENCING CLASSES
1.1 Introduction
A Self-Referencing Class is a class that contains a pointer to an object of the same class type.
This concept is extremely important in data structures, especially in:
• Linked Lists
• Trees
• Graphs


In simple words:

Ek class jo apnay hi type ke object ka address store kare.



1.2 Basic Concept
Instead of storing data only, a self-referencing class also stores a link (pointer) to another object.


General Structure
class Node {
public:
int data;
Node* next; // Self-reference pointer
};


Yahan Node* next ka matlab hai ke yeh pointer kisi doosray Node ko point karega.


1.3 Memory Representation
Each object contains:
• Data part
• Address of next object


Example:
[10 | address] → [20 | address] → [30 | NULL]



1.4 Example: Singly Linked List Node
Question Statement
Write a C++ program to create a simple linked list using self-referencing class.

, Program
#include <iostream> // Input/Output library
using namespace std;

// Self-referencing class
class Node {
public:
int data; // Har node ka data store karega
Node* next; // Pointer jo next node ka address store karega

// Constructor
Node(int value) {
data = value; // Jo value pass hogi wo data me store hogi
next = NULL; // Initially next NULL hoga (koi next node nahi)
}
};

int main() {
// Step 1: Nodes dynamically create karna (heap memory me)
Node* head = new Node(10); // First node (head) with value 10
Node* second = new Node(20); // Second node with value 20
Node* third = new Node(30); // Third node with value 30

// Step 2: Nodes ko link karna (chain banana)
head->next = second; // Head node ka next, second ko point karega
second->next = third; // Second ka next, third ko point karega
third->next = NULL; // Last node ka next NULL (end of list)

// Step 3: Traversing (list ko print karna)
Node* temp = head; // Temporary pointer head se start karega

while (temp != NULL) { // Jab tak temp NULL na ho
cout << temp->data << " -> "; // Current node ka data print
temp = temp->next; // Next node par move karo
}

cout << "NULL"; // End of linked list show karne ke liye

return 0; // Program successfully end
}


Explanation (Line by Line)
• Node* next; → pointer to next node
• new Node(10) → dynamic memory allocation
• head->next = second; → linking nodes
• Loop → traversal


1.5 Applications
Self-referencing classes are used in:
• Linked Lists
• Binary Trees
• Graph adjacency lists

Información del documento

Subido en
7 de octubre de 2026
Número de páginas
10
Escrito en
2026/2027
Tipo
Notas de lectura
Profesor(es)
Zain
Contiene
Todas las clases
$3.49

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
0
Seguidores
0
Artículos
43
Última venta
-



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes