Can somebody please fill in annotations on almost every line for somebody who is
ID: 3547789 • Letter: C
Question
Can somebody please fill in annotations on almost every line for somebody who is learning C++?? The program is this. My friend has done most of them but need the rest of the lines to let me know what they mean:
#include<iostream>
using namespace std;
class linklist{ // defining class linklist
private:
struct node{ // defining the node structure
int data; // data field of the node
node *link; // link points to a node type
};
struct node *p; // declaring node variable
void append(int num){ // function to append a node at the end
node *t, *q; // declaring node variable
if(p == NULL){ // condition when list is empty, create the first node
p = new node;
p -> data = num;
p -> link = NULL;
}
else{ // if list is not empty
q = p;
while(q -> link != NULL){
q = q -> link;
}
t = new node;
q -> link = t;
t -> data = num;
t -> link = NULL;
}
}
void addatbeg(int num){ // function to add a node in the beginning of the list
node *q;
q = new node;
q -> data = num;
q -> link = p;
p = q;
}
void addafter(int c, int num){ // function to add a node after a number of nodes
node *q;
q = p;
int i;
for(i = 1; i < c; i++){
q = q -> link;
if(q -> link == NULL){
cout << "There are less than " << c << "nodes" << endl;
}
}
node *t;
t = new node;
t -> data = num;
t -> link = q -> link;
q -> link = t;
}
void del(int num){ // function to delete a node
node *q, *r;
q = p;
if(q -> data == num){
p = q -> link;
delete q;
menu();
}
r = q;
while(q != NULL){
if(q -> data == num){
r -> link = q -> link;
delete q;
menu();
}
r = q;
q = q -> link;
}
}
void display(){ // function to display all the nodes
node *q;
q = p;
cout << "The elements are " << endl;
for(q = p; q != NULL; q = q -> link){
cout << q -> data << " ";
}
cout << endl;
}
int countnode(){ // function to count thew number of nodes
node *q;
int count = 0;
for(q = p; q != NULL; q = q -> link){
count++;
}
return count;
}
public:
linklist(){ // condstructor, initializes node p to NULL
p = NULL;
}
void menu(){
cout << "Enter your choice: 1. Append 2. Add At Beginning 3. Add after number of nodes 4. Delete 5. Display 6. Count 7. Exit ";
int choice;
cin >> choice;
switch(choice){
int item;
case 1:
cout << "Enter the number you want to append:" << endl;
cin >> item;
append(item);
menu();
break;
case 2:
cout << "Enter the number you want to add at beginning:" << endl;
cin >> item;
addatbeg(item);
menu();
break;
case 3:
int pos;
cout << "Enter the number you want to add and after how many nodes:" << endl;
cin >> item >> pos;
addafter(pos, item);
menu();
break;
case 4:
cout << "Enter the number you want to delete:" << endl;
cin >> item;
del(item);
menu();
break;
case 5:
display();
menu();
break;
case 6:
cout << "The number of elements in the linked list = " << countnode() << endl;
menu();
break;
case 7:
return;
}
}
};
int main(){
linklist obj;
obj.menu();
}
Explanation / Answer
#include<iostream>
using namespace std;
// defining class linklist
class linklist
{
// defining the node structure
private:
struct node
{
int data; // data field of the node
node *link; // link points to a node type
};
struct node *p; // declaring node variable
// function to append a node at the end
void append(int num)
{
node *t, *q; // declaring node variable
if(p == NULL){ // condition when list is empty, create the first node
p = new node;
p -> data = num;
p -> link = NULL;
}
else{ // if list is not empty
q = p;
while(q -> link != NULL){
q = q -> link;
}
t = new node;
q -> link = t;
t -> data = num;
t -> link = NULL;
}
}
void addatbeg(int num){ // function to add a node in the beginning of the list
node *q;
q = new node;
q -> data = num;
q -> link = p;
p = q;
}
void addafter(int c, int num){ // function to add a node after a number of nodes
node *q;
q = p;
int i;
for(i = 1; i < c; i++){
q = q -> link;
if(q -> link == NULL){
cout << "There are less than " << c << "nodes" << endl;
}
}
node *t;
t = new node;
t -> data = num;
t -> link = q -> link;
q -> link = t;
}
void del(int num){ // function to delete a node
node *q, *r;
q = p;
if(q -> data == num){
p = q -> link;
delete q;
menu();
}
r = q;
while(q != NULL){
if(q -> data == num){
r -> link = q -> link;
delete q;
menu();
}
r = q;
q = q -> link;
}
}
void display(){ // function to display all the nodes
node *q;
q = p;
cout << "The elements are " << endl;
for(q = p; q != NULL; q = q -> link){
cout << q -> data << " ";
}
cout << endl;
}
int countnode(){ // function to count thew number of nodes
node *q;
int count = 0;
for(q = p; q != NULL; q = q -> link){
count++;
}
return count;
}
public:
linklist(){ // condstructor, initializes node p to NULL
p = NULL;
void menu(){
cout << "Enter your choice: 1. Append 2. Add At Beginning 3. Add after number of nodes 4. Delete 5. Display 6. Count 7. Exit ";
int choice;
cin >> choice;
switch(choice){
int item;
case 1:
cout << "Enter the number you want to append:" << endl;
cin >> item;
append(item);
menu();
break;
case 2:
cout << "Enter the number you want to add at beginning:" << endl;
cin >> item;
addatbeg(item);
menu();
break;
case 3:
int pos;
cout << "Enter the number you want to add and after how many nodes:" << endl;
cin >> item >> pos;
addafter(pos, item);
menu();
break;
case 4:
cout << "Enter the number you want to delete:" << endl;
cin >> item;
del(item);
menu();
break;
case 5:
display();
menu();
break;
case 6:
cout << "The number of elements in the linked list = " << countnode() << endl;
menu();
break;
case 7:
return;
}
}
}; //class close braces
int main(){
linklist obj; //create object of class linklist
obj.menu(); // call menu function ,this is public member of class,we can access it from other function
}
this program is right, there is one class called linklist ,class has some private member function like append,display etc
and some public member function like menu.private member can only access by its public member function not by some other function but public member can access by main function and other function in same files.