create an input file consisting of negative and positive numbers to test the fun
ID: 3760726 • Letter: C
Question
create an input file consisting of negative and positive numbers to test the functionality of the program. please create a doubly linked list and follow the guidelines mentioned in the uploaded image.
Program: Read in a file of positive integers (>0) from "integer.dat" (perform file validation-e.g. file exists, empty file, etc.) and create a doubly linked list of integers in ascending sorted order -print out any error messages to the screen and to a file "log.txt" if the integer is 0) or delete integers from the list and print the entire list or the even number list in ascending or descending order. The action will be written to the screen and to the log file. Notify the user with a message if the number to add is already in the list or if the number is not in the list if they to delete a number not in the list. Remember to have appropriate welcome and ending messages to the screen and the log file.
Explanation / Answer
struct dnode
{
struct dnode *prev;
int data;
struct dnode *next;
};
void display(struct dnode *,char);
void append(struct dnode **, int );
void deletel(struct dnode **, int );
void insertasc(struct dnode **s, int num);
void inserteven(struct dnode **s,int num);
{
struct dnode *p, *r;
p=*s;
r=(struct dnode*)malloc(sizeof(struct dnode));
r->data=num;
if(*s==NULL || p->data>num)
{
r->next=NULL;
r->prev=NULL;
*s=r;
(*s)->next=p;
}
else
{
while(p->next!=NULL)
{
if(p->next->data>num)
{
r->prev=p->prev;
r->next=p->next;
return;
}
p=p->next;
}
}
}
void inserteven(struct dnode **s, int num)
{
if (num%2==0)
{
struct dnode *p, *r;
p=*s;
r=(struct dnode*)malloc(sizeof(struct dnode));
r->data=num;
if(*s==NULL || p->data>num)
{
r->next=NULL;
r->prev=NULL;
*s=r;
(*s)->next=p;
}
else
{
while(p->next!=NULL)
{
if(p->next->data>num)
{
r->prev=p->prev;
r->next=p->next;
return;
}
p=p->next;
}
}
}
}
void deletel(struct dnode **s, int num)
{
struct dnode *p;
p=*s;
if(*s==NULL)
cout<<"empty list";
else
{
while(p!=NULL)
{
if(p->data==num)
{
if(p==*s)
{
*s=p->next;
(*s)->prev=NULL;
free(p);
return;
}
else
{
if(p->next==NULL)
{
p->prev->next=NULL;
}
else
{
p->next->prev=p->prev;
p->prev->next=p->next;
}
free(p);
return;
}
}
p=p->next;
}
}
}
void display(struct dnode *p, char order)
{
struct dnode *temp;
if (order==’A’)
{
while(p)
{
cout<< p->data;
p=p->next;
}
cout<<endl
}
else
{
while(p)
{
temp=p;
p=p->next;
}
p=temp;
while(p)
{
cout<< p->data;
p=p->prev;
}
cout<<endl;
}
int main () {
string line;
int n;
struct dnode *p=NULL;
struct dnode *e=NULL;
ofstream ofile(“log.txt”);
ifstream myfile ("integer.dat”);
if (myfile.is_open())
{
while ( myfile>>n) )
{
if (n>0)
{
insertasc(&p,n);
inserteven (&e,n); }
else
{ ofile<<”not greater than ZERO”;
Cout<<”not greater than ZERO”;
}
myfile.close();
cout<<” do you want to display ascending order/ descending order”;
cin>> ord;
display(&p,ord);
cout<<”Printing even list in ascending order “;
display(&e,’A’);
}
else
{
cout << "Unable to open file";
ofile<<”unable to open file”;
}
cout<<”do you want to delete any item please enter the item to delete”;
cin>>n;
deletel(&p,n);
display(&p,’A’);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.