Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Write a doubly linked list. Should not have to perform any operations inside

ID: 3705287 • Letter: C

Question

C++

Write a doubly linked list.

Should not have to perform any operations inside of main.

Should be able to call the functions inside of your class to have a doubly linked list made for them of the type node.

All of the information will be stored inside of a class called Node and we will have another class calledDoublyLinkedList, that will perform all of the operations for us. This will seperate the data structure from the data.

Read information from a text file and storing it inside of a Node that is inside of our DoublyLinkedList Class.

Main should contain:

Function to read text file. Calling of DoublyLinkedList Class to append, display, delete & Insert using the following code, Id = 4, name = Becky, type = Rat, description = Broken tail, age = 5, weight= 2.54

Node Class should contain:

Visit ID, Pet Name, Pet Type, Visit Description, Age, Weight

DoublyLinkedList class should be able to perform these operations:

Append a Node - Will take 2 integers, 1 double, and 3 strings.

display forward - should take nothing and be constant.

display backward - should take nothing and be constant.

insert a node based on visitID - This will insert the new information in the correct spot depending on the visitID. (will take 2 integers 1 double and 3 string.)

Delete a node - will take an integer.

The text file is comma separated:

0,Billy,Cat,Had a fever,9,22
1,Beth,Snake,Broke fang,2,10
2,Bart,Dog,Will not eat,4,55.9
3,Brittany,Fish,Floating belly up,1,.005
5,Bobby,Horse,Hurt leg,10,1020.35

Explanation / Answer

#include<iostream>
#include<fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Node
{
public:
    //Visit ID, Pet Name, Pet Type, Visit Description, Age, Weight
    int visit_id;
    char pet_name[20];
    char pet_type[20];
    char visit_desc[20];
    int age;
    double weight;
};
class DoublyLinkedList
{
public:
   int getData(Node obj[],int size)    //to get data from file and store it in class obj
   {
     int data_count=-1;    //initialized data count to -1 so that we can track how much data lines we have read from file and                                          //how much space in obj array we have to proceed more operations
     std::ifstream infile("C:/Users/temp-01968/Desktop/Data.txt");
     char buff[100];
     while(infile>>buff)//get data from file in buff lini by line
     {
      buff[getlen(buff)]='';
      data_count++;
      setLocalStorage(obj,data_count,buff);//send buff data to another function to seperate it by , and store it in specified                                                                         //class mumber variables
      if(data_count>size)//check if number of data lines we have read should less then or exual to size od obj array "size".
      {
       cout<<" 50 Data Records Can Be Readed.. Readed ";
       break;
      }
     }
     infile.close();
     if(data_count==-1)//print message if no data read from file
     {
      cout<<" No Data In File. ";
     }
     return data_count;
   }
   void setLocalStorage(Node obj[],int index,char buff[])//to store data in class member variables
   {
     int i,j=0;
     char temp[20];
     int buff_len=getlen(buff);//get length of buff so that we can to till end of data..no beyond from it
     for(i=0;i<buff_len&&buff[i]!=',';i++)//get first field form buff till first comma(,);
     {
      temp[j]=buff[i];
      j++;
      if(j>=20)//check if field data is greater then 20
      {
       break;
      }
     }
     temp[j]='';//set a string terminator at the end of temp array
     obj[index].visit_id=atoi(temp); //store the data in specified class member variable....I have used a function atoi() which convert string to integer
     j=0;
     for(i=i+1;i<buff_len&&buff[i]!=',';i++)
     {
      temp[j]=buff[i];
      j++;
      if(j>=20)
      {
       break;
      }
     }
     temp[j]='';
     copystr(obj[index].pet_name,temp);
     j=0;
     for(i=i+1;i<buff_len&&buff[i]!=',';i++)
     {
      temp[j]=buff[i];
      j++;
      if(j>=20)
      {
       break;
      }
     }
     temp[j]='';
     copystr(obj[index].pet_type,temp);
     char blank[]="";
     copystr(temp,blank);
     j=0;
     for(i=i+1;i<buff_len&&buff[i]!=',';i++)
     {
      temp[j]=buff[i];
      j++;
      if(j>=20)
      {
       break;
      }
     }
     temp[j]='';
     copystr(obj[index].visit_desc,temp);
     j=0;
     for(i=i+1;i<buff_len&&buff[i]!=',';i++)
     {
      temp[j]=buff[i];
      j++;
      if(j>=20)
      {
       break;
      }
     }
     temp[j]='';
     obj[index].age=atoi(temp);
     j=0;
     for(i=i+1;i<buff_len&&buff[i]!=' ';i++)
     {
      temp[j]=buff[i];
      j++;
      if(j>=20)
      {
       break;
      }
     }
     temp[j]='';
     obj[index].age=atof(temp);
   }
   int getlen(char str[]) const//to get length of string
   {
     int len=-1;
     while(str[++len]!='');
     return len;
   }
   void copystr(char str1[],char str2[])//to copy a string into another string
   {
     int i=0;
     while(str2[i]!='')
     {
      str1[i]=str2[i];
      i++;
     }
     str1[i]='';
   }
   display_forward(Node obj[],int data_count)//to display data from head to tail
      {
   cout<<" Visit ID Pet Name Pet Type Pet Description Age Weight ";
   cout<<" --------------------------------------------------------------------------------- ";
   for(int i=0;i<=data_count;i++)
   {
      cout<<obj[i].visit_id<<" "<<obj[i].pet_name<<" "<<obj[i].pet_type<<" "<<obj[i].visit_desc<<" "<<obj[i].age<<" "<<obj[i].weight<<" ";
   }
   }
   display_backword(Node obj[],int data_count)//to display data from tail to head
      {
   cout<<" Visit ID Pet Name Pet Type Pet Description Age Weight ";
   cout<<" --------------------------------------------------------------------------------- ";
   for(int i=data_count;i>=0;i--)
   {
      cout<<obj[i].visit_id<<" "<<obj[i].pet_name<<" "<<obj[i].pet_type<<" "<<obj[i].visit_desc<<" "<<obj[i].age<<" "<<obj[i].weight<<" ";
   }
   }
   int remove_index(Node obj[],int id,int data_count)// to remove a data line by Visit_ID
   {
    int index=-1;
    for(int i=0;i<=data_count;i++)
    {
      if(obj[i].visit_id==id)
      {
       index=i;
       break;
    }
    }
    if(index!=-1)
    {
      revert(obj,index,data_count);//to adjust the remaining data so that the recently releaased space is adjust
    cout<<" ---------------------------------------------------------";
    cout<<" Data Removed ";
    cout<<" ---------------------------------------------------------";
    data_count--;//decrease the data count by 1 if we remove a data line form obj array
    }
    else
    {
      cout<<" ---------------------------------------------------------";
    cout<<" Data Not Found ";
    cout<<" ---------------------------------------------------------";
   }
    return data_count;
   }

   void revert(Node obj[],int index,int data_count)//for adjust data
   {
    for(int i=index;i<data_count;i++)
    {
       obj[i].visit_id=obj[i+1].visit_id;
       copystr(obj[i].pet_name,obj[i+1].pet_name);
       copystr(obj[i].pet_type,obj[i+1].pet_type);
       copystr(obj[i].visit_desc,obj[i+1].visit_desc);
       obj[i].age=obj[i+1].age;
       obj[i].weight=obj[i+1].weight;
    }
   }
   int insert(Node obj[],int data_count,int id_,char name_[],char type_[],char desc_[],int age_,double weight_)//to insert data                                                                                                                                             //in obj array accordind to Visit_ID
   {
      int index=-1;
    for(int i=0;i<=data_count;i++)
    {
      if(obj[i].visit_id>id_)
      {
       index=i;
       break;
    }
    if(obj[i].visit_id==id_)
    {
     cout<<" ---------------------------------------------------------";
     cout<<" ID Already Present...!!! ";
     cout<<" ---------------------------------------------------------";
     return data_count;
    }
    }
    if(index==-1)
    {
      index=data_count+1;
   }
    forword_one(obj,index,data_count);
    obj[index].visit_id=id_;
    copystr(obj[index].pet_name,name_);
    copystr(obj[index].pet_type,type_);
    copystr(obj[index].visit_desc,desc_);
    obj[index].age=age_;
    obj[index].weight=weight_;
    data_count++;//increase the data count by 1 if we add a data line in obj array
    return data_count;
   }
   void forword_one(Node obj[],int index,int data_count)//for adjust data to extrect a space for new comming data line
   {
    for(int i=data_count;i>=index;i--)
    {
       obj[i+1].visit_id=obj[i].visit_id;
       copystr(obj[i+1].pet_name,obj[i].pet_name);
       copystr(obj[i+1].pet_type,obj[i].pet_type);
       copystr(obj[i+1].visit_desc,obj[i].visit_desc);
       obj[i+1].age=obj[i].age;
       obj[i+1].weight=obj[i].weight;
    }
   }
};
void commit(Node obj[],int size)//for commit data back into file
{
int i=0;
std::ofstream outfile("C:/Users/temp-01968/Desktop/Data.txt");
for(i=0;i<=size;i++)
{
   outfile<< obj[i].visit_id;
   outfile<< ",";
   outfile<<obj[i].pet_name;
   outfile<< ",";
   outfile<< obj[i].pet_type;
   outfile<< ",";
   outfile<< obj[i].age;
outfile<< obj[i].weight;
   if(i!=size)
   {
     outfile<<" ";
   }
}
outfile.close();
}
int main()//main function
{
int data_count=-1;
Node obj[50];
DoublyLinkedList op;
data_count=op.getData(obj,50);
cout<<" "<<data_count+1<<" Data Fetched ";
int ch=-1;
while(ch!=0)
{
   cout<<" 1.Insert New Data ";
   cout<<" 2.Append New Data ";
   cout<<" 3.Dispaly Data (forward) ";
   cout<<" 4.Dispaly Data (backword) ";
   cout<<" 5.Delete node ";
   cout<<" 0.Quit ";
   cout<<" ---------------------------------------------------------";
   cout<<" Enter Your Choice:";
   cin>>ch;
   cout<<" ---------------------------------------------------------";
   switch(ch)
   {
    case 1:
     if(data_count>=50)
     {
      cout<<" ---------------------------------------------------------";
      cout<<" Local Storage Is Full ";
      cout<<" ---------------------------------------------------------";
     }
     else
     {
      cout<<" ---------------------------------------------------------";
      data_count=op.insert(obj,data_count,2,"Becky","Rat","Broken_tail",5,2.54);
      cout<<" ---------------------------------------------------------";
     }
     break;
    case 2:
     if(data_count<0)
     {
      cout<<" ---------------------------------------------------------";
      cout<<" Data is Not Present. ";
      cout<<" ---------------------------------------------------------";
     }
     else
     {
     cout<<" Pending...Please Wait. ";
     }
     break;
    case 3:
    //display_forward
      if(data_count<0)
      {
        cout<<" ---------------------------------------------------------";
        cout<<" Data is Not Present. ";
        cout<<" ---------------------------------------------------------";
      }
      else
      {
      op.display_forward(obj,data_count);
   }
      break;
    case 4:
      if(data_count<0)
      {
        cout<<" ---------------------------------------------------------";
        cout<<" Data is Not Present. ";
        cout<<" ---------------------------------------------------------";
      }
      else
      {
      op.display_backword(obj,data_count);
   }
      break;
    case 5:  
     if(data_count<0)
{
       cout<<" ---------------------------------------------------------";
       cout<<" Data is Not Present. ";
       cout<<" ---------------------------------------------------------";
}
else
{
     int index=-1;
     cout<<" Enter ID to Remove:";
     cin>>index;
    data_count=op.remove_index(obj,index,data_count);
}
     break;
    default:
     if(ch!=0)
     {
      cout<<" ---------------------------------------------------------";
      cout<<" ...!!!...Invalid Choice...!!!... ";
      cout<<" ---------------------------------------------------------";
     }
     else
     {
      commit(obj,data_count);
     }
   }
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote