C++ Question : Download Lab22.cpp. Write a menu-driven C++ program to implement
ID: 3817254 • Letter: C
Question
C++
Question :
Download Lab22.cpp. Write a menu-driven C++ program to implement the bellow Linked Lists operations. Start your code by reading the file input.txt (download from blackboard). Insert all the values one by one to the end of the list.
void insertAfter(node *head)
Precondition: Takes the header node and prompts the user to enter two integer numbers. First number is the number that needs to be inserted. Second number is the number after which the first number will be inserted.
Post-condition: Insert the first number next to the position where the second number is located. If the second number is not found in the list, a message will be showed.
void deleteLast(node *head)
Precondition: Takes the header node.
Post-condition: Deletes the last number from the list. Displays a message if the list is already empty.
void frequency(node *head)
Precondition: Takes the header node and prompts the user to enter the integer number that needs to be counted. Post-condition: Shows the frequency of that number.
EXTRA CREDIT 50 points
void sort(node *head);
Precondition: Takes the header node.
Post-condition: Sort the nodes based on the numbers in ascending order using insertion sort or bubble sort.
void printList(node *head)
Post-condition: Prints the numbers of the list from beginning to the end. It shows a message if the list is empty.
Explanation / Answer
HI FRiend you have not posted Lab22.cpp. So i can not see the structure/members of LinkedList class.
But I have assumed structure of Node and implemented all methods.
You can take help from my implementation and can understand the logic.
// Lets assume structure of Node is:
class node{
public:
int data;
node *next;
node(int d){
data = d;
next = NULL;
}
};
void insertAfter(node *head){
if(head == NULL){
cout<<"List is Empty!!"<<endl;
return;
}
int num, item;
cout<<"Enter number to be insert: ";
cin>>num;
cout<<"Enter after which number you want to insert: ";
cin>>item;
// moving head till item or end
while(head != NULL && head->data != item){
head = head->next;
}
// item not found
if(head == NULL){
cout<<item<<" is not avaialble in list"<<endl;
}else{
// creating new node
node *newNode = new node(num);
newNode->next = head->next;
head->next = newNode;
}
}
void deleteLast(node *head){
if(head == NULL){
cout<<"List is Empty!!"<<endl;
return;
}
// only one node was in list
if(head->next == NULL){
head = NULL;
return;
}
// moving head till second last node
while(head->next != NULL){
head = head->next;
}
head->next = NULL; // deleting last node
}
void frequency(node *head){
if(head == NULL){
cout<<"List is Empty!!"<<endl;
return;
}
int item;
cout<<"Enter numbet to be count: ";
cin>>item;
int count = 0;
while(head != NULL){
if(head->data == item)
count++;
head = head->next;
}
cout<<"Frequency of "<<item<<" is: "<<count<<endl;
}
Typedef is a keyword that is used to give a new symbolic name for the existing name in a C program. This is same like defining alias for the commands.
Consider the below structure.
struct student
{
int mark [2];
char name [10];
float average;
}
Variable for the above structure can be declared in two ways.
1st way :
struct student record; /* for normal variable */
struct student *record; /* for pointer variable */
2nd way :
typedef struct student status;
When we use “typedef” keyword before struct <tag_name> like above, after that we can simply use type definition “status” in the C program to declare structure variable.
Now, structure variable declaration will be, “status record”.
This is equal to “struct student record”. Type definition for “struct student” is status. i.e. status = “struct student”
AN ALTERNATIVE WAY FOR STRUCTURE DECLARATION USING TYPEDEF IN C:
typedef struct student
{
int mark [2];
char name [10];
float average;
} status;
To declare structure variable, we can use the below statements.
status record1; /* record 1 is structure variable */
status record2; /* record 2 is structure variable */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.