Question

in c++ Create a circular, doubly linked list from a file (input.txt), containing a list of...

in c++ Create a circular, doubly linked list from a file (input.txt), containing a list of names. The number of names is unknown. Prompt the user for the number of nodes to delete and then delete accordingly from the list. Assumption: The number inputted by the user will not exceed the actual number of nodes in the list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the solution below:

(Consider below strategy as nothing was mentioned in the question)

I have added the new node at the end of the list.

I have deleted node from the begining of the list

code.cpp

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

//structure of the node
struct node {
string data;//to store data
node *next;//to store next
node *prev;//to store prev
};

//function to delete node form list from the begin
void deleteNode(struct node *&head){
//if list is empty
if(head==NULL){
cout<<"No elements to delete.\n";
}
//if head is the only element in the list
else if(head->next==head){
cout<<head->data<<" deleted.\n";
delete head;//delete head
head = NULL; //set head to NULL
}
//if there is more then one node in the list
else{
//store the head in temp
node *temp;
temp = head;
//last node of the list
node *last;
last = head->prev;
//update last to point to head next
last->next = head->next;
//head next's prev will be last node
head->next->prev = last;

cout<<head->data<<" deleted.\n";
//head would be head->next node
head = last->next;

//delete temp node
delete temp;
}

}


//insertNode at the end.
void insertNode (string name, struct node *&head){
node * newNode;
newNode= new node;//creates new node.
newNode->data=name;//stores value.

//If the list is empty, create a single node
if(head==NULL){
newNode->next = newNode;
newNode->prev = newNode;

head = newNode;
}
// If list is not empty
else{
node *last;
//last node of the list
last = head->prev;

//next of new node is head
newNode->next = head;
//prev of head is newNode
head->prev = newNode;
//prev of new node is last
newNode->prev = last;
//next of last node is newNode
last->next = newNode;
}
}

//display th list in doubly link manner
void display(struct node* head) {
node *temp;
temp = head;

printf("\nThe List is:\n");
//traverse the list from begin
while (temp->next != head)
{
cout<<temp->data<<"<-->";
temp = temp->next;
}
//print last node data and first to make it as a doubly circular
cout<<temp->data<<"<-->"<<head->data<<"\n";
}

int main()
{
//head of the list
struct node *head=NULL;
int counter = 0;
//string to read from file
string name;
//stream for file
ifstream ifile;//to read file

//open file
ifile.open("input.txt"); //open file
   if(!ifile)
   {
       cout<<"Error in opening file..!!";
       exit(0);
   }
   //read till end of file
   while(!ifile.eof())
   {
ifile>>name; //read content form file
insertNode(name,head);//insert node into the list
counter++;
//display list after insertion
display(head);
   }
   //ask user to delete node from list
   int num;
   cout<<"Enter number of node to be deleted(will be deleted from begining of list): ";
cin>>num;

//delete num of node from list
while(num!=0){
deleteNode(head);
display(head);
num--;
}

return 0;
}

input.txt:

John
Ricky
Steve
Neil
Smith

Output:

Just to show it as a circular list, I have dispaly the head of the element again at the end

Add a comment
Know the answer?
Add Answer to:
in c++ Create a circular, doubly linked list from a file (input.txt), containing a list of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT