SlideShare a Scribd company logo
1 of 6
Download to read offline
Using the header(dlist) and mainline file (dlistapp) below
You are going to write public functions for a doubly linked list class.
The dlist class is declared in the file dlist.h as follows:
struct dlist_node
{
char contents; // contents in the node
dlist_node *back, // pointer to previous node in the list
*next; // pointer to the next node in the list
};
typedef dlist_node* dptr;
class dlist
{
private:
dptr front, // pointer to the front of the list
current; // pointer to current node in the list
public:
dlist (); // constructor creates an empty list
void insert (char ch); // inserts a new node
void remove (); // removes a node
void Move_Right(int distance); // moves current right
void Move_Left(int distance); // moves current left
void print (); // prints the list
};
The public functions that you need to define are:
dlist (): Constructor that initializes the list to be empty.
voidinsert (char ch): Adds a new node to the right of current containing ch and points current at
the new node. Should insert first node correctly.
void remove (): Removes the node from the list pointed to by current. Points current at the node
after the deleted node (if present) else points current at the node before the deleted node (if
present). Should remove last node correctly and recycle nodes. Should not fail if list is empty.
void Move_Right (int distance): Moves current to the right distance nodes. If the given distance
will move current off the end of the list, current should be set to point at the rightmost node.
Should not fail if list is empty.
void Move_Left (int distance): Moves current to the left distance nodes. If the given distance will
move current off the end of the list, current should be set to point at the leftmost node. Should
not fail if list is empty.
void print (): Prints all the nodes in the list. The value pointed to by current should be printed in
braces.print does not output any spaces or linefeeds. For example, if the data in the linklist
represents CIS 361, the output would be :
CIS 3{6}1
The dlistapp.cpp application file is coded except for the calls to the public functions. Add the
calls as directed by the comments in the file.
dlist.h
dlistapp.cpp
Sample run
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): d
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): r
Enter the distance to move right: 10
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): l
Enter the distance to move left: 2
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): p
The list is:
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): i
Enter the character to insert: h front current
Solution
#include
using namespace std;
struct dlist_node
{
char contents; // contents in the node
dlist_node *back, // pointer to previous node in the list
*next; // pointer to the next node in the list
};
typedef dlist_node* dptr;
class dlist
{
private:
dptr front, // pointer to the front of the list
current; // pointer to current node in the list
public:
dlist (); // constructor creates an empty list
void insert (char ch); // inserts a new node
void remove (); // removes a node
void Move_Right (int distance); // moves current to right
void Move_Left (int distance); // moves current to left
void print (); // prints the list
};
dlist::dlist()
{
front = NULL;
current = NULL;
}
void dlist::insert(char ch)
{
dptr node = new dlist_node;
node->contents = ch;
node->back = current;
node->next = NULL;
current = node;
if (front == NULL)
front = node;
}
void dlist::remove()
{
dptr node = current;
dptr prev = current->back;
if (prev)
prev->next = current->next;
delete current;
if (prev && prev->next == NULL)
current = prev;
else
current = prev->next;
}
void dlist::Move_Right(int distance)
{
int i;
for(i = 0;i < distance;i++)
{
if (current && current->next)
current = current->next;
else
break;
}
}
void dlist::Move_Left(int distance)
{
int i;
for(i = 0;i < distance;i++)
{
if (current && current->back)
current = current->back;
else
break;
}
}
void dlist::print()
{
dptr n = front;
while(n != NULL)
{
cout<contents<<" ";
n = n->next;
}
}
int main(int argc, char *argv[])
{
char choice, ch;
int distance;
dlist *lis = new dlist;
/**** Declare a dlist variable ****/
cout << "Select p (print), i (insert), d (delete), r (right),";
cout << " l (left) or q (quit): ";
cin >> choice;
while (choice != 'q')
{
switch (choice)
{
case 'p' :
{
cout << " The list is: ";
lis.print();
cout << endl;
break;
}
case 'i' :
{
cout << " Enter the character to insert: ";
cin >> ch;
lis.insert(ch);
break;
}
case 'r' :
{
cout << " Enter the distance to move right: ";
cin >> distance;
lis.Move_Right(distance);
break;
}
case 'l' :
{
cout << " Enter the distance to move left: ";
cin >> distance;
lis.Move_Left(distance);
break;
}
case 'd' :
{
lis.remove();
cout << endl;
break;
}
default : cout << " Must enter p, i, d, r, l, or q! ";
}
cout << " Select p (print), i (insert), d (delete), r (right),";
cout << " l (left) or q (quit): ";
cin >> choice;
}
cout << "  Editing Complete  ";
return 0;
}

More Related Content

Similar to Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf

Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
amitbagga0808
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
tienlivick
 
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdfC++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
feelinggift
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
flashfashioncasualwe
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
callawaycorb73779
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
vishalateen
 
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdfIn C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
stopgolook
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdf
connellalykshamesb60
 
File Type cppAdd the following to your linked list of strings pro.pdf
File Type cppAdd the following to your linked list of strings pro.pdfFile Type cppAdd the following to your linked list of strings pro.pdf
File Type cppAdd the following to your linked list of strings pro.pdf
footworld1
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
There are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxThere are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docx
clarkjanyce
 

Similar to Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf (20)

C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdfC++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdfIn C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdf
 
File Type cppAdd the following to your linked list of strings pro.pdf
File Type cppAdd the following to your linked list of strings pro.pdfFile Type cppAdd the following to your linked list of strings pro.pdf
File Type cppAdd the following to your linked list of strings pro.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
There are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxThere are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docx
 
Final ds record
Final ds recordFinal ds record
Final ds record
 
C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
 

More from udit652068

Did BP respond in a manner that was appropriate with the oil spill t.pdf
Did BP respond in a manner that was appropriate with the oil spill t.pdfDid BP respond in a manner that was appropriate with the oil spill t.pdf
Did BP respond in a manner that was appropriate with the oil spill t.pdf
udit652068
 
Describe and provide at least two examples of direct transmission of.pdf
Describe and provide at least two examples of direct transmission of.pdfDescribe and provide at least two examples of direct transmission of.pdf
Describe and provide at least two examples of direct transmission of.pdf
udit652068
 
Define e-commerce and describe how it differs from e-business.So.pdf
Define e-commerce and describe how it differs from e-business.So.pdfDefine e-commerce and describe how it differs from e-business.So.pdf
Define e-commerce and describe how it differs from e-business.So.pdf
udit652068
 
You are to write a C++ program to produce an inventory repor.pdf
You are to write a C++ program to produce an inventory repor.pdfYou are to write a C++ program to produce an inventory repor.pdf
You are to write a C++ program to produce an inventory repor.pdf
udit652068
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
udit652068
 
Write a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdfWrite a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdf
udit652068
 
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdfTOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
udit652068
 
These are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdfThese are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdf
udit652068
 
The selection of officials in which of the following branches of the.pdf
The selection of officials in which of the following branches of the.pdfThe selection of officials in which of the following branches of the.pdf
The selection of officials in which of the following branches of the.pdf
udit652068
 

More from udit652068 (20)

Discuss briefly and with examples, What key measures need to be take.pdf
Discuss briefly and with examples, What key measures need to be take.pdfDiscuss briefly and with examples, What key measures need to be take.pdf
Discuss briefly and with examples, What key measures need to be take.pdf
 
Did BP respond in a manner that was appropriate with the oil spill t.pdf
Did BP respond in a manner that was appropriate with the oil spill t.pdfDid BP respond in a manner that was appropriate with the oil spill t.pdf
Did BP respond in a manner that was appropriate with the oil spill t.pdf
 
Describe and provide at least two examples of direct transmission of.pdf
Describe and provide at least two examples of direct transmission of.pdfDescribe and provide at least two examples of direct transmission of.pdf
Describe and provide at least two examples of direct transmission of.pdf
 
Define VRIO in business strategy Define VRIO in business strat.pdf
Define VRIO in business strategy Define VRIO in business strat.pdfDefine VRIO in business strategy Define VRIO in business strat.pdf
Define VRIO in business strategy Define VRIO in business strat.pdf
 
Define e-commerce and describe how it differs from e-business.So.pdf
Define e-commerce and describe how it differs from e-business.So.pdfDefine e-commerce and describe how it differs from e-business.So.pdf
Define e-commerce and describe how it differs from e-business.So.pdf
 
Building Social Business - EssaySolutionBuilding social busine.pdf
Building Social Business - EssaySolutionBuilding social busine.pdfBuilding Social Business - EssaySolutionBuilding social busine.pdf
Building Social Business - EssaySolutionBuilding social busine.pdf
 
You are to write a C++ program to produce an inventory repor.pdf
You are to write a C++ program to produce an inventory repor.pdfYou are to write a C++ program to produce an inventory repor.pdf
You are to write a C++ program to produce an inventory repor.pdf
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
Write a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdfWrite a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdf
 
Which of the following defines a method doubleEach that returns an ar.pdf
Which of the following defines a method doubleEach that returns an ar.pdfWhich of the following defines a method doubleEach that returns an ar.pdf
Which of the following defines a method doubleEach that returns an ar.pdf
 
When the Fed provides more reserves toprovides more reserves to.pdf
When the Fed provides more reserves toprovides more reserves to.pdfWhen the Fed provides more reserves toprovides more reserves to.pdf
When the Fed provides more reserves toprovides more reserves to.pdf
 
What is a common infrastructure and what does it provideSolutio.pdf
What is a common infrastructure and what does it provideSolutio.pdfWhat is a common infrastructure and what does it provideSolutio.pdf
What is a common infrastructure and what does it provideSolutio.pdf
 
What are the eukaryotic kingdomsQuestion 4 optionsBacteria, Ar.pdf
What are the eukaryotic kingdomsQuestion 4 optionsBacteria, Ar.pdfWhat are the eukaryotic kingdomsQuestion 4 optionsBacteria, Ar.pdf
What are the eukaryotic kingdomsQuestion 4 optionsBacteria, Ar.pdf
 
What are some of the commonly seen WLAN devices What role does each.pdf
What are some of the commonly seen WLAN devices What role does each.pdfWhat are some of the commonly seen WLAN devices What role does each.pdf
What are some of the commonly seen WLAN devices What role does each.pdf
 
Virology.Explain the make up and role of different complexes for .pdf
Virology.Explain the make up and role of different complexes for .pdfVirology.Explain the make up and role of different complexes for .pdf
Virology.Explain the make up and role of different complexes for .pdf
 
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdfTOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
TOPOLOGY 541Let M be a set with two members a and b. Define the fu.pdf
 
These are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdfThese are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdf
 
The test solution is made basic and drops of 0.1 M Ca(NO3)2 are adde.pdf
The test solution is made basic and drops of 0.1 M Ca(NO3)2 are adde.pdfThe test solution is made basic and drops of 0.1 M Ca(NO3)2 are adde.pdf
The test solution is made basic and drops of 0.1 M Ca(NO3)2 are adde.pdf
 
The selection of officials in which of the following branches of the.pdf
The selection of officials in which of the following branches of the.pdfThe selection of officials in which of the following branches of the.pdf
The selection of officials in which of the following branches of the.pdf
 
the distance between the N-terminal and C-ternimal amino acids varie.pdf
the distance between the N-terminal and C-ternimal amino acids varie.pdfthe distance between the N-terminal and C-ternimal amino acids varie.pdf
the distance between the N-terminal and C-ternimal amino acids varie.pdf
 

Recently uploaded

Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
cupulin
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 

Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf

  • 1. Using the header(dlist) and mainline file (dlistapp) below You are going to write public functions for a doubly linked list class. The dlist class is declared in the file dlist.h as follows: struct dlist_node { char contents; // contents in the node dlist_node *back, // pointer to previous node in the list *next; // pointer to the next node in the list }; typedef dlist_node* dptr; class dlist { private: dptr front, // pointer to the front of the list current; // pointer to current node in the list public: dlist (); // constructor creates an empty list void insert (char ch); // inserts a new node void remove (); // removes a node void Move_Right(int distance); // moves current right void Move_Left(int distance); // moves current left void print (); // prints the list }; The public functions that you need to define are: dlist (): Constructor that initializes the list to be empty. voidinsert (char ch): Adds a new node to the right of current containing ch and points current at the new node. Should insert first node correctly. void remove (): Removes the node from the list pointed to by current. Points current at the node after the deleted node (if present) else points current at the node before the deleted node (if present). Should remove last node correctly and recycle nodes. Should not fail if list is empty. void Move_Right (int distance): Moves current to the right distance nodes. If the given distance will move current off the end of the list, current should be set to point at the rightmost node. Should not fail if list is empty. void Move_Left (int distance): Moves current to the left distance nodes. If the given distance will move current off the end of the list, current should be set to point at the leftmost node. Should
  • 2. not fail if list is empty. void print (): Prints all the nodes in the list. The value pointed to by current should be printed in braces.print does not output any spaces or linefeeds. For example, if the data in the linklist represents CIS 361, the output would be : CIS 3{6}1 The dlistapp.cpp application file is coded except for the calls to the public functions. Add the calls as directed by the comments in the file. dlist.h dlistapp.cpp Sample run Select p (print), i (insert), d (delete), r (right), l (left) or q (quit): d Select p (print), i (insert), d (delete), r (right), l (left) or q (quit): r Enter the distance to move right: 10 Select p (print), i (insert), d (delete), r (right), l (left) or q (quit): l Enter the distance to move left: 2 Select p (print), i (insert), d (delete), r (right), l (left) or q (quit): p The list is: Select p (print), i (insert), d (delete), r (right), l (left) or q (quit): i Enter the character to insert: h front current Solution #include using namespace std; struct dlist_node { char contents; // contents in the node dlist_node *back, // pointer to previous node in the list *next; // pointer to the next node in the list }; typedef dlist_node* dptr;
  • 3. class dlist { private: dptr front, // pointer to the front of the list current; // pointer to current node in the list public: dlist (); // constructor creates an empty list void insert (char ch); // inserts a new node void remove (); // removes a node void Move_Right (int distance); // moves current to right void Move_Left (int distance); // moves current to left void print (); // prints the list }; dlist::dlist() { front = NULL; current = NULL; } void dlist::insert(char ch) { dptr node = new dlist_node; node->contents = ch; node->back = current; node->next = NULL; current = node; if (front == NULL) front = node; } void dlist::remove() { dptr node = current; dptr prev = current->back; if (prev) prev->next = current->next; delete current;
  • 4. if (prev && prev->next == NULL) current = prev; else current = prev->next; } void dlist::Move_Right(int distance) { int i; for(i = 0;i < distance;i++) { if (current && current->next) current = current->next; else break; } } void dlist::Move_Left(int distance) { int i; for(i = 0;i < distance;i++) { if (current && current->back) current = current->back; else break; } } void dlist::print() { dptr n = front; while(n != NULL) { cout<contents<<" "; n = n->next; }
  • 5. } int main(int argc, char *argv[]) { char choice, ch; int distance; dlist *lis = new dlist; /**** Declare a dlist variable ****/ cout << "Select p (print), i (insert), d (delete), r (right),"; cout << " l (left) or q (quit): "; cin >> choice; while (choice != 'q') { switch (choice) { case 'p' : { cout << " The list is: "; lis.print(); cout << endl; break; } case 'i' : { cout << " Enter the character to insert: "; cin >> ch; lis.insert(ch); break; } case 'r' : { cout << " Enter the distance to move right: "; cin >> distance; lis.Move_Right(distance); break;
  • 6. } case 'l' : { cout << " Enter the distance to move left: "; cin >> distance; lis.Move_Left(distance); break; } case 'd' : { lis.remove(); cout << endl; break; } default : cout << " Must enter p, i, d, r, l, or q! "; } cout << " Select p (print), i (insert), d (delete), r (right),"; cout << " l (left) or q (quit): "; cin >> choice; } cout << " Editing Complete "; return 0; }