Doubly Linked
Single File Programming Question
Problem Statement
Krishna needs to create a doubly linked list to store and display a sequence of integers. Your task
is to help write a program to read a list of integers from input, store them in a doubly linked list,
and then display the list.
Input format :
The first line of input consists of an integer n, representing the number of integers in the list.
The second line of input consists of n space-separated integers.
Output format :
The output prints a single line displaying the integers in the order they were added to the doubly
linked list, separated by spaces.
Refer to the sample output for the formatting specifications.
Code constraints :
In this scenario, given test cases will fall under the following constraints:
0 ≤ n ≤ 10
1 ≤ elements ≤ 150
Sample test cases :
Input 1 :
5
1 2 3 4 5
Output 1 :
1 2 3 4 5
Input 2 :
8
90 80 70 60 50 40 30 20
Output 2 :
90 80 70 60 50 40 30 20
Input 3 :
0
Output 3 :
List is empty
Key:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
Node* head = NULL;
void insert(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
void display() {
if (head == NULL) {
printf("List is empty\n");
return;
}
Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
int n;
scanf("%d", &n);
if (n == 0) {
printf("List is empty\n");
return 0;
}
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insert(value);
}
display();
return 0;
}
2.
Problem Statement
Tom is a software developer working on a project where he has to check if a doubly linked list is
a palindrome. He needs to write a program to solve this problem. Write a program to help Tom
check if a given doubly linked list is a palindrome or not.
Input format :
The first line consists of an integer N, representing the number of elements in the linked list.
The second line consists of N space-separated integers representing the linked list elements.
Output format :
The first line displays the space-separated integers, representing the doubly linked list.
The second line displays one of the following:
1. If the doubly linked list is a palindrome, print "The doubly linked list is a palindrome".
2. If the doubly linked list is not a palindrome, print "The doubly linked list is not a
palindrome".
Refer to the sample output for the formatting specifications.
Code constraints :
In this scenario, the test cases fall under the following constraints:
2 ≤ N ≤ 20
-100 ≤ elements ≤ 100
Sample test cases :
Input 1 :
5
1 2 3 2 1
Output 1 :
1 2 3 2 1
The doubly linked list is a palindrome
Input 2 :
5
1 2 3 4 5
Output 2 :
1 2 3 4 5
The doubly linked list is not a palindrome
Input 3 :
6
-1 -2 -3 -3 -2 -1
Output 3 :
-1 -2 -3 -3 -2 -1
The doubly linked list is a palindrome
Key:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
int isPalindrome(Node* head) {
Node* tail = head;
while (tail->next != NULL) {
tail = tail->next;
}
while (head != NULL && tail != NULL && head != tail && head->prev != tail)
{
if (head->data != tail->data) {
return 0; // Not a palindrome
}
head = head->next;
tail = tail->prev;
}
return 1; // Is a palindrome
}
int main() {
int N;
scanf("%d", &N);
Node* head = NULL;
Node* tail = NULL;
for (int i = 0; i < N; i++) {
int value;
scanf("%d", &value);
Node* newNode = createNode(value);
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
// Print the doubly linked list
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
// Check if it is a palindrome
if (isPalindrome(head)) {
printf("The doubly linked list is a palindrome\n");
} else {
printf("The doubly linked list is not a palindrome\n");
}
return 0;
}
3. Problem Statement
You are given an unsorted doubly linked list containing n nodes that are appended to the front of
the list. Write a program to remove duplicate nodes from the given list.
Input format :
The first line of input consists of an integer n, representing the number of elements in the list.
The second line of input consists of n space-separated integers representing the list elements.
Output format :
The output prints the final after removing duplicate nodes, separated by a space.
Refer to the sample output for formatting specifications.
Code constraints :
In this scenario, the test cases fall under the following constraints:
2 ≤ n ≤ 30
1 ≤ elements in the list ≤ 100
Sample test cases :
Input 1 :
10
12 12 10 4 8 4 6 4 4 8
Output 1 :
8 4 6 10 12
Input 2 :
4
1 1 2 2
Output 2 :
2 1
Key:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
void removeDuplicates(Node** head) {
Node* current = *head;
Node* temp;
int hash[101] = {0}; // Assuming elements are between 1 and 100
while (current != NULL) {
if (hash[current->data] == 0) {
hash[current->data] = 1; // Mark as seen
} else {
// Duplicate found, remove it
temp = current;
if (current->prev) {
current->prev->next = current->next;
}
if (current->next) {
current->next->prev = current->prev;
}
current = current->next;
free(temp);
continue;
}
current = current->next;
}
}
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
int n;
scanf("%d", &n);
Node* head = NULL;
Node* tail = NULL;
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
Node* newNode = createNode(value);
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
}
head = newNode;
if (tail == NULL) {
tail = newNode;
}
}
removeDuplicates(&head);
printList(head);
return 0;
}
4.
Problem Statement
Sam is learning about two-way linked lists. He came across a problem where he had to populate
a two-way linked list and print the original as well as the reverse order of the list. Assist him with
a suitable program.
Input format :
The first line of input consists of an integer n, representing the number of elements in the list.
The second line consists of n space-separated integers, representing the elements.
Output format :
The first line displays the message: "List in original order:"
The second line displays the elements of the doubly linked list in the original order.
The third line displays the message: "List in reverse order:"
The fourth line displays the elements of the doubly linked list in reverse order.
Refer to the sample output for the formatting specifications.
Code constraints :
1 ≤ n ≤ 30
Sample test cases :
Input 1 :
5
1 2 3 4 5
Output 1 :
List in original order:
1 2 3 4 5
List in reverse order:
5 4 3 2 1
Input 2 :
8
45 63 95 74 14 25 36 96
Output 2 :
List in original order:
45 63 95 74 14 25 36 96
List in reverse order:
96 36 25 14 74 95 63 45
Key:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
void printList(Node* head) {
Node* temp = head;
printf("List in original order:\n");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void printReverse(Node* tail) {
printf("List in reverse order:\n");
while (tail != NULL) {
printf("%d ", tail->data);
tail = tail->prev;
}
printf("\n");
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
Node* head = NULL;
Node* tail = NULL;
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
Node* newNode = createNode(value);
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
printList(head);
printReverse(tail);
// Free memory (not shown for brevity)
return 0;
}
5.
Problem Statement
Saran is tasked with implementing a doubly linked list in data structures, allowing users to
append elements to the end of the list, and remove the last element. He needs to ensure efficient
memory management in the implementation.
Assist him with a suitable program.
Input format :
The first line of input consists of an integer n, representing the size of the list.
The second line consists of n space-separated integers, representing the elements of the list.
Output format :
The output displays integers, representing the list after removing the last element, separated by a
space.
Refer to the sample output for formatting specifications.
Code constraints :
The given test cases fall under the following constraints:
1 ≤ n ≤ 30
1 ≤ elements of the list ≤ 100
Sample test cases :
Input 1 :
5
48 22 5 62 87
Output 1 :
48 22 5 62
Input 2 :
7
65 20 87 61 44 67 31
Output 2 :
65 20 87 61 44 67
Key:
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the doubly linked list
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Function to append a node to the end of the list
void append(struct Node** head_ref, int new_data) {
struct Node* new_node = createNode(new_data);
struct Node* last = *head_ref;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
new_node->prev = last;
}
// Function to remove the last node from the list
void removeLast(struct Node** head_ref) {
if (*head_ref == NULL) return;
struct Node* last = *head_ref;
while (last->next != NULL) {
last = last->next;
}
if (last->prev != NULL) {
last->prev->next = NULL;
} else {
*head_ref = NULL; // List becomes empty
}
free(last);
}
// Function to print the list
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
int n;
scanf("%d", &n);
struct Node* head = NULL;
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
append(&head, value);
}
removeLast(&head);
printList(head);
return 0;
}