0% found this document useful (0 votes)
3 views

C++ Unit-2 Notes

Unit 2 of the syllabus covers elementary data structures, including definitions of data and information, the importance of data structures, and their types (linear and non-linear). It discusses basic operations on data structures, algorithm complexity, and provides examples of C++ implementations for arrays, including insertion, deletion, searching, and sorting. The unit emphasizes the significance of selecting appropriate data structures for efficient data management and performance.

Uploaded by

Laxita Gaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C++ Unit-2 Notes

Unit 2 of the syllabus covers elementary data structures, including definitions of data and information, the importance of data structures, and their types (linear and non-linear). It discusses basic operations on data structures, algorithm complexity, and provides examples of C++ implementations for arrays, including insertion, deletion, searching, and sorting. The unit emphasizes the significance of selecting appropriate data structures for efficient data management and performance.

Uploaded by

Laxita Gaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

BASIC DATA STRUCTURES USING

C++ (24CSH-103)
Unit 2 : Elementary Data Structures

Unit 2 Syllabus ::

Compiled by : Subhayu
Chapter 1: Introduction to Data
Structure
1. Concept of Data and Information
📌 What is Data?
●​ Data refers to raw facts and figures without meaning.
●​ Examples: "John", 23, "Delhi", 3.14, etc.

📌 What is Information?
●​ Infor mation is processed data that has meaning and context.
●​ Example: "John is 23 years old and li ves in Delhi."

Key Difference:
Feature Data Information
Definition Ra w facts Processed data

Meaning No meaning Meaningful

Example 98, 76, 89 "Average score of

student = 87.6"

2. Introduction to Data Structures


📌 What is a Data Structure?
●​ A Data Structure is a way of organizing and storing data to perfor m
operations efficiently.

●​ Example: Storing student records in a database.


📌 Importance of Data Structures
✅ Efficient Data Management → Faster retrie val, sorting, searching​
✅ Optimized Resource Usage → Memory efficiency, reduced computation time​
✅ Reusability → Functions and structures can be reused​
✅ Better Algorithm Performance → Optimized operations

3. Types of Data Structures


Data structures are categorized into two main types:
📌 Linear Data Structures
●​ Data is stored sequentially.
●​ Operations (insertion, deletion, tra versal) follow a linear order.
●​ Examples:
○​ Arrays → Fi xed-size collection of elements

○​ Linked Lists → Dynamically allocated, sequential elements

○​ Stacks → LIFO (Last In, First Out)

○​ Queues → FIFO (First In, First Out)

📌 Non-Linear Data Structures


●​ Data is not stored sequentially.
●​ Examples:
○​ Trees → Hierarchical structure with nodes (e.g., Binary Trees)

○​ Graphs → Nodes (vertices) connected by edges (used in net working,

maps)

○​ Hash Tables → Key-value pairs with fast lookup (used in databases,


caches)

Type Structure Example Use Case


Linear Ar ray Storing student marks

Linear Stack Undo/Redo functionality

Linear Queue Printer job scheduling

Non-Linear Tree File system structure

Non-Linear Graph Social media connections

4. Operations on Data Structures


All data structures support basic operations:

Operation Description
Insertion Adding a ne w element

Deletion Removing an element

Traversal Accessing elements sequentially

Searching Finding an element (Linear Search, Binary Search)

Sorting Ar ranging elements (Bubble Sort, Quick Sort)

Updation Modifying an element

Example: Array Operations


#include <iostream>

using namespace std;

int main() {

int ar r[5] = {10, 20, 30, 40, 50}; // Insertion

cout << "Ar ray elements: ";


for (int i = 0; i < 5; i++) {

cout << ar r[i] << " "; // Tra versal

retur n 0;

5. Algorithm Complexity
📌 What is Algorithm Complexity?
●​ Complexity measures the efficiency of an algorithm in ter ms of time and
space.
●​ Helps in selecting the best approach for a problem.
📌 Types of Complexity
1️⃣ Time Complexity
●​ Time taken by an algorithm to run as input size inc reases.

●​ Big-O Notation is used to express worst-case complexity.


Complexit Example Algorithm Efficiency
y
O(1) Accessing an ar ray element Fastest

O(log n) Binary Search Very Efficient

O(n) Linear Search Moderate

O(n²) Bubble Sort Slow

O(2ⁿ) Recursi ve Fibonacci Very Slow

2️⃣ Space Complexity


●​ Amount of memory required to run an algorithm.
●​ Includes input size, au xiliary memory, and recursion stack.

Example Problem: Finding the Maximum in an


Array
Problem Statement:
Write a C++ program to find the ma ximum element in an ar ray.

Solution:
#include <iostream>

using namespace std;

int findMa x(int ar r[], int n) {

int ma x = ar r[0]; // Assume first element is ma x

for (int i = 1; i < n; i++) {

if (ar r[i] > ma x) {

ma x = ar r[i]; // Update ma x if larger value found

retur n ma x;

int main() {

int ar r[] = {23, 45, 67, 12, 89, 34};

int n = sizeof(ar r) / sizeof(ar r[0]);

cout << "Ma ximum element: " << findMa x(ar r, n) << endl;

retur n 0;

}
Output:
Ma ximum element: 89

Summary:​
✅ Data = Raw facts, Information = Processed data​
✅ Data Structures = Organized way to store and manipulate data​
✅ Types: Linear (Array, Stack, Queue) & Non-Linear (Tree, Graph, Hash
Table)​
✅ Operations: Insertion, Deletion, Tra versal, Searching, Sorting​
✅ Algorithm Complexity: Time (O(1) to O(2ⁿ)) & Space Complexity

Theory Questions & Answers


(Chapter 1: Introduction to Data
Structure)
1️⃣ What is a Data Structure? Why is it important?
Answer:​
A Data Structure is a way of organizing and managing data to enable

efficient access and modification.

Importance:
●​ Improves performance in searching, sorting, and data manipulation.
●​ Helps in efficient memory utilization and a voids redundancy.

●​ Supports code reusability and modularity.


●​ Essential for sol ving complex computational problems like AI, databases,
and net working.

2️⃣ What are the different types of Data Structures?


Answer:
1.​ Linear Data Structures → Elements are ar ranged in a sequential order.
○​ Examples: Array, Stack, Queue, Linked List

2.​ Non-Linear Data Structures → Elements are ar ranged in hierarchical

or interconnected patter ns.

○​ Examples: Tree, Graph, Hash Table

3️⃣ What are the basic operations on Data Structures?


Answer:
1.​ Insertion → Adding an element.
2.​ Deletion → Removing an element.

3.​ Traversal → Accessing elements sequentially.

4.​ Searching → Finding an element in a data structure.

5.​ Sorting → Ar ranging elements in a specific order.

6.​ Updation → Modifying an existing element.

4️⃣ What is Algorithm Complexity? Explain Time Complexity and


Space Complexity.
Answer:​
Algorithm Complexity refers to the efficiency of an algorithm based on input
size.

1.​ Time Complexity → Measures the execution time of an algorithm.


○​ Example:O(1), O(n), O(log n), O(n²)
2.​ Space Complexity → Measures the memory required by an algorithm.

○​ Example: O(1) (Constant Space), O(n) (Linear Space)

5️⃣ Differentiate between Linear and Non-Linear Data Structures.


Answer:
Feature Linear Data Structure Non-Linear Data Structure
Storage Sequential Hierarchical/Interconnected

Traversal One-by-one Multiple paths

Examples Ar ray, Stack, Queue Tree, Graph, Hash Table

6️⃣ What is the difference between Abstract Data Type (ADT) and
Data Structure?
Answer:
●​ Abstract Data Type (ADT) → A conceptual model that defines how data is
stored and operated on.

○​ Example: Stack ADT, Queue ADT


●​ Data Structure → A concrete implementation of an ADT in
programming.

○​ Example: Stack using Array/Linked List, Queue using


Array/Linked List

7️⃣ Explain the significance of choosing the right Data Structure for
a problem.
Answer:
●​ The choice of data structure affects performance, memory usage, and
execution speed.
●​ Example:

○​ Searching → Use Binary Search Tree or Hash Table for faster


lookup.

○​ Stack Operations → Use Stack (LIFO) structure for function calls.


○​ Graph Algorithms → Use Adjacency List or Adjacency Matrix
based on space-time trade-offs.
Chapter 2: Arrays
1️⃣ Introduction to Arrays
An array is a collection of elements of the same data type, stored contiguously
in memory. It allows indexed access to elements.

Key Features of Arrays:


●​ Fixed size → Declared at the time of c reation.
●​ Efficient access → Direct access using index (O(1)).
●​ Homogeneous data → Stores only one type of data.
Example (Declaration & Initialization in C++):
int ar r[5] = {10, 20, 30, 40, 50}; // Declaring and Initializing an ar ray

2️⃣ Representation of Linear Arrays in Memory


Each element in an ar ray is stored contiguously, and the address of an element
is calculated using:

Address=Base Address+(Index×Size of Data Type)

Example:​

If ar r[0] is at memory 2000 and int occupies 4 bytes, then:


●​ ar r[1] → 2004
●​ ar r[2] → 2008

●​ ar r[3] → 2012
3️⃣ Traversing an Array
Definition: Visiting each element of the ar ray sequentially.​
Example (Traversing an array in C++):
#include <iostream>

using namespace std;

int main() {

int ar r[5] = {10, 20, 30, 40, 50};

for(int i = 0; i < 5; i++) {

cout << ar r[i] << " "; // Output: 10 20 30 40 50

retur n 0;

Time Complexity: O(n)

4️⃣ Insertion & Deletion in Arrays


Insertion (Adding an element at a specific position)
●​ Shifting elements to the right before inserting.
●​ Worst-case time complexity: O(n)
Example: Insert 25 at index 2 in arr[5] = {10, 20, 30, 40, 50}
#include <iostream>

using namespace std;

int main() {

int ar r[6] = {10, 20, 30, 40, 50};

int pos = 2, value = 25;

for (int i = 5; i > pos; i--) {


ar r[i] = ar r[i - 1];

ar r[pos] = value;

for (int i = 0; i < 6; i++) {

cout << ar r[i] << " "; // Output: 10 20 25 30 40 50

retur n 0;

Deletion (Removing an element from a specific position)


●​ Shifting elements to the left after deletion.
●​ Worst-case time complexity: O(n)
Example: Delete element at index 2 from arr[5] = {10, 20, 30, 40, 50}
#include <iostream>

using namespace std;

int main() {

int ar r[5] = {10, 20, 30, 40, 50};

int pos = 2;

for (int i = pos; i < 4; i++) {

ar r[i] = ar r[i + 1];

for (int i = 0; i < 4; i++) {

cout << ar r[i] << " "; // Output: 10 20 40 50

retur n 0;

}
5️⃣ Searching in Arrays
1. Linear Search (Sequential Search)
●​ Checks elements one by one.
●​ Best-case: O(1), Worst-case: O(n).
●​ Works for unsorted data.

int linearSearch(int ar r[], int n, int key) {

for(int i = 0; i < n; i++) {

if(ar r[i] == key) retur n i; // Found at index i

retur n -1; // Not found

2. Binary Search (Efficient for Sorted Arrays)


●​ Divide and conquer approach.
●​ Best-case: O(1), Worst-case: O(log n).
●​ Only works for sorted arrays.
int binarySearch(int ar r[], int left, int right, int key) {

while (left <= right) {

int mid = left + (right - left) / 2;

if (ar r[mid] == key) retur n mid;

if (ar r[mid] < key) left = mid + 1;

else right = mid - 1;

retur n -1;

}
6️⃣ Sorting in Arrays
1. Bubble Sort (Repeated swapping)
●​ Time Complexity: O(n²)
void bubbleSort(int ar r[], int n) {

for(int i = 0; i < n-1; i++) {

for(int j = 0; j < n-i-1; j++) {

if(ar r[j] > ar r[j+1])

swap(ar r[j], ar r[j+1]);

2. Insertion Sort (Shifting and inserting)


●​ Time Complexity: O(n²), Best-case: O(n)
void insertionSort(int ar r[], int n) {

for(int i = 1; i < n; i++) {

int key = ar r[i];

int j = i - 1;

while(j >= 0 && ar r[j] > key) {

ar r[j+1] = ar r[j];

j--;

ar r[j+1] = key;

3. Selection Sort (Find min and swap)


●​ Time Complexity: O(n²)
void selectionSort(int ar r[], int n) {

for(int i = 0; i < n-1; i++) {

int minIndex = i;

for(int j = i+1; j < n; j++) {

if(ar r[j] < ar r[minIndex])

minIndex = j;

swap(ar r[i], ar r[minIndex]);

7️⃣ Two-Dimensional Arrays (2D Arrays)


Declaration & Initialization
int matri x[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Traversing a 2D Array
for(int i = 0; i < 3; i++) {

for(int j = 0; j < 3; j++) {

cout << matri x[i][j] << " ";

cout << endl;

Operations on 2D Arrays
1.​ Addition of Matrices
2.​ Multiplication of Matrices
3.​ Transpose of a Matrix

Time Complexities of Various Operations on Arrays


Operation Best Case Worst Case
Traversal O(n) O(n)

Insertion O(1) (End) O(n) (Middle)

Deletion O(1) (End) O(n) (Middle)

Linear O(1) O(n)


Search
Binary O(1) O(log n)
Search
Sorting O(n log n) (Best - Quick O(n²) (Bubble

Sort) Sort)

Chapter 2: Practice Questions


Theory-Based Questions and Answers
1️⃣ What is an array in C++? Explain its advantages.
Answer:​
An array is a fixed-size collection of elements of the same data type, stored

contiguously in memory.
Advantages:
●​ Efficient indexing: Fast access using index (O(1) time complexity).
●​ Memory locality: Elements are stored in consecuti ve memory locations,
improving cache efficiency.

●​ Easy to implement: Simplifies handling multiple variables of the same


type.

2️⃣ How is the memory address of an array element calculated?


Answer:​
For a 1D array, the address of an element ar r[i] is gi ven by:

Address=Base Address+(i×Size of Data Type)\text{Address} = \text{Base

Address} + (i \times \text{Size of Data Type})Address=Base Address+(i×Size of

Data Type)

For example, if ar r[0] is stored at 2000 and int occupies 4 bytes, then:
●​ ar r[1] → 2004

●​ ar r[2] → 2008

3️⃣ What are the different types of array operations?


Answer:
●​ Traversal: Accessing each element sequentially.
●​ Insertion: Adding an element at a specific position.
●​ Deletion: Removing an element.
●​ Searching: Finding an element (Linear Search, Binary Search).
●​ Sorting: Ar ranging elements (Bubble Sort, Insertion Sort, Selection Sort).

4️⃣ Compare Linear Search and Binary Search.


Feature Linear Search Binary Search
Data Type Works on unsorted Works only on sorted

data data

Best Case O(1) O(1)

Worst O(n) O(log n)


Case
Approach Sequential Di vide and Conquer

scanning

5️⃣ Explain Bubble Sort, Insertion Sort, and Selection Sort.


Answer:
●​ Bubble Sort: Repeatedly swaps adjacent elements if they are in the wrong
order (O(n²)).

●​ Insertion Sort: Picks elements one by one and inserts them in the cor rect
position (O(n²)).

●​ Selection Sort: Finds the minimum element and swaps it with the first
unsorted element (O(n²)).

6️⃣ What are 2D arrays? How are they declared in C++?


Answer:​
A 2D array is a collection of elements ar ranged in rows and columns.​

Declaration:
int matri x[3][3]; // 3x3 matri x

Initialization:
int matri x[2][2] = {{1, 2}, {3, 4}};

7️⃣ What are the advantages and disadvantages of arrays?


Answer:​
✅ Advantages:
●​ Fast access using indexing.

●​ Efficient for operations like sorting and searching.

❌ Disadvantages:
●​ Fi xed size (wasted memory if not fully used).

●​ Insertion and deletion are slow (O(n)).

📌 Coding-Based Questions with Solutions


1️⃣ Write a C++ program to traverse and print all elements of an
array.
#include <iostream>

using namespace std;

int main() {

int ar r[] = {10, 20, 30, 40, 50};

int n = sizeof(ar r) / sizeof(ar r[0]); // Finding ar ray size

for(int i = 0; i < n; i++) {

cout << ar r[i] << " "; // Output: 10 20 30 40 50

retur n 0;

}
2️⃣ Implement a program for Linear Search in C++.
int linearSearch(int ar r[], int n, int key) {

for(int i = 0; i < n; i++) {

if(ar r[i] == key) retur n i;

retur n -1;

3️⃣ Write a program for Binary Search in C++.


int binarySearch(int ar r[], int left, int right, int key) {

while (left <= right) {

int mid = left + (right - left) / 2;

if (ar r[mid] == key) retur n mid;

if (ar r[mid] < key) left = mid + 1;

else right = mid - 1;

retur n -1;

4️⃣ Implement Bubble Sort in C++.


void bubbleSort(int ar r[], int n) {

for(int i = 0; i < n-1; i++) {

for(int j = 0; j < n-i-1; j++) {

if(ar r[j] > ar r[j+1])

swap(ar r[j], ar r[j+1]);

}
}

5️⃣ Implement Insertion Sort in C++.


void insertionSort(int ar r[], int n) {

for(int i = 1; i < n; i++) {

int key = ar r[i];

int j = i - 1;

while(j >= 0 && ar r[j] > key) {

ar r[j+1] = ar r[j];

j--;

ar r[j+1] = key;

6️⃣ Implement Selection Sort in C++.


void selectionSort(int ar r[], int n) {

for(int i = 0; i < n-1; i++) {

int minIndex = i;

for(int j = i+1; j < n; j++) {

if(ar r[j] < ar r[minIndex])

minIndex = j;

swap(ar r[i], ar r[minIndex]);

}
7️⃣ Implement Matrix Addition for 2D Arrays in C++.
#include <iostream>

using namespace std;

int main() {

int A[2][2] = {{1, 2}, {3, 4}};

int B[2][2] = {{5, 6}, {7, 8}};

int C[2][2]; // Resultant matri x

for(int i = 0; i < 2; i++) {

for(int j = 0; j < 2; j++) {

C[i][j] = A[i][j] + B[i][j]; // Adding cor responding elements

cout << "Resultant Matri x: \n";

for(int i = 0; i < 2; i++) {

for(int j = 0; j < 2; j++) {

cout << C[i][j] << " ";

cout << endl;

retur n 0;

}
Chapter 3: Pointers - Detailed Notes
📌 Introduction to Pointers
A pointer is a variable that stores the memory address of another variable.
Instead of holding an actual value, it stores a reference to where the value is

stored in memory.

💡 Why Do We Need Pointers?


1.​ Efficient Memory Usage → Helps in dynamic memory allocation.
2.​ Faster Execution → Direct memory access speeds up operations.

3.​ Data Structures Implementation → Essential for linked lists, trees,

graphs.

4.​ Function Efficiency → Allows passing large structures efficiently without


copying.

📌 Declaration and Initialization of Pointers


1️⃣ Declaration:
int *ptr; // Declares a pointer to an integer

char *cptr; // Declares a pointer to a character

float *fptr; // Declares a pointer to a float

💡 The * indicates that the variable is a pointer.


2️⃣ Initialization:
int x = 10;

int *ptr = &x; // Pointer stores the address of x


💡 The & operator gi ves the address of x.
3️⃣ Dereferencing a Pointer (Accessing the Value at an Address)
cout << *ptr; // Outputs the value of x (10)

💡 The * operator is used to dereference a pointer (get the value stored at that
memory address).

📌 Pointer Arithmetic
💡 Pointer arithmetic allows operations on memory addresses.
Operatio Meaning
n
ptr++ Moves to the next memory location

ptr-- Moves to the pre vious memory location

ptr + n Moves n memory locations ahead

ptr - n Moves n memory locations back

Example:

int ar r[] = {10, 20, 30};

int *ptr = ar r;

ptr++; // Moves to ar r[1]

cout << *ptr; // Outputs 20

📌 Dynamic Memory Allocation with Pointers


Memory can be allocated dynamically using ne w and deallocated using delete.
Allocating Memory Dynamically
int *ptr = ne w int; // Allocates memory for one integer

*ptr = 50;

cout << *ptr; // Outputs 50

Deallocating Memory
delete ptr; // Frees the allocated memory

💡 Always free dynamically allocated memory to pre vent memory leaks.

📌 Arrays and Pointers


In C++, an ar ray name acts as a pointer to the first element.
Example: Accessing an Array Using a Pointer
int ar r[3] = {10, 20, 30};

int *ptr = ar r; // Points to the first element

cout << *(ptr + 1); // Outputs 20

💡 ptr + 1 moves the pointer to the next element in the ar ray.

📌 Pointers and Functions


Pointers can be passed to functions to modify variables directly.

Call by Reference Using Pointers


void modify(int *p) {

*p = 50; // Modifies the original value

int main() {

int x = 10;

modify(&x);

cout << x; // Outputs 50

💡 Passing by pointer allows modifying the original variable inside the


function.

📌 Pointers to Pointers (Double Pointers)


A pointer can store the address of another pointer.

Example:
int x = 10;

int *ptr = &x;

int **dptr = &ptr;

cout << **dptr; // Outputs 10

💡 **dptr dereferences t wice to get the value of x.

📌 Linked Lists Using Pointers


A linked list is a dynamic data structure where each element (node) points to
the next.

Structure of a Node in a Linked List


struct Node {

int data;

Node* next;

};

💡 Each node contains data and a pointer to the next node.

📌 Summary
✔Pointers store memory addresses and allow direct memory manipulation.​
✔ Pointer arithmetic helps in navigation through memory.​

✔ Dynamic memory allocation is done using new and delete.​

✔ Pointers enable efficient data structures like linked lists.


📌 Theory-Based Questions & Answers
What is a pointer in C++?​
1️⃣

Ans: A pointer is a variable that stores the memory address of another


variable. It allows direct memory access and manipulation.

Why are pointers important in C++?​


2️⃣

Ans: Pointers are c rucial for dynamic memory allocation, efficient data
structure implementation, function optimization, and low-level memory
access.
What is pointer dereferencing?​
3️⃣

Ans: Dereferencing a pointer means accessing the value stored at the memory
address it points to. This is done using the * operator.​

👉 Example:
int x = 10;

int *ptr = &x;

cout << *ptr; // Outputs 10

How does pointer arithmetic work?​


4️⃣

Ans: Pointer arithmetic in vol ves incrementing (ptr++), decrementing (ptr--),


adding (ptr + n), or subtracting (ptr - n) from a pointer to na vigate through
memory addresses.​

👉 Example:
int ar r[] = {10, 20, 30};

int *ptr = ar r;

cout << *(ptr + 1); // Outputs 20

What is a double pointer (pointer to a pointer)?​


5️⃣

Ans: A double pointer stores the address of another pointer, allowing


multi-le vel indirection.​

👉 Example:
int x = 10;

int *ptr = &x;

int **dptr = &ptr;

cout << **dptr; // Outputs 10

What is dynamic memory allocation, and why is it useful?​


6️⃣

Ans: Dynamic memory allocation assigns memory at runtime using ne w and


releases it using delete. It helps when the required memory size is unknown at

compile time.

How are pointers used in linked lists?​


7️⃣

Ans: Pointers allow dynamic node c reation in linked lists. Each node contains
data and a pointer to the next node.​

👉 Example:
struct Node {

int data;

Node* next;

};

📌 Coding-Based Questions & Solutions


1️⃣ Write a C++ program to demonstrate basic pointer usage.
#include <iostream>

using namespace std;

int main() {
int x = 10;

int *ptr = &x;

cout << "Value of x: " << *ptr << endl;

cout << "Address of x: " << ptr << endl;

retur n 0;

2️⃣ Write a program to swap two numbers using pointers.


#include <iostream>

using namespace std;

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x = 5, y = 10;

swap(&x, &y);

cout << "After swapping: x = " << x << ", y = " << y;

retur n 0;

3️⃣ Write a program to allocate memory dynamically and store a value.


#include <iostream>

using namespace std;


int main() {

int *ptr = ne w int;

*ptr = 50;

cout << "Dynamically allocated value: " << *ptr << endl;

delete ptr;

retur n 0;

4️⃣ Write a program to traverse an array using a pointer.


#include <iostream>

using namespace std;

int main() {

int ar r[] = {10, 20, 30, 40};

int *ptr = ar r;

for (int i = 0; i < 4; i++) {

cout << "Element " << i << ": " << *(ptr + i) << endl;

retur n 0;

5️⃣ Write a program to create and delete a dynamic array using pointers.
#include <iostream>

using namespace std;

int main() {

int n;
cout << "Enter size: ";

cin >> n;

int *ar r = ne w int[n];

cout << "Enter " << n << " elements: ";

for (int i = 0; i < n; i++) {

cin >> ar r[i];

cout << "Ar ray elements: ";

for (int i = 0; i < n; i++) {

cout << ar r[i] << " ";

delete[] ar r; // Freeing memory

retur n 0;

6️⃣ Write a program to demonstrate a pointer to a pointer.


#include <iostream>

using namespace std;

int main() {

int x = 10;

int *ptr = &x;

int **dptr = &ptr;

cout << "Value of x: " << **dptr << endl;

retur n 0;
}

7️⃣ Write a program to create a simple linked list node using pointers.
#include <iostream>

using namespace std;

struct Node {

int data;

Node* next;

};

int main() {

Node* head = ne w Node();

head->data = 5;

head->next = NULL;

cout << "Node data: " << head->data << endl;

delete head;

retur n 0;

You might also like