1.
Write a Program to create, Initialize and • *a = *b;
access a pointer variable. • *b = temp;
• #include <stdio.h> • }
• int main() { • void permute(char *str, int start, int end) {
• int var = 20; • if (start == end) {
• int *ptr; • printf("%s\n", str);
• ptr = &var; • } else {
• printf("Value of var variable: %d\n", var); • for (int i = start; i <= end; i++) {
• printf("Address stored in ptr variable: %p\n", • swap((str + start), (str + i));
ptr); • permute(str, start + 1, end);
• printf("Value of *ptr variable: %d\n", *ptr); • swap((str + start), (str + i));
• return 0; • }
•} • }
• }
2. Write a Program to calculate the length of the • int main() {
string using a pointer. • char str[] = "abc";
• #include <stdio.h> • int length = strlen(str);
• int main() { • printf("Permutations of '%s':\n", str);
• char str[] = "Hello, World!"; • permute(str, 0, length - 1);
• char *ptr = str; • return 0;
• int length = 0; • }
• while (*ptr != '\0') {
• length++; 9. Write a program to convert Prefix notation to
• ptr++; postfix notation.
•} • #include <stdio.h>
• printf("Length of the string: %d\n", length); • #include <string.h>
• return 0; • #include <stdlib.h>
•} • int isOperator(char ch) {
• return (ch == '+' || ch == '-' || ch == '*' || ch ==
3. Write a Program to swap numbers using '/' || ch == '^');
pointer. •}
• #include <stdio.h> • void prefixToPostfix(char *prefix) {
• void swap(int *a, int *b) { • int length = strlen(prefix);
• int temp = *a; • char stack[length];
• *a = *b; • int top = -1;
• *b = temp; • for (int i = length - 1; i >= 0; i--) {
•} • char ch = prefix[i];
• int main() { • if (!isOperator(ch)) {
• int num1 = 10; • stack[++top] = ch;
• int num2 = 20; • } else {
• printf("Before swapping: num1 = %d, num2 = • char operand1 = stack[top--];
%d\n", num1, num2); • char operand2 = stack[top--];
• swap(&num1, &num2); • printf("%c%c%c ", operand1, operand2, ch);
• printf("After swapping: num1 = %d, num2 = •}
%d\n", num1, num2); •}
• return 0; •}
•} • int main() {
• char prefix[] = "+A*BC";
4. Print all permutations of a given string using • printf("Prefix expression: %s\n", prefix);
pointers. • printf("Postfix expression: ");
• #include <stdio.h> • prefixToPostfix(prefix);
• #include <string.h> • printf("\n");
• void swap(char *a, char *b) { • return 0;
• char temp = *a; 1 •}
6. Write Program to implement Push, Pop and 7. Convert infix notation to postfix notation.
Traverse operation on STACK. • #include <stdio.h>
• #include <stdio.h> • #include <stdlib.h>
• #include <stdlib.h> • #include <string.h>
• #define MAX_SIZE 5 • #define MAX_SIZE 50
• int stack[MAX_SIZE]; • char stack[MAX_SIZE];
• int top = -1; • int top = -1;
• void push(int value) { • void push(char value) {
• if (top == MAX_SIZE - 1) { • stack[++top] = value;
• printf("Stack is full. Cannot push %d.\n", •}
value); • char pop() {
• } else { • return stack[top--];
• stack[++top] = value; •}
• printf("Pushed %d onto the stack.\n", value); • int precedence(char operator) {
•} • if (operator == '+' || operator == '-') return 1;
•} • else if (operator == '*' || operator == '/') return
• int pop() { 2;
• if (top == -1) { • else return 0;
• printf("Stack is empty. Cannot pop.\n"); •}
• return -1; • void infixToPostfix(char *infix) {
• } else { • char *postfix = (char *)malloc(strlen(infix) + 1);
• int value = stack[top--]; • int i, j = 0;
• printf("Popped %d from the stack.\n", value); • for (i = 0; i < strlen(infix); i++) {
• return value; • if (infix[i] == '(') {
•} • push(infix[i]);
•} • } else if (infix[i] == ')') {
• void traverse() { • while (stack[top] != '(') {
• if (top == -1) { • postfix[j++] = pop();
• printf("Stack is empty.\n"); •}
• } else { • pop();
• printf("Stack elements: "); • } else if (infix[i] == '+' || infix[i] == '-' || infix[i]
• for (int i = 0; i <= top; i++) { == '*' || infix[i] == '/') {
• printf("%d ", stack[i]); • while (top != -1 && precedence(stack[top]) >=
•} precedence(infix[i])) {
• printf("\n"); • postfix[j++] = pop();
•} •}
•} • push(infix[i]);
• int main() { • } else {
• push(10); • postfix[j++] = infix[i];
• push(20); •}
• push(30); •}
• traverse(); • while (top != -1) {
• pop(); • postfix[j++] = pop();
• traverse(); •}
• return 0; • postfix[j] = '\0';
•} • printf("Postfix notation: %s\n", postfix);
• free(postfix);
•}
• int main() {
• char infix[] = "A+B*C-D/E";
• printf("Infix notation: %s\n", infix);
• infixToPostfix(infix);
• return 0;
2 •}
8. Write Program to convert Infix notation to 10. Write Program to perform the operation
prefix notation. Insert, Delete and Display on Queue.
• #include <stdio.h> • #include <stdio.h>
• #include <string.h> • #include <stdlib.h>
• #include <stdlib.h> • #define MAX_SIZE 5
• int isOperator(char ch) { • int queue[MAX_SIZE];
• return (ch == '+' || ch == '-' || ch == '*' || ch == • int front = -1, rear = -1;
'/' || ch == '^'); • void insert(int value) {
•} • if (rear == MAX_SIZE - 1) {
• int precedence(char ch) { • printf("Queue is full. Cannot insert %d.\n",
• if (ch == '+' || ch == '-') return 1; value);
• else if (ch == '*' || ch == '/') return 2; • } else {
• else if (ch == '^') return 3; • if (front == -1) front = 0;
• return 0; • rear++;
•} • queue[rear] = value;
• void infixToPrefix(char *infix, char *prefix) { • printf("Inserted %d into the queue.\n", value);
• int length = strlen(infix); •}
• char stack[length]; •}
• int top = -1; • int delete() {
• int prefixIndex = length - 1; • if (front == -1) {
• for (int i = length - 1; i >= 0; i--) { • printf("Queue is empty. Cannot delete.\n");
• char ch = infix[i]; • return -1;
• if (!isOperator(ch)) { • } else {
• prefix[prefixIndex--] = ch; • int value = queue[front];
• } else { • if (front == rear) {
• while (top >= 0 && precedence(stack[top]) >= • front = -1;
precedence(ch)) { • rear = -1;
• prefix[prefixIndex--] = stack[top--]; • } else {
•} • front++;
• stack[++top] = ch; •}
•} • printf("Deleted %d from the queue.\n", value);
•} • return value;
• while (top >= 0) { •}
• prefix[prefixIndex--] = stack[top--]; •}
•} • void display() {
• prefix[length] = '\0'; • if (front == -1) {
•} • printf("Queue is empty.\n");
• int main() { • } else {
• char infix[] = "(A+B)*C"; • printf("Queue elements: ");
• int length = strlen(infix); • for (int i = front; i <= rear; i++) {
• char prefix[length + 1]; • printf("%d ", queue[i]);
• printf("Infix expression: %s\n", infix); •}
• infixToPrefix(infix, prefix); • printf("\n");
• printf("Prefix expression: %s\n", prefix); •}
• return 0; •}
•} • int main() {
• insert(10);
• insert(20);
• insert(30);
• display();
• delete();
• display();
• return 0;
3 •}
11. Write Program to implement Circular queue. • for (int j = 0; j < q->size; j++) {
• #include <stdio.h> • printf("%d ", q->data[i]);
• #include <stdlib.h> • i = (i + 1) % MAX_SIZE;
• #define MAX_SIZE 5 • }
• typedef struct { • printf("\n");
• int data[MAX_SIZE]; • }
• int front; • int main() {
• int rear; • Queue* q = createQueue();
• int size; • enqueue(q, 10);
• } Queue; • enqueue(q, 20);
• Queue* createQueue() { • enqueue(q, 30);
• Queue* q = (Queue*)malloc(sizeof(Queue)); • enqueue(q, 40);
• q->front = 0; • enqueue(q, 50);
• q->rear = 0; • printQueue(q);
• q->size = 0; • dequeue(q);
• return q; • dequeue(q);
•} • printQueue(q);
• int isEmpty(Queue* q) { • return 0;
• return q->size == 0; • }
•}
• int isFull(Queue* q) { 5. Store n students' information using structure.
• return q->size == MAX_SIZE; • #include <stdio.h>
•} • struct Student {
• void enqueue(Queue* q, int value) { • char name[50];
• if (isFull(q)) { • int age;
• printf("Queue is full. Cannot add %d.\n", • float marks;
value); • };
• return; • int main() {
•} • int n;
• q->data[q->rear] = value; • printf("Enter the number of students: ");
• q->rear = (q->rear + 1) % MAX_SIZE; • scanf("%d", &n);
• q->size++; • struct Student students[n];
• printf("%d added to the queue.\n", value); • for (int i = 0; i < n; i++) {
•} • printf("Enter student %d's name: ", i + 1);
• int dequeue(Queue* q) { • scanf("%s", students[i].name);
• if (isEmpty(q)) { • printf("Enter student %d's age: ", i + 1);
• printf("Queue is empty. Cannot remove • scanf("%d", &students[i].age);
element.\n"); • printf("Enter student %d's marks: ", i + 1);
• return -1; • scanf("%f", &students[i].marks);
•} •}
• int value = q->data[q->front]; • printf("\nStudents' Information:\n");
• q->front = (q->front + 1) % MAX_SIZE; • for (int i = 0; i < n; i++) {
• q->size--; • printf("Name: %s, Age: %d, Marks: %.2f\n",
• printf("%d removed from the queue.\n", students[i].name, students[i].age,
value); students[i].marks);
• return value; •}
•} • return 0;
• void printQueue(Queue* q) { •}
• if (isEmpty(q)) {
• printf("Queue is empty.\n");
• return;
•}
• printf("Queue elements: ");
• int i = q->front; 4
12. Write Program to implement Double ended • } else {
queue. • front++;
• #include <stdio.h> • }
• #include <stdlib.h> • printf("Deleted %d from the front.\n",
• #define MAX 5 removed);
• int deque[MAX]; • }
• int front = -1, rear = -1; • void deleteRear() {
• int isFull() { • if (isEmpty()) {
• return ((front == 0 && rear == MAX - 1) || • printf("Deque is empty.\n");
(front == rear + 1)); • return;
•} • }
• int isEmpty() { • int removed = deque[rear];
• return (front == -1); • if (front == rear) {
•} • front = -1; rear = -1;
• void insertFront(int key) { • } else if (rear == 0) {
• if (isFull()) { • rear = MAX - 1;
• printf("Deque is full.\n"); • } else {
• return; • rear--;
•} • }
• if (front == -1) { • printf("Deleted %d from the rear.\n",
• front = 0; rear = 0; removed);
• } else if (front == 0) { • }
• front = MAX - 1; • void displayDeque() {
• } else { • if (isEmpty()) {
• front--; • printf("Deque is empty.\n");
•} • return;
• deque[front] = key; • }
• printf("Inserted %d at the front.\n", key); • printf("Deque elements are: ");
•} • int i = front;
• void insertRear(int key) { • while (1) {
• if (isFull()) { • printf("%d ", deque[i]);
• printf("Deque is full.\n"); • if (i == rear) break;
• return; • i = (i + 1) % MAX;
•} • }
• if (rear == -1) { • printf("\n");
• front = 0; rear = 0; • }
• } else if (rear == MAX - 1) { • int main() {
• rear = 0; • insertRear(5);
• } else { • displayDeque();
• rear++; • insertFront(15);
•} • displayDeque();
• deque[rear] = key; • insertRear(25);
• printf("Inserted %d at the rear.\n", key); • displayDeque();
•} • deleteFront();
• void deleteFront() { • displayDeque();
• if (isEmpty()) { • deleteRear();
• printf("Deque is empty.\n"); • displayDeque();
• return; • return 0;
•} • }
• int removed = deque[front];
• if (front == rear) {
• front = -1; rear = -1;
• } else if (front == MAX - 1) {
• front = 0; 5
13. Write Program to implement Priority queue. • insert(10, 2);
• #include <stdio.h> • insert(20, 1);
• #include <stdlib.h> • insert(30, 3);
• #define MAX_SIZE 5 • display();
• int queue[MAX_SIZE]; • delete();
• int front = -1, rear = -1; • display();
• void insert(int value, int priority) { • return 0;
• if (rear == MAX_SIZE - 1) { • }
• printf("Queue is full. Cannot insert %d.\n",
value); 17. Write a Program to sort given Array using
• } else { Quick sort technique.
• if (front == -1) { • #include <stdio.h>
• front = 0; rear = 0; • int partition(int arr[], int low, int high) {
• queue[rear] = value; • int pivot = arr[high];
• } else { • int i = low - 1;
• int i; • for (int j = low; j < high; j++) {
• for (i = rear; i >= 0; i--) { • if (arr[j] < pivot) {
• if (priority > i) { • i++;
• queue[i + 1] = queue[i]; • int temp = arr[i];
• } else { • arr[i] = arr[j];
• break; • arr[j] = temp;
•} •}
•} •}
• queue[i + 1] = value; • int temp = arr[i + 1];
• rear++; • arr[i + 1] = arr[high];
•} • arr[high] = temp;
•} • return i + 1;
•} •}
• int delete() { • void quickSort(int arr[], int low, int high) {
• if (front == -1) { • if (low < high) {
• printf("Queue is empty. Cannot delete.\n"); • int pivot = partition(arr, low, high);
• return -1; • quickSort(arr, low, pivot - 1);
• } else { • quickSort(arr, pivot + 1, high);
• int value = queue[front]; •}
• if (front == rear) { •}
• front = -1; rear = -1; • void printArray(int arr[], int size) {
• } else { • for (int i = 0; i < size; i++) {
• front++; • printf("%d ", arr[i]);
•} •}
• return value; • printf("\n");
•} •}
•} • int main() {
• void display() { • int arr[] = {64, 34, 25, 12, 22, 11, 90};
• if (front == -1) { • int size = sizeof(arr) / sizeof(arr[0]);
• printf("Queue is empty.\n"); • printf("Original array: ");
• } else { • printArray(arr, size);
• printf("Queue elements: "); • quickSort(arr, 0, size - 1);
• for (int i = front; i <= rear; i++) { • printf("Sorted array: ");
• printf("%d ", queue[i]); • printArray(arr, size);
•} • return 0;
• printf("\n"); •}
•}
•}
• int main() { 6
14. Write a Program to search an element using 15. Write a Program to sort given Array using
Linear search. Insertion sort technique.
• #include <stdio.h> • #include <stdio.h>
• int linearSearch(int arr[], int size, int key) { • void insertionSort(int arr[], int size) {
• for (int i = 0; i < size; i++) { • for (int i = 1; i < size; i++) {
• if (arr[i] == key) return i; • int key = arr[i];
•} • int j = i - 1;
• return -1; • while (j >= 0 && arr[j] > key) {
•} • arr[j + 1] = arr[j];
• int main() { • j--;
• int arr[] = {10, 20, 30, 40, 50}; •}
• int size = sizeof(arr) / sizeof(arr[0]); • arr[j + 1] = key;
• int key = 30; •}
• int result = linearSearch(arr, size, key); •}
• if (result == -1) { • void printArray(int arr[], int size) {
• printf("Element not found in the array.\n"); • for (int i = 0; i < size; i++) {
• } else { • printf("%d ", arr[i]);
• printf("Element found at index %d.\n", result); •}
•} • printf("\n");
• return 0; •}
•} • int main() {
• int arr[] = {64, 34, 25, 12, 22, 11, 90};
16. Write a Program to sort given Array using • int size = sizeof(arr) / sizeof(arr[0]);
Bubble sort technique. • printf("Original array: ");
• #include <stdio.h> • printArray(arr, size);
• void bubbleSort(int arr[], int size) { • insertionSort(arr, size);
• for (int i = 0; i < size - 1; i++) { • printf("Sorted array: ");
• for (int j = 0; j < size - i - 1; j++) { • printArray(arr, size);
• if (arr[j] > arr[j + 1]) { • return 0;
• int temp = arr[j]; •}
• arr[j] = arr[j + 1];
• arr[j + 1] = temp;
•}
•}
•}
•}
• void printArray(int arr[], int size) {
• for (int i = 0; i < size; i++) {
• printf("%d ", arr[i]);
•}
• printf("\n");
•}
• int main() {
• int arr[] = {64, 34, 25, 12, 22, 11, 90};
• int size = sizeof(arr) / sizeof(arr[0]);
• printf("Original array: ");
• printArray(arr, size);
• bubbleSort(arr, size);
• printf("Sorted array: ");
• printArray(arr, size);
• return 0;
•}
7
18. Write a Program to sort given Array using 19. Write Program to implement Singly Linked
selection sort technique. List.
• #include <stdio.h> • #include <stdio.h>
• void selectionSort(int arr[], int size) { • #include <stdlib.h>
• for (int i = 0; i < size - 1; i++) { • typedef struct Node {
• int minIndex = i; • int data;
• for (int j = i + 1; j < size; j++) { • struct Node* next;
• if (arr[j] < arr[minIndex]) { • } Node;
• minIndex = j; • Node* createNode(int data) {
•} • Node* newNode = (Node*)
•} malloc(sizeof(Node));
• int temp = arr[minIndex]; • newNode->data = data;
• arr[minIndex] = arr[i]; • newNode->next = NULL;
• arr[i] = temp; • return newNode;
•} •}
•} • void insertNode(Node** head, int data) {
• void printArray(int arr[], int size) { • Node* newNode = createNode(data);
• for (int i = 0; i < size; i++) { • if (*head == NULL) {
• printf("%d ", arr[i]); • *head = newNode;
•} • } else {
• printf("\n"); • Node* temp = *head;
•} • while (temp->next != NULL) {
• int main() { • temp = temp->next;
• int arr[] = {64, 34, 25, 12, 22, 11, 90}; •}
• int size = sizeof(arr) / sizeof(arr[0]); • temp->next = newNode;
• printf("Original array: "); •}
• printArray(arr, size); •}
• selectionSort(arr, size); • void printList(Node* head) {
• printf("Sorted array: "); • while (head != NULL) {
• printArray(arr, size); • printf("%d -> ", head->data);
• return 0; • head = head->next;
•} •}
• printf("NULL\n");
•}
• int main() {
• Node* head = NULL;
• insertNode(&head, 10);
• insertNode(&head, 20);
• insertNode(&head, 30);
• printList(head);
• return 0;
•}
8
20. Write Program to implement Double Linked • Node* temp = *head;
List. • while (temp->next != NULL) {
• #include <stdio.h> • if (temp->next->data == data) {
• #include <stdlib.h> • Node* nodeToDelete = temp->next;
• typedef struct Node { • temp->next = temp->next->next;
• int data; • if (temp->next != NULL) {
• struct Node* prev; • temp->next->prev = temp;
• struct Node* next; • }
• } Node; • free(nodeToDelete);
• Node* createNode(int data) { • return;
• Node* newNode = (Node*) • }
malloc(sizeof(Node)); • temp = temp->next;
• newNode->data = data; • }
• newNode->prev = NULL; • printf("Node not found\n");
• newNode->next = NULL; • }
• return newNode; • void printList(Node* head) {
•} • while (head != NULL) {
• void insertAtBeginning(Node** head, int data) { • printf("%d -> ", head->data);
• Node* newNode = createNode(data); • head = head->next;
• if (*head == NULL) { • }
• *head = newNode; • printf("NULL\n");
• } else { • }
• newNode->next = *head; • int main() {
• (*head)->prev = newNode; • Node* head = NULL;
• *head = newNode; • insertAtEnd(&head, 10);
•} • insertAtEnd(&head, 20);
•} • insertAtEnd(&head, 30);
• void insertAtEnd(Node** head, int data) { • insertAtBeginning(&head, 5);
• Node* newNode = createNode(data); • printf("Double Linked List: ");
• if (*head == NULL) { • printList(head);
• *head = newNode; • deleteNode(&head, 20);
• } else { • printf("After deleting 20: ");
• Node* temp = *head; • printList(head);
• while (temp->next != NULL) { • return 0;
• temp = temp->next; • }
•}
• temp->next = newNode;
• newNode->prev = temp;
•}
•}
• void deleteNode(Node** head, int data) {
• if (*head == NULL) {
• printf("List is empty\n");
• return;
•}
• if ((*head)->data == data) {
• Node* temp = *head;
• *head = (*head)->next;
• if (*head != NULL) {
• (*head)->prev = NULL;
•}
• free(temp);
• return;
•} 9