The code below is for: (Programming language is C) a. Create a sorted linked list using tenStudent array (copy from array into the linked list will be done). b. Append an element to the end of a list c. Delete the last element from a list. d. Delete the nth element from a list. >>>>>>>>>>>>>>>>>>>>>>>>>> I need to complete the code to do : 1. Using the linked list which is populated at step a, Create a binary tree. The new tree will be created during the deletion of the linked list. The rest of question details in picture.. (Programming language is C) .. thank you The Code: #include #include struct student { int TC; char F_name[12]; char L_name[12]; int age; char gender[2]; }; struct student tenStudent[10] = { 123,"X1","Y1",21,"M", 234,"X2","Y2",26,"F", 128,"X3","Y3",18,"M", 432,"X4","Y4",27,"M", 287,"X5","Y5",34,"F", 423,"X6","Y6",21,"M", 634,"X7","Y7",16,"F", 828,"X8","Y8",15,"M", 252,"X9","Y9",27,"F", 887,"X10","Y10",34,"F", }; struct Linked_list_node { struct student info; struct Linked_list_node* next; }; struct Linked_list_node* head = NULL; void append(struct student info) { struct Linked_list_node* newNode = (struct Linked_list_node*)malloc(sizeof(struct Linked_list_node)); newNode->info = info; newNode->next = NULL; if (head == NULL) { head = newNode; return; } struct Linked_list_node* ptr = head; while (ptr->next != NULL) { ptr = ptr->next; } ptr->next = newNode; } void delete_last() { if (head == NULL) { printf("Linked list is empty"); return; } //if linked list contain only one element then make head=null if (head->next == NULL) { free(head); //delete the memory allocated for head node head = NULL; return; } struct Linked_list_node* ptr = head, * prev = NULL; while (ptr->next != NULL) { prev = ptr; ptr = ptr->next; } free(ptr); //delete memory allocated for last node prev->next = NULL; //make previous node of last node equals null } void delete_nth_node(int n) { if (head == NULL) { printf("Linked list empty"); return; } struct Linked_list_node* ptr = head, * prev = NULL, * temp; //check if linked list contain only one node,if yes than make change in head if (n == 1) { temp = head; head = head->next; free(temp); return; } //find nth node and store it in ptr pointer for (int i = 1; i < n && ptr != NULL; i++) { prev = ptr; ptr = ptr->next; } if (ptr == NULL) { printf("n is greater than size of linked list"); } else { prev->next = ptr->next; free(ptr); } } void sort_linked_list() { struct Linked_list_node* ptr1 = head; struct Linked_list_node* ptr2 = head; //sort the linked list on the basis of TC number while (ptr1 != NULL) { ptr2 = ptr1->next; while (ptr2 != NULL) { if (ptr1->info.TC > ptr2->info.TC) { struct student temp = ptr1->info; ptr1->info = ptr2->info; ptr2->info = temp; } ptr2 = ptr2->next; } ptr1 = ptr1->next; } } void print() { struct Linked_list_node* ptr = head; printf("***************LINKED LIST********************\n\n"); while (ptr != NULL) { printf("%d\t", ptr->info.TC); printf("%s\t", ptr->info.F_name); printf("%s\t", ptr->info.L_name); printf("%d\t", ptr->info.age); printf("%s\n", ptr->info.gender); ptr = ptr->next; } } int main() { //create a head node for linked list // head= (struct Linked_list_node*)malloc(sizeof(struct Linked_list_node)); //add first student to the linked list append(tenStudent[0]); head->info = tenStudent[0]; //set next pointer to null head->next = NULL; //assign a temporary pointer for linked list struct Linked_list_node* ptr = head; int i = 1; //for each student in the given array, add it to the linked list while (i < 10) { append(tenStudent[i]); i++; } sort_linked_list(); //print the linked list ptr = head; printf("Sorted linked list\n"); print(); //create a new student node struct student new_stu = { 600,"X11","Y11",56,"M" }; append(new_stu); //again print the linked list printf("Linked list after adding new node at the end\n"); print(); printf("Linked list after deleting node at the end\n"); delete_last(); print(); delete_nth_node(1); //linked list after deleting 1st node printf("Linked list after deleting 1st node\n"); print(); return 0; }
The code below is for: (Programming language is C)
a. Create a sorted linked list using tenStudent array (copy from array into the linked list will be done).
b. Append an element to the end of a list
c. Delete the last element from a list.
d. Delete the nth element from a list.
>>>>>>>>>>>>>>>>>>>>>>>>>>
I need to complete the code to do :
1. Using the linked list which is populated at step a, Create a binary tree. The new tree will be created during the deletion of the linked list.
The rest of question details in picture.. (Programming language is C) .. thank you
The Code:
#include<stdio.h>
#include<stdlib.h>
struct student
{
int TC;
char F_name[12];
char L_name[12];
int age;
char gender[2];
};
struct student tenStudent[10] = {
123,"X1","Y1",21,"M",
234,"X2","Y2",26,"F",
128,"X3","Y3",18,"M",
432,"X4","Y4",27,"M",
287,"X5","Y5",34,"F",
423,"X6","Y6",21,"M",
634,"X7","Y7",16,"F",
828,"X8","Y8",15,"M",
252,"X9","Y9",27,"F",
887,"X10","Y10",34,"F",
};
struct Linked_list_node
{
struct student info;
struct Linked_list_node* next;
};
struct Linked_list_node* head = NULL;
void append(struct student info) {
struct Linked_list_node* newNode = (struct Linked_list_node*)malloc(sizeof(struct Linked_list_node));
newNode->info = info;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
return;
}
struct Linked_list_node* ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newNode;
}
void delete_last()
{
if (head == NULL) {
printf("Linked list is empty");
return;
}
//if linked list contain only one element then make head=null
if (head->next == NULL)
{
free(head); //delete the memory allocated for head node
head = NULL;
return;
}
struct Linked_list_node* ptr = head, * prev = NULL;
while (ptr->next != NULL)
{
prev = ptr;
ptr = ptr->next;
}
free(ptr); //delete memory allocated for last node
prev->next = NULL; //make previous node of last node equals null
}
void delete_nth_node(int n)
{
if (head == NULL) {
printf("Linked list empty");
return;
}
struct Linked_list_node* ptr = head, * prev = NULL, * temp;
//check if linked list contain only one node,if yes than make change in head
if (n == 1) {
temp = head;
head = head->next;
free(temp);
return;
}
//find nth node and store it in ptr pointer
for (int i = 1; i < n && ptr != NULL; i++) {
prev = ptr;
ptr = ptr->next;
}
if (ptr == NULL) {
printf("n is greater than size of linked list");
}
else {
prev->next = ptr->next;
free(ptr);
}
}
void sort_linked_list()
{
struct Linked_list_node* ptr1 = head;
struct Linked_list_node* ptr2 = head;
//sort the linked list on the basis of TC number
while (ptr1 != NULL)
{
ptr2 = ptr1->next;
while (ptr2 != NULL)
{
if (ptr1->info.TC > ptr2->info.TC)
{
struct student temp = ptr1->info;
ptr1->info = ptr2->info;
ptr2->info = temp;
}
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
}
void print()
{
struct Linked_list_node* ptr = head;
printf("***************LINKED LIST********************\n\n");
while (ptr != NULL)
{
printf("%d\t", ptr->info.TC);
printf("%s\t", ptr->info.F_name);
printf("%s\t", ptr->info.L_name);
printf("%d\t", ptr->info.age);
printf("%s\n", ptr->info.gender);
ptr = ptr->next;
}
}
int main()
{
//create a head node for linked list
// head= (struct Linked_list_node*)malloc(sizeof(struct Linked_list_node));
//add first student to the linked list
append(tenStudent[0]);
head->info = tenStudent[0];
//set next pointer to null
head->next = NULL;
//assign a temporary pointer for linked list
struct Linked_list_node* ptr = head;
int i = 1;
//for each student in the given array, add it to the linked list
while (i < 10)
{
append(tenStudent[i]);
i++;
}
sort_linked_list();
//print the linked list
ptr = head;
printf("Sorted linked list\n");
print();
//create a new student node
struct student new_stu = { 600,"X11","Y11",56,"M" };
append(new_stu);
//again print the linked list
printf("Linked list after adding new node at the end\n");
print();
printf("Linked list after deleting node at the end\n");
delete_last();
print();
delete_nth_node(1);
//linked list after deleting 1st node
printf("Linked list after deleting 1st node\n");
print();
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images