You are to implement a class intCHDList as a single file intCHDList.cpp. The cla
ID: 3531930 • Letter: Y
Question
- You are to implement a class intCHDList as a single file intCHDList.cpp.
- The class creates circular, headed, doubly-linked lists of integers to implement the list functions.
- Notes on circular, headed, doubly-linked lists can be downloaded from the bottom of this page.
- No other implementation is acceptable
- You must use the DLnode class as the nodes your list.
- The driver program prog4.cpp can be downloaded at the bottom of his page.
- You will uncomment lines in each test to show your list class can do that function.
- You are graded on how many tests you pass (i.e. your list duplicates the C++ list - except for node addresses //main file #include <iostream> #include <list> #include "intCHDList.cpp" // uncomment this for final
using namespace std;
// display the values of nodes in the list void printIntList(list<int> l) { cout << " ["; list<int>::iterator it; for(it=l.begin(); it != l.end(); ++it) cout << *it << " "; cout << "] "; }
// display the addresses of nodes in the list void locDisplay(list<int> l) { cout << "["; list<int>::iterator it; for(it=l.begin(); it != l.end(); ++it) cout << &it << " "; cout << "] "; }
int main() {
// create a list of integers cout << " --- initializing list" << endl; list<int> iList; // intCHDList mList; // uncomment this for final cout << " C++ list is "; printIntList(iList); cout <<"and has size: " << iList.size() << " is Empty?: " << iList.empty() << endl; cout << " >>> My list is "; //uncomment this for final // mList.display(); // uncomment for final // cout << " and has size: "<< mList.size() << " is Empty?: " << mList.empty() << endl; // uncomment this for final
// insert 0..4 in orderinto the list, report list size and show filled list cout << endl << " --- inserting 0..4 at end of list" << endl; for (int i=0 ; i<5; i++) { iList.push_back(i); // mList.insert(i,i); // uncomment this for final } cout << " C++ list is now "; printIntList(iList); cout << " and has size " << iList.size() << endl; // cout << " >>> my list is now: "; //uncooment for final // mList.display(); // uncomment this and next for final // cout <<" and has size: " << mList.size() << endl;
// to search for the ith element in the C++ list cout << endl <<" ---Looking for [2] element in list" << endl; int i=1; list<int>::iterator it; // iterator is our only indexed access to list!! for (it=iList.begin(); i<3; i++, it++); cout << "The [2] element in the C++ list has value " << * it << endl; // cout << ">>> The [2] element in my list has value " << mList.get(2)->value << endl; // uncomment ths for final
// remove the found element from the list cout << endl << " --- deleting [2] element in the list" << endl; cout << " C++ list is now: "; iList.remove(* it); printIntList(iList); cout << endl; // cout << ">>> my list is now: "; //uncomment for final // mList.dele(2); //uncomment for final // mList.display(); //uncomment for final // cout << endl; //uncomment for final
// insert a new element as the third in the list cout << endl << " --- inserting 5 as the [2] item in the list" << endl; for(it=iList.begin(), i=1; it != iList.end() && i<3; ++it, i++); // access 2nd by iteratorr iList.insert(it,5); // using pointer found above cout << " C++ list is now: "; printIntList(iList); cout << endl; // mList.insert(5,2); //uncomment for final // cout << " >>> my list is now: "; //uncomment for final // mList.display(); //uncomment for final // cout << endl; //uncomment for final
// show the actual nodes in the lists cout << endl << " --- inspecting structures by showing data node addresses" << endl; locDisplay(iList); cout << endl; // mList.locDisplay(); //uncomment for final // cout << endl; //uncomment for final
/* the operations of the C++ list class make it easier to operate with values rather than locations in sequence - we give examples of two here: */ // delete the value 4 cout << endl << " --- removing the VALUE 4" <<endl; iList.remove(4); cout << " C++ list is now"; printIntList(iList); cout << endl; // mList.remove(4); //uncomment for final // cout << " >>> my list is now: "; //uncomment for final // mList.display(); //uncomment for final // cout << endl; //uncomment for final
system("pause"); return 0; }
//dlnode class that has to be implemented in to the main.cpp file class DLnode { public :
int value; DLnode * next ; DLnode * previous;
DLnode() { next = previous = 0; value=0; }
DLnode(int val){ next = previous = 0; value = val; }
DLnode (int val, DLnode * nxt, DLnode * prv){ value = val; next = nxt; previous = prv; }
};
- The class creates circular, headed, doubly-linked lists of integers to implement the list functions.
using namespace std;
// display the values of nodes in the list void printIntList(list<int> l) { cout << " ["; list<int>::iterator it; for(it=l.begin(); it != l.end(); ++it) cout << *it << " "; cout << "] "; }
// display the addresses of nodes in the list void locDisplay(list<int> l) { cout << "["; list<int>::iterator it; for(it=l.begin(); it != l.end(); ++it) cout << &it << " "; cout << "] "; }
int main() {
// create a list of integers cout << " --- initializing list" << endl; list<int> iList; // intCHDList mList; // uncomment this for final cout << " C++ list is "; printIntList(iList); cout <<"and has size: " << iList.size() << " is Empty?: " << iList.empty() << endl; cout << " >>> My list is "; //uncomment this for final // mList.display(); // uncomment for final // cout << " and has size: "<< mList.size() << " is Empty?: " << mList.empty() << endl; // uncomment this for final
// insert 0..4 in orderinto the list, report list size and show filled list cout << endl << " --- inserting 0..4 at end of list" << endl; for (int i=0 ; i<5; i++) { iList.push_back(i); // mList.insert(i,i); // uncomment this for final } cout << " C++ list is now "; printIntList(iList); cout << " and has size " << iList.size() << endl; // cout << " >>> my list is now: "; //uncooment for final // mList.display(); // uncomment this and next for final // cout <<" and has size: " << mList.size() << endl;
// to search for the ith element in the C++ list cout << endl <<" ---Looking for [2] element in list" << endl; int i=1; list<int>::iterator it; // iterator is our only indexed access to list!! for (it=iList.begin(); i<3; i++, it++); cout << "The [2] element in the C++ list has value " << * it << endl; // cout << ">>> The [2] element in my list has value " << mList.get(2)->value << endl; // uncomment ths for final
// remove the found element from the list cout << endl << " --- deleting [2] element in the list" << endl; cout << " C++ list is now: "; iList.remove(* it); printIntList(iList); cout << endl; // cout << ">>> my list is now: "; //uncomment for final // mList.dele(2); //uncomment for final // mList.display(); //uncomment for final // cout << endl; //uncomment for final
// insert a new element as the third in the list cout << endl << " --- inserting 5 as the [2] item in the list" << endl; for(it=iList.begin(), i=1; it != iList.end() && i<3; ++it, i++); // access 2nd by iteratorr iList.insert(it,5); // using pointer found above cout << " C++ list is now: "; printIntList(iList); cout << endl; // mList.insert(5,2); //uncomment for final // cout << " >>> my list is now: "; //uncomment for final // mList.display(); //uncomment for final // cout << endl; //uncomment for final
// show the actual nodes in the lists cout << endl << " --- inspecting structures by showing data node addresses" << endl; locDisplay(iList); cout << endl; // mList.locDisplay(); //uncomment for final // cout << endl; //uncomment for final
/* the operations of the C++ list class make it easier to operate with values rather than locations in sequence - we give examples of two here: */ // delete the value 4 cout << endl << " --- removing the VALUE 4" <<endl; iList.remove(4); cout << " C++ list is now"; printIntList(iList); cout << endl; // mList.remove(4); //uncomment for final // cout << " >>> my list is now: "; //uncomment for final // mList.display(); //uncomment for final // cout << endl; //uncomment for final
system("pause"); return 0; }
//dlnode class that has to be implemented in to the main.cpp file class DLnode { public :
int value; DLnode * next ; DLnode * previous;
DLnode() { next = previous = 0; value=0; }
DLnode(int val){ next = previous = 0; value = val; }
DLnode (int val, DLnode * nxt, DLnode * prv){ value = val; next = nxt; previous = prv; }
};
#include <iostream> #include <list> #include "intCHDList.cpp" // uncomment this for final
using namespace std;
// display the values of nodes in the list void printIntList(list<int> l) { cout << " ["; list<int>::iterator it; for(it=l.begin(); it != l.end(); ++it) cout << *it << " "; cout << "] "; }
// display the addresses of nodes in the list void locDisplay(list<int> l) { cout << "["; list<int>::iterator it; for(it=l.begin(); it != l.end(); ++it) cout << &it << " "; cout << "] "; }
int main() {
// create a list of integers cout << " --- initializing list" << endl; list<int> iList; // intCHDList mList; // uncomment this for final cout << " C++ list is "; printIntList(iList); cout <<"and has size: " << iList.size() << " is Empty?: " << iList.empty() << endl; cout << " >>> My list is "; //uncomment this for final // mList.display(); // uncomment for final // cout << " and has size: "<< mList.size() << " is Empty?: " << mList.empty() << endl; // uncomment this for final
// insert 0..4 in orderinto the list, report list size and show filled list cout << endl << " --- inserting 0..4 at end of list" << endl; for (int i=0 ; i<5; i++) { iList.push_back(i); // mList.insert(i,i); // uncomment this for final } cout << " C++ list is now "; printIntList(iList); cout << " and has size " << iList.size() << endl; // cout << " >>> my list is now: "; //uncooment for final // mList.display(); // uncomment this and next for final // cout <<" and has size: " << mList.size() << endl;
// to search for the ith element in the C++ list cout << endl <<" ---Looking for [2] element in list" << endl; int i=1; list<int>::iterator it; // iterator is our only indexed access to list!! for (it=iList.begin(); i<3; i++, it++); cout << "The [2] element in the C++ list has value " << * it << endl; // cout << ">>> The [2] element in my list has value " << mList.get(2)->value << endl; // uncomment ths for final
// remove the found element from the list cout << endl << " --- deleting [2] element in the list" << endl; cout << " C++ list is now: "; iList.remove(* it); printIntList(iList); cout << endl; // cout << ">>> my list is now: "; //uncomment for final // mList.dele(2); //uncomment for final // mList.display(); //uncomment for final // cout << endl; //uncomment for final
// insert a new element as the third in the list cout << endl << " --- inserting 5 as the [2] item in the list" << endl; for(it=iList.begin(), i=1; it != iList.end() && i<3; ++it, i++); // access 2nd by iteratorr iList.insert(it,5); // using pointer found above cout << " C++ list is now: "; printIntList(iList); cout << endl; // mList.insert(5,2); //uncomment for final // cout << " >>> my list is now: "; //uncomment for final // mList.display(); //uncomment for final // cout << endl; //uncomment for final
// show the actual nodes in the lists cout << endl << " --- inspecting structures by showing data node addresses" << endl; locDisplay(iList); cout << endl; // mList.locDisplay(); //uncomment for final // cout << endl; //uncomment for final
/* the operations of the C++ list class make it easier to operate with values rather than locations in sequence - we give examples of two here: */ // delete the value 4 cout << endl << " --- removing the VALUE 4" <<endl; iList.remove(4); cout << " C++ list is now"; printIntList(iList); cout << endl; // mList.remove(4); //uncomment for final // cout << " >>> my list is now: "; //uncomment for final // mList.display(); //uncomment for final // cout << endl; //uncomment for final
system("pause"); return 0; }
//dlnode class that has to be implemented in to the main.cpp file class DLnode { public :
int value; DLnode * next ; DLnode * previous;
DLnode() { next = previous = 0; value=0; }
DLnode(int val){ next = previous = 0; value = val; }
DLnode (int val, DLnode * nxt, DLnode * prv){ value = val; next = nxt; previous = prv; }
};
class DLnode { public :
int value; DLnode * next ; DLnode * previous;
DLnode() { next = previous = 0; value=0; }
DLnode(int val){ next = previous = 0; value = val; }
DLnode (int val, DLnode * nxt, DLnode * prv){ value = val; next = nxt; previous = prv; }
};
Explanation / Answer
//Doubly Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct dll
{
int data;
struct dll *next,*prev;
}node;
int n;
node *head;
void create();
void insert();
void delete();
void display();
int main()
{
int choice;
do
{
printf(" >>>>>>DOUBLY LINKED LIST<<<<<< ");
printf(" 1.CREATE 2.INSERT 3.DELETE 4.DISPLAY 0.EXIT");
printf(" WHAT IS YOUR CHOICE?? ");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
delete();
break;
case 4:
display();
break;
default:
exit(0);
}
}while(choice<=4); }
void create()
{
int i;
node *new,*temp;
printf(" ENTER NUMBER OF NODES TO BE CREATED: ");
scanf("%d",&n);
head=(node*)malloc(sizeof(node));
head->prev=head;
head->next=NULL;
temp=head;
printf(" ENTER THE DATA IN THE NODES: ");
scanf("%d",&head->data);
for(i=2;i<=n;i++)
{
new=(node*)malloc(sizeof(node));
scanf("%d",&new->data);
temp->next=new;
new->prev=temp;
temp->prev=new;
temp=temp->next;
}
display();
}
void display()
{
node *temp;
temp=head;
do
{
printf("%d=> ",temp->data);
temp=temp->next;
}
while(temp!=NULL);
getch();
}
void insert()
{
node *temp,*new,*temp1;
int loc,data,i;
printf(" ENTER THE DATA IN THE NEW NODE: ");
scanf("%d",&data);
printf(" ENTER THE LOCATION OF THE NEW NODE: ");
scanf("%d",&loc);
new=(node*)malloc(sizeof(node));
if(loc==1)
{
new->data=data;
new->next=NULL;
new->prev=head->prev;
temp=head->prev;
temp->prev=new;
}
else
{
temp=head;
for(i=2;(i<loc&&(temp->next!=head));i++)
{temp=temp->next;}
temp1=temp->next;
new->data=data;
temp->next=new;
new->prev=temp;
temp1->prev=new;
new->next=temp1;
}
n++;
printf(" AFTER INSERTION: ");
display();
}
void delete()
{
node *new,*temp,*tempr,*templ;
int ne;
printf(" ENTER THE DATA TO BE DELETED: ");
scanf("%d",&ne);
temp=head;
while(temp->data!=ne)
{
temp=temp->next;
}
templ=temp->prev;
tempr=temp->next;
free(temp);
templ->next=tempr;
tempr->prev=templ;
printf(" AFTER DELETION ");
display();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.