Introduction: In this project you will add methods to an existing linked list cl
ID: 3891624 • Letter: I
Question
Introduction: In this project you will add methods to an existing linked list class. Description: Modify the author's "MyLinkedList" class to add the following methods. Perform checking of the parameters and throw exceptions where appropriate. 15 points each (a-e) a. SWap receives two index positions as parameters and swaps the two nodes (the nodes, not just the values inside) at these positions, provided both positions are within the current size D. reverse returns a new MyLinkedList that has the elements in reverse order. C. erase receives an index position and number of elements as parameters, and removes elements beginning at the index position for the number of elements specified, provided the index position is within the size and together with the number of elements does not exceed the size d. insertList receives a List and an index position as parameters, and copies all of the passed list into the existing list at the position specified by the parameter, provided the index position does not exceed the size e. shift receives an integer and shifts the list this many nodes forward or backward for example, if passed 2, the first two nodes move to the tail, or if passed -3, the last three nodes move to the front. +2: abcde -> cdeab -3 abcde - cdeab 25 points f. main add code to the main method to demonstrate each of your methodsExplanation / Answer
Implemented Source Code:-
--------------------------------
#include<iostream>
#include<iomanip>
#include<string.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
struct node
{
char FirstName[100];
char LastName[100];
int uniqueid,serial_no;
struct node *next;
};
struct node array[100];
struct node *first=NULL,*last=NULL,*swapping,*temp1=NULL;
class LinkedList
{
public:
char First_Name,Last_Name;
int unique_id;
int exam1_grade,exam2_grade,final_Grade;
public:
void display()
{
struct node *temp2;
temp2=temp1;
cout<<"-------------------------------------------------------"<<endl;
cout<<" S.No"<<setw(15)<<"FirstName"<<setw(15)<<"LastName"<<setw(15)<<"Unique_id"<<endl;
cout<<" ------------------------------------------------------"<<endl;
while(temp2!=NULL)
{
cout<<temp2->serial_no<<setw(15)<<temp2->FirstName<<setw(15)<<temp2->LastName<<setw(15)<<temp2->uniqueid<<endl;
temp2=temp2->next;
}
}
public:
static void reverse(struct node** head)
{
struct node* prev = NULL;
struct node* current = *head;
struct node* next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
public:
int search(int id_value)
{
int index=0;
struct node* temp3 = temp1;
while(temp3!=NULL)
{
if(id_value==temp3->uniqueid)
{
return index;
}
temp3=temp3->next;
index++;
}
return -1;
}
public:
void addnewnode(struct node** head,int serialno,char firstName[],char LastName[],int idvalue)
{
struct node* new_node=(struct node*)malloc(sizeof(struct node));
last=*head;
new_node->serial_no=serialno;
strcpy(new_node->FirstName,firstName);
strcpy(new_node->LastName,LastName);
new_node->uniqueid=idvalue;
new_node->next=NULL;
while (last->next != NULL)
last = last->next;
last->next = new_node;
}
public:
void DeleteNode(struct node **head, int position)
{
if (*head == NULL)
return;
struct node* temp = *head;
if (position == 0)
{
*head = temp->next;
free(temp);
return;
}
for(int i=0; temp!=NULL && i<position-1; i++)
temp = temp->next;
if(temp == NULL || temp->next == NULL)
return;
last = temp->next->next;
free(temp->next);
temp->next = last;
}
};
int main()
{
LinkedList obj;
char firstname[50];
char lastName[50];
char tempstr[100];
int id,sno,serialno=13,retval=0;
char option,choice;
ifstream myfile;
myfile.open("linkedlist.data");
if(myfile.is_open())
{
for(int i=0;i<13;i++)
{
temp1 = (struct node*)malloc(sizeof(struct node));
memset(firstname,0,sizeof(firstname));
memset(lastName,0,sizeof(lastName));
myfile>>sno;
myfile>>firstname;
myfile>>lastName;
myfile>>id;
temp1->serial_no=sno;
strcpy(temp1->FirstName,firstname);
strcpy(temp1->LastName,lastName);
temp1->uniqueid=id;
temp1->next=swapping;
swapping=temp1;
}
obj.reverse(&temp1);//Reversing the Linked List Elements
cout<<" The Content From The File is "<<endl;
obj.display();
}
else
{
cout<<" ! Couldn't Able to Open The File"<<endl;
}
while(true)
{
cout<<" ****** MENU *****"<<endl;
cout<<" Enter 'P'.To print the List "<<endl;
cout<<" Enter 'a' To Add a New Node to the List"<<endl;
cout<<" Enter 'd' To Delete a Node from the List"<<endl;
cout<<" Enter 'q' To Quit from the Menu"<<endl;
cout<<" Please Enter Any Option"<<endl;
cin>>option;
switch(option)
{
case 'p':
obj.display();
break;
case 'a':
serialno++;
label:
cout<<" Please Enter FirstName"<<endl;
cin>>firstname;
cout<<" Please Enter LastName"<<endl;
cin>>lastName;
cout<<" Please Enter Unique id:"<<endl;
cin>>id;
cout<<" You Entered! "<<firstname<<" "<<lastName<<" "<<id<<endl;
cout<<" Correct?(Y/N)"<<endl;
cin>>choice;
if(choice=='Y'||choice=='y')
{
retval=obj.search(id);
if(retval!=-1)
{
cout<<" The Unique id is Already Exist try with Different!"<<endl;
}
else
{
obj.addnewnode(&temp1,serialno,firstname,lastName,id);
cout<<" New node Details are Added Successfully"<<endl;
cout<<" The New List is"<<endl;
obj.display();
}
}
else
goto label;
break;
case 'd':
cout<<" Please Enter the Unique id to remove from the List"<<endl;
cin>>id;
retval=obj.search(id);
if(retval==-1)
{
cout<<" The id is Not Exist Please try again"<<endl;
}
else
{
obj.DeleteNode(&temp1,retval);
cout<<" The Node is Deleted Successfully"<<endl;
cout<<" The New List is:"<<endl;
obj.display();
}
break;
case 'q':
exit(0);
default:
cout<<" Invalid Option!"<<endl;
}
}
}
Inputfile:-
----------
1 Guthrie Lauren 45
2 Liu Fangzhou 388
3 Madsen Neil 453
4 Shelton Xavier 899
5 Gillespie Jessica 3322
6 Bhullar Parvinder 3471
7 Reilly Sean 3838
8 Kefauver Nicholas 4532
9 Kohn David 5410
10 Chu Ryan 5621
11 Jin Xing 7734
12 Berkovich Michael 45387
13 Stopar Alexander 45781
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.