SlideShare a Scribd company logo
Tutorials-II
Problem 2: Write a program in C to insert a node in the
middle of a Singly Linked List.
Test Data and Expected Output :
Input the number of nodes (3 or more) : 4
Input data for node 1 : 10
Input data for node 2 : 20
Input data for node 3 : 30
Input data for node 4 : 40
Data entered in the list are :
Data = 10
Data = 20
Data = 30
Data = 40
Input data to insert in the list : 25
Input the position to insert new node : 3
Insertion completed successfully.
Updated Linked List:
Data entered in the list are :
Data = 10
Data = 20
Data = 25
Data = 30
Data = 40
Problem 1: Write a program in C to create
and display a Singly Linked List.
Test Data :
Input the number of nodes : 4
Input data for node 1 : 10
Input data for node 2 : 20
Input data for node 3 : 30
Input data for node 4 : 40
Expected Output :
Data entered in the list are:
Data = 10
Data = 20
Data = 30
Data = 40
Problem 3: Write a C program that converts a singly
linked list into an array and returns it.
Test Data and Expected Output :
Linked List: Convert a Singly Linked list into a string
-------------------------------------------------------------
Input the number of nodes: 3
Input data for node 1 : 10
Input data for node 2 : 20
Input data for node 3 : 30
Return data entered in the list as a string:
The linked list: 10 20 30
Problem 4: Write a C program to merge two sorted
singly linked lists into a single sorted linked list.
Test Data and Expected Output :
Two sorted singly linked lists:
1 7 19 29
3 13 23
After merging the said two sorted lists:
1 3 7 13 19 23 29
Problem 5: Write a Count() function that counts the
number of times a given int occurs in a list.
Problem 6: Write a GetNth() function that takes a
linked list and an integer index and returns the data
value stored in the node at that index position.
Problem 7: Write a function DeleteList() that takes a
list, deallocates all of its memory and sets its head
pointer to NULL (the empty list).
Problem 8: Suppose 5 employee works in Capgemini. Each employee is having details
as Name, Employee Id, CTC in LPA, Experience in years. Create a linked list that stores
details for 5 employees. Use C code.
Problem 9: Suppose 3 employee works in Google. Each employee is accompanied with
details as Name, Employee Id, CTC in LPA, Experience in years. Suppose, each employee
gets new password each day at the time they are reporting to the company (1st Day of
Joining). The password is to be assigned for today and a new password is to be displayed
for each employee for next day (login credentials for next day). The password needs to be
of 8 digits. Create a linked list that stores all these details for the employees. Use C code.
Problem 10: Given a 2-D matrix. Convert it into a linked list matrix such that each node is
linked to its next right and down node and display it. Use C Code.
*The idea is to create a new node for each element of the matrix and then create its next
down and right nodes in a recursive manner.
#include <stdio.h>
#include <stdlib.h>
typedef struct stud {
int Data;
struct stud* next;} node;
node *create_list() // Function to create a linked list
{
int k, n, count = 1;
node *p, *head;
printf ("n Input the number of nodes :");
scanf ("%d", &n);
for (k=0; k<n; k++) // Create the list
{
if (k == 0) {
head = (node *) malloc(sizeof(node));
p = head;
}
else {
p->next = (node *) malloc(sizeof(node));
p = p->next;
}
printf("n Input data for node %d:", count);
scanf ("%d", &p->Data);
count++;
}
p->next = NULL; // Set the last node's next to NULL
return (head);
}
void display (node *head) // Function to traverse and display a
linked list
{
node *p;
p = head;
while (p != NULL)
{
printf ("nData = %d ",
p->Data);
p = p->next;
}
printf ("n");
}
int main() {
node *head = NULL; // Declare head pointer
head = create_list(); // Create the list and assign it to head
display(head); // Display the list
return 0; // Return success
}
Problem 1: Singly Linked Link List
#include <stdio.h>
#include <stdlib.h>
typedef struct stud {
int Data;
struct stud* next;
struct stud* prev; // Pointer to the previous node
} node;
node* create_list() // Function to create a doubly linked list
{
int k, n, count = 1;
node *p, *head = NULL, *prev_node = NULL;
printf("n Input the number of nodes: ");
scanf("%d", &n);
for (k = 0; k < n; k++) // Create the list
{
if (k == 0) {
head = (node*) malloc(sizeof(node)); // Create the first node
p = head;
p->prev = NULL; // First node has no previous node
}
else {
p->next = (node*) malloc(sizeof(node)); // Allocate new node
prev_node = p; // Store the current node as previous before moving
p = p->next; // Move to the newly created node
p->prev = prev_node; // Link back to the previous node
}
printf("n Input data for node %d: ", count);
scanf("%d", &p->Data);
p->next = NULL; // Set next pointer to NULL initially
count++;
}
return head;
}
void display(node* head) // Function to traverse and display a doubly linked list
{
node *p = head;
printf("nForward traversal:n");
while (p != NULL) {
printf("Data = %dn", p->Data);
p = p->next;
}
}
void display_reverse(node* head) // Function to traverse in reverse
{
node *p = head;
// Traverse to the last node
while (p->next != NULL) {
p = p->next;
}
printf("nReverse traversal:n");
while (p != NULL) {
printf("Data = %dn", p->Data);
p = p->prev;
}
}
int main() {
node *head = NULL; // Declare head pointer
head = create_list(); // Create the doubly linked list
display(head); // Display the list in forward direction
display_reverse(head); // Display the list in reverse direction
return 0; // Return success
}
Problem 1: Doubly Linked Link List
Problem 1: Circular Linked Link List
#include <stdio.h>
#include <stdlib.h>
typedef struct stud {
int Data;
struct stud* next;
} node;
node *create_list() // Function to create a circular linked list
{
int k, n, count = 1;
node *p, *head, *tail = NULL; // Adding tail to connect last node to head
printf ("n Input the number of nodes: ");
scanf ("%d", &n);
for (k = 0; k < n; k++) // Create the list
{
if (k == 0) {
head = (node *) malloc(sizeof(node));
p = head;
}
else {
p->next = (node *) malloc(sizeof(node));
p = p->next;
}
printf("n Input data for node %d: ", count);
scanf("%d", &p->Data);
count++;
if (k == n - 1) {
tail = p; // Mark the last node
}
}
p->next = head; // Set the last node's next to head to make it circular
return head;
}
void display(node *head) // Function to traverse and display a
circular linked list
{
node *p = head;
if (head == NULL) {
printf("The list is empty.n");
return;
}
do {
printf("nData = %d", p->Data);
p = p->next;
} while (p != head); // Stop when we reach the head again
printf("n");
}
int main() {
node *head = NULL; // Declare head pointer
head = create_list(); // Create the list and assign it to head
display(head); // Display the list
return 0; // Return success
}
#include <stdio.h>
#include <stdlib.h>
typedef struct stud {
int Data;
struct stud* next;
} node;
node* create_list() // Function to create a linked list
{
int k, n, count = 1;
node *p, *head;
printf("nInput the number of nodes (3 or more) : ");
scanf("%d", &n);
for (k = 0; k < n; k++) // Create the list
{
if (k == 0) {
head = (node *) malloc(sizeof(node));
p = head;
}
else {
p->next = (node *) malloc(sizeof(node));
p = p->next;
}
printf("nInput data for node %d: ", count);
scanf("%d", &p->Data);
count++;
}
p->next = NULL; // Set the last node's next to NULL
return (head);
}
void display(node *head) // Function to traverse and display a linked list
{
printf("nData entered in the list are: ");
node *p = head;
while (p != NULL)
{
printf("nData = %d", p->Data);
p = p->next;
}
printf("n");
}
void insert_at_position(node **head_ref, int data, int position) {
node *new_node = (node *)malloc(sizeof(node));
new_node->Data = data;
// If inserting at the head (position 1)
if (position == 1) {
new_node->next = *head_ref;
*head_ref = new_node;
return;
}
node *temp = *head_ref;
for (int i = 1; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
// If position is greater than the number of nodes
if (temp == NULL) {
printf("nPosition is out of boundsn");
free(new_node);
return;
}
// Insert the node
new_node->next = temp->next;
temp->next = new_node;
printf("nInsertion completed successfully.");
}
int main() {
node *head = NULL; // Declare head pointer
int position, data;
head = create_list(); // Create the list and assign it to head
display(head); // Display the list
// Insert a new node at a specific position
printf("nInput data to insert in the list : ");
scanf("%d", &data);
printf("Input the position to insert new node : ");
scanf("%d", &position);
insert_at_position(&head, data, position);
printf("nUpdated Linked List:");
display(head); // Display the updated list
return 0;
}
Problem 2
#include <stdio.h>
#include <stdlib.h>
// Node structure for the linked list
struct Node {
int data;
struct Node* next;
};
// 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;
return newNode;
}
// Function to convert the linked list to an array
void linkedListToArray(struct Node* head, int* arr, int n) {
struct Node* temp = head;
for (int i = 0; i < n && temp != NULL; i++) {
arr[i] = temp->data;
temp = temp->next;
}
}
// Function to display the array
void displayArray(int* arr, int n) {
printf("The linked list: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("n");
}
int main() {
struct Node* head = NULL;
struct Node* temp = NULL;
int n, data;
printf("Input the number of nodes: ");
scanf("%d", &n);
// Creating the linked list
for (int i = 1; i <= n; i++) {
printf("Input data for node %d: ", i);
scanf("%d", &data);
struct Node* newNode = createNode(data);
if (head == NULL) {
head = newNode;
} else {
temp->next = newNode;
}
temp = newNode;
}
// Create an array to hold the linked list elements
int arr[n];
// Convert the linked list to an array
linkedListToArray(head, arr, n);
// Display the array
printf("nReturn data entered in the list as a string:n");
displayArray(arr, n);
return 0;
}
Problem 3
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* mergeSortedLists(struct Node* list1, struct Node* list2) {
struct Node* mergedList = NULL;
struct Node** lastPtrRef = &mergedList;
while (1) {
if (list1 == NULL) {
*lastPtrRef = list2;
break;
} else if (list2 == NULL) {
*lastPtrRef = list1;
break;
}
if (list1->data <= list2->data) {
*lastPtrRef = list1;
list1 = list1->next;
} else {
*lastPtrRef = list2;
list2 = list2->next;
}
lastPtrRef = &((*lastPtrRef)->next);
}
return mergedList;
}
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("n");
}
int main() {
struct Node* list1 = newNode(1);
list1->next = newNode(7);
list1->next->next = newNode(19);
list1->next->next->next = newNode(29);
struct Node* list2 = newNode(3);
list2->next = newNode(13);
list2->next->next = newNode(23);
struct Node* mergedList = mergeSortedLists(list1, list2);
printf("After merging the said two sorted lists:n");
printList(mergedList);
return 0;
}
Problem 4
Problem 5:
int Count(struct node* head, int searchFor) {
struct node* current = head;
int count = 0;
while (current != NULL) {
if (current->data == searchFor) count++;
current = current->next;
}
return count;
}
Problem 6:
int GetNth(struct node* head, int index) {
struct node* current = head;
int count = 0; // the index of the node we're currently looking at
while (current != NULL) {
if (count == index) return(current->data);
count++;
current = current->next;
}
assert(0); // if we get to this line, the caller was asking
// for a non-existent element so we assert fail.
}
Problem 7:
void DeleteList(struct node** headRef) {
struct node* current = *headRef; // deref headRef to get the real head
struct node* next;
while (current != NULL) {
next = current->next; // note the next pointer
free(current); // delete the node
current = next; // advance to the next node
}
*headRef = NULL; // Again, deref headRef to affect the real head back
// in the caller.
}
#include <stdio.h>
#include <stdlib.h>
// Define the structure for an employee
struct Employee {
char name[100];
int employeeId;
float CTC;
int experience;
struct Employee* next;
};
// Function to create a new employee node
struct Employee* createEmployee() {
struct Employee* newEmployee = (struct
Employee*)malloc(sizeof(struct Employee));
printf("Enter Name: ");
scanf(" %[^n]", newEmployee->name);
printf("Enter Employee ID: ");
scanf("%d", &newEmployee->employeeId);
printf("Enter CTC (in LPA): ");
scanf("%f", &newEmployee->CTC);
printf("Enter Experience (in years): ");
scanf("%d", &newEmployee->experience);
newEmployee->next = NULL; // Set the next pointer to NULL
return newEmployee;
}
// Function to add an employee to the list
void appendEmployee(struct Employee** head) {
struct Employee* newEmployee = createEmployee();
if (*head == NULL) {
*head = newEmployee; // If the list is empty, make this the head
} else {
struct Employee* temp = *head;
while (temp->next != NULL) { // Traverse to the end of the list
temp = temp->next;
}
temp->next = newEmployee; // Add the new employee at the end
}
}
// Function to display employee details
void displayEmployees(struct Employee* head) {
if (head == NULL) {
printf("No employees in the list.n");
return;
}
struct Employee* temp = head;
while (temp != NULL) {
printf("nName: %sn", temp->name);
printf("Employee ID: %dn", temp->employeeId);
printf("CTC: %.2f LPAn", temp->CTC);
printf("Experience: %d yearsn", temp->experience);
temp = temp->next; // Move to the next employee
}
}
Problem 8
int main() {
struct Employee* head = NULL;
int numEmployees = 20;
// Input and append 20 employees to the list
for (int i = 0; i < numEmployees; i++) {
printf("nEnter details for Employee %d:n", i + 1);
appendEmployee(&head);
}
// Display all employee details
printf("nEmployee Details:n");
displayEmployees(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define PASSWORD_LENGTH 8
// Define the structure for an employee
struct Employee {
char name[100];
int employeeId;
float CTC;
int experience;
char password[PASSWORD_LENGTH + 1]; // Field for storing the password
struct Employee* next;
};
// Function to create a random password
void generatePassword(char* password) {
const char charset[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (int i = 0; i < PASSWORD_LENGTH; i++) {
int randomIndex = rand() % (sizeof(charset) - 1);
password[i] = charset[randomIndex];
}
password[PASSWORD_LENGTH] = '0'; // Null-terminate the password
}
Problem 9
// Function to create a new employee node
struct Employee* createEmployee() {
struct Employee* newEmployee = (struct Employee*)malloc(sizeof(struct Employee));
printf("Enter Name: ");
scanf(" %[^n]", newEmployee->name);
printf("Enter Employee ID: ");
scanf("%d", &newEmployee->employeeId);
printf("Enter CTC: ");
scanf("%f", &newEmployee->CTC);
printf("Enter Experience (in years): ");
scanf("%d", &newEmployee->experience);
// Generate and assign a password to the employee
generatePassword(newEmployee->password);
newEmployee->next = NULL; // Set the next pointer to NULL
return newEmployee;
}
// Function to assign a new password each day
void assignDailyPassword(struct Employee* employee) {
generatePassword(employee->password); // Generate a new password
}
// Function to add an employee to the list
void appendEmployee(struct Employee** head) {
struct Employee* newEmployee = createEmployee();
if (*head == NULL) {
*head = newEmployee; // If the list is empty, make this the head
} else {
struct Employee* temp = *head;
while (temp->next != NULL) { // Traverse to the end of the list
temp = temp->next;
}
temp->next = newEmployee; // Add the new employee at the end
}
}
// Function to display employee details along with the current password
void displayEmployees(struct Employee* head) {
if (head == NULL) {
printf("No employees in the list.n");
return;
}
struct Employee* temp = head;
while (temp != NULL) {
printf("nName: %sn", temp->name);
printf("Employee ID: %dn", temp->employeeId);
printf("CTC: %.2fn", temp->CTC);
printf("Experience: %d yearsn", temp->experience);
printf("Password: %sn", temp->password); // Display the current password
temp = temp->next; // Move to the next employee
}
}
// Function to simulate daily reporting and password generation
void dailyReport(struct Employee* head) {
struct Employee* temp = head;
while (temp != NULL) {
assignDailyPassword(temp); // Assign a new password for the day
temp = temp->next;
}
}
int main() {
srand(time(0)); // Initialize random number generator for password
generation
struct Employee* head = NULL;
int numEmployees = 1; // Change this to 20 in real use
// Input and append employees to the list
for (int i = 0; i < numEmployees; i++) {
printf("nEnter details for Employee %d:n", i + 1);
appendEmployee(&head);
}
// Simulate the first day's reporting
printf("nEmployee Details (First Day):n");
displayEmployees(head);
// Simulate the second day where new passwords are assigned
printf("nAssigning new passwords for the next day...n");
dailyReport(head);
// Display employee details after new passwords are assigned
printf("nEmployee Details (Next Day):n");
displayEmployees(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node
struct Node {
int data;
struct Node *right;
struct Node *down;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->right = NULL;
newNode->down = NULL;
return newNode;
}
// Function to convert a 2D matrix into a linked list matrix
struct Node* convertToLinkedListMatrix(int rows, int cols, int matrix[rows][cols]) {
struct Node* head[rows][cols]; // Array of pointers to store the nodes
// Create all nodes
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
head[i][j] = createNode(matrix[i][j]);
}
}
Problem 10
// Link nodes together: Right and Down
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (j < cols - 1)
head[i][j]->right = head[i][j + 1]; // Link to the right
if (i < rows - 1)
head[i][j]->down = head[i + 1][j]; // Link downward
}
}
// Return the head of the matrix (top-left corner)
return head[0][0];
}
// Function to display the linked list matrix
void displayLinkedListMatrix(struct Node* head) {
struct Node* rowPtr = head;
while (rowPtr != NULL) {
struct Node* colPtr = rowPtr;
while (colPtr != NULL) {
printf("%d ", colPtr->data);
colPtr = colPtr->right; // Move to the next node in the row
}
printf("n");
rowPtr = rowPtr->down; // Move to the next row
}
}
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Convert the 2D matrix to a linked list matrix
struct Node* head = convertToLinkedListMatrix(3, 3, matrix);
// Display the linked list matrix
displayLinkedListMatrix(head);
return 0;
}
Thanks!

More Related Content

Similar to TutorialII_Updated____niceupdateprogram.pdf (20)

UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptxUNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
shesnasuneer
 
linkedlist.pptx
linkedlist.pptxlinkedlist.pptx
linkedlist.pptx
MeghaKulkarni27
 
Final ds record
Final ds recordFinal ds record
Final ds record
Ganisius Ganish
 
DS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docxDS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docx
VeerannaKotagi1
 
LINKED LIST.pptx
LINKED LIST.pptxLINKED LIST.pptx
LINKED LIST.pptx
ManojUniversity
 
Linked list
Linked listLinked list
Linked list
A. S. M. Shafi
 
Linked list
Linked listLinked list
Linked list
Lovely Professional University
 
What is Linked List in C.docx
What is Linked List in C.docxWhat is Linked List in C.docx
What is Linked List in C.docx
nona800027
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
Himadri Sen Gupta
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
Dabbal Singh Mahara
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
KasthuriKAssistantPr
 
Deleting a node from the list(SINGLE LINKED LIST)
Deleting a node from the list(SINGLE LINKED LIST)Deleting a node from the list(SINGLE LINKED LIST)
Deleting a node from the list(SINGLE LINKED LIST)
JayasankarShyam
 
Linked list part-2
Linked list part-2Linked list part-2
Linked list part-2
Soni Gupta
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
arnold 7490
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
skilljiolms
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
ChrisSosaJacob
 
ANOITO2341988888888888888888888885555.ppt
ANOITO2341988888888888888888888885555.pptANOITO2341988888888888888888888885555.ppt
ANOITO2341988888888888888888888885555.ppt
robertobula2
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
SherinRappai
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
AravindAnand21
 

Recently uploaded (20)

GROUP 7 CASE STUDY Real Life Incident.pptx
GROUP 7 CASE STUDY Real Life Incident.pptxGROUP 7 CASE STUDY Real Life Incident.pptx
GROUP 7 CASE STUDY Real Life Incident.pptx
mardoglenn21
 
Ethical Frameworks for Trustworthy AI – Opportunities for Researchers in Huma...
Ethical Frameworks for Trustworthy AI – Opportunities for Researchers in Huma...Ethical Frameworks for Trustworthy AI – Opportunities for Researchers in Huma...
Ethical Frameworks for Trustworthy AI – Opportunities for Researchers in Huma...
Karim Baïna
 
Splunk itsi infrastructure components implementation and integration
Splunk itsi infrastructure components implementation and integrationSplunk itsi infrastructure components implementation and integration
Splunk itsi infrastructure components implementation and integration
willmorekanan
 
An Algorithmic Test Using The Game of Poker
An Algorithmic Test Using The Game of PokerAn Algorithmic Test Using The Game of Poker
An Algorithmic Test Using The Game of Poker
Graham Ware
 
apidays New York 2025 - Agentic AI Future by Seena Ganesh (Staples)
apidays New York 2025 - Agentic AI Future by Seena Ganesh (Staples)apidays New York 2025 - Agentic AI Future by Seena Ganesh (Staples)
apidays New York 2025 - Agentic AI Future by Seena Ganesh (Staples)
apidays
 
RRTgvghfjfguyfgyuguyguyfgyfyfytffff.pptx
RRTgvghfjfguyfgyuguyguyfgyfyfytffff.pptxRRTgvghfjfguyfgyuguyguyfgyfyfytffff.pptx
RRTgvghfjfguyfgyuguyguyfgyfyfytffff.pptx
ravindersaini1616
 
Embracing AI in Project Management: Final Insights & Future Vision
Embracing AI in Project Management: Final Insights & Future VisionEmbracing AI in Project Management: Final Insights & Future Vision
Embracing AI in Project Management: Final Insights & Future Vision
KavehMomeni1
 
Ppt. AP Bio_ Lecture Presentation Ch. 09.ppt
Ppt. AP Bio_ Lecture Presentation Ch. 09.pptPpt. AP Bio_ Lecture Presentation Ch. 09.ppt
Ppt. AP Bio_ Lecture Presentation Ch. 09.ppt
jimmygoat123456789
 
Understanding LLM Temperature: A comprehensive Guide
Understanding LLM Temperature: A comprehensive GuideUnderstanding LLM Temperature: A comprehensive Guide
Understanding LLM Temperature: A comprehensive Guide
Tamanna36
 
time_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptxtime_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptx
stefanopinto1113
 
apidays New York 2025 - AI for All by Ananya Upadhyay (United Rentals, Inc.)
apidays New York 2025 - AI for All by Ananya Upadhyay (United Rentals, Inc.)apidays New York 2025 - AI for All by Ananya Upadhyay (United Rentals, Inc.)
apidays New York 2025 - AI for All by Ananya Upadhyay (United Rentals, Inc.)
apidays
 
Lec 12.pdfghhjjhhjkkkkkkkkkkkjfcvhiiugcvvh
Lec 12.pdfghhjjhhjkkkkkkkkkkkjfcvhiiugcvvhLec 12.pdfghhjjhhjkkkkkkkkkkkjfcvhiiugcvvh
Lec 12.pdfghhjjhhjkkkkkkkkkkkjfcvhiiugcvvh
saifalroby72
 
How Data Annotation Services Drive Innovation in Autonomous Vehicles.docx
How Data Annotation Services Drive Innovation in Autonomous Vehicles.docxHow Data Annotation Services Drive Innovation in Autonomous Vehicles.docx
How Data Annotation Services Drive Innovation in Autonomous Vehicles.docx
sofiawilliams5966
 
Data Analytics and visualization-PowerBi
Data Analytics and visualization-PowerBiData Analytics and visualization-PowerBi
Data Analytics and visualization-PowerBi
Krishnapriya975316
 
Blue Dark Professional Geometric Business Project Presentation .pdf
Blue Dark Professional Geometric Business Project Presentation .pdfBlue Dark Professional Geometric Business Project Presentation .pdf
Blue Dark Professional Geometric Business Project Presentation .pdf
mohammadhaidarayoobi
 
time_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptxtime_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptx
stefanopinto1113
 
IST606_SecurityManagement-slides_ 4 pdf
IST606_SecurityManagement-slides_ 4  pdfIST606_SecurityManagement-slides_ 4  pdf
IST606_SecurityManagement-slides_ 4 pdf
nwanjamakane
 
GST PPT-2 pdf version.pdfhhhhvgehrhhhrhgrhrhrhbrhrhrhhhrhrhrhhrhrhrhrhhrhrhrh
GST PPT-2 pdf version.pdfhhhhvgehrhhhrhgrhrhrhbrhrhrhhhrhrhrhhrhrhrhrhhrhrhrhGST PPT-2 pdf version.pdfhhhhvgehrhhhrhgrhrhrhbrhrhrhhhrhrhrhhrhrhrhrhhrhrhrh
GST PPT-2 pdf version.pdfhhhhvgehrhhhrhgrhrhrhbrhrhrhhhrhrhrhhrhrhrhrhhrhrhrh
rajat367791
 
The fundamental concept of nature of knowledge
The fundamental concept of nature of knowledgeThe fundamental concept of nature of knowledge
The fundamental concept of nature of knowledge
tarrebulehora
 
BADS-MBA-Unit 1 that what data science and Interpretation
BADS-MBA-Unit 1 that what data science and InterpretationBADS-MBA-Unit 1 that what data science and Interpretation
BADS-MBA-Unit 1 that what data science and Interpretation
srishtisingh1813
 
GROUP 7 CASE STUDY Real Life Incident.pptx
GROUP 7 CASE STUDY Real Life Incident.pptxGROUP 7 CASE STUDY Real Life Incident.pptx
GROUP 7 CASE STUDY Real Life Incident.pptx
mardoglenn21
 
Ethical Frameworks for Trustworthy AI – Opportunities for Researchers in Huma...
Ethical Frameworks for Trustworthy AI – Opportunities for Researchers in Huma...Ethical Frameworks for Trustworthy AI – Opportunities for Researchers in Huma...
Ethical Frameworks for Trustworthy AI – Opportunities for Researchers in Huma...
Karim Baïna
 
Splunk itsi infrastructure components implementation and integration
Splunk itsi infrastructure components implementation and integrationSplunk itsi infrastructure components implementation and integration
Splunk itsi infrastructure components implementation and integration
willmorekanan
 
An Algorithmic Test Using The Game of Poker
An Algorithmic Test Using The Game of PokerAn Algorithmic Test Using The Game of Poker
An Algorithmic Test Using The Game of Poker
Graham Ware
 
apidays New York 2025 - Agentic AI Future by Seena Ganesh (Staples)
apidays New York 2025 - Agentic AI Future by Seena Ganesh (Staples)apidays New York 2025 - Agentic AI Future by Seena Ganesh (Staples)
apidays New York 2025 - Agentic AI Future by Seena Ganesh (Staples)
apidays
 
RRTgvghfjfguyfgyuguyguyfgyfyfytffff.pptx
RRTgvghfjfguyfgyuguyguyfgyfyfytffff.pptxRRTgvghfjfguyfgyuguyguyfgyfyfytffff.pptx
RRTgvghfjfguyfgyuguyguyfgyfyfytffff.pptx
ravindersaini1616
 
Embracing AI in Project Management: Final Insights & Future Vision
Embracing AI in Project Management: Final Insights & Future VisionEmbracing AI in Project Management: Final Insights & Future Vision
Embracing AI in Project Management: Final Insights & Future Vision
KavehMomeni1
 
Ppt. AP Bio_ Lecture Presentation Ch. 09.ppt
Ppt. AP Bio_ Lecture Presentation Ch. 09.pptPpt. AP Bio_ Lecture Presentation Ch. 09.ppt
Ppt. AP Bio_ Lecture Presentation Ch. 09.ppt
jimmygoat123456789
 
Understanding LLM Temperature: A comprehensive Guide
Understanding LLM Temperature: A comprehensive GuideUnderstanding LLM Temperature: A comprehensive Guide
Understanding LLM Temperature: A comprehensive Guide
Tamanna36
 
time_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptxtime_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptx
stefanopinto1113
 
apidays New York 2025 - AI for All by Ananya Upadhyay (United Rentals, Inc.)
apidays New York 2025 - AI for All by Ananya Upadhyay (United Rentals, Inc.)apidays New York 2025 - AI for All by Ananya Upadhyay (United Rentals, Inc.)
apidays New York 2025 - AI for All by Ananya Upadhyay (United Rentals, Inc.)
apidays
 
Lec 12.pdfghhjjhhjkkkkkkkkkkkjfcvhiiugcvvh
Lec 12.pdfghhjjhhjkkkkkkkkkkkjfcvhiiugcvvhLec 12.pdfghhjjhhjkkkkkkkkkkkjfcvhiiugcvvh
Lec 12.pdfghhjjhhjkkkkkkkkkkkjfcvhiiugcvvh
saifalroby72
 
How Data Annotation Services Drive Innovation in Autonomous Vehicles.docx
How Data Annotation Services Drive Innovation in Autonomous Vehicles.docxHow Data Annotation Services Drive Innovation in Autonomous Vehicles.docx
How Data Annotation Services Drive Innovation in Autonomous Vehicles.docx
sofiawilliams5966
 
Data Analytics and visualization-PowerBi
Data Analytics and visualization-PowerBiData Analytics and visualization-PowerBi
Data Analytics and visualization-PowerBi
Krishnapriya975316
 
Blue Dark Professional Geometric Business Project Presentation .pdf
Blue Dark Professional Geometric Business Project Presentation .pdfBlue Dark Professional Geometric Business Project Presentation .pdf
Blue Dark Professional Geometric Business Project Presentation .pdf
mohammadhaidarayoobi
 
time_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptxtime_series_forecasting_constructor_uni.pptx
time_series_forecasting_constructor_uni.pptx
stefanopinto1113
 
IST606_SecurityManagement-slides_ 4 pdf
IST606_SecurityManagement-slides_ 4  pdfIST606_SecurityManagement-slides_ 4  pdf
IST606_SecurityManagement-slides_ 4 pdf
nwanjamakane
 
GST PPT-2 pdf version.pdfhhhhvgehrhhhrhgrhrhrhbrhrhrhhhrhrhrhhrhrhrhrhhrhrhrh
GST PPT-2 pdf version.pdfhhhhvgehrhhhrhgrhrhrhbrhrhrhhhrhrhrhhrhrhrhrhhrhrhrhGST PPT-2 pdf version.pdfhhhhvgehrhhhrhgrhrhrhbrhrhrhhhrhrhrhhrhrhrhrhhrhrhrh
GST PPT-2 pdf version.pdfhhhhvgehrhhhrhgrhrhrhbrhrhrhhhrhrhrhhrhrhrhrhhrhrhrh
rajat367791
 
The fundamental concept of nature of knowledge
The fundamental concept of nature of knowledgeThe fundamental concept of nature of knowledge
The fundamental concept of nature of knowledge
tarrebulehora
 
BADS-MBA-Unit 1 that what data science and Interpretation
BADS-MBA-Unit 1 that what data science and InterpretationBADS-MBA-Unit 1 that what data science and Interpretation
BADS-MBA-Unit 1 that what data science and Interpretation
srishtisingh1813
 

TutorialII_Updated____niceupdateprogram.pdf

  • 2. Problem 2: Write a program in C to insert a node in the middle of a Singly Linked List. Test Data and Expected Output : Input the number of nodes (3 or more) : 4 Input data for node 1 : 10 Input data for node 2 : 20 Input data for node 3 : 30 Input data for node 4 : 40 Data entered in the list are : Data = 10 Data = 20 Data = 30 Data = 40 Input data to insert in the list : 25 Input the position to insert new node : 3 Insertion completed successfully. Updated Linked List: Data entered in the list are : Data = 10 Data = 20 Data = 25 Data = 30 Data = 40 Problem 1: Write a program in C to create and display a Singly Linked List. Test Data : Input the number of nodes : 4 Input data for node 1 : 10 Input data for node 2 : 20 Input data for node 3 : 30 Input data for node 4 : 40 Expected Output : Data entered in the list are: Data = 10 Data = 20 Data = 30 Data = 40
  • 3. Problem 3: Write a C program that converts a singly linked list into an array and returns it. Test Data and Expected Output : Linked List: Convert a Singly Linked list into a string ------------------------------------------------------------- Input the number of nodes: 3 Input data for node 1 : 10 Input data for node 2 : 20 Input data for node 3 : 30 Return data entered in the list as a string: The linked list: 10 20 30 Problem 4: Write a C program to merge two sorted singly linked lists into a single sorted linked list. Test Data and Expected Output : Two sorted singly linked lists: 1 7 19 29 3 13 23 After merging the said two sorted lists: 1 3 7 13 19 23 29 Problem 5: Write a Count() function that counts the number of times a given int occurs in a list. Problem 6: Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Problem 7: Write a function DeleteList() that takes a list, deallocates all of its memory and sets its head pointer to NULL (the empty list).
  • 4. Problem 8: Suppose 5 employee works in Capgemini. Each employee is having details as Name, Employee Id, CTC in LPA, Experience in years. Create a linked list that stores details for 5 employees. Use C code. Problem 9: Suppose 3 employee works in Google. Each employee is accompanied with details as Name, Employee Id, CTC in LPA, Experience in years. Suppose, each employee gets new password each day at the time they are reporting to the company (1st Day of Joining). The password is to be assigned for today and a new password is to be displayed for each employee for next day (login credentials for next day). The password needs to be of 8 digits. Create a linked list that stores all these details for the employees. Use C code. Problem 10: Given a 2-D matrix. Convert it into a linked list matrix such that each node is linked to its next right and down node and display it. Use C Code. *The idea is to create a new node for each element of the matrix and then create its next down and right nodes in a recursive manner.
  • 5. #include <stdio.h> #include <stdlib.h> typedef struct stud { int Data; struct stud* next;} node; node *create_list() // Function to create a linked list { int k, n, count = 1; node *p, *head; printf ("n Input the number of nodes :"); scanf ("%d", &n); for (k=0; k<n; k++) // Create the list { if (k == 0) { head = (node *) malloc(sizeof(node)); p = head; } else { p->next = (node *) malloc(sizeof(node)); p = p->next; } printf("n Input data for node %d:", count); scanf ("%d", &p->Data); count++; } p->next = NULL; // Set the last node's next to NULL return (head); } void display (node *head) // Function to traverse and display a linked list { node *p; p = head; while (p != NULL) { printf ("nData = %d ", p->Data); p = p->next; } printf ("n"); } int main() { node *head = NULL; // Declare head pointer head = create_list(); // Create the list and assign it to head display(head); // Display the list return 0; // Return success } Problem 1: Singly Linked Link List
  • 6. #include <stdio.h> #include <stdlib.h> typedef struct stud { int Data; struct stud* next; struct stud* prev; // Pointer to the previous node } node; node* create_list() // Function to create a doubly linked list { int k, n, count = 1; node *p, *head = NULL, *prev_node = NULL; printf("n Input the number of nodes: "); scanf("%d", &n); for (k = 0; k < n; k++) // Create the list { if (k == 0) { head = (node*) malloc(sizeof(node)); // Create the first node p = head; p->prev = NULL; // First node has no previous node } else { p->next = (node*) malloc(sizeof(node)); // Allocate new node prev_node = p; // Store the current node as previous before moving p = p->next; // Move to the newly created node p->prev = prev_node; // Link back to the previous node } printf("n Input data for node %d: ", count); scanf("%d", &p->Data); p->next = NULL; // Set next pointer to NULL initially count++; } return head; } void display(node* head) // Function to traverse and display a doubly linked list { node *p = head; printf("nForward traversal:n"); while (p != NULL) { printf("Data = %dn", p->Data); p = p->next; } } void display_reverse(node* head) // Function to traverse in reverse { node *p = head; // Traverse to the last node while (p->next != NULL) { p = p->next; } printf("nReverse traversal:n"); while (p != NULL) { printf("Data = %dn", p->Data); p = p->prev; } } int main() { node *head = NULL; // Declare head pointer head = create_list(); // Create the doubly linked list display(head); // Display the list in forward direction display_reverse(head); // Display the list in reverse direction return 0; // Return success } Problem 1: Doubly Linked Link List
  • 7. Problem 1: Circular Linked Link List #include <stdio.h> #include <stdlib.h> typedef struct stud { int Data; struct stud* next; } node; node *create_list() // Function to create a circular linked list { int k, n, count = 1; node *p, *head, *tail = NULL; // Adding tail to connect last node to head printf ("n Input the number of nodes: "); scanf ("%d", &n); for (k = 0; k < n; k++) // Create the list { if (k == 0) { head = (node *) malloc(sizeof(node)); p = head; } else { p->next = (node *) malloc(sizeof(node)); p = p->next; } printf("n Input data for node %d: ", count); scanf("%d", &p->Data); count++; if (k == n - 1) { tail = p; // Mark the last node } } p->next = head; // Set the last node's next to head to make it circular return head; } void display(node *head) // Function to traverse and display a circular linked list { node *p = head; if (head == NULL) { printf("The list is empty.n"); return; } do { printf("nData = %d", p->Data); p = p->next; } while (p != head); // Stop when we reach the head again printf("n"); } int main() { node *head = NULL; // Declare head pointer head = create_list(); // Create the list and assign it to head display(head); // Display the list return 0; // Return success }
  • 8. #include <stdio.h> #include <stdlib.h> typedef struct stud { int Data; struct stud* next; } node; node* create_list() // Function to create a linked list { int k, n, count = 1; node *p, *head; printf("nInput the number of nodes (3 or more) : "); scanf("%d", &n); for (k = 0; k < n; k++) // Create the list { if (k == 0) { head = (node *) malloc(sizeof(node)); p = head; } else { p->next = (node *) malloc(sizeof(node)); p = p->next; } printf("nInput data for node %d: ", count); scanf("%d", &p->Data); count++; } p->next = NULL; // Set the last node's next to NULL return (head); } void display(node *head) // Function to traverse and display a linked list { printf("nData entered in the list are: "); node *p = head; while (p != NULL) { printf("nData = %d", p->Data); p = p->next; } printf("n"); } void insert_at_position(node **head_ref, int data, int position) { node *new_node = (node *)malloc(sizeof(node)); new_node->Data = data; // If inserting at the head (position 1) if (position == 1) { new_node->next = *head_ref; *head_ref = new_node; return; } node *temp = *head_ref; for (int i = 1; temp != NULL && i < position - 1; i++) { temp = temp->next; } // If position is greater than the number of nodes if (temp == NULL) { printf("nPosition is out of boundsn"); free(new_node); return; } // Insert the node new_node->next = temp->next; temp->next = new_node; printf("nInsertion completed successfully."); } int main() { node *head = NULL; // Declare head pointer int position, data; head = create_list(); // Create the list and assign it to head display(head); // Display the list // Insert a new node at a specific position printf("nInput data to insert in the list : "); scanf("%d", &data); printf("Input the position to insert new node : "); scanf("%d", &position); insert_at_position(&head, data, position); printf("nUpdated Linked List:"); display(head); // Display the updated list return 0; } Problem 2
  • 9. #include <stdio.h> #include <stdlib.h> // Node structure for the linked list struct Node { int data; struct Node* next; }; // 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; return newNode; } // Function to convert the linked list to an array void linkedListToArray(struct Node* head, int* arr, int n) { struct Node* temp = head; for (int i = 0; i < n && temp != NULL; i++) { arr[i] = temp->data; temp = temp->next; } } // Function to display the array void displayArray(int* arr, int n) { printf("The linked list: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("n"); } int main() { struct Node* head = NULL; struct Node* temp = NULL; int n, data; printf("Input the number of nodes: "); scanf("%d", &n); // Creating the linked list for (int i = 1; i <= n; i++) { printf("Input data for node %d: ", i); scanf("%d", &data); struct Node* newNode = createNode(data); if (head == NULL) { head = newNode; } else { temp->next = newNode; } temp = newNode; } // Create an array to hold the linked list elements int arr[n]; // Convert the linked list to an array linkedListToArray(head, arr, n); // Display the array printf("nReturn data entered in the list as a string:n"); displayArray(arr, n); return 0; } Problem 3
  • 10. #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* mergeSortedLists(struct Node* list1, struct Node* list2) { struct Node* mergedList = NULL; struct Node** lastPtrRef = &mergedList; while (1) { if (list1 == NULL) { *lastPtrRef = list2; break; } else if (list2 == NULL) { *lastPtrRef = list1; break; } if (list1->data <= list2->data) { *lastPtrRef = list1; list1 = list1->next; } else { *lastPtrRef = list2; list2 = list2->next; } lastPtrRef = &((*lastPtrRef)->next); } return mergedList; } struct Node* newNode(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next = NULL; return node; } void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("n"); } int main() { struct Node* list1 = newNode(1); list1->next = newNode(7); list1->next->next = newNode(19); list1->next->next->next = newNode(29); struct Node* list2 = newNode(3); list2->next = newNode(13); list2->next->next = newNode(23); struct Node* mergedList = mergeSortedLists(list1, list2); printf("After merging the said two sorted lists:n"); printList(mergedList); return 0; } Problem 4
  • 11. Problem 5: int Count(struct node* head, int searchFor) { struct node* current = head; int count = 0; while (current != NULL) { if (current->data == searchFor) count++; current = current->next; } return count; } Problem 6: int GetNth(struct node* head, int index) { struct node* current = head; int count = 0; // the index of the node we're currently looking at while (current != NULL) { if (count == index) return(current->data); count++; current = current->next; } assert(0); // if we get to this line, the caller was asking // for a non-existent element so we assert fail. } Problem 7: void DeleteList(struct node** headRef) { struct node* current = *headRef; // deref headRef to get the real head struct node* next; while (current != NULL) { next = current->next; // note the next pointer free(current); // delete the node current = next; // advance to the next node } *headRef = NULL; // Again, deref headRef to affect the real head back // in the caller. }
  • 12. #include <stdio.h> #include <stdlib.h> // Define the structure for an employee struct Employee { char name[100]; int employeeId; float CTC; int experience; struct Employee* next; }; // Function to create a new employee node struct Employee* createEmployee() { struct Employee* newEmployee = (struct Employee*)malloc(sizeof(struct Employee)); printf("Enter Name: "); scanf(" %[^n]", newEmployee->name); printf("Enter Employee ID: "); scanf("%d", &newEmployee->employeeId); printf("Enter CTC (in LPA): "); scanf("%f", &newEmployee->CTC); printf("Enter Experience (in years): "); scanf("%d", &newEmployee->experience); newEmployee->next = NULL; // Set the next pointer to NULL return newEmployee; } // Function to add an employee to the list void appendEmployee(struct Employee** head) { struct Employee* newEmployee = createEmployee(); if (*head == NULL) { *head = newEmployee; // If the list is empty, make this the head } else { struct Employee* temp = *head; while (temp->next != NULL) { // Traverse to the end of the list temp = temp->next; } temp->next = newEmployee; // Add the new employee at the end } } // Function to display employee details void displayEmployees(struct Employee* head) { if (head == NULL) { printf("No employees in the list.n"); return; } struct Employee* temp = head; while (temp != NULL) { printf("nName: %sn", temp->name); printf("Employee ID: %dn", temp->employeeId); printf("CTC: %.2f LPAn", temp->CTC); printf("Experience: %d yearsn", temp->experience); temp = temp->next; // Move to the next employee } } Problem 8
  • 13. int main() { struct Employee* head = NULL; int numEmployees = 20; // Input and append 20 employees to the list for (int i = 0; i < numEmployees; i++) { printf("nEnter details for Employee %d:n", i + 1); appendEmployee(&head); } // Display all employee details printf("nEmployee Details:n"); displayEmployees(head); return 0; }
  • 14. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define PASSWORD_LENGTH 8 // Define the structure for an employee struct Employee { char name[100]; int employeeId; float CTC; int experience; char password[PASSWORD_LENGTH + 1]; // Field for storing the password struct Employee* next; }; // Function to create a random password void generatePassword(char* password) { const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; for (int i = 0; i < PASSWORD_LENGTH; i++) { int randomIndex = rand() % (sizeof(charset) - 1); password[i] = charset[randomIndex]; } password[PASSWORD_LENGTH] = '0'; // Null-terminate the password } Problem 9
  • 15. // Function to create a new employee node struct Employee* createEmployee() { struct Employee* newEmployee = (struct Employee*)malloc(sizeof(struct Employee)); printf("Enter Name: "); scanf(" %[^n]", newEmployee->name); printf("Enter Employee ID: "); scanf("%d", &newEmployee->employeeId); printf("Enter CTC: "); scanf("%f", &newEmployee->CTC); printf("Enter Experience (in years): "); scanf("%d", &newEmployee->experience); // Generate and assign a password to the employee generatePassword(newEmployee->password); newEmployee->next = NULL; // Set the next pointer to NULL return newEmployee; } // Function to assign a new password each day void assignDailyPassword(struct Employee* employee) { generatePassword(employee->password); // Generate a new password }
  • 16. // Function to add an employee to the list void appendEmployee(struct Employee** head) { struct Employee* newEmployee = createEmployee(); if (*head == NULL) { *head = newEmployee; // If the list is empty, make this the head } else { struct Employee* temp = *head; while (temp->next != NULL) { // Traverse to the end of the list temp = temp->next; } temp->next = newEmployee; // Add the new employee at the end } } // Function to display employee details along with the current password void displayEmployees(struct Employee* head) { if (head == NULL) { printf("No employees in the list.n"); return; } struct Employee* temp = head; while (temp != NULL) { printf("nName: %sn", temp->name); printf("Employee ID: %dn", temp->employeeId); printf("CTC: %.2fn", temp->CTC); printf("Experience: %d yearsn", temp->experience); printf("Password: %sn", temp->password); // Display the current password temp = temp->next; // Move to the next employee } }
  • 17. // Function to simulate daily reporting and password generation void dailyReport(struct Employee* head) { struct Employee* temp = head; while (temp != NULL) { assignDailyPassword(temp); // Assign a new password for the day temp = temp->next; } } int main() { srand(time(0)); // Initialize random number generator for password generation struct Employee* head = NULL; int numEmployees = 1; // Change this to 20 in real use // Input and append employees to the list for (int i = 0; i < numEmployees; i++) { printf("nEnter details for Employee %d:n", i + 1); appendEmployee(&head); } // Simulate the first day's reporting printf("nEmployee Details (First Day):n"); displayEmployees(head); // Simulate the second day where new passwords are assigned printf("nAssigning new passwords for the next day...n"); dailyReport(head); // Display employee details after new passwords are assigned printf("nEmployee Details (Next Day):n"); displayEmployees(head); return 0; }
  • 18. #include <stdio.h> #include <stdlib.h> // Define the structure for a node struct Node { int data; struct Node *right; struct Node *down; }; // Function to create a new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->right = NULL; newNode->down = NULL; return newNode; } // Function to convert a 2D matrix into a linked list matrix struct Node* convertToLinkedListMatrix(int rows, int cols, int matrix[rows][cols]) { struct Node* head[rows][cols]; // Array of pointers to store the nodes // Create all nodes for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { head[i][j] = createNode(matrix[i][j]); } } Problem 10
  • 19. // Link nodes together: Right and Down for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (j < cols - 1) head[i][j]->right = head[i][j + 1]; // Link to the right if (i < rows - 1) head[i][j]->down = head[i + 1][j]; // Link downward } } // Return the head of the matrix (top-left corner) return head[0][0]; } // Function to display the linked list matrix void displayLinkedListMatrix(struct Node* head) { struct Node* rowPtr = head; while (rowPtr != NULL) { struct Node* colPtr = rowPtr; while (colPtr != NULL) { printf("%d ", colPtr->data); colPtr = colPtr->right; // Move to the next node in the row } printf("n"); rowPtr = rowPtr->down; // Move to the next row } } int main() { int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Convert the 2D matrix to a linked list matrix struct Node* head = convertToLinkedListMatrix(3, 3, matrix); // Display the linked list matrix displayLinkedListMatrix(head); return 0; }