Question: Take the code I have written here and change it into a doubly linked list. You cannot write the doubly linked list without understanding the
Take the code I have written here and change it into a doubly linked list. You cannot write the doubly linked list without understanding the singly linked list presented. There are tons of good tutorials on singly and doubly linked lists online.
Written by Professor Kenneth L Moore
For CIT Data Structures and Programming C
This code implements a singly linked list
Students must change it to a doubly linked list
#include
#include
using namespace std;
define a node for storage and linking
class node
public:
string name;
node next;
node prev; to be implemented by students
;
class linkedList
public:
linkedList:topNULL
bool emptyreturn top NULL;
node getTopreturn top;
void setTopnode ntop n;
void addstring;
int menu;
void removestring;
~linkedList;
void reversePrint; to be implemented by students
friend ostream& operator ostream& const linkedList&; default output is inorder print.
private:
node top;
node end; to be used for reverse print and implemented by students
;
void main
linkedList l;
cout lempty endl;
int option ;
string s;
bool go true;
whilego
option lmenu;
switchoption
case : cout "enter a name: ;cin s; ladds; break;
case : cout "enter name to be deleted: ; cin s; lremoves;break;
case : cout l; break;
case : cout "can not be done with a singly linked list" endl;
case : cout "exiting" endl; go false; break;
l goes out of scope and calls ~linkedList
can not call this method "delete" "delete" is a reserved keyword.
void linkedList::removestring s
bool found false;
node curr getTopprevNULL;
whilecurr NULL
match found, delete
ifcurrname s
found true;
found at top
ifprev NULL
node temp getTop;
setTopcurrnext;
deletetemp;
found in list not top
else
prevnext currnext;
deletecurr;
not found, advance pointers
iffound
prev curr;
curr currnext;
found, exit loop
else curr NULL;
iffoundcout "Deleted s endl;
else cout s Not Found endl;
void linkedList::addstring s
node n new node;
nname s;
nnext NULL;
take care of empty list case
ifempty
top n;
take care of node belongs at beginning case
else ifgetTopname s
nnext getTop;
setTopn;
take care of inorder and end insert
else
insert in order case
node curr getTopprev curr;
whilecurr NULL
ifcurrname sbreak;
prev curr;
curr currnext;
ifcurr NULL search found insert point
nnext curr;
prevnext n;
take care of end of list insertion
else ifcurr NULL search did not find insert point
prevnext n;
ostream& operator ostream& os const linkedList& ll
linkedList x ll; put this in and the code blows up why?
node n lltop;
ifn NULLcout "List is empty." endl;
else
whilen NULL
os nname endl;
n nnext;
return os;
return memory to heap
linkedList::~linkedList
cout ~linkedList called." endl;
node curr getTopdel;
whilecurr NULL
del curr;
curr currnext;
deletedel;
int linkedList::menu
int choice ;
whilechoice choice
cout
Enter your choice" endl;
cout Add a name." endl;
cout Delete a name." endl;
cout Show list." endl;
cout Show reverse list. endl; to be implemented by students
cout EXIT endl;
cin choice;
return choice;
Example Output:
Enter your choice
Add a name.
Delete a name.
Show list.
Show reverse list.
EXIT
enter a name: liz
Enter your choice
Add a name.
Delete a name.
Show list.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
