0% found this document useful (0 votes)
14 views7 pages

C Data Structure Programs

Uploaded by

amitbhai194b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

C Data Structure Programs

Uploaded by

amitbhai194b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Singly Linked List and Display

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

void display(struct Node* head) {


struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;

head = (struct Node*)malloc(sizeof(struct Node));


second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));

head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;

display(head);
return 0;
}

2. Doubly Linked List and Display

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* prev;
struct Node* next;
};

void display(struct Node* node) {


struct Node* last;
while (node != NULL) {
printf("%d <-> ", node->data);
last = node;
node = node->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;

head = (struct Node*)malloc(sizeof(struct Node));


second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));

head->data = 10;
head->prev = NULL;
head->next = second;

second->data = 20;
second->prev = head;
second->next = third;

third->data = 30;
third->prev = second;
third->next = NULL;

display(head);
return 0;
}

3. Circular Linked List and Display

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

void display(struct Node* head) {


struct Node* temp = head;
if (head != NULL) {
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
}
printf("(back to head)\n");
}
int main() {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));

head->data = 100;
second->data = 200;
third->data = 300;

head->next = second;
second->next = third;
third->next = head;

display(head);
return 0;
}

4. BST and Inorder Traversal

#include <stdio.h>
#include <stdlib.h>

struct Node {
int key;
struct Node* left, *right;
};

struct Node* newNode(int item) {


struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

struct Node* insert(struct Node* node, int key) {


if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}

void inorder(struct Node* root) {


if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}

int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

inorder(root);
return 0;
}

5. Fibonacci Series Using Recursion

#include <stdio.h>

int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}

int main() {
int n = 10;
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}

6. Binary Search

#include <stdio.h>

int binarySearch(int arr[], int n, int x) {


int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == x)
return mid;
else if (arr[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}

int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr)/sizeof(arr[0]);
int x = 30;
int result = binarySearch(arr, n, x);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}

7. Linear Search (All Cases)

#include <stdio.h>

int linearSearch(int arr[], int n, int x) {


for (int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}

int main() {
int arr[] = {5, 10, 15, 20};
int x = 15;
int n = sizeof(arr)/sizeof(arr[0]);
int result = linearSearch(arr, n, x);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}

8. Selection Sort

#include <stdio.h>

void selectionSort(int arr[], int n) {


for (int i = 0; i < n-1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

9. Insertion Sort

#include <stdio.h>

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

10. Singly Linked List Display in Reverse

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

void displayReverse(struct Node* node) {


if (node == NULL)
return;
displayReverse(node->next);
printf("%d -> ", node->data);
}

int main() {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));

head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;

displayReverse(head);
printf("NULL\n");
return 0;
}

You might also like