Introduction to programming in C Week 8
Course Name: Introduction to programming in C
Course Link: Click Here
These are Introduction to programming in C Assignment 8 Answers
Question 1
In this question, you have to write code to remove the first node in a doubly linked list containing a specified number. The code to create the linked list is already given.
The main() function calls a function removeData(head,tail,n) to remove the first node containing data n from the linked list.
Complete the code by writing the missing function. You can also write additional functions which you may need.
These are Introduction to programming in C Assignment 8 Answers
Input Format
You will be given a positive number N in the first line.
This will be followed by a line containing N integers.
This will be followed by an integer M.
Output format
The output will be the list after deleting M.
Note : Only delete the first occurrence of M in list. If M is not present in the list, no change should happen to the list. Maintain the remaining elements in the original order.
Note : You do not have to add any additional code to print the list – once you code the removeData() function, the code will print the contents of the remaining list.
These are Introduction to programming in C Assignment 8 Answers
Solution
#include <stdio.h>
#include <stdlib.h>
struct DoublyLinkedList {
int data;
struct DoublyLinkedList *prev, *next;
};
void initializeList(struct DoublyLinkedList **head, struct DoublyLinkedList **tail, int data) {
*head = (struct DoublyLinkedList*) malloc(sizeof(struct DoublyLinkedList));
(*head)->data = data;
(*head)->prev = NULL;
(*head)->next = NULL;
*tail = *head;
}
void appendNode(struct DoublyLinkedList **tail, int data) {
struct DoublyLinkedList *newNode = (struct DoublyLinkedList*) malloc(sizeof(struct DoublyLinkedList));
newNode->data = data;
newNode->prev = *tail;
newNode->next = NULL;
(*tail)->next = newNode;
*tail = newNode;
}
void printList(struct DoublyLinkedList *head, struct DoublyLinkedList *tail) {
while (head != NULL) {
printf("%d", head->data);
if (head != tail) {
printf(",");
}
head = head->next;
}
}
void removeData(struct DoublyLinkedList **headptr, struct DoublyLinkedList **tailptr, int n) {
struct DoublyLinkedList *current = *headptr;
// Find the node to be removed
while (current != NULL && current->data != n) {
current = current->next;
}
// If the node is not found, do nothing
if (current == NULL) {
return;
}
// If the node is the head
if (current == *headptr) {
*headptr = current->next;
if (*headptr != NULL) {
(*headptr)->prev = NULL;
}
}
// If the node is the tail
else if (current == *tailptr) {
*tailptr = current->prev;
(*tailptr)->next = NULL;
}
// If the node is in the middle
else {
current->prev->next = current->next;
current->next->prev = current->prev;
}
free(current);
}
int main() {
int n;
int i = 0;
int m;
struct DoublyLinkedList *head, *tail;
scanf("%d", &n);
if (n <= 0) {
return 0;
}
scanf("%d", &m);
initializeList(&head, &tail, m);
for (i = 1; i < n; i++) {
/* read the remaining elements */
scanf("%d", &m);
appendNode(&tail, m);
}
scanf("%d", &n);
removeData(&head, &tail, n);
printList(head, tail);
return 0;
}
These are Introduction to programming in C Assignment 8 Answers
Question 2
You are given a non-negative number less than 10000000 (1 followed by 7 zeroes). You have to divide the number into the following components, and print them in the following order.
1. The part of the number which is less than a thousand.
2. The part of the number which is between a thousand and a lakh (10^5)
3. The part of the number which is between a lakh and a crore (10^7)
You should terminate printing when no higher power of 10 is present to be printed.
Sample input
134847
Sample Output
847
34
1
These are Introduction to programming in C Assignment 8 Answers
Solution
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
int crore = num / 10000000;
num %= 10000000;
int lakh = num / 100000;
num %= 100000;
int thousand = num / 1000;
num %= 1000;
int lessThanThousand = num;
if (crore > 0) {
printf("%d\n", lessThanThousand);
printf("%d\n", thousand);
printf("%d\n", lakh);
printf("%d", crore);
} else if (lakh > 0) {
printf("%d\n", lessThanThousand);
printf("%d\n", thousand);
printf("%d", lakh);
} else if (thousand > 0) {
printf("%d\n", lessThanThousand);
printf("%d", thousand);
} else {
printf("%d", lessThanThousand);
}
return 0;
}
These are Introduction to programming in C Assignment 8 Answers
More Weeks of Introduction to programming in C: Click Here
More Nptel courses: https://progiez.com/nptel
* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first. COPYING MATERIALS IS STRICTLY PROHIBITED.
These are Introduction to programming in C Assignment 8 Answers
More from PROGIEZ
