Question

In: Computer Science

Write down a C program which will create a list (simple linear linked list) of nodes....

Write down a C program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a grade-point average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote (by printing student’s information on screen). (Please comment the codes)

Solutions

Expert Solution

//C program

#include<stdio.h>
#include<stdlib.h>


//structure student
struct student{
   int id;
   float grade;
};

//Node structure
struct Node{
   struct student* st;
   struct Node*next;
};

//adding new node to link list
struct Node* addNode(struct Node * head , int id , float grade){
   //declaring and memory allocation to struct student pointer
   struct student * newSt = (struct student*) malloc(sizeof(struct student));
   newSt->id = id;
   newSt->grade = grade;
  
   //declaring and memory allocation to struct Node pointer
   struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
   newNode->st = newSt;
   newNode->next = NULL;
  
   //if list is empty
   if(head==NULL)return newNode;
  
   struct Node*current = head;
  
   while(current->next)current = current->next;
  
   current->next = newNode;
  
   return head;
}

//funcion to display linkedlist
void display(struct Node*head){
   while(head){
       printf("ID : %d\n",head->st->id);
       printf("Grade : %.2f\n\n",head->st->grade);
       head = head->next;
   }
}

//main driver function
int main(){
   //variable declaration
   int id;
   float grade;
   struct Node* head = NULL;
//opening file
FILE* fp = fopen("input.txt", "r"); // read mode
//if file does not opened
if (fp == NULL)
{
printf("Error while opening the file.\n");
return 0;
}
//reading data from file
while (!feof (fp))
{
fscanf (fp, "%d", &id);
fscanf (fp, "%f", &grade);
   //adding new data to existing linkedlist as a ne node
   head = addNode(head,id,grade);
}
//displaying linkedlist
display(head);
//closing file
fclose (fp);
return 0;
}


Related Solutions

1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
C++ Write a program to create a linked list which stores the details of employees(Employee number,...
C++ Write a program to create a linked list which stores the details of employees(Employee number, employee name, rate, hours worked). Create a menu to manage the emoployee data. MENU 1. ADD EMPLOYEE DETAILS 2. DELETE EMPLOYEE 3. SEARCH EMPLOYEE 4. PRINT EMPLOYEE PAYROLL 5. EXIT When the user selected option #4 the program should print the following pay report for each employee: EmpNo.     Name      Rate    Hours    Regular Pay      Overtime Pay     Gross Pay Any hours worked above 40 hours are...
Bank Linked List Project: Create a bank linked list project program to mimic a simple bank...
Bank Linked List Project: Create a bank linked list project program to mimic a simple bank account system (open account, deposit, withdraw, loans etc.). Requirements: 1. Use linked list (queues and/or stacks) 2. Classes 3. Arrays 4. Add, delete, remove, search methods (use Dev. C++ to create the program)
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first node should not be able to change places. The nodes are given by: Struct nodeEl { int el; struct nodeEl * next; }; typdef struct nodeEl node; The list header (of type node *) is the first parameter of the subroutine. The second and third parameters consist of integers and are the places in the list where the nodes are to change places. The...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Write down the C++ Program
 Write a function, which accept three integer values as arguments find the largest of three and then return the largest value to main program. Write a main program which will call the function by passing three integer values and print the value returned by the function.?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT