Using following main file and solve the task Instead of ID and GPA, now consider
ID: 3778328 • Letter: U
Question
Using following main file and solve the task- Instead of ID and GPA, now consider following fields: Name, ID and Contact number
- Insert last (already done)
- Display All (already done)
- Insert at the beginning
- Delete the first element
- Delete the last element
- Search an element (by ID)
- Insert after an element
- Display how many elements are there in the list
- Find the element with highest (biggest) ID
- Find the element with lowest (smallest) ID
// main file
please solve all it ... Thank You :) Using following main file and solve the task - Instead of ID and GPA, now consider following fields: Name, ID and Contact number
- Insert last (already done)
- Display All (already done)
- Insert at the beginning
- Delete the first element
- Delete the last element
- Search an element (by ID)
- Insert after an element
- Display how many elements are there in the list
- Find the element with highest (biggest) ID
- Find the element with lowest (smallest) ID
// main file
please solve all it ... Thank You :) - Instead of ID and GPA, now consider following fields: Name, ID and Contact number
- Insert last (already done)
- Display All (already done)
- Insert at the beginning
- Delete the first element
- Delete the last element
- Search an element (by ID)
- Insert after an element
- Display how many elements are there in the list
- Find the element with highest (biggest) ID
- Find the element with lowest (smallest) ID
// main file
please solve all it ... Thank You :) // main file
please solve all it ... Thank You :)
Explanation / Answer
// main file
#include<iostream>
#include<string>
using namespace std;
struct node{
//change to name , ID and contact number
string name;
int ID; ;
string phone;
//double GPA;
node *link;
};
node *head=NULL, *t;
void insertNode()
{
node *temp=new node;
cout<<"Enter name..";
cin>>temp->name;
cout<<"Enter the ID..";
cin>>temp->ID;
cout<<"Enter Phone..";
cin>>temp->phone;
temp->link=NULL;
if(head==NULL)//list is empty
{
head=temp;
}
else
{
t=head;
while(t->link!=NULL)
t=t->link;
t->link=temp;
}
}
void displayAll()
{
node *t=head;
while(t!=NULL)
{
cout<<"Name="<<t->name<<endl;
cout<<"ID="<<t->ID<<endl;
cout<<"Phone="<<t->phone<<endl;
t= t->link;
}
}
void isertAtbegin()
{
node *cur = head;
node *temp=new node;
cout<<"Enter name..";
cin>>temp->name;
cout<<"Enter the ID..";
cin>>temp->ID;
cout<<"Enter Phone..";
cin>>temp->phone;
head = temp;
temp->link = cur;
}
void deleteFirstElement()
{
node *cur = head,*prev=head;
head = head->link;
free(cur);
}
void searchElement(int ID)
{
node *cur = head;
int found = 0,pos=0;
while( cur!=NULL)
{
if( ID == cur->ID )
{
found = 1;
break;
}
cur = cur->link;
++pos;
}
if( found )
cout<<"Element with Id= "<<ID<<" found at Position = "<<pos+1<<endl;
else
cout<<"Elementnot not found "<<endl;
}
void insertAfterElement(int position)
{
node *cur = head,*next=head;
int pos=1;
while(cur!=NULL)
{
if( pos == position )
{
next = cur->link;
node *temp=new node;
cout<<"Enter name..";
cin>>temp->name;
cout<<"Enter the ID..";
cin>>temp->ID;
cout<<"Enter Phone..";
cin>>temp->phone;
temp->link=next;
cur->link = temp;
break;
}
cur = cur->link;
++pos;
}
}
void displayNumberOfelements()
{
node *cur = head;
int count = 0;
while( cur!=NULL)
{
++count;
cur = cur->link;
}
cout<<"Number of elements in list = "<<count<<endl;
}
void highestId()
{
node *cur = head;
int max = head->ID;
while( cur!=NULL)
{
if(max < cur ->ID)
{
max = cur ->ID;
}
cur = cur->link;
}
cout<<"The element with highest (biggest) ID = "<<max<<endl;
}
void LowestId()
{
node *cur = head;
int min = head->ID;
while( cur!=NULL)
{
if(min > cur ->ID)
{
min = cur ->ID;
}
cur = cur->link;
}
cout<<"the element with lowest (smallest) ID = "<<min<<endl;
}
int main()
{
int choice=-1;
while(choice!=0)
{
cout<<" 0-Exit 1-Insert 2-Display 3-insertAtbeginning 4-deletefirstelement 5-searchElement 6-insertafterelement 7-displaynumberofelements 8-highestID 9-lowestId ...";
cin>>choice;
if(choice==1)
insertNode();
if(choice==2)
displayAll();
if(choice==3)
isertAtbegin();
if(choice==4)
deleteFirstElement();
if(choice==5)
searchElement(12);
if(choice==6)
insertAfterElement(2);
if(choice==7)
displayNumberOfelements();
if(choice==8)
highestId();
if(choice==9)
LowestId();
}//while loop
return 0 ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.