Programming Language & Its Applications — Pointers, Structure and Data Files in C, NEC licence examination syllabus (Nepal Engineering Council).
The arrow operator, self-referential structs, and the moment C stops being a calculator and starts building data structures.
A struct containing a pointer to its own type is the seed of every dynamic data structure — linked lists, trees, graphs, hash-table buckets. This is the single most consequential idea in the C syllabus, because everything in the DSA paper is built from it. Once you can allocate a node and link it to another, array-size limits stop being a constraint.
ps and you are looking at a linked list — the Linux kernel keeps every running process in one, defined in sched.h as a struct holding pointers to the next and previous task. Your browser's Back button is a linked list. Undo in any editor is a linked list. The Nepali word-suggestion on your phone keyboard walks a trie, which is a tree of these nodes. When you understand this page, you understand the shape of all of them.#include <stdio.h>
#include <string.h>
typedef struct { int dd, mm, yyyy; } Date;
typedef struct {
int roll;
char name[20];
Date dob;
} Student;
int main(void) {
Student s = {101, "Ram Bahadur", {15, 3, 2004}};
Student *p = &s;
/* three spellings of the same access */
printf("%d %d %d\n", s.roll, p->roll, (*p).roll);
/* chaining into the nested struct */
printf("%s born %02d/%02d/%d\n",
p->name, p->dob.dd, p->dob.mm, p->dob.yyyy);
/* modifying through the pointer changes s */
p->dob.yyyy = 2005;
printf("s.dob.yyyy is now %d\n", s.dob.yyyy);
return 0;
}
Output:
101 101 101
Ram Bahadur born 15/03/2004
s.dob.yyyy is now 2005
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *make_node(int v) {
Node *n = malloc(sizeof *n);
if (!n) { perror("malloc"); exit(1); }
n->data = v; n->next = NULL;
return n;
}
/* head can CHANGE, so we take Node** */
void push_front(Node **head, int v) {
Node *n = make_node(v);
n->next = *head;
*head = n;
}
void push_back(Node **head, int v) {
Node *n = make_node(v);
if (!*head) { *head = n; return; }
Node *c = *head;
while (c->next) c = c->next;
c->next = n;
}
int delete_value(Node **head, int v) {
Node **cur = head;
while (*cur) {
if ((*cur)->data == v) {
Node *dead = *cur;
*cur = dead->next; /* unlink */
free(dead);
return 1;
}
cur = &(*cur)->next;
}
return 0;
}
void reverse(Node **head) {
Node *prev = NULL, *cur = *head;
while (cur) {
Node *nxt = cur->next;
cur->next = prev;
prev = cur; cur = nxt;
}
*head = prev;
}
void print(const char *tag, const Node *h) {
printf("%-10s", tag);
for (; h; h = h->next) printf("%d -> ", h->data);
printf("NULL\n");
}
void destroy(Node **head) {
while (*head) { Node *d = *head; *head = d->next; free(d); }
}
int main(void) {
Node *list = NULL;
push_back(&list, 10);
push_back(&list, 20);
push_back(&list, 30);
print("built", list);
push_front(&list, 5);
print("push 5", list);
delete_value(&list, 20);
print("del 20", list);
reverse(&list);
print("reversed", list);
destroy(&list);
print("destroyed", list);
return 0;
}
Output:
built 10 -> 20 -> 30 -> NULL
push 5 5 -> 10 -> 20 -> 30 -> NULL
del 20 5 -> 10 -> 30 -> NULL
reversed 30 -> 10 -> 5 -> NULL
destroyed NULL
#include <stdio.h>
#include <stdlib.h>
typedef struct TNode {
int data;
struct TNode *left, *right;
} TNode;
TNode *insert(TNode *root, int v) {
if (!root) {
TNode *n = malloc(sizeof *n);
n->data = v; n->left = n->right = NULL;
return n;
}
if (v < root->data) root->left = insert(root->left, v);
else if (v > root->data) root->right = insert(root->right, v);
return root; /* duplicates ignored */
}
void inorder(const TNode *r) { /* gives SORTED order */
if (!r) return;
inorder(r->left);
printf("%d ", r->data);
inorder(r->right);
}
int height(const TNode *r) {
if (!r) return 0;
int l = height(r->left), q = height(r->right);
return 1 + (l > q ? l : q);
}
int search(const TNode *r, int v, int *steps) {
while (r) { (*steps)++;
if (v == r->data) return 1;
r = (v < r->data) ? r->left : r->right;
}
return 0;
}
int main(void) {
int vals[] = {50, 30, 70, 20, 40, 60, 80};
TNode *root = NULL;
for (int i = 0; i < 7; i++) root = insert(root, vals[i]);
printf("inorder (sorted): "); inorder(root); printf("\n");
printf("height = %d\n", height(root));
int steps = 0;
printf("search 60: %s in %d steps\n",
search(root, 60, &steps) ? "found" : "absent", steps);
steps = 0;
printf("search 45: %s in %d steps\n",
search(root, 45, &steps) ? "found" : "absent", steps);
return 0;
}
Output:
inorder (sorted): 20 30 40 50 60 70 80
height = 3
search 60: found in 3 steps
search 45: absent in 3 steps
SELECT ... WHERE id = 5 on a ten-million-row table returns instantly instead of scanning ten million rows. Your filesystem indexes directories the same way. If you want to go deeper, look up "why databases use B-trees and not binary trees": the answer is disk block size, and it is one of the most satisfying explanations in computer science.struct Node *next, not struct Node next) and be able to explain why. The linked-list operations — create, insert at front/end, delete, reverse, traverse — are standard full-mark programming questions. Know the array-vs-list comparison table with reasons, and be able to draw a BST from an insertion sequence and give its height. Explain why Node** is needed when the head may change.Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…