Solution and explanation
Expected output
Q16: 10 -> 20 -> 30 -> NULL
Q17: 5 -> 10 -> 20 -> 30 -> NULL
Q18: 5 -> 10 -> 20 -> 30 -> 40 -> NULL
Q19: 5 -> 10 -> 30 -> 40 -> NULL
Q20: Position of 30 = 2
Position of 99 = -1
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node* createNode(int value) {
Node *node = (Node*)malloc(sizeof(Node));
if (node == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
node->data = value;
node->next = NULL;
return node;
}
// Q16: Create and display a linked list
void display(Node *head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
// Q17: Insert at the beginning
Node* insertBeginning(Node *head, int value) {
Node *node = createNode(value);
node->next = head;
return node;
}
// Q18: Insert at the end
Node* insertEnd(Node *head, int value) {
Node *node = createNode(value);
if (head == NULL) return node;
Node *current = head;
while (current->next != NULL)
current = current->next;
current->next = node;
return head;
}
// Q19: Delete the first node containing a value
Node* deleteValue(Node *head, int value) {
if (head == NULL) return NULL;
if (head->data == value) {
Node *temp = head->next;
free(head);
return temp;
}
Node *current = head;
while (current->next != NULL &&
current->next->data != value) {
current = current->next;
}
if (current->next != NULL) {
Node *temp = current->next;
current->next = temp->next;
free(temp);
}
return head;
}
// Q20: Search for a value; return its zero-based position
int search(Node *head, int value) {
int position = 0;
while (head != NULL) {
if (head->data == value) return position;
head = head->next;
position++;
}
return -1;
}
void freeList(Node *head) {
while (head != NULL) {
Node *temp = head;
head = head->next;
free(temp);
}
}
int main(void) {
Node *head = NULL;
// Q16: Build a list
head = insertEnd(head, 10);
head = insertEnd(head, 20);
head = insertEnd(head, 30);
printf("Q16: ");
display(head);
// Q17
head = insertBeginning(head, 5);
printf("Q17: ");
display(head);
// Q18
head = insertEnd(head, 40);
printf("Q18: ");
display(head);
// Q19
head = deleteValue(head, 20);
printf("Q19: ");
display(head);
// Q20
printf("Q20: Position of 30 = %d\n", search(head, 30));
printf("Position of 99 = %d\n", search(head, 99));
freeList(head);
return 0;
}
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
// Q16: Display linked list
void display(Node* head) {
while (head != nullptr) {
cout << head->data << " -> ";
head = head->next;
}
cout << "NULL\n";
}
// Q17: Insert at beginning
Node* insertBeginning(Node* head, int value) {
Node* node = new Node(value);
node->next = head;
return node;
}
// Q18: Insert at end
Node* insertEnd(Node* head, int value) {
Node* node = new Node(value);
if (head == nullptr) return node;
Node* current = head;
while (current->next != nullptr)
current = current->next;
current->next = node;
return head;
}
// Q19: Delete first occurrence
Node* deleteValue(Node* head, int value) {
if (head == nullptr) return nullptr;
if (head->data == value) {
Node* temp = head->next;
delete head;
return temp;
}
Node* current = head;
while (current->next != nullptr &&
current->next->data != value) {
current = current->next;
}
if (current->next != nullptr) {
Node* temp = current->next;
current->next = temp->next;
delete temp;
}
return head;
}
// Q20: Search; return zero-based position or -1
int search(Node* head, int value) {
int position = 0;
while (head != nullptr) {
if (head->data == value) return position;
head = head->next;
position++;
}
return -1;
}
void freeList(Node* head) {
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
}
int main() {
Node* head = nullptr;
head = insertEnd(head, 10);
head = insertEnd(head, 20);
head = insertEnd(head, 30);
cout << "Q16: ";
display(head);
head = insertBeginning(head, 5);
cout << "Q17: ";
display(head);
head = insertEnd(head, 40);
cout << "Q18: ";
display(head);
head = deleteValue(head, 20);
cout << "Q19: ";
display(head);
cout << "Q20: Position of 30 = " << search(head, 30) << '\n';
cout << "Position of 99 = " << search(head, 99) << '\n';
freeList(head);
return 0;
}
Java
public class LinkedListQuestions {
static class Node {
int data;
Node next;
Node(int value) {
data = value;
next = null;
}
}
// Q16: Display linked list
static void display(Node head) {
while (head != null) {
System.out.print(head.data + " -> ");
head = head.next;
}
System.out.println("NULL");
}
// Q17: Insert at beginning
static Node insertBeginning(Node head, int value) {
Node node = new Node(value);
node.next = head;
return node;
}
// Q18: Insert at end
static Node insertEnd(Node head, int value) {
Node node = new Node(value);
if (head == null) return node;
Node current = head;
while (current.next != null)
current = current.next;
current.next = node;
return head;
}
// Q19: Delete first occurrence
static Node deleteValue(Node head, int value) {
if (head == null) return null;
if (head.data == value)
return head.next;
Node current = head;
while (current.next != null &&
current.next.data != value) {
current = current.next;
}
if (current.next != null)
current.next = current.next.next;
return head;
}
// Q20: Search; return zero-based position or -1
static int search(Node head, int value) {
int position = 0;
while (head != null) {
if (head.data == value) return position;
head = head.next;
position++;
}
return -1;
}
public static void main(String[] args) {
Node head = null;
head = insertEnd(head, 10);
head = insertEnd(head, 20);
head = insertEnd(head, 30);
System.out.print("Q16: ");
display(head);
head = insertBeginning(head, 5);
System.out.print("Q17: ");
display(head);
head = insertEnd(head, 40);
System.out.print("Q18: ");
display(head);
head = deleteValue(head, 20);
System.out.print("Q19: ");
display(head);
System.out.println("Q20: Position of 30 = " + search(head, 30));
System.out.println("Position of 99 = " + search(head, 99));
}
}