Module 4 Program 5 Re-submit Assignment Due No Due DatePoints 25 Submitting a fi
ID: 3586702 • Letter: M
Question
Module 4 Program 5 Re-submit Assignment Due No Due DatePoints 25 Submitting a file upload File Types cpp, h, jpg, png, doc, and doc Available Sep 17 at 12am- Oct 13 at 11:59pm 27 days Write a C++ program that implements the circular linked list as discussed on pages 129-133. Demonstrate the program works by creating a main function that as shown on page 133. Print the back and front item in the list after each addition and deletion. Make sure to upload your source code file(s) and any screen print files of your results.Explanation / Answer
#include<iostream>
#include<string>
typedef string Elem;
class CNode
{
private:
Elem elem;
CNode * next;
friend class CircleList;
};
class CircleList
{
public:
CircleList();
~CircleList();
bool empty() const;
const Elem& front();
const Elem& back();
void advance();
void add(const Elem& e);
void remove();
void display();
private:
CNode* cursor;
};
CircleList::CircleList()
{
cursor=NULL;
}
CircleList::~CircleList()
{
while(!empty())
{
remove();
}
}
bool CircleList::empty() const
{
return cursor== NULL;
}
const Elem& CircleList::front()
{
return cursor->next->elem;
}
const Elem& CircleList::back()
{
return cursor->elem;
}
void CircleList::advance()
{
cursor=cursor->next;
}
void CircleList::add(const Elem& e)
{
CNode * v= new CNode;
v->elem = e;
if(cursor==NULL)
{
v-> next=v;
cursor=v;
}
else
{
v->next=cursor->next;
cursor->next=v;
}
}
void CircleList::remove()
{
CNode* old = cursor-> next;
if(old== cursor)
cursor=NULL;
else
cursor->next = old-> next;
delete old;
}
void CircleList::display()
{
while(!empty())
{
cout<<cursor->next->elem<<endl;
remove();
}
}
void main()
{
CircleList playList;
playList.add("Stayin Aliva");
playList.add("Le Freak");
playList.add("Jeva talkin");
playList.advance();
playList.advance();
//playList.advance();
playList.add("Disco Inferio");
playList.display();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.