0% found this document useful (0 votes)
15 views

Solutions For Repeated Programming Questions

Uploaded by

legalbooksales
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)
15 views

Solutions For Repeated Programming Questions

Uploaded by

legalbooksales
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/ 8

For Your Reference - https://chatgpt.

com/share/6735d8a9-0298-800e-9a84-36f951caac21

Linked List Operations

1. Create and Display Linked List (Singly or Doubly) (4 Marks)

o Explanation:

▪ Create Function: For each element, create a new node and link it to the
previous node.

▪ Display Function: Traverse the list from the head node and print each node’s
data until reaching NULL.

o Code:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->next = NULL;

return newNode;

void displayList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

printf("%d -> ", current->data);

current = current->next;

printf("NULL\n");

int main() {

struct Node* head = createNode(1);


head->next = createNode(2);

head->next->next = createNode(3);

displayList(head);

return 0;

2. Delete the First Node in a Singly Linked List (4 Marks)

o Explanation:

▪ Update head to point to the second node.

▪ Free the original head node.

o Code:

void deleteFirstNode(struct Node** head) {

if (*head == NULL) return;

struct Node* temp = *head;

*head = (*head)->next;

free(temp);

3. Reverse a String Using Stack (4 Marks)

o Explanation:

▪ Push each character of the string onto the stack.

▪ Pop each character from the stack to reverse the string.

o Code:

#include <stdio.h>

#include <string.h>

#define MAX 100

void reverseString(char* str) {

int len = strlen(str);

char stack[MAX];

int top = -1;

// Push characters onto stack

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

stack[++top] = str[i];
}

// Pop characters to reverse

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

str[i] = stack[top--];

int main() {

char str[] = "hello";

reverseString(str);

printf("Reversed string: %s\n", str);

return 0;

4. Traverse and Display a Linked List in Reverse Order (4 Marks)

o Explanation:

▪ Traverse to the end and print nodes in reverse using recursion.

o Code:

void displayReverse(struct Node* head) {

if (head == NULL) return;

displayReverse(head->next);

printf("%d -> ", head->data);

Stack Operations and Applications

1. Convert Infix to Postfix Expression (5 Marks)

o Explanation:

▪ Use a stack to hold operators.

▪ Follow operator precedence and associativity rules.

o Code:

#include <stdio.h>

#include <ctype.h>
#define MAX 100

char stack[MAX];

int top = -1;

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

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

int precedence(char c) { return (c == '+' || c == '-') ? 1 : (c == '*' || c == '/') ? 2 : -1; }

void infixToPostfix(char* expr) {

for (int i = 0; expr[i] != '\0'; i++) {

if (isalnum(expr[i])) printf("%c", expr[i]);

else if (expr[i] == '(') push(expr[i]);

else if (expr[i] == ')') while (top != -1 && stack[top] != '(') printf("%c", pop()); pop();

else {

while (top != -1 && precedence(stack[top]) >= precedence(expr[i])) printf("%c", pop());

push(expr[i]);

while (top != -1) printf("%c", pop());

int main() {

char expr[] = "A+B*C-D";

infixToPostfix(expr);

return 0;

Sorting Algorithms

1. Insertion Sort (4 Marks)

o Explanation:

▪ Starts with the second element, inserting it into the correct position in the
sorted section.

o Code:
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;

2. Selection Sort (Quick Sort or Merge Sort) (4 Marks)

o Selection Sort Explanation:

▪ Repeatedly finds the minimum element and places it in the sorted portion.

o Code for Selection Sort:

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

for (int i = 0; i < n - 1; i++) {

int minIdx = i;

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[minIdx]) minIdx = j;

int temp = arr[minIdx];

arr[minIdx] = arr[i];

arr[i] = temp;

Tree and Graph Traversals

1. Binary Tree Traversal Functions (Inorder, Preorder, Postorder) (4 Marks)

o Explanation:

▪ Recursive functions to visit nodes in specific orders.


o Code for Inorder, Preorder, and Postorder Traversals:

void inorder(struct Node* node) {

if (node == NULL) return;

inorder(node->left);

printf("%d ", node->data);

inorder(node->right);

void preorder(struct Node* node) {

if (node == NULL) return;

printf("%d ", node->data);

preorder(node->left);

preorder(node->right);

void postorder(struct Node* node) {

if (node == NULL) return;

postorder(node->left);

postorder(node->right);

printf("%d ", node->data);

2. Graph Traversals (BFS and DFS) (5 Marks)

o DFS Explanation:

▪ Depth-first search using recursion or a stack.

o BFS Explanation:

▪ Breadth-first search using a queue.

o Code for DFS:

void DFS(int vertex, int visited[], int graph[][MAX], int n) {

printf("%d ", vertex);

visited[vertex] = 1;

for (int i = 0; i < n; i++) if (graph[vertex][i] && !visited[i]) DFS(i, visited, graph, n);

}
Queue Operations

1. Insert Element in Circular or Static Queue (4 Marks)

o Explanation:

▪ Circular queue uses modulo operation for wrapping around.

o Code:

void enqueue(int queue[], int* front, int* rear, int item, int size) {

if ((*rear + 1) % size == *front) return; // Queue is full

*rear = (*rear + 1) % size;

queue[*rear] = item;

if (*front == -1) *front = 0; // Initialize front if empty

2. Double-Ended Queue (Dequeue) (4 Marks)

o Explanation:

▪ Allows insertions and deletions from both front and rear ends.

o Code (Operations Illustration):

void insertFront(int deque[], int* front, int* rear, int item, int size) { /* Implementation */ }

void insertRear(int deque[], int* front, int* rear, int item, int size) { /* Implementation */ }

void deleteFront(int deque[], int* front, int* rear, int size) { /* Implementation */ }

void deleteRear(int deque[], int* front, int* rear, int size) { /* Implementation */ }

Miscellaneous Data Structure Programs

1. Binary Search (4 Marks)

o Explanation:

▪ Binary search on sorted data using divide and conquer.

o Code:

int binarySearch(int arr[], int l, int r, int x) {

while (l <= r) {

int mid = l + (r - l) / 2;

if (arr[mid] == x) return mid;

else if (arr[mid] < x) l = mid + 1;

else r = mid - 1;
}

return -1;

2. Polynomial Evaluation (5 Marks)

o Explanation:

▪ Evaluate polynomial by substituting values and summing up terms.

o Code:

int evaluatePolynomial(int coeff[], int exp[], int n, int x) {

int result = 0;

for (int i = 0; i < n; i++) result += coeff[i] * pow(x, exp[i]);

return result;

You might also like