Data should be read from a file using a comma delimited format
Use a linked list to store the Course schedule while in memory
Allow the user to display all courses
Allow the user to add courses
Allow the user to delete courses
Save the data at the end of the program. Use at least two structures and one enum.
EX: Computer Science I, CSC110, Mon, Wed, 8:00, 9:00, Online, Zoom, Instructor
1.. Allow the user to add course
{
Node head; // head of list
//linked list node declaration
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
/* Insert a new node at the front of the list */
public void push(int new_data)
{
//allocate and assign data to the node
Node newNode = new Node(new_data);
//new node becomes head of linked list
newNode.next = head;
//head points to new node
head = newNode;
}
// Given a node,prev_node insert node after prev_node
public void insertAfter(Node prev_node, int new_data)
{
//check if prev_node is null.
if (prev_node == null)
{
System.out.println("The given node is required and cannot be null");
return;
}
//allocate node and assign data to it
Node newNode = new Node(new_data);
//next of new Node is next of prev_node
newNode.next = prev_node.next;
//prev_node->next is the new node.
prev_node.next = newNode;
}
//inserts a new node at the end of the list
public void append(intnew_data)
{
//allocate the node and assign data
Node newNode = new Node(new_data);
//if linked list is empty, then new node will be the head
if (head == null)
{
head = new Node(new_data);
return;
}
//set next of new node to null as this is the last node
newNode.next = null;
// if not the head node traverse the list and add it to the last
Node last = head;
while (last.next != null)
last = last.next;
//next of last becomes new node
last.next = newNode;
return;
}
//display contents of linked list
public void displayList()
{
Node pnode = head;
while (pnode != null)
{
System.out.print(pnode.data+"-->");
pnode = pnode.next;
}
if(pnode == null)
System.out.print("null");
}
}
//Main class to call linked list class functions and construct a linked list
class Main{
public static void main(String[] args)
{
/* create an empty list */
LinkedList lList = new LinkedList();
// Insert 40.
lList.append(40);
// Insert 20 at the beginning.
lList.push(20);
// Insert 10 at the beginning.
lList.push(10);
// Insert 50 at the end.
lList.append(50);
// Insert 30, after 20.
lList.insertAfter(lList.head.next, 30);
System.out.println("\nFinal linked list: ");
lList. displayList ();
}
}
2..Allow the user to delete courses:
#include <iostream>
using namespace std;
/* Link list node */
struct Node {
int data;
struct Node* next;
};
//delete first node in the linked list
Node* deleteFirstNode(struct Node* head)
{
if (head == NULL)
return NULL;
// Move the head pointer to the next node
Node* tempNode = head;
head = head->next;
delete tempNode;
return head;
}
//delete last node from linked list
Node* removeLastNode(struct Node* head)
{
if (head == NULL)
return NULL;
if (head->next == NULL) {
delete head;
return NULL;
}
// first find second last node
Node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
// Delete the last node
delete (second_last->next);
// set next of second_last to null
second_last->next = NULL;
return head;
}
// create linked list by adding nodes at head
void push(struct Node** head, int new_data)
{
struct Node* newNode = new Node;
newNode->data = new_data;
newNode->next = (*head);
(*head) = newNode;
}
// main function
int main()
{
/* Start with the empty list */
Node* head = NULL;
// create linked list
push(&head, 2);
push(&head, 4);
push(&head, 6);
push(&head, 8);
push(&head, 10);
Node* temp;
cout<<"Linked list created "<<endl; for (temp = head; temp != NULL; temp = temp->next)
cout << temp->data << "-->";
if(temp == NULL)
cout<<"NULL"<<endl;
//delete first node
head = deleteFirstNode(head);
cout<<"Linked list after deleting head node"<<endl; for (temp = head; temp != NULL; temp = temp->next)
cout << temp->data << "-->";
if(temp == NULL)
cout<<"NULL"<<endl;
//delete last node
head = removeLastNode(head);
cout<<"Linked list after deleting last node"<<endl; for (temp = head; temp != NULL; temp = temp->next)
cout << temp->data << "-->";
if(temp == NULL)
cout<<"NULL";
return 0;
}
Comments
Leave a comment