Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Write a program that reads a list of students (first names only) from a file. It is possible for the names to be in

C++ Write a program that reads a list of students (first names only) from a file. It is possible for the names to be in unsorted order in the file but they have to be placed in sorted order within the linked list.

The program should use a doubly linked list.

Each node in the doubly linked list should have the students name, a pointer to the next student, and a pointer to the previous student. Here is a sample visual. The head points to the beginning of the list. The tail points to the end of the list.

When inserting consider all the following conditions:

if(!head){ //no other nodes

}else if (strcmp(data, head->name)<0){ //smaller than head

}else if (strcmp(data, tail->name)>0){ //larger than tail

}else{ //somewhere in the middle

}

When deleting a student consider all the following conditions:

student may be at the head, the tail or in the middle

Below, you will find a sample of what the file looks like. Notice the names are in unsorted order but the information placed in the linked list (above visual) is in sorted order. The name of the file should be input.txt.

In the text file, the word delete followed by a name, should delete the node with that specific students name from the doubly linked list. If the name is not found, then nothing is deleted.

(NOTE: The above visual represents only the first three lines from the text file below.)

Jim

jill

John

delete jill

Bob

Jack

delete jim

At the end of the program, traverse through the contents of the linked list in both ascending and descending order, using the doubly linked list, and write the contents into the file output.txt. For example, given the above list, here is the sample display:

Bob

Jack

John

=============

John

Jack

Bob

Here is the code I have but it doesn't delete jim from the list, please help!!!!

//Double linked list for student names #include #include #include

using namespace std;

//create node with double pointers and student name struct node{ string name; node *prev; node *next; }*header=NULL,*tail=NULL; //declare global names header and tail

//function adds name to double linked list void addName(string name) { //code here if(header==NULL) //when empty header { header=new node;//allocate new node header->name=name;//store name to list header->next=header->prev=NULL;//set prev and next to null tail=header;//header is also referred by tail } else if(namename) //when smaller new name { header->prev=new node;//allocate new node before header header->prev->name=name;//store name header->prev->next=header;//connect prev of header to header header=header->prev;//new header is new node(prev of header) header->prev=NULL;//again set prev of header is NULL } else if(name>tail->name) //when greater new name than tail { tail->next=new node;//allocate new node after tail tail->next->name=name;//store name tail->next->prev=tail;//connect next of tail to tail tail=tail->next;//new tail is going to tail now tail->next=NULL;//again set tail next is null } else //some where in middle { node *temp=header;//temp for traversing while(temp->namenext; } //when temp name greater than name loop is stopped node *nn=new node; //allocate new node nn nn->name=name;//store name temp->prev->next=nn;//connect nn is before temp nn->prev=temp->prev; nn->next=temp; temp->prev=nn; } }

string convertLower(string st) { string nst="";//new string for(unsigned int i=0;i=65 && st[i]<=90) //when upper case found nst+=tolower(st[i]);//convert lower and add to new string return nst;//return new string }

//function deletes name for double linked list void deleteName(string name) { //code here node *temp=header;//start from header if(convertLower(header->name)==convertLower(name)) //when found at head { header=header->next;//move header to next header->prev=NULL;//prev is null delete temp; //delete it } else if(convertLower(tail->name)==convertLower(name)) //when found at tail { temp=tail;//refer temp to tail tail=tail->prev;//move tail to previous tail->next=NULL;//connect back null to end delete temp;//delete it } else //other than tail and head, may be at middle { while(temp!=NULL && convertLower(temp->name)!=convertLower(name)) //repeat until it is found or null is reached temp=temp->next;//move to next if(temp!=NULL) //when found { temp->next->prev=temp->prev; temp->prev->next=temp->next; delete temp;//now alone temp is remove } } }

int main() { ifstream infile("input.txt");//open file for reading string line,name=""; infile>>line;//reading first line or word while(!infile.eof()) //repeat until end of file { if(line=="delete") //when command delete is found { infile>>name;//read name to delete deleteName(name);//call delete function } else { addName(line);//add function with line as argument } infile>>line;//reading the next line or word } infile.close();//close the file //print order in ascending ofstream outfile("output.txt"); //open file for writing node *temp=header; while(temp!=NULL)//traverse forward { cout<name<name<next; } cout<<"================"<name<name<prev;//previous moving } outfile.close();//close the file return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago