Page 1 of 10
C Programs for Data Structures
1. Tower of Hanoi (Recursive)
c Copy Edit
#include <stdio.h>
void TOH(int n, char from, char to, char aux) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", from, to);
return;
}
TOH(n-1, from, aux, to);
printf("Move disk %d from %c to %c\n", n, from, to);
TOH(n-1, aux, to, from);
}
int main() {
TOH(3, 'A', 'C', 'B');
TA
return 0;
}
O
PK
Output:
SA
css Copy Edit
H
Move disk 1 from A to C
S
Move disk 2 from A to B
KA
Move disk 1 from C to B
Move disk 3 from A to C
AA
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
2. Stack using Arrays
c Copy Edit
#include <stdio.h>
#define SIZE 5
int stack[SIZE], top = -1;
void push(int val) {
if(top == SIZE - 1) printf("Overflow\n");
else stack[++top] = val;
}
void pop() {
if(top == -1) printf("Underflow\n");
else printf("Popped %d\n", stack[top--]);
}
Page 2 of 10
void display() {
for(int i=0;i<=top;i++) printf("%d ", stack[i]);
}
int main() {
push(10); push(20); pop(); display();
}
Output:
nginx Copy Edit
Popped 20
10
3. Stack using Pointers
c Copy Edit
#include <stdio.h>
TA
#include <stdlib.h>
struct Node { int data; struct Node *next; };
O
struct Node *top = NULL;
void push(int val) {
PK
struct Node *n = malloc(sizeof(struct Node));
n->data = val; n->next = top; top = n;
SA
}
void pop() {
H
if(!top) printf("Underflow\n");
S
else {
KA
printf("Popped %d\n", top->data);
struct Node *t = top; top = top->next; free(t);
AA
}
}
void display() {
for(struct Node *t = top; t; t = t->next) printf("%d ", t->data);
}
int main() {
push(10); push(20); pop(); display();
}
Output:
nginx Copy Edit
Popped 20
10
4. Queue using Arrays
c
Page 3 of 10
Copy Edit
#include <stdio.h>
#define SIZE 5
int queue[SIZE], front = 0, rear = -1;
void enqueue(int val) {
if(rear == SIZE - 1) printf("Overflow\n");
else queue[++rear] = val;
}
void dequeue() {
if(front > rear) printf("Underflow\n");
else printf("Deleted %d\n", queue[front++]);
}
void display() {
for(int i = front; i <= rear; i++) printf("%d ", queue[i]);
}
int main() {
enqueue(10); enqueue(20); dequeue(); display();
}
Output:
nginx Copy Edit
Deleted 10
TA
20
O
PK
SA
5. Queue using Pointers
S H
c Copy Edit
KA
#include <stdio.h>
AA
#include <stdlib.h>
struct Node { int data; struct Node *next; };
struct Node *front = NULL, *rear = NULL;
void enqueue(int val) {
struct Node *n = malloc(sizeof(struct Node));
n->data = val; n->next = NULL;
if(!rear) front = rear = n;
else rear->next = n, rear = n;
}
void dequeue() {
if(!front) printf("Underflow\n");
else {
printf("Deleted %d\n", front->data);
struct Node *t = front; front = front->next; free(t);
if(!front) rear = NULL;
}
}
void display() {
for(struct Node *t = front; t; t = t->next) printf("%d ", t->data);
}
int main() {
Page 4 of 10
enqueue(10); enqueue(20); dequeue(); display();
}
Output:
nginx Copy Edit
Deleted 10
20
6. Singly Linked List
c Copy Edit
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node *next; };
struct Node *head = NULL;
void insert(int val) {
TA
struct Node *n = malloc(sizeof(struct Node));
n->data = val; n->next = head; head = n;
O
}
void delete() {
PK
if(!head) return;
struct Node *t = head; head = head->next; free(t);
SA
}
void display() {
H
for(struct Node *t = head; t; t = t->next) printf("%d ", t->data);
S
}
KA
int main() {
insert(10); insert(20); delete(); display();
AA
Output:
Copy Edit
10
7. Doubly Linked List
c Copy Edit
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node *prev, *next; };
struct Node *head = NULL;
void insert(int val) {
struct Node *n = malloc(sizeof(struct Node));
n->data = val; n->prev = NULL; n->next = head;
Page 5 of 10
if(head) head->prev = n; head = n;
}
void delete() {
if(!head) return;
struct Node *t = head; head = head->next;
if(head) head->prev = NULL; free(t);
}
void display() {
for(struct Node *t = head; t; t = t->next) printf("%d ", t->data);
}
int main() {
insert(10); insert(20); delete(); display();
}
Output:
Copy Edit
10
TA
8. Circular Linked List
O
c Copy Edit
PK
#include <stdio.h>
SA
#include <stdlib.h>
struct Node { int data; struct Node *next; };
H
struct Node *last = NULL;
S
void insert(int val) {
KA
struct Node *n = malloc(sizeof(struct Node));
n->data = val;
AA
if(!last) last = n, n->next = n;
else n->next = last->next, last->next = n, last = n;
}
void delete() {
if(!last) return;
struct Node *temp = last->next;
if(last == temp) last = NULL;
else last->next = temp->next;
free(temp);
}
void display() {
if(!last) return;
struct Node *t = last->next;
do { printf("%d ", t->data); t = t->next; } while(t != last->next);
}
int main() {
insert(10); insert(20); delete(); display();
}
Output:
Copy Edit
Page 6 of 10
20
9. Bubble Sort
c Copy Edit
#include <stdio.h>
void bubble(int a[], int n) {
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(a[j]>a[j+1]) {
int t=a[j]; a[j]=a[j+1]; a[j+1]=t;
}
}
int main() {
int a[]={5,3,2,4,1}, n=5;
bubble(a,n);
for(int i=0;i<n;i++) printf("%d ",a[i]);
}
TA
Output:
O
PK
Copy Edit
SA
12345
S H
KA
10. Selection Sort
AA
c Copy Edit
#include <stdio.h>
void selection(int a[], int n) {
for(int i=0;i<n-1;i++) {
int min=i;
for(int j=i+1;j<n;j++)
if(a[j]<a[min]) min=j;
int t=a[i]; a[i]=a[min]; a[min]=t;
}
}
int main() {
int a[]={5,3,2,4,1}, n=5;
selection(a,n);
for(int i=0;i<n;i++) printf("%d ",a[i]);
}
Output:
Copy Edit
Page 7 of 10
12345
11. Insertion Sort
c Copy Edit
#include <stdio.h>
void insertion(int a[], int n) {
for(int i=1;i<n;i++) {
int key=a[i], j=i-1;
while(j>=0 && a[j]>key) a[j+1]=a[j--];
a[j+1]=key;
}
}
int main() {
int a[]={5,3,2,4,1}, n=5;
insertion(a,n);
for(int i=0;i<n;i++) printf("%d ",a[i]);
}
TA
Output:
O
PK
Copy Edit
SA
12345
S H
KA
12. Linear Search (Iterative and Recursive)
AA
c Copy Edit
#include <stdio.h>
int linearIter(int a[], int n, int key) {
for(int i=0;i<n;i++) if(a[i]==key) return i;
return -1;
}
int linearRec(int a[], int n, int key, int i) {
if(i==n) return -1;
if(a[i]==key) return i;
return linearRec(a,n,key,i+1);
}
int main() {
int a[]={2,4,6,8,10}, key=6;
printf("Iter: %d\n", linearIter(a,5,key));
printf("Rec: %d\n", linearRec(a,5,key,0));
}
Output:
makefile Copy Edit
Page 8 of 10
Iter: 2
Rec: 2
13. Binary Search (Iterative and Recursive)
c Copy Edit
#include <stdio.h>
int binIter(int a[], int n, int key) {
int l=0, h=n-1;
while(l<=h) {
int m=(l+h)/2;
if(a[m]==key) return m;
else if(a[m]<key) l=m+1;
else h=m-1;
}
return -1;
}
int binRec(int a[], int l, int h, int key) {
TA
if(l>h) return -1;
int m=(l+h)/2;
O
if(a[m]==key) return m;
PK
else if(a[m]<key) return binRec(a,m+1,h,key);
else return binRec(a,l,m-1,key);
SA
}
int main() {
H
int a[]={2,4,6,8,10}, key=8;
printf("Iter: %d\n", binIter(a,5,key));
S
printf("Rec: %d\n", binRec(a,0,4,key));
KA
}
AA
Output:
makefile Copy Edit
Iter: 3
Rec: 3
14. Tree Traversal
c Copy Edit
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node *l, *r; };
struct Node* new(int v) {
struct Node *n = malloc(sizeof(struct Node));
n->data = v; n->l = n->r = NULL; return n;
}
Page 9 of 10
void inorder(struct Node *r) {
if(r) { inorder(r->l); printf("%d ", r->data); inorder(r->r); }
}
void preorder(struct Node *r) {
if(r) { printf("%d ", r->data); preorder(r->l); preorder(r->r); }
}
void postorder(struct Node *r) {
if(r) { postorder(r->l); postorder(r->r); printf("%d ", r->data); }
}
int main() {
struct Node *root = new(1);
root->l = new(2); root->r = new(3);
inorder(root); printf("\n"); preorder(root); printf("\n"); postorder(root);
}
Output:
Copy Edit
213
123
231 TA
O
PK
15a. DFS (Graph)
SA
c Copy Edit
H
#include <stdio.h>
S
int g[10][10], vis[10], n;
KA
void dfs(int v) {
vis[v] = 1; printf("%d ", v);
AA
for(int i = 0; i < n; i++)
if(g[v][i] && !vis[i]) dfs(i);
}
int main() {
n = 4;
int edges[4][2] = {{0,1},{0,2},{1,2},{2,3}};
for(int i=0;i<4;i++) g[edges[i][0]][edges[i][1]] = g[edges[i][1]][edges[i][0]] = 1;
dfs(0);
}
Output:
Copy Edit
0123
15b. BFS (Graph)
c
Page 10 of 10
Copy Edit
#include <stdio.h>
int g[10][10], vis[10], n, q[10], f=0, r=-1;
void bfs(int v) {
q[++r] = v; vis[v] = 1;
while(f <= r) {
int u = q[f++];
printf("%d ", u);
for(int i = 0; i < n; i++)
if(g[u][i] && !vis[i]) q[++r] = i, vis[i] = 1;
}
}
int main() {
n = 4;
int edges[4][2] = {{0,1},{0,2},{1,2},{2,3}};
for(int i=0;i<4;i++) g[edges[i][0]][edges[i][1]] = g[edges[i][1]][edges[i][0]] = 1;
bfs(0);
}
Output:
Copy Edit
0123
TA
O
PK
SA
Let me know if you'd like this as a downloadable PDF file or ZIP file with C files.
S H
KA
AA