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

Compile and run this C++ program. Below is LinkedList.cpp //LinkedList.cpp // No

ID: 3614752 • Letter: C

Question

Compile and run this C++ program.


Below is LinkedList.cpp

//LinkedList.cpp

// Note: This implementation of LinkedList is based on
// Chapter 4 of Bruno R. Preiss's Textbook:
// "Data Structures and Algorithms with Object-Oriented Design Patterns
//    in C++"

#include <cstdlib>
#include <cassert>
#include "LinkedList.h"
using namespace std;

//---------------------------------------------------
//List Element Members
//---------------------------------------------------
ListElement::ListElement(int _datum, ListElement * _next):
   datum (_datum), next (_next)
{}


int ListElement::getDatum () const
{
   return datum;
}


ListElement const* ListElement::getNext () const
{
   return next;
}



//---------------------------------------------------
//LinkedList Members
//---------------------------------------------------
LinkedList::LinkedList () :
    head (0)
{}

void LinkedList::insertItem(int item)
{
   ListElement *currPtr = head;
   ListElement *prevPtr = NULL;
   ListElement *newNodePtr;      //points to a new node

   while(currPtr != NULL && item > currPtr->datum)
   {
      prevPtr = currPtr;
      currPtr = currPtr->next;
   }

   newNodePtr = new ListElement(item, currPtr);
  
   if (prevPtr == NULL) // insert at the beginning
      head=newNodePtr;
   else
      prevPtr->next = newNodePtr;
}

void LinkedList::makeList()
{
   int InValue;
   ListElement *currPtr;
   ListElement *newNodePtr;

   cout << "Enter values for a linked list, one per line." << endl
   << "Enter 999 to end the list." << endl;

   cin >> InValue;
   //Adding elements to end so that "next" will be NULL
   newNodePtr=new ListElement(InValue, NULL);
   head=newNodePtr; //assign head to the first Node
   currPtr=head;
   cin >> InValue;

   while ( InValue != 999)
   {
      newNodePtr=new ListElement(InValue, NULL);
      currPtr->next=newNodePtr;     //link the new node to list
      currPtr=newNodePtr;           //move the currPtr so it is at end of list
      cin >> InValue;
   }
            
}

void LinkedList::appendItem (int item)
{
   //REPLACE THE LINE BELOW WITH YOUR OWN CODE
   cout << "You must implement this function" <<endl;
}

void LinkedList::deleteItem (int item)
{
   //REPLACE THE LINE BELOW WITH YOUR OWN CODE
   cout << "You must implement this function" <<endl;
}


void LinkedList::printList ()
{
   //REPLACE THE LINE BELOW WITH YOUR OWN CODE
   cout << "You must implement this function" <<endl;
}



Below is LinkedList.h

// LinkedList.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <cstdlib>
#include <iostream>
using namespace std;



class LinkedList; // needed for friend line below

class ListElement
{
private:  
   int datum;
   ListElement* next;
  
public:
   ListElement (int, ListElement*);

   int getDatum () const;
   ListElement const* getNext () const;

   friend class LinkedList; // gives LinkedList access to datum and next
};


class LinkedList
{
private:
   ListElement* head;

public:
   LinkedList ();

   void insertItem (int);
   void makeList ();
   void appendItem (int);
   void deleteItem (int);
   void printList ();
};

#endif


Below is Main.cpp

#include<iostream>
#include"LinkedList.h"
using namespace std;

int readInt(string);
int main()
{

   char choice;
   int item;
   LinkedList a;

   cout << "This program demonstrates the linked list. " << endl;
   cout << "Initially, you will be asked to create the list." << endl;
   cout << "You will be later prompted to manipulate the list." <<endl << endl;

   a.makeList();

   choice='b';

   while(choice != 'q')
   {
      cout << "*******************************************************" << endl;
      cout<<"i: Insert (Insert an element and keep the list ordered) ";
      cout<<"a: Append (Append an element to the end of the list) ";
      cout<<"d: Delete (Delete a node with the given value) ";
      cout<<"p: Print (Print the content of the current list) ";
      cout<<"q: Quit   (Quit the program) ";
      cout << "*******************************************************" << endl << endl;
      cout<<"    Please enter your choice here:";
      cin>>choice;
    
      switch(choice)
      {
   case 'i':
        item=readInt("to insert:");
        a.insertItem(item);
        break;
      
   case 'a':
        item=readInt("to append to the end:");
        a.appendItem(item);
      &nb

Explanation / Answer

//hope this will help you.

void LinkedList::printList()
{
   //REPLACE THE LINE BELOW WITH YOUR OWN CODE
   cout << "Elements in linklist are :"<<endl;
     ListElement *currPtr;
     currPtr = head;
     while(currPtr != NULL)
     {
    cout<<currPtr->datum<<endl;
     currPtr = currPtr->next;
     }
}

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