[Solved]Csci 2010 Lab11 Link Lists Lab 11a Linked Lists Preparation Create Visual Studio C Project Q37158948
CSCI 2010 Lab11 Link-Lists
Lab 11A Linked-Lists
Preparation
- Create a Visual Studio C++ Project C2010Lab11A
- Add the following to the project.
//LinkedList.cpp
#include <cstdlib>
#include “LinkedList.h”
using namespace std;
//—————————————————
//List Element Members
//—————————————————
ListElement::ListElement(int d, ListElement * n)
{
datum=d;
next=n;
}
int ListElement::getDatum () const
{
return datum;
}
ListElement const* ListElement::getNext () const
{
return next;
}
//—————————————————
//LinkedList Members
//—————————————————
LinkedList::LinkedList ()
{
head=NULL;
}
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 thebeginning
head=newNodePtr;
else
prevPtr->next =newNodePtr;
}
void LinkedList::makeList()
{
int InValue;
ListElement *currPtr;
ListElement *newNodePtr;
cout << “Enter values for a linked list, oneper line.” << endl
<< “Enter 999 to end thelist.” << endl;
cin >> InValue;
//Adding elements to end so that “next” will beNULL
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 newnode 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;
}
// 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 accessto datum and next
};
class LinkedList
{
private:
ListElement* head;
public:
LinkedList ();
void insertItem (int);
void makeList ();
void appendItem (int);
void deleteItem (int);
void printList ();
};
#endif
//main.cpp
#include<iostream>
#include”LinkedList.h”
#include <string>
using namespace std;
int readInt(string);
int main()
{
char choice;
int item;
LinkedList a;
cout << “This program demonstrates the linkedlist. ” << endl;
cout << “Initially, you will be asked to createthe list.” << endl;
cout << “You will be later prompted tomanipulate the list.” <<endl << endl;
a.makeList();
choice=’b’;
while(choice != ‘q’)
{
cout <<“*******************************************************” <<endl;
cout<<“i: Insert (Insert anelement and keep the list ordered)n”;
cout<<“a: Append (Append anelement to the end of the list)n”;
cout<<“d: Delete (Delete anode with the given value)n”;
cout<<“p: Print (Print thecontent of the current list)n”;
cout<<“q: Quit (Quit the program)n”;
cout <<“*******************************************************” <<endl << endl;
cout<<“n Pleaseenter your choice here:”;
cin>>choice;
switch(choice)
{
case ‘i’:
item=readInt(“toinsert:”);
a.insertItem(item);
break;
case ‘a’:
item=readInt(“to append tothe end:”);
a.appendItem(item);
break;
case ‘d’:
item=readInt(“todelete:”);
a.deleteItem(item);
break;
case ‘p’:
cout << “The content ofthe current ordered list is: “<<endl;
a.printList();
break;
case ‘q’:
break;
default:
cout<<“n Invalidchoice. Please try again.n”;
break;
}
}
cout<<“n Byen”;
return 0;
}
int readInt(string descr)
{
int item;
cout << “n Please enter an integervalue ” << descr;
cin >> item;
while (item < 0)
{
cout << “n Please tryagain:”;
cin >> item;
}
return item;
}
The purpose of the program is to demonstrate operations on asimple singly linked list.
Exercise 1
- Examine the header file and the program carefully.
- Fill in the right side of the table with the lines of codeprovided at the top. The purpose here is to help you relate thealgorithmic description with the code while viewing the graphicalrepresentation of the nodes in a linked list.
- The diagrams in the following table represent a progression ofinserts into an empty linked list.
- Match the following lines of code with the appropriateillustration on the left.
Use each line only once:
- currPtr=head;
- newNodePtr=new ListElement(8,NULL);
- currPtr->next=newNodePtr;
- head=newNodePtr;
- newNodePtr=new ListElement(2,NULL);
- currPtr=newNodePtr;
Note what changes from one picture to the next. Thisrepresents the sequence of operations.

Fill in answer.

Fill in answer.

Fill in answer.

Fill in answer.

Fill in answer.

Fill in answer.
Exercise 2
Now you should be ready to start adding code to thatprogram.
- First, in LinkedList.cpp, add the code to print out the linkedlist. The function called LinkedList::printList() is provided inthe code; you need to add the implementation.
- Compile and run this C++ program.
- Now, add the code to add a single node element to the end ofthe list. The function called LinkedList::appendItem() is providedin the code; you need to add the implementation.
- Compile and run this C++ program.
- Add the code to delete a node from the list. The functioncalled LinkedList::deleteItem() is provided in the code; you needto add the implementation.
- Compile and run this C++ program.
Try deleting the first, last and middle node. Also try to removesomething that is not in the list (an appropriate message should beprinted).
When you are happy with the results, script the following testrun.
Test Run
- Make the list: 4 12 16 999
- Insert an 8
- Append a 30
- Delete the 4
- Delete the 30
- Delete the 12
- Try to delete a 2
(Paste your .h and .cpp files and a screen capture ofthe program output here – upload this document tolab11A.)
We were unable to transcribe this imagehead 704FO newNodePtr 704F0 704F0 NULL currPtr 704FO head 704F0 newNodePtr 704F0 704F0 NULL We were unable to transcribe this imagecurrPtr 704F0 head 704F0 newNodePtr 70500 704F0 70500 70500 NULL We were unable to transcribe this imageShow transcribed image text
head 704FO newNodePtr 704F0 704F0 NULL
currPtr 704FO head 704F0 newNodePtr 704F0 704F0 NULL
currPtr 704F0 head 704F0 newNodePtr 70500 704F0 70500 70500 NULL
Expert Answer
Answer to CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the follo… . . .
OR

