Below is LinkedList.cpp //LinkedList.cpp // Note: This implementation of LinkedL
ID: 3614755 • Letter: B
Question
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)
Explanation / Answer
Enter values for a linkedlist, one per line.
Enter 999 to end the list.
4
12
16
999
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:i
Please enteran integer value to insert:8
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:p
The content of the current ordered list is:
Elements in linklist are :
4
8
12
16
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:a
Please enteran integer value to append to the end:30
You must implement this function
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:p
The content of the current ordered list is:
Elements in linklist are :
4
8
12
16
30
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:d
Please enteran integer value to delete:4
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:d
Please enteran integer value to delete:30
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:d
Please enteran integer value to delete:12
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:p
The content of the current ordered list is:
Elements in linklist are :
8
16
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:d
Please enteran integer value to delete:2
Item not found:*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Please enter your choice here:p
The content of the current ordered list is:
Elements in linklist are :
8
16
*******************************************************
i: Insert (Insert an element and keep the list ordered)
a: Append (Append an element to the end of the list)
d: Delete (Delete a node with the given value)
p: Print (Print the content of the current list)
q: Quit (Quit the program)
*******************************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.