KEMBAR78
Datastructure 1 | PDF | Queue (Abstract Data Type) | C++
0% found this document useful (0 votes)
14 views48 pages

Datastructure 1

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

Datastructure 1

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

1.

Array Insertion (Insert 25 at index 2)


#include <stdio.h>
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int n = 5, pos = 2, val = 25;
for (int i = n; i > pos; i--)
arr[i] = arr[i - 1];
arr[pos] = val;
n++;
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
10 20 25 30 40 50

2.Array Deletion (Delete element at index 2)


#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = 5, pos = 2;
for (int i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
n--;
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
10 20 40 50

3.Linear Search (Find 9)


#include <stdio.h>
int main() {
int arr[] = {5, 8, 3, 9, 2}, n = 5, key = 9;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Found at index %d\n", i);
return 0;
}
}
printf("Not Found\n");
return 0;
}
Output:
Found at index 3

4.Binary Search (Find 30)


#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int low = 0, high = 4, mid, key = 30;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
printf("Found at index %d\n", mid);
return 0;
} else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
printf("Not Found\n");
return 0;
}

Output:
Found at index 2

5. Bubble Sort
#include <stdio.h>
int main() {
int arr[] = {5, 1, 4, 2, 8}, n = 5;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
12458
6.Selection Sort
#include <stdio.h>
int main() {
int arr[] = {64, 25, 12, 22, 11}, n = 5;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min])
min = j;
int t = arr[min];
arr[min] = arr[i];
arr[i] = t;
}
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
11 12 22 25 64

7.Insertion Sort
#include <stdio.h>
int main() {
int arr[] = {12, 11, 13, 5, 6}, n = 5;
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key)
arr[j + 1] = arr[j--];
arr[j + 1] = key;
}
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
5 6 11 12 13

8. Stack Using Array (Push/Pop/Display)


#include <stdio.h>
#define SIZE 5
int stack[SIZE], top = -1;

void push(int val) {


if (top == SIZE - 1)
printf("Stack Overflow\n");
else
stack[++top] = val;
}

void pop() {
if (top == -1)
printf("Stack Underflow\n");
else
printf("Popped: %d\n", stack[top--]);
}

void display() {
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}

int main() {
push(10); push(20); push(30);
display();
pop();
display();
return 0;
}
Output:
30 20 10
Popped: 30
20 10

9. Queue Using Array


#include <stdio.h>
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;

void enqueue(int val) {


if (rear == SIZE - 1)
printf("Queue Full\n");
else {
if (front == -1) front = 0;
queue[++rear] = val;
}
}

void dequeue() {
if (front == -1 || front > rear)
printf("Queue Empty\n");
else
printf("Dequeued: %d\n", queue[front++]);
}

void display() {
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
int main() {
enqueue(10); enqueue(20); enqueue(30);
display();
dequeue();
display();
return 0;
}
Output:
10 20 30
Dequeued: 10
20 30

10. Singly Linked List – Insertion at Beginning


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

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

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

newNode = (struct Node*)malloc(sizeof(struct


Node));
newNode->data = 10;
newNode->next = head;
head = newNode;

newNode = (struct Node*)malloc(sizeof(struct


Node));
newNode->data = 20;
newNode->next = head;
head = newNode;

printList(head);
return 0;
}
Output:
20 -> 10 -> NULL

11. Singly Linked List – Deletion from Beginning


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

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

void printList(struct Node* head) {


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

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

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

printf("Before deletion: ");


printList(head);

struct Node* temp = head;


head = head->next;
free(temp);

printf("After deletion: ");


printList(head);
return 0;
}
Output:
Before deletion: 10 -> 20 -> NULL
After deletion: 20 -> NULL

🔹 12. Singly Linked List Traversal


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

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

void printList(struct Node* head) {


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

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

newNode = (struct Node*)malloc(sizeof(struct


Node));
newNode->data = 10;
newNode->next = head;
head = newNode;

newNode = (struct Node*)malloc(sizeof(struct


Node));
newNode->data = 20;
newNode->next = head;
head = newNode;

printList(head);
return 0;
}
Output:
20 -> 10 -> NULL

13. Circular Queue using Array


#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;

void enqueue(int val) {


if ((front == 0 && rear == SIZE - 1) || (rear + 1 ==
front)) {
printf("Queue is Full\n");
return;
}
if (front == -1) front = rear = 0;
else if (rear == SIZE - 1) rear = 0;
else rear++;
queue[rear] = val;
}

void dequeue() {
if (front == -1) {
printf("Queue is Empty\n");
return;
}
printf("Dequeued: %d\n", queue[front]);
if (front == rear) front = rear = -1;
else if (front == SIZE - 1) front = 0;
else front++;
}

void display() {
if (front == -1) {
printf("Queue is Empty\n");
return;
}
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear) break;
i = (i + 1) % SIZE;
}
printf("\n");
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
enqueue(40);
display();
dequeue();
display();
return 0;
}
Output:
10 20 30 40
Dequeued: 10
20 30 40

14. Stack using Linked List


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

struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL;

void push(int val) {


struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = top;
top = newNode;
}

void pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return;
}
printf("Popped: %d\n", top->data);
struct Node* temp = top;
top = top->next;
free(temp);
}
void display() {
struct Node* temp = top;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
push(10); push(20); push(30);
display();
pop();
display();
return 0;
}
Output:
30 20 10
Popped: 30
20 10

15. Queue using Linked List


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

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

struct Node *front = NULL, *rear = NULL;

void enqueue(int val) {


struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if (rear == NULL)
front = rear = newNode;
else {
rear->next = newNode;
rear = newNode;
}
}

void dequeue() {
if (front == NULL) {
printf("Queue is Empty\n");
return;
}
printf("Dequeued: %d\n", front->data);
struct Node* temp = front;
front = front->next;
free(temp);
}
void display() {
struct Node* temp = front;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
display();
dequeue();
display();
return 0;
}
Output:
10 20 30
Dequeued: 10
20 30

16. Binary Tree Creation


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

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

struct Node* createNode(int val) {


struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->left = newNode->right = NULL;
return newNode;
}

int main() {
struct Node* root = createNode(10);
root->left = createNode(20);
root->right = createNode(30);
printf("Tree created with root: %d\n", root-
>data);
return 0;
}
Output:
Tree created with root: 10

🔹 17. Inorder Traversal (Left-Root-Right)


void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
Output (for tree in 17):
20 10 30

🔹 18. Preorder Traversal (Root-Left-Right)


void preorder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
Output:
10 20 30
🔹

19. Postorder Traversal (Left-Right-Root)


void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
Output:
20 30 10
20.Reverse an Array
#include <stdio.h>

int main() {
int arr[100], n, i;

printf("Enter size of array: ");


scanf("%d", &n);

printf("Enter %d elements:\n", n);


for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Original array:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

printf("\nReversed array:\n");
for(i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}

return 0;
}
Output:
Enter size of array: 5
Enter 5 elements:
12345
Original array:
12345
Reversed array:
54321

21. Merge Two Sorted Arrays


#include <stdio.h>
int main() {
int a[] = {1, 3, 5}, b[] = {2, 4, 6};
int c[10], i = 0, j = 0, k = 0, n1 = 3, n2 = 3;

while (i < n1 && j < n2) {


if (a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while (i < n1) c[k++] = a[i++];
while (j < n2) c[k++] = b[j++];

for (i = 0; i < n1 + n2; i++)


printf("%d ", c[i]);
return 0;
}
Output: 1 2 3 4 5 6
🔹 22. Count Frequency of Elements in Array
#include <stdio.h>
int main() {
int arr[] = {1, 2, 2, 3, 3, 3}, freq[100] = {0}, n = 6;

for (int i = 0; i < n; i++)


freq[arr[i]]++;

for (int i = 0; i < 10; i++)


if (freq[i] > 0)
printf("%d appears %d times\n", i, freq[i]);

return 0;
}
OUTPUT:
1 appears 1 times
2 appears 2 times
3 appears 3 times
23. Matrix Addition
#include <stdio.h>
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}}, c[2][2];

for (int i = 0; i < 2; i++)


for (int j = 0; j < 2; j++)
c[i][j] = a[i][j] + b[i][j];

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
Output:
68
10 12
24. Find Middle of Linked List
#include <stdio.h>
#include <stdlib.h>

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

void findMiddle(struct Node* head) {


struct Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
printf("Middle: %d\n", slow->data);
}

int main() {
struct Node* head = malloc(sizeof(struct
Node));
head->data = 1;
head->next = malloc(sizeof(struct Node));
head->next->data = 2;
head->next->next = malloc(sizeof(struct
Node));
head->next->next->data = 3;
head->next->next->next = NULL;

findMiddle(head);
return 0;
}
OUTPUT:
Middle: 2
25. Check Palindrome Array
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 2, 1}, n = 5, flag = 1;
for (int i = 0; i < n / 2; i++) {
if (arr[i] != arr[n - i - 1]) {
flag = 0;
break;
}
}
if (flag)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}
OUTPUT: Palindrome
26. Find Maximum Element in Linked List
#include <stdio.h>
#include <stdlib.h>

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

int findMax(struct Node* head) {


int max = head->data;
while (head) {
if (head->data > max)
max = head->data;
head = head->next;
}
return max;
}

int main() {
struct Node* head = malloc(sizeof(struct
Node));
head->data = 10;
head->next = malloc(sizeof(struct Node));
head->next->data = 50;
head->next->next = NULL;

printf("Max: %d\n", findMax(head));


return 0;
}
OUTPUT:
Max: 50
27. Check Balanced Parentheses using Stack
#include <stdio.h>
#define SIZE 100
char stack[SIZE];
int top = -1;

void push(char c) {
stack[++top] = c;
}

char pop() {
return stack[top--];
}

int isBalanced(char* exp) {


for (int i = 0; exp[i]; i++) {
if (exp[i] == '(') push('(');
else if (exp[i] == ')') {
if (top == -1) return 0;
pop();
}
}
return top == -1;
}

int main() {
char exp[] = "(a+b)*(c-d)";
if (isBalanced(exp))
printf("Balanced\n");
else
printf("Not Balanced\n");
return 0;
}
OUTPUT: Balanced
28. Count Nodes in Binary Tree (Recursive)
#include <stdio.h>
#include <stdlib.h>

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

int countNodes(struct Node* root) {


if (root == NULL) return 0;
return 1 + countNodes(root->left) +
countNodes(root->right);
}

struct Node* createNode(int val) {


struct Node* newNode =
malloc(sizeof(struct Node));
newNode->data = val;
newNode->left = newNode->right = NULL;
return newNode;
}

int main() {
struct Node* root = createNode(10);
root->left = createNode(20);
root->right = createNode(30);
root->left->left = createNode(40);

printf("Total Nodes: %d\n",


countNodes(root));
return 0;
}
OUTPUT:
Total Nodes: 4

29. Find Height of Binary Tree (Complete


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

// Binary Tree Node structure


struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Create a new node


struct Node* createNode(int val) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->left = newNode->right = NULL;
return newNode;
}

// Function to find height of binary tree


int height(struct Node* root) {
if (root == NULL)
return 0;

int leftHeight = height(root->left);


int rightHeight = height(root->right);

return 1 + (leftHeight > rightHeight ? leftHeight :


rightHeight);
}

// Main function
int main() {
// Manually creating the following tree:
// 10
// / \
// 20 30
// /
// 40

struct Node* root = createNode(10);


root->left = createNode(20);
root->right = createNode(30);
root->left->left = createNode(40);

printf("Height of the binary tree is: %d\n",


height(root));
return 0;
}
✅ Output:

Height of the binary tree is: 3


30. Convert Array to Linked List
#include <stdio.h>
#include <stdlib.h>

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

struct Node* createFromArr(int arr[], int n) {


struct Node *head = NULL, *temp = NULL;
for (int i = 0; i < n; i++) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = arr[i];
newNode->next = NULL;
if (head == NULL)
head = temp = newNode;
else {
temp->next = newNode;
temp = newNode;
}
}
return head;
}

void printList(struct Node* head) {


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

int main() {
int arr[] = {10, 20, 30}, n = 3;
struct Node* head = createFromArr(arr, n);
printList(head);
return 0;
}
OUTPUT:

10 -> 20 -> 30 -> NULL

You might also like