Need a project done?

C++ Programming Developer

Search This Blog

Reading link list from File in C++

To Read a link list from file, we've adopted the following procedure:
1. Write each node individually to a file in the function of getData().
2. Read Each node from file one by one and insert it in the link list .


//Reading link list from File in C++

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <Windows.h>
using namespace std;

class linkList{
public:
void getData();
void insert(linkList **head, linkList **tail); // insert at tail
void printAll(linkList *head); // Display all records.
private:
char name[100];
int rollNo;
linkList *next;
};
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::getData(){
cout << "Enter Name: "; cin >> name;
cout << "Enter Roll Number: "; cin >> rollNo;
next = NULL;
ofstream os("nodes of my link list.dat", ios::binary | ios::out | ios::app);
os.write( reinterpret_cast <char*> (this), sizeof(*this) );
os.close();
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::insert(linkList **head, linkList **tail){ // insert at tail
if (*head == NULL && *tail == NULL){
*head = (this);
*tail = (this);
}
else{
(*tail)->next = this;
*tail = this;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::printAll(linkList *head){
for (linkList *temp = head; temp!=NULL; temp=temp->next){
cout << "Name: " << temp->name << endl
<<  "Roll Number: " << temp->rollNo << endl << endl;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
/////////////////////////////////////////////////// Main() //////////////////////////////////////
int main(){
linkList *pointerOfLinkList  = 0, *head = 0, *tail = 0;

ifstream is;
while (true){
cout << "1. Insert Node\n"
<< "2. Print All\n"
<< "0. Press IT to load link list from file.\t";
int opt; cin >> opt;

switch(opt){
case 1:
pointerOfLinkList = new linkList;
pointerOfLinkList->getData();
pointerOfLinkList->insert(&head,&tail);
cout << "Inserted" << endl;
break;
case 2:
pointerOfLinkList->printAll(head);
break;
case 8:
break;
case 9:
break;
case 0:
is.open("nodes of my link list.dat", ios::binary | ios::in);

pointerOfLinkList = new linkList;
is.read(reinterpret_cast<char*>(pointerOfLinkList),sizeof(*pointerOfLinkList));
while (!is.eof()){
pointerOfLinkList->insert(&head,&tail);
pointerOfLinkList = new linkList;
is.read(reinterpret_cast<char*>(pointerOfLinkList),sizeof(*pointerOfLinkList));
}
is.close();
break;
default:
cout << "Wrong Key pressed" << endl;
}
_getch();
system("cls");
}
return 0;
}

Insert at tail Link List


//Program to insert at tail in Link List using Classes.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <Windows.h>
using namespace std;

class linkList{
public:
void getData();
void insert(linkList **head, linkList **tail); // insert at tail
void printAll(linkList *head); // Display all records.
private:
char name[100];
int rollNo;
linkList *next;
};
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::getData(){
cout << "Enter Name: "; cin >> name;
cout << "Enter Roll Number: "; cin >> rollNo;
next = NULL;
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::insert(linkList **head, linkList **tail){ // insert at tail
if (*head == NULL && *tail == NULL){
*head = (this);
*tail = (this);
}
else{
(*tail)->next = this;
*tail = this;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::printAll(linkList *head){
for (linkList *temp = head; temp!=NULL; temp=temp->next){
cout << "Name: " << temp->name << endl
<<  "Roll Number: " << temp->rollNo << endl << endl;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
/////////////////////////////////////////////////// Main() //////////////////////////////////////
int main(){
linkList *pointerOfLinkList  = 0, *head = 0, *tail = 0;


while (true){
cout << "1. Insert Node\n"
<< "2. Print All\n"
<< "0. Exit\t";
int opt; cin >> opt;

switch(opt){
case 1:
pointerOfLinkList = new linkList;
pointerOfLinkList->getData();
pointerOfLinkList->insert(&head,&tail);
cout << "Inserted" << endl;
break;
case 2:
pointerOfLinkList->printAll(head);
break;
case 8:
break;
case 9:
break;
case 0:
return 0;
default:
cout << "Wrong Key pressed" << endl;
}
_getch();
system("cls");
}
return 0;
}

Initialize Static Array in C++


Initialize Static Array in C++ program is given below:

#include <iostream>
using namespace std;

class myClass{
private:
static int array[100]; //Static Array in C++
public:
void display();
};
////////////////////////////////////////////////////////////////
int myClass::array[100] = {0}; //Initialize Static Array in C++

void myClass::display(){
for (int i = 0; i<100; i++){
cout << "array[" << i << "] = " << array[i] << endl;
}
}
//``````````````````````````````````````````````````````````````

int main(){
myClass obj;
obj.display();
return 0;
}
"Don't let anyone ever make you feel like you don't deserve what you want."