Question
Please use C++
CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student
with member functions. (No class definition declaration will be no credit for the project.) Turn in source programs and Execu
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

//student class
class Student
{
   public:
   int id;
   string name;
   string address;
   float gpa;
};

class StudentNode
{
   public:
   Student student;
   StudentNode *next;
};


//declare head node
StudentNode *head=NULL;

//function to insert a node
void insert(Student std)
{
   StudentNode *temp, *prev;
  
   //create a new node
   StudentNode *newnode = new StudentNode;

   //fill with data
   newnode->student = std;
   newnode->next=NULL;
  
   //check list is empty or not
   if(head==NULL)
   {
       head = newnode;
   }
   else
   {
       temp = head;
       while(temp->student.id < newnode->student.id)
       {
           prev = temp;
           temp = temp->next;
           if(temp==NULL) break;
       }
      
       newnode->next = temp;
       //check insert before the head node or not
       if(temp==head)
       {
           head = newnode;
           return;
       }
        prev->next=newnode;
   }
}


//function to delete kth node
void deleteNode(int k)
{
   StudentNode *temp, *prev, *nextnode;
  
   //check list is empty or not
   if(head==NULL)
   {
       cout << "Student List is empty! " << endl;
       return;
   }
   else
   {
       temp = head;
       for(int i=1; i<k; i++)
       {
           prev = temp;
           temp = temp->next;
       }
      
       nextnode = temp->next;
      
       prev->next = nextnode;
      
       temp->next = NULL;
       delete temp;
   }
}


//function to display the student list
void display()
{
   StudentNode *temp;
   temp = head;
  
   cout<<"ID       Name       Address       GPA"<<endl;
   while(temp!=NULL)
   {
       cout<<"__________________________________________________________"<<endl;
       cout<<"| ";
       cout<<left<<setfill(' ')<<setw(10)<<temp->student.id;
       cout<<setw(5)<<"| ";
       cout<<left<<setfill(' ')<<setw(10)<<temp->student.name;
       cout<<setw(5)<<"| ";
       cout<<left<<setfill(' ')<<setw(10)<<temp->student.address;
       cout<<setw(5)<<"| ";
       cout<<left<<setfill(' ')<<setw(10)<<temp->student.gpa;
       cout<<" |"<<endl;
       temp = temp->next;
   }
   cout<<"__________________________________________________________"<<endl;  
}

//main function
int main()
{
   const int MAX = 15;
   Student std[MAX], tmp;
   int k;

   for(int i=0; i<MAX; i++)
   {
       cout <<"Enter ID: ";
       cin >> std[i].id;
       cout <<"Enter NAME: ";
       cin >> std[i].name;
       cout <<"Enter ADDRESS: ";
       cin >> std[i].address;
       cout <<"Enter GPA: ";
       cin >> std[i].gpa;  
   }
  
   for(int i=0; i<MAX; i++)
   {
       insert(std[i]);
   }
  
   display();
  
   cout << "\nEnter the k for delete kth node :";
   cin >> k;
  
   deleteNode(k);
  
   cout << "After deletion: "<< endl;
   display();
  
   cout << "\nEnter a node for insert :" << endl;
   cout <<"Enter ID: ";
   cin >> tmp.id;
   cout <<"Enter NAME: ";
   cin >> tmp.name;
   cout <<"Enter ADDRESS: ";
   cin >> tmp.address;
   cout <<"Enter GPA: ";
   cin >> tmp.gpa;  
  
   insert(tmp);
  
   cout << "After insertion: "<< endl;
   display();
      
   return 0;
}

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...

    1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...

  • [C++] Create three functions for a singly linked list: - Function 1: Insert a string into...

    [C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };

  • In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...

    In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • Build a singly linked list with 10 noes. The data are "This is my first project...

    Build a singly linked list with 10 noes. The data are "This is my first project in Data structure and algorithm". First node is "This" second "is" and so on. Build insert function, insert "the " before "Data" and delete function, delete "my" before "first". Search "in" and output it location and then append "C++" and output the linked list again. In writing this program use menu which includes ”1. Create; 2. Insert; 3. Delete; 4. Append; 5. Search; 6....

  • Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have tw...

    Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have two state variables namely data and nextNode. The doubly list class should contain the following methods: Middlelnsert- insert a node somewhere in the middle of the list Startinsert-insert a node at start of the Linked list Endinsert- insert a node at the end of the Linked list Delete-delete a node Traverse-prints all the node's data Reverse-reverses the linked list . . Note: Choose...

  • Create a linked list of at least 15 different values that are prompted for and entered...

    Create a linked list of at least 15 different values that are prompted for and entered while the program is running. Appending a node attaches that node to the end of the list while inserting a node places it in order maintaining a sorted list. Create a menu driven program where you have the options to appended, insert, display, and delete a node. Display the list after first appending data showing it in no specific order, delete all nodes and...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • implement delete node function in c++ language I just need a basic doubly linked list code for the delete node portion of the code // Delete node containing word from list if it is present void...

    implement delete node function in c++ language I just need a basic doubly linked list code for the delete node portion of the code // Delete node containing word from list if it is present void delNode (DLList list, char *str) ( // Delete node containing word from list if it is present void delNode (DLList list, char *str) (

  • Exercise-1:15 points Develop a node class and a singly list class. The node class should have two...

    Exercise-1:15 points Develop a node class and a singly list class. The node class should have two state variables namely data and nextNode. The singly list class should contain the following methods . Middlelnsert-insert a node somewhere in the middle of the list . Startinsert-insert a node at start of the Linked list Endinsert-insert a node at the end of the Linked list . Delete-delete a node Traverse-prints all the node's data Reverse -reverses the linked list Note: Choose appropriate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT