Given List.h header file and client program ola6.cc, implement List.cc program.
ID: 3761439 • Letter: G
Question
Given List.h header file and client program ola6.cc, implement List.cc program.
List.h
#include
using namespace std;
struct Node // a node on the list
{
int item; // a data item on the list
Node *next; // pointer to next node
}; // end struct
// ********************************************************
// Header file List.h for the ADT list.
// Pointer-based implementation.
// *********************************************************
class List
{
public:
// constructors and destructor:
List(); // default constructor
List(string str); // constructor for List from str=“123” builds list 3->2->1
// list operations:
bool isEmpty() const; //is list empty
int getLength() const; //length of list
void insertFront(int newItem); //insert a new node at the front
void insertBack(int newItem); //insert a new node at the back
int removeFront();//remove a node from the front and return node value
void printList(); //print linked list in a form of list 10 -> 5
void printListBack(); //print linked list in a form of list 5->10
private:
int size; // number of items in list
Node *head; // pointer to linked list of items
}; // end class
// End of header file.
ola6.cc
#include <iostream>
#include "List.h"
using namespace std;
int main()
{
List myList; //list objects to test List implementation
List otherList("12345678901234567"); //list objects to test List implementation
int choice; //user choice
int value; //value to add or value in the deleted node
// ask user to choose operations on list
do
{
//display options
cout<<" "
<<" 1: add a new node at the front of myList "
<<" 2: add a new node at the end of myList "
<<" 3: delete the first node of myList "
<<" 4: display otherList "
<<" 5: display myList "
<<" 6: display myList back to front "
<<" 7: quit ";
cout<<"choose your option from the above: ";
cin>>choice;
switch (choice)
{
case 1:
//add a new node at the front of myList
cout<<"item value? ";
cin>>value;
myList.insertFront(value);
break;
case 2:
//add a new node at the end of myList
cout<<"item value? ";
cin>>value;
myList.insertBack(value);
break;
case 3:
//delete the first node of myList
value = myList.removeFront();
cout<<<" is removed from myList ";
break;
case 4:
//display otherList
otherList.printList(); //print otherList
break;
case 5:
//display myList
myList.printList(); //print myList
break;
case 6:
//print back to front
myList.printListBack(); //print linked list back to front list 5 -> 10
break;
case 7:
break;
} //end of switch
} while (choice <7);
return 0;
} // end main
Provide public member functions that perform each of the following tasks:
default construtor
constructor with one parameter(string) //add each character as an integer and add to the front
add a new node at the front of myList
add a new node at the back of myList
delete the first node of myList
display(traverse) myList
display(traverse backward) myList
Program input:
Program expects input from stdin by prompting the user for choice of operation and appropriate value for that operation.
You may test your program with input data: I will test your program with this input.
4
1
18
2
19
5
6
1
11
2
22
5
6
3
1
32
2
35
5
6
3
5
6
7
Program output:
The program prints the list or node value from the removed node onto the stdout:
Explanation / Answer
Answer :
Default Constructor :
List()
Add a new node at the front of myList :(method)
void insertFront(int newItem)
{
node *temp;
int ch;
if(top1+top2 >=5)
{
cout <<"dqueue overflow";
return ;
}
if( top1+top2 == 0)
{
head = new node;
head->data=newItem;
head->next=NULL;
head->prev=NULL;
tail=head;
top1++;
}
else
{
top1++;
temp=new node;
temp->data=newItem;
temp->next=head;
temp->prev=NULL;
head->prev=temp;
head=temp;
}
}
Add a new node at the back of myList : (method)
void insertBack(int newItem){
node *temp;
int ch;
if(top1+top2 >=5)
{
cout <<"dqueue overflow";
return ;
}
if( top1+top2 == 0)
{
head = new node;
head->data=newItem;
head->next=NULL;
head->prev=NULL;
tail=head;
top1++;
}
else
{
top2++;
temp=new node;
temp->data=newItem;
temp->next=NULL;
temp->prev=tail;
tail->next=temp;
tail=temp;
}
}
Delete the first node of myList : (method)
int removeFirst()
{
int ch;
cout <<"Delete 1.First Node";
cin >>ch;
if(top1 + top2 <=0)
{
cout <<" Dqueue under flow";
return;
}
if(ch==1)
{
head=head->next;
head->prev=NULL;
top1--;
}
else
cout<<"Please Choose Correct Option to delete: ";
return head;
}
Display(traverse) myList :(method)
void printList()
{
node *temp;
if(top1+top2 <=0)
{
cout <<"under flow";
return ;
}
temp=head;
while(temp!=NULL)
{
cout << temp->data <<" ";
temp=temp->next;
}
}
Display(traverse backward) myList :
void printListBack()
{
node *temp;
if(top1+top2 <=0)
{
cout <<"under flow";
return ;
}
temp=tail;
while( temp!=NULL)
{
cout <<temp->data << " ";
temp=temp->prev;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.