Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the following struct definitions struct Node{ int data; Node *next; } stru

ID: 3771175 • Letter: G

Question

Given the following struct definitions

struct Node{

int data;

Node *next;

}

struct Mylist{

Node *head

int number;

};

Write C++ code for the following questions:
a. Implement a function
void pushData(MyList &list, int entry);
The function adds a new entry at the front of a linked list MyList. The total number of nodes should be increased by one. The linked list might be empty.

b. Implement a function
int popData(MyList &list);

The function removes the node at the front of the linked list MyList and returns the data of the node. The total number of nodes should be decreased by one. The linked list might be empty.

c. Implement a function

printData(const MyList &list);
The function prints all data values that stored in the linked list. The linked list might be empty.

Explanation / Answer

#include<iostream>
using namespace std;

struct Node{
              int data;
              Node *next;
};

struct Mylist{
              Node *head;
              int number;
}list;

void pushData(Mylist &list, int entry)
{
    Node *temp=new Node;
    temp->data=entry;
    temp->next=list.head;
    list.head=temp;
    list.number++;  
    cout<<" Element pushed successfully!";
}
int popData(Mylist &list)
{ int d;
   if(list.head==NULL)
   {
   cout<<" Underflow error! No element available to delete";
   return 0;
    }
    else
    {
     d = list.head->data;
   list.head=list.head->next;
     list.number--;
     return d;
   }
}
void printData(const Mylist &list)
{
if(list.head==NULL)
   cout<<" List is empty.";
else
{
cout<<" The list of elements is : ";
Node *temp=new Node;
temp = list.head;
while(temp->next!=NULL)
{
   cout<<temp->data<<" -> ";  
}  
cout<<temp->data;
}
}
int main()
{

   int ch,del,entry;
   list.head=NULL;
   list.number=0;
  
   while(true)
   {
   cout<<" Menu: 1.Push 2.Pop 3.Print 4.Exit";
   cout<<" Enter your choice : ";
   cin>>ch;
   switch(ch)
   {
   case 1:
   cout<<" Enter the data you wish to push : ";
   cin>>entry;
   pushData(list,entry);
   break;
   case 2:
   del=popData(list);
   if(del!=0)
   cout<<" "<<del<<" is deleted.";
   break;
    case 3:
    printData(list);
    break;
    case 4:
       exit(0);
    default :
     cout<<" Invalid Choice! ";
      break;  
    }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote