Doubly Linked
Doubly Linked
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.
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.
Code constraints :
In this scenario, given test cases will fall under the following constraints:
0 ≤ n ≤ 10
1 ≤ elements ≤ 150
Key:
#include <stdio.h>
#include <stdlib.h>
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;
}
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.
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".
Code constraints :
In this scenario, the test cases fall under the following constraints:
2 ≤ N ≤ 20
Key:
#include <stdio.h>
#include <stdlib.h>
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);
// 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.
Code constraints :
In this scenario, the test cases fall under the following constraints:
2 ≤ n ≤ 30
Key:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
Node* head = NULL;
Node* tail = NULL;
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.
Output format :
The second line displays the elements of the doubly linked list in the original order.
The fourth line displays the elements of the doubly linked list in reverse order.
Code constraints :
1 ≤ n ≤ 30
Key:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printList(head);
printReverse(tail);
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.
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.
Code constraints :
The given test cases fall under the following constraints:
1 ≤ n ≤ 30
Key:
#include <stdio.h>
#include <stdlib.h>
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
new_node->prev = last;
}
if (last->prev != NULL) {
last->prev->next = NULL;
} else {
*head_ref = NULL; // List becomes empty
}
free(last);
}
int main() {
int n;
scanf("%d", &n);
removeLast(&head);
printList(head);
return 0;
}