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...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse */ #include <stdio.h> #include <stdlib.h> struct node { int visited; int a; struct node *next; }; int main() { struct node *head = NULL; generate(head); printf("\nPrinting the list in linear order\n"); linear(head); printf("\nPrinting the list in reverse order\n"); display(head); delete(head); return 0; } void display(struct node *head) { struct node *temp = head, *prev = head; while (temp->visited == 0) { while (temp->next !=...
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...
In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
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...
Create a C++ integer linked list program that performs the following methods below: Please create these...
Create a C++ integer linked list program that performs the following methods below: Please create these three source files: intList.h, intList.cpp, & intListTest.cpp. Implement recursive routines in the intList class to do the following: Print the list in reverse order Return the value in the middle node Return the average of all the odd values Remove every node containing an odd value Modify main (in IntListTest.cpp) so it does the following: Insert the numbers 1, 3, 4, 6, 7, 10,...
Write a program that create a single linked list and consist of all the necessary functions...
Write a program that create a single linked list and consist of all the necessary functions to do the following Add an element to the list, insertion position can be anywhere in the list (first, last and middle) delete an element from the list, deletion position can be anywhere in the list (first, last and middle) Note: You need to add proper documentation to your programs and you need to include the output of each program C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT