Need a project done?

C++ Programming Developer

Search This Blog

Linked List Write in file C++


 #include <iostream>  
 #include <fstream>  
 using namespace std;  
   
 ofstream os("linkedList.txt",ios::binary);  
   
 struct link{  
  int data;  
  link* next;  
 };  
   
 class linkedList{  
 public:  
  linkedList();  
  void addItem(int);  
  void print(void);  
 private:  
  link* first;  
 };  
   
 linkedList::linkedList(){  
  first = NULL;  
 }  
   
 void linkedList::addItem(int n){  
  link* temp = new link;  
  temp->data = n;  
  temp->next = first;  
  first = temp;  
  os.write(reinterpret_cast<char*>(temp),sizeof(temp));  
 }  
   
 void linkedList::print(){  
  for (link* p = first; p != NULL; p = p->next){  
  cout << p->data << endl;  
  }  
 }  
   
 int main(){  
  int n;   
   
  linkedList ll;  
   
  do{  
  cout << "Enter number to insert: (-1 to print): "; cin >> n;  
    
  ll.addItem(n);  
  } while (n != -1);  
    
  ll.print();  
  return 0;  
 }  
"Don't let anyone ever make you feel like you don't deserve what you want."