I have two files and need some help re-formatting it. I have a main.cpp and a dL
ID: 3724404 • Letter: I
Question
I have two files and need some help re-formatting it. I have a main.cpp and a dList.cpp that I will post here and the main is supposed to run the dList.cpp with no included files. The expected output it is posted in the picture. can anyone help me get it close to this
main file
----------------------------------------------------------------------------
#include<string.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
#include "dList.cpp"
#define SMALL 200
#define MAX 100000
#define ROUNDS 100
int main()
{
int i, x[MAX];
char ch[MAX];
for(i=0;i<SMALL;i++)
{
x[i] = 2*SMALL-i;
ch[i] = (int)'a'+ (i%26);
}
dList A(x,ch,SMALL), B;
A.out(10);
node *tmp = A.search(2*SMALL-8);
A.moveFront(tmp);
A.out(10);
A.moveBack(tmp);
A.out(10,'b');
A.find('b');
A.sort();
A.out(10);
A.out(10,'b');
A.addBack(500,'d');
A.addFront(501,'z');
A.out(10);
A.out(10,'b');
B.addFront(1,'a');
B.addBack(2,'b');
B.out(2);
for(int j=0; j<ROUNDS; j++)
{
cout << endl << "round " << j << endl;
for(i=0;i<MAX;i++)
{
x[i] = 2*MAX-i;
ch[i] = (int)('a')+(i%26);
}
dList A(x,ch,MAX);
node *tmp = A.search(2*MAX-8);
A.moveFront(tmp);
A.moveBack(tmp);
//A.sort();
A.out(10);
A.out(10,'b');
}
}
-------------------------------------------------------------------------------
dList.cpp
-----------------------------------------------------------------------------
struct node
{
int key;
char type;
struct node *l;
struct node *r;
};
class dList
{
public:
struct node *head=NULL;
dList()
{
head=NULL;
cout<<"Empty List is prepaired" << endl;
}
dList(int key_arr[], char type_arr[],int size)
{
for(int i=size-1;i>=0;i--)
{
struct node *new1=(struct node *)malloc(sizeof(struct node));
new1->key=key_arr[i];
new1->type=type_arr[i];
new1->r=head;
new1->l=NULL;
if(head!=NULL)
{
head->l=new1;
}
head=new1;
}
cout<<"List is prepaired" << endl;
}
void addFront(int key_, char type_)
{
struct node *new1=(struct node *)malloc(sizeof(struct node));
new1->key=key_;
new1->type=type_;
new1->r=head;
new1->l=NULL;
if(head!=NULL)
{
head->l=new1;
}
head=new1;
cout<<"New node is added at the front of list" << endl;
}
void addBack(int key_, char type_)
{
struct node *traverse;
traverse=head;
while(traverse->r!=NULL)
{
traverse=traverse->r;
}
struct node *new1=(struct node *)malloc(sizeof(struct node));
new1->key=key_;
new1->type=type_;
traverse->r=new1;
new1->r=NULL;
new1->l=traverse;
cout<<"New node is added at the end of list" << endl;
}
node* search(int key_)
{
struct node *traverse;
traverse=head;
while(traverse!=NULL)
{
if(traverse->key==key_)
{
return traverse;
}
traverse=traverse->r;
}
return NULL;
}
void find(char type_)
{
struct node *traverse;
traverse=head;
int flag=0;
while(traverse!=NULL)
{
if(toupper(traverse->type)==toupper(type_))
{
flag=1;
cout<< endl << "Key:"<<traverse->key<<" have Same Type "<<type_<< endl;
}
traverse=traverse->r;
}
if(!flag)
{
cout<< endl << "No Key Corresponding to "<<type_<<"in the list" << endl;
}
}
void moveFront(struct node *shift)
{
struct node *traverse;
traverse=head;
int flag=0;
if(head->l==NULL&&head->r==NULL)
{
cout<< endl << "List have only 1 node" << endl;
}
while(traverse!=NULL&&shift!=NULL)
{
if(shift->key==traverse->key)
{
flag=1;
break;
}
traverse=traverse->r;
}
if(flag)
{
if(traverse==head)
{
cout<< endl << "This node is already at the front of the list" << endl;
}
else if(traverse->r==NULL)
{
traverse->l->r=NULL;
traverse->l=NULL;
head->l=traverse;
traverse->r=head;
head=traverse;
cout<< endl << "Node Have Been Shifted." << endl;
}
else
{
traverse->l->r=traverse->r;
traverse->r->l=traverse->l;
traverse->l=NULL;
head->l=traverse;
traverse->r=head;
head=traverse;
cout<< endl << "Node Have Been Shifted." << endl;
}
}
else
{
cout<< endl << "This address is not belongs to this list" << endl;
}
}
void moveBack(struct node *shift)
{
if(head->l==NULL&&head->r==NULL)
{
cout<< endl << "List have only 1 node" << endl;
}
if(head==NULL)
{
cout<< endl << "No list found"<< endl;
}
struct node *traverse;
traverse=head;
int flag=0;
struct node *end;
end=head;
while(end->r!=NULL)
{
end=end->r;
}
while(traverse!=NULL&&shift!=NULL)
{
if(shift->key==traverse->key)
{
cout<< endl << "Found" << endl;
flag=1;
break;
}
traverse=traverse->r;
}
if(flag)
{
if(traverse->r==NULL)
{
cout<< endl << "This node is already at the End of the list" << endl;
}
else if(traverse==head)
{
head=head->r;
head->l=NULL;
traverse->l=end;
traverse->r=NULL;
end->r=traverse;
cout<< endl << "Node Have Been Shifted." << endl;
}
else
{
traverse->l->r=traverse->r;
traverse->r->l=traverse->l;
traverse->l=end;
traverse->r=NULL;
end->r=traverse;
cout<< endl << "Node Have Been Shifted." << endl;
}
}
else
{
cout<< endl << "This address is not belongs to the list" << endl;
}
}
void out(int k, char from= 'f')
{
int flag=0;
int count=0;
struct node *traverse;
if(tolower(from)=='b')
{
struct node *end=head;
while(end->r!=NULL)
{
end=end->r;
}
traverse=end;
cout<< endl << "Your last "<<k<<" Elements in reverse order" << endl;
while(traverse!=head&&count<k)
{
cout<< endl << traverse->key<< " " << traverse -> type <<endl;
count++;
flag=1;
traverse=traverse->l;
}
if(!flag)
{
cout<<"No elements in the list" << endl;
}
}
else if(tolower(from)=='f')
{
traverse=head;
cout<< endl << " Your first "<<k<<" Elements" << endl;
while(traverse!=NULL&&count<k)
{
cout<< endl <<traverse->key<<" "<<traverse->type<< endl;
count++;
flag=1;
traverse=traverse->r;
}
if(!flag)
{
cout<<"No elements in the list" << endl;
}
}
else
{
cout<< endl << "Invalid start indicater provided" << endl;
}
}
void sort()
{
int key_[10000];
char type[10000];
int i=0;
struct node *temp;
while(head!=NULL)
{
key_[i]=head->key;
type[i]=head->type;
temp=head;
head=head->r;
i++;
free(temp);
}
cout<< endl <<i<<endl;
//now sort the array..............................................................................................!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for(int j=i-1;j>=0;j--)
{
struct node *new1=(struct node *)malloc(sizeof(struct node));
new1->key=key_[j];
new1->type=type[j];
new1->r=head;
new1->l=NULL;
if(head!=NULL)
{
head->l=new1;
}
head=new1;
}
cout<<"Sorted List is ready" << endl;
}
};
------------------------------------------------------------------------------
You will notice a unix command named ulimit below. Ulimit is used to control the amount of memory that a process can use. I will use the ulimit command to determine if your code has a memory leak. As you see below, the solution code runs with a ulimit of 17000 for 100 rounds and would continue indefinitely. It is ok if your program needs a slightly higher ulimit, say 20000, but it should not have any memory leaks. The execution below takes just under 1 second per round on the server NOTE: You will have to raise the ulimit to be able to run other programs, like editing or compiling! bdxon@cs-intro :~> ulimit -sv 100000 bdixon@cs-intro:> g+ dListSampleMain.cpp -03 o Project2 bdixon@cs-intro:~> ulimit -Sv 17000 bdixon@cs-intro: ~> ./Project2 400 a 399 b398 c 397 d 396 e 395 f 394 g 393 h 392 i 391 j 392 i 400 a 399 398 c 397 d 396 e 395 f 394 g 393 400 a 399 b 398 c 397 d 396 e 395 f 394 g 393 h 391 j 390k 399 b 373b 347b321 b 295b 269 243 b 217 b 201 r 202 q203 p 204 o 205 n 206 m 207 208 k 209 j 210 i 400 a 399 b 398 c 397 d 396 e 395 f 394 501 z 201 r 202 500 d 400 a 399 b 398 397 d 396 e 395 394 g 393 1 a 2 b h 391 g 393 h 392 i 391 r 202 q 203 p 204 o 205 n 206 m 207 1 208 k 209j h 392 i n 206 round 0 100001 d 100002 c 100003 b 100004 a 100005 z 100006 100008 w100009 v 100010 u 200000 a 199999 b 199998 c199997 d 199996 e 199995 f 199994 g 199993 h 199992 i 199991 j y 100007 :x roundl 100001 d 100002 c 100003 b 100004 a 100005 z 100006 100008 w100009 v 100010 u 200000 a 199999 b 199998 c199997 d 199996 e 199995 f 199994 g 199993 h 199992 i 199991 j y 100007 :x roundl 100001 d 100002 c 100003 b 100004 a 100005 z 100006 100008 w100009 v 100010 u 200000 a 199999 b 199998 c199997 d 199996 e 199995 f 199994 g 199993 h 199992 i 199991 j y 100007 x round 99 100001 d 100002 c 100003 b 100004 a 100005 z 100006 100008 w100009 v 100010 u y 100007 :xExplanation / Answer
Hi, so to format it follow the given syntax :
code written by you : cout<< endl <<traverse->key<<" "<<traverse->type<< endl;
Changes to be made : cout<< flush <<traverse->key<<" "<<traverse->type<<" "<< flush;
Forcing all buffered output to actually be printed is known as "flushing" the stream. A flush can be forced by calling the flush function associated with each output stream, inserting the magic variable flush into the stream, or inserting endl.