ent ID numbers and their corresponding GPAs in a structure. ID numbers should be 8 digits. (Ex. 11926743) and GPA should be (0.0, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0) Add students Remove students Display the list of students Start of code:
C LANGUAGE ONLY. ANSWER ASAP.
Using the program provided below:
Add the following features that are listed below to the provided program:
- Store student ID numbers and their corresponding GPAs in a structure. ID numbers should be 8 digits. (Ex. 11926743) and GPA should be (0.0, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0)
- Add students
- Remove students
- Display the list of students
Start of code:
FILE NAME: linked_lists.h
#ifndef LINKED_LISTS_H
#define LINKED_LISTS_H
struct node {
int data;
struct node* next;
};
void insertAtBeginning(struct node** head, int data);
void insertAtEnd(struct node** head, int data);
void insertAfter(struct node* prev_node, int data);
void deleteNode(struct node** head, int key);
void swapNodes(struct node** head, int x, int y);
void printList(struct node* head);
#endif
FILE NAME: linked_lists.c
#include <stdio.h>
#include <stdlib.h>
#include "linked_lists.h"
void insertAtBeginning(struct node** head, int data) {
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
new_node->next = *head;
*head = new_node;
}
void insertAtEnd(struct node** head, int data) {
struct node* new_node = (struct node*)malloc(sizeof(struct node));
struct node* last = *head;
new_node->data = data;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}
void insertAfter(struct node* prev_node, int data) {
if (prev_node == NULL) {
printf("The given previous node cannot be NULL\n");
return;
}
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
void deleteNode(struct node** head, int key) {
struct node* temp = *head, *prev;
if (temp != NULL && temp->data == key) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
void swapNodes(struct node** head, int x, int y) {
if (x == y) return;
struct node *prevX = NULL, *currX = *head;
while (currX && currX->data != x) {
prevX = currX;
currX = currX->next;
}
struct node *prevY = NULL, *currY = *head;
while (currY && currY->data != y) {
prevY = currY;
currY = currY->next;
}
if (currX == NULL || currY == NULL) return;
if (prevX != NULL) prevX->next = currY;
else *head = currY;
if (prevY != NULL) prevY->next = currX;
else *head = currX;
struct node* temp = currY->next;
currY->next = currX->next;
currX->next = temp;
}
void printList(struct node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
struct node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtBeginning(&head, 3);
insertAfter(head->next, 4);
printf("Linked List: ");
printList(head);
deleteNode(&head, 3);
printf("Linked List after deletion of 3: ");
printList(head);
swapNodes(&head, 1, 2);
printf("Linked List after swapping 1 and 2: ");
printList(head);
return 0;
}
THANK YOU!
Step by step
Solved in 5 steps with 3 images