Introduction to Programming in C Week 8 Answers
Week 8: Assignment 8-Question 1
Due on 2022-04-21, 23:59 IST
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 n from the linked list.
Complete the code by writing the missing function. You can also write additional functions which you may need.
Solution:
#include < stdio.h>
#include < stdlib.h>
struct DoublyLinkedList
{
int data;
struct DoublyLinkedList *prev;
struct DoublyLinkedList *next;
};
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;
}
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;
printf("%d",curr->data);
curr = curr->next;
while (curr != NULL)
{
printf(",%d",curr->data);
curr = curr->next;
}
return;
}
void removeData(struct DoublyLinkedList **head,struct DoublyLinkedList **tail,int data)
{
int pos = 0;
struct DoublyLinkedList *pre_node;
if((*head)->data == data)
{
if((*head)->next != NULL)
{
(*head)->next->prev = NULL;
*head = (*head)->next;
return;
}
else
{
*head = NULL;
return;
}
}
else if((*head)->data != data && (*head)->next == NULL)
{
return;
}
struct DoublyLinkedList *curr=*head;
while(curr->next != NULL && curr->data != data)
{
pre_node = curr;
curr = curr->next;
}
if(curr->data == data)
{
pre_node->next = pre_node->next->next;
if(pre_node->next != NULL)
{
pre_node->next->prev = pre_node;
}
else
*tail = pre_node;
free(curr);
}
}
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++)
{
scanf("%d",&m);
appendNode(&tail,m);
}
scanf("%d",&n);
removeData(&head,&tail,n);
printList(head,tail);
return 0;
}
Week 8:Assignment 8-Question 2
Due on 2022-04-21, 23:59 IST
You are given a non-negative number less than or equal to 100000000 (1 followed by 8 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 3. The part of the number which is between a lakh and a crore and so on. You should terminate printing when no higher power of 10 is present to be printed.
Solution: Private Case Passed
#include < stdio.h >
int main()
{
int n;
int divisor;
scanf("%d",&n);
divisor=1000;
printf("%d\n",n%divisor);
n=n/divisor;
divisor=100;
while(n!=0)
{
printf("%d\n",n%divisor);
n=n/divisor;
}
}