Introduction to Programming in C Week 8
Session: JAN-APR 2024
Course Name: Introduction to Programming in C
Course Link: Click Here
For answers or latest updates join our telegram channel: Click here to join
These are Introduction to Programming in C Assignment 8 Answers
Question 1
Deletion from Doubly linked list
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.
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.
Solution:
#include <stdio.h>
#include <stdlib.h>
struct DoublyLinkedList {
int data;
struct DoublyLinkedList * prev;
struct DoublyLinkedList * next;
};
/*
--------------------------
Create a new node
--------------------------
*/
struct DoublyLinkedList * createNode(int n) {
struct DoublyLinkedList * newNodeptr;
newNodeptr = (struct DoublyLinkedList * )
malloc(sizeof(struct DoublyLinkedList));
newNodeptr -> data = n;
newNodeptr -> prev = NULL;
newNodeptr -> next = NULL;
return newNodeptr;
}
/*
--------------------------------
add a node at the end of a doubly linked list.
Tailptr is the address of the pointer to the end of the current list.
After adding the node, tail points to the new node inserted.
--------------------------------
*/
void appendNode(struct DoublyLinkedList ** tailptr, int n) {
struct DoublyLinkedList * newNode;
newNode = createNode(n);
newNode -> prev = * tailptr;
( * tailptr) -> next = newNode;
* tailptr = newNode;
}
void initializeList(
struct DoublyLinkedList ** headptr,
struct DoublyLinkedList ** tailptr,
int n) {
struct DoublyLinkedList * newNode;
newNode = createNode(n);
* headptr = newNode;
* tailptr = newNode;
return;
}
void printList(
struct DoublyLinkedList * head,
struct DoublyLinkedList * tail) {
struct DoublyLinkedList * curr = head;
while (curr != NULL) {
if (curr -> next != NULL)
printf("%d,", curr -> data);
else printf("%d", curr -> data);
curr = curr -> next;
}
return;
}
/*
---------------------------------------
remove the node that contains data as n
---------------------------------------
*/
void removeData(
struct DoublyLinkedList ** headptr,
struct DoublyLinkedList ** tailptr,
int n) {
struct DoublyLinkedList *curr = *headptr;
while (curr != NULL) {
if (curr->data == n) {
if (curr->prev == NULL) {
// If the node to be removed is the head
*headptr = curr->next;
if (*headptr != NULL) {
(*headptr)->prev = NULL;
}
free(curr);
return;
} else {
// If the node to be removed is not the head
curr->prev->next = curr->next;
if (curr->next != NULL) {
curr->next->prev = curr->prev;
}
free(curr);
return;
}
}
curr = curr->next;
}
}
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;
}
For answers or latest updates join our telegram channel: Click here to join
These are Introduction to Programming in C Assignment 8 Answers
Question 2
Priority Queue using Linked List
In this question, a linked list is partially implemented where each element in the linked list is a structure of the following format:
struct node{
int id;
int priority;
struct node *next;
};
Solution:
#include <stdio.h>
#include <stdlib.h>
struct node {
int id;
int priority;
struct node *next;
};
struct node *create_node(int id, int val) {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->id = id;
newNode->priority = val;
newNode->next = NULL;
return newNode;
}
struct node *append(struct node *list, struct node *e) {
e->next = list;
return e;
}
struct node *search(struct node *list, int id) {
struct node *curr = list;
while (curr != NULL) {
if (curr->id == id) {
return curr;
}
curr = curr->next;
}
return NULL;
}
void change_priority(struct node *list, int id, int val) {
struct node *e = search(list, id);
if (e != NULL) {
e->priority = val;
}
}
struct node *delete_node(struct node *list, struct node *e) {
if (e == NULL) {
return list;
}
if (e == list) {
struct node *newHead = e->next;
free(e);
return newHead;
}
struct node *curr = list;
while (curr->next != NULL) {
if (curr->next == e) {
curr->next = e->next;
break;
}
curr = curr->next;
}
free(e);
return list;
}
struct node *extract_max(struct node *list) {
struct node *maxNode = NULL;
struct node *curr = list;
while (curr != NULL) {
if (maxNode == NULL || curr->priority > maxNode->priority) {
maxNode = curr;
}
curr = curr->next;
}
if (maxNode != NULL) {
printf("Max: %d\n", maxNode->id);
} else {
printf("Max: -1\n");
}
return delete_node(list, maxNode);
}
int find_priority(struct node *list, int id)
{
struct node *e = search(list, id);
if (e != NULL)
return e->priority;
return -1;
}
int main() {
char op;
int id, val;
struct node *list = NULL;
int flag = 1;
do {
scanf("%c ", &op);
switch (op) {
case 'A':
scanf("%d %d", &id, &val);
list = append(list, create_node(id, val));
break;
case 'S':
scanf("%d", &id);
printf("%d %d\n", id, find_priority(list, id));
break;
case 'C':
scanf("%d %d", &id, &val);
change_priority(list, id, val);
break;
case 'M':
list = extract_max(list);
break;
case 'E':
flag = 0;
break;
}
} while (flag == 1);
return 0;
}
For answers or latest updates join our telegram channel: Click here to join
These are Introduction to Programming in C Assignment 8 Answers
More Solutions of Introduction to Programming in C: Click Here
More NPTEL Solutions: https://progiez.com/nptel-assignment-answers/
These are Introduction to Programming in C Assignment 8 Answers
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.
These are Introduction to Programming in C Assignment 8 Answers
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
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)
These are Introduction to Programming in C Assignment 8 Answers
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
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
These are Introduction to Programming in C Assignment 8 Answers