Note: Dereferencing is the process of getting the value stored at a location referenced by a pointer.
Pointers are essential in data structures for several reasons:
Here are a few code examples and implementations to illustrate the practical use of pointers in data structures and algorithms:
// Dynamic allocation of an integer array
int size = 5;
int* dynamicArray = (int*)malloc(size * sizeof(int));
// Dynamic allocation of a structure
struct Node {
int data;
struct Node* next;
};
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
// Node structure for a linked list
struct Node {
int data;
struct Node* next;
};
// Inserting a node at the beginning of the linked list
void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
// Accessing elements in an array using pointer arithmetic
int array[] = {1, 2, 3, 4, 5};
int* ptr = array;
// Print array elements using pointer arithmetic
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
// Swapping two integers using pointers
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Given a binary tree
struct Node { int val; Node *left; Node *right; Node *next; }
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Example :
Input: root = [1,2,3,4,5,null,7] Output: [1,#,2,3,#,4,5,7,#] Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
Solution:
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
if (root == nullptr) {
return nullptr;
}
queue<Node*> q;
q.push(root);
while (!q.empty()) {
int levelSize = q.size();
for (int i = 0; i < levelSize; i++) {
Node* curr = q.front();
q.pop();
if (i < levelSize - 1) {
curr->next = q.front();
}
if (curr->left != nullptr) {
q.push(curr->left);
}
if (curr->right != nullptr) {
q.push(curr->right);
}
}
}
return root;
}
};
next
pointer to its right node at the same level. Given the head
of a linked list, remove the nth
node from the end of the list and return its head.
Example:
Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
Solution:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* fast = dummy;
ListNode* slow = dummy;
// Move fast pointer n nodes ahead
for (int i = 0; i <= n; i++) {
fast = fast->next;
}
// Handle case when n is equal to the length of the list
if (fast == nullptr) {
ListNode* temp = dummy->next;
dummy->next = dummy->next->next;
delete temp;
return dummy->next;
}
// Move both pointers simultaneously until fast reaches the last node
while (fast != nullptr) {
fast = fast->next;
slow = slow->next;
}
// Remove the nth node
ListNode* temp = slow->next;
slow->next = slow->next->next;
delete temp;
return dummy->next;
}
};
fast
and slow
, to find the (n-1)th node from the end. next
pointer of the (n-1)th node. Note: also read about Problems based on Pattern Matching
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…