Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Structures and Algorithms: Implementation in C, Schemes and Mind Maps of Data Structures and Algorithms

A comprehensive collection of c programming examples demonstrating the implementation of various data structures, including stacks, queues, linked lists, and arrays. It covers fundamental operations like insertion, deletion, traversal, and searching, along with sorting algorithms such as bubble sort, selection sort, insertion sort, and quick sort. Valuable for students learning data structures and algorithms in c, offering practical code examples and explanations.

Typology: Schemes and Mind Maps

2023/2024

Uploaded on 01/07/2025

yash-raj-31
yash-raj-31 šŸ‡®šŸ‡³

1 document

1 / 57

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
P a g e | 1
Experiment: 1
WAP for implementation of stack using Array.
1. #include <stdio.h>
2. #define MAX 5
3.
4. int stack[MAX];
5. int top = -1;
6.
7. int isFull() {
8. return top == MAX - 1;
9. }
10.
11. int isEmpty() {
12. return top == -1;
13. }
14.
15. void push(int value) {
16. if (isFull()) {
17. printf("Stack Overflow\n");
18. } else {
19. stack[++top] = value;
20. }
21. }
22.
23. int pop() {
24. if (isEmpty()) {
25. printf("Stack Underflow\n");
26. return -1;
27. } else {
28. return stack[top--];
29. }
30. }
31.
32. int peek() {
33. if (isEmpty()) {
34. printf("Stack is empty\n");
35. return -1;
36. } else {
37. return stack[top];
38. }
39. }
40.
41. void display() {
42. if (isEmpty()) {
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39

Partial preview of the text

Download Data Structures and Algorithms: Implementation in C and more Schemes and Mind Maps Data Structures and Algorithms in PDF only on Docsity!

Experiment: 1

WAP for implementation of stack using Array.

  1. #include <stdio.h>
  2. #define MAX 5
  3. int stack[MAX];
  4. int top = -1;
  5. int isFull() {
  6. return top == MAX - 1;
  7. }
  8. int isEmpty() {
  9. return top == -1;
  10. }
  11. void push(int value) {
  12. if (isFull()) {
  13. printf("Stack Overflow\n");
  14. } else {
  15. stack[++top] = value;
  16. }
  17. }
  18. int pop() {
  19. if (isEmpty()) {
  20. printf("Stack Underflow\n");
  21. return -1;
  22. } else {
  23. return stack[top--];
  24. }
  25. }
  26. int peek() {
  27. if (isEmpty()) {
  28. printf("Stack is empty\n");
  29. return -1;
  30. } else {
  31. return stack[top];
  32. }
  33. }
  34. void display() {
  35. if (isEmpty()) {
  1. printf("Stack is empty\n");
  2. } else {
  3. printf("Stack elements are:\n");
  4. for (int i = 0; i <= top; i++) {
  5. printf("%d ", stack[i]);
  6. }
  7. printf("\n");
  8. }
  9. }
  10. int main() {
  11. push(10);
  12. push(20);
  13. push(30);
  14. display();
  15. printf("Top element is %d\n", peek());
  16. printf("Popped element is %d\n", pop());
  17. display();
  18. return 0;
  19. }

Experiment: 2

WAP for implementation of Queue using Array.

  1. #include <stdio.h>
  2. #define MAX 5
  3. int queue[MAX];
  4. int front = -1;
  5. int rear = -1;
  6. int isFull() {
  7. return (rear + 1) % MAX == front;
  8. }
  9. int isEmpty() {
  10. return front == -1;
  11. }
  12. void enqueue(int value) {
  13. if (isFull()) {
  14. printf("Queue Overflow\n");
  15. } else {
  16. if (front == -1) {
  17. front = 0;
  18. }
  19. rear = (rear + 1) % MAX;
  20. queue[rear] = value;
  21. }
  22. }
  23. int dequeue() {
  24. if (isEmpty()) {
  25. printf("Queue Underflow\n");
  26. return -1;
  27. } else {
  28. int value = queue[front];
  29. if (front == rear) {
  1. front = rear = -1;
  2. } else {
  3. front = (front + 1) % MAX;
  4. }
  5. return value;
  6. }
  7. }
  8. int peek() {
  9. if (isEmpty()) {
  10. printf("Queue is empty\n");
  11. return -1;
  12. } else {
  13. return queue[front];
  14. }
  15. }
  16. void display() {
  17. if (isEmpty()) {
  18. printf("Queue is empty\n");
  19. } else {
  20. printf("Queue elements are:\n");
  21. int i;
  22. for (i = front; i != rear; i = (i + 1) % MAX) {
  23. printf("%d ", queue[i]);
  24. }
  25. printf("%d\n", queue[i]);
  26. }
  27. }
  28. int main() {
  29. enqueue(10);
  30. enqueue(20);
  31. enqueue(30);
  32. display();
  33. printf("Front element is %d\n", peek());
  34. printf("Dequeued element is %d\n", dequeue());
  35. display();
  36. return 0;
  37. }

Experiment: 3

WAP for implementation of stack using Linked list.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Node {
  4. int data;
  5. struct Node* next;
  6. };
  7. struct Node* top = NULL;
  8. int isEmpty() {
  9. return top == NULL;
  10. }
  11. void push(int value) {
  12. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  13. if (newNode == NULL) {
  14. printf("Stack Overflow\n");
  15. } else {
  16. newNode->data = value;
  17. newNode->next = top;
  18. top = newNode;
  19. }
  20. }
  21. int pop() {
  22. if (isEmpty()) {
  23. printf("Stack Underflow\n");
  24. return -1;
  25. } else {
  26. struct Node* temp = top;
  27. int value = temp->data;
  28. top = top->next;
  29. free(temp);
  1. return value;
  2. }
  3. }
  4. int peek() {
  5. if (isEmpty()) {
  6. printf("Stack is empty\n");
  7. return -1;
  8. } else {
  9. return top->data;
  10. }
  11. }
  12. void display() {
  13. if (isEmpty()) {
  14. printf("Stack is empty\n");
  15. } else {
  16. struct Node* temp = top;
  17. printf("Stack elements are:\n");
  18. while (temp != NULL) {
  19. printf("%d ", temp->data);
  20. temp = temp->next;
  21. }
  22. printf("\n");
  23. }
  24. }
  25. int main() {
  26. push(11);
  27. push(21);
  28. push(30);
  29. display();
  30. printf("Top element is %d\n", peek());
  31. printf("Popped element is %d\n", pop());
  32. display();
  33. return 0;
  34. }

Experiment: 4

WAP for creation and implementation of linked list.

  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. // Define a node structure

  4. struct Node {

  5. int data;

  6. struct Node* next;

  7. };

  8. // Function to create a new node

  9. struct Node* createNode(int data) {

  10. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

  11. if (!newNode) {

  12. printf("Memory allocation failed\n");

  13. exit(1);

  14. }

  15. newNode->data = data;

  16. newNode->next = NULL;

  17. return newNode;

  18. }

  19. // Function to insert a node at the beginning of the linked list

  20. void insertAtBeginning(struct Node** head, int data) {

  21. struct Node* newNode = createNode(data);

  22. newNode->next = *head;

  23. *head = newNode;

  24. }

  25. // Function to print the linked list

  26. void printList(struct Node* head) {

  27. struct Node* temp = head;

  28. while (temp != NULL) {

  29. printf("%d -> ", temp->data);

  30. temp = temp->next;

  31. }

  32. printf("NULL\n");

  33. }

  34. // Function to free the linked list memory

  35. void freeList(struct Node* head) {

  36. struct Node* temp;

  37. while (head != NULL) {

  38. temp = head;

  39. head = head->next;

  40. free(temp);

  41. }

  42. }

  43. int main() {

  44. struct Node* head = NULL;

  45. // Insert elements into the linked list

  46. insertAtBeginning(&head, 3);

  47. insertAtBeginning(&head, 2);

  48. insertAtBeginning(&head, 1);

  49. // Print the linked list

  50. printList(head);

  51. // Free the linked list memory

  52. freeList(head);

  53. return 0;

  54. }

Experiment:

WAP for insertion in singly linked list from beginning, end

any position.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // Define a node structure
  4. struct Node {
  5. int data;
  6. struct Node* next;
  7. };
  8. // Function to create a new node
  9. struct Node* createNode(int data) {
  10. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  11. if (!newNode) {
  12. printf("Memory allocation failed\n");
  13. exit(1);
  14. }
  15. newNode->data = data;
  16. newNode->next = NULL;
  17. return newNode;
  18. }
  19. // Function to insert a node at the beginning of the linked list
  20. void insertAtBeginning(struct Node** head, int data) {
  21. struct Node* newNode = createNode(data);
  22. newNode->next = *head;
  23. *head = newNode;
  24. }
  25. // Function to insert a node at the end of the linked list
  26. void insertAtEnd(struct Node** head, int data) {
  27. struct Node* newNode = createNode(data);
  28. if (*head == NULL) {
  29. *head = newNode;
  30. } else {
  31. struct Node* temp = *head;
  32. while (temp->next != NULL) {
  1. temp = temp->next;
  2. }
  3. temp->next = newNode;
  4. }
  5. }
  6. // Function to insert a node at a given position in the linked list
  7. void insertAtPosition(struct Node** head, int data, int position) {
  8. struct Node* newNode = createNode(data);
  9. if (position == 1) {
  10. newNode->next = *head;
  11. *head = newNode;
  12. return;
  13. }
  14. struct Node* temp = *head;
  15. for (int i = 1; i < position - 1 && temp != NULL; i++) {
  16. temp = temp->next;
  17. }
  18. if (temp == NULL) {
  19. printf("Position out of bounds\n");
  20. free(newNode);
  21. } else {
  22. newNode->next = temp->next;
  23. temp->next = newNode;
  24. }
  25. }
  26. // Function to print the linked list
  27. void printList(struct Node* head) {
  28. struct Node* temp = head;
  29. while (temp != NULL) {
  30. printf("%d -> ", temp->data);
  31. temp = temp->next;
  32. }
  33. printf("NULL\n");
  34. }
  35. // Function to free the linked list memory
  36. void freeList(struct Node* head) {
  37. struct Node* temp;
  38. while (head != NULL) {
  39. temp = head;
  40. head = head->next;
  41. free(temp);

Output:

Experiment: 6

WAP for creation and implementation of doubly linked list.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // Define a node structure
  4. struct Node {
  5. int data;
  6. struct Node* next;
  7. struct Node* prev;
  8. };
  9. // Function to create a new node
  10. struct Node* createNode(int data) {
  11. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  12. if (!newNode) {
  13. printf("Memory allocation failed\n");
  14. exit(1);
  15. }
  16. newNode->data = data;
  17. newNode->next = NULL;
  18. newNode->prev = NULL;
  19. return newNode;
  20. }
  21. // Function to insert a node at the beginning of the doubly linked list
  22. void insertAtBeginning(struct Node** head, int data) {
  23. struct Node* newNode = createNode(data);
  24. if (*head != NULL) {
  25. (*head)->prev = newNode;
  26. }
  27. newNode->next = *head;
  28. *head = newNode;
  29. }
  30. // Function to insert a node at the end of the doubly linked list
  31. void insertAtEnd(struct Node** head, int data) {
  32. struct Node* newNode = createNode(data);
  33. if (*head == NULL) {
  34. *head = newNode;

Output:

Experiment: 7

WAP to search an element using linear search.

  1. #include <stdio.h>
  2. // Function to perform linear search
  3. int linearSearch(int arr[], int size, int target) {
  4. for (int i = 0; i < size; i++) {
  5. if (arr[i] == target) {
  6. return i; // Return the index of the target element
  7. }
  8. }
  9. return -1; // Return -1 if the target element is not found
  10. }
  11. int main() {
  12. int arr[] = {10, 20, 30, 40, 50};
  13. int size = sizeof(arr) / sizeof(arr[0]);
  14. int target = 30;
  15. int result = linearSearch(arr, size, target);
  16. if (result != -1) {
  17. printf("Element %d found at index %d\n", target, result);
  18. } else {
  19. printf("Element %d not found in the array\n", target);
  20. }
  21. return 0;
  22. }