1.
Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node* next; };
struct Node* head = NULL;
void insert(int x){
struct Node* temp = malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
head = temp;
}
void display(){
struct Node* t = head;
while(t){ printf("%d ", t->data); t = t->next; }
}
int main(){
insert(10); insert(20); insert(30);
display();
return 0;
}
2. Stack using Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node* next; };
struct Node* top = NULL;
void push(int x){
struct Node* t = malloc(sizeof(struct Node));
t->data = x; t->next = top; top = t;
}
void pop(){
if(!top) return;
struct Node* temp = top;
top = top->next;
free(temp);
}
void display(){
struct Node* t = top;
while(t){ printf("%d ", t->data); t=t->next; }
}
int main(){
push(10); push(20); push(30);
pop();
display();
return 0;
}
3. BST Insert + Preorder
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node* l; struct Node* r; };
struct Node* newNode(int x){
struct Node* n = malloc(sizeof(struct Node));
n->data=x; n->l=n->r=NULL;
return n;
}
struct Node* insert(struct Node* root, int x){
if(!root) return newNode(x);
if(x < root->data) root->l = insert(root->l, x);
else root->r = insert(root->r, x);
return root;
}
void preorder(struct Node* root){
if(!root) return;
printf("%d ", root->data);
preorder(root->l);
preorder(root->r);
}
int main(){
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
preorder(root);
return 0;
}
4. Graph BFS
#include <stdio.h>
int main(){
int n=4;
int g[4][4]={{0,1,1,0},
{1,0,1,1},
{1,1,0,0},
{0,1,0,0}};
int q[10], front=0, rear=0, visited[4]={0};
int start=0;
q[rear++] = start;
visited[start]=1;
while(front<rear){
int v = q[front++];
printf("%d ", v);
for(int i=0;i<n;i++){
if(g[v][i] && !visited[i]){
visited[i]=1;
q[rear++]=i;
}
}
}
return 0;
}
5. Queue using Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node* next; };
struct Node *front=NULL, *rear=NULL;
void enqueue(int x){
struct Node* t = malloc(sizeof(struct Node));
t->data=x; t->next=NULL;
if(!rear) front=rear=t;
else{ rear->next=t; rear=t; }
}
void dequeue(){
if(!front) return;
struct Node* t = front;
front = front->next;
if(!front) rear=NULL;
free(t);
}
void display(){
struct Node* t = front;
while(t){ printf("%d ", t->data); t=t->next; }
}
int main(){
enqueue(10); enqueue(20); enqueue(30);
dequeue();
display();
return 0;
}
6. Graph DFS
#include <stdio.h>
int g[4][4]={{0,1,1,0},
{1,0,1,1},
{1,1,0,0},
{0,1,0,0}};
int visited[4]={0};
void dfs(int v){
visited[v]=1;
printf("%d ", v);
for(int i=0;i<4;i++){
if(g[v][i] && !visited[i]) dfs(i);
}
}
int main(){
dfs(0);
return 0;
}
7. Circular Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node{ int data; struct Node* next; };
struct Node* tail = NULL;
void insert(int x){
struct Node* t = malloc(sizeof(struct Node));
t->data=x;
if(!tail){
tail=t; t->next=t;
} else {
t->next=tail->next;
tail->next=t;
tail=t;
}
}
void display(){
if(!tail) return;
struct Node* p = tail->next;
do{
printf("%d ", p->data);
p=p->next;
}while(p!=tail->next);
}
int main(){
insert(10); insert(20); insert(30);
display();
return 0;
}
8. Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *prev, *next;
};
struct Node* head=NULL;
void insert(int x){
struct Node* t = malloc(sizeof(struct Node));
t->data=x; t->next=head; t->prev=NULL;
if(head) head->prev=t;
head=t;
}
void display(){
struct Node* p=head;
while(p){ printf("%d ", p->data); p=p->next; }
}
int main(){
insert(10); insert(20); insert(30);
display();
return 0;
}
9. BST Postorder
#include <stdio.h>
#include <stdlib.h>
struct Node{ int data; struct Node *l, *r; };
struct Node* newNode(int x){
struct Node* n = malloc(sizeof(struct Node));
n->data=x; n->l=n->r=NULL;
return n;
}
struct Node* insert(struct Node* root, int x){
if(!root) return newNode(x);
if(x < root->data) root->l = insert(root->l, x);
else root->r = insert(root->r, x);
return root;
}
void postorder(struct Node* root){
if(!root) return;
postorder(root->l);
postorder(root->r);
printf("%d ", root->data);
}
int main(){
struct Node* root=NULL;
root = insert(root, 50);
insert(root, 20);
insert(root, 60);
postorder(root);
return 0;
}