Template of Linked List
Template of Linked List
Template of Linked List
#include <iostream>
class double_linked
struct node
T data;
node* prev;
node* next;
};
node* head;
node* tail;
public:
template<int N>
push_back(arr[i]);
void push_back(T);
void push_front(T);
T pop_back();
T pop_front();
~double_linked()
while(head)
node* temp(head);
head=head->next;
delete temp;
};
if( tail->prev )
tail->prev->next = tail;
if( empty() )
head = tail;
{
head = new node(data, NULL, head);
if( head->next )
head->next->prev = head;
if( empty() )
tail = head;
template<typename T>
T double_linked<T>::pop_back()
if( empty() )
node* temp(tail);
T data( tail->data );
tail = tail->prev ;
if( tail )
tail->next = NULL;
else
head = NULL ;
delete temp;
return data;
template<typename T>
T double_linked<T>::pop_front()
if( empty() )
throw("double_linked : list empty");
node* temp(head);
T data( head->data );
head = head->next ;
if( head )
head->prev = NULL;
else
tail = NULL;
delete temp;
return data;
int main()
dlist.push_back( 11 );
dlist.push_front( 100 );
while( dlist )
double_linked<double>d1list (array);
d1list.push_back(81.2);
d1list.push_front(12.0);
while (d1list)
std::cout<<d1list.pop_back()<<""<<"\n"; }
OUTPUT:
11
19
32
100
81.2
23.4
56.1
58.2
41.2
12
Minimum spanning tree using kruskal algorithm:
#include<iostream>
class kruskal
private:
int graph_edge[100][4];
int tree[10][10];
int sets[100][10];
int top[100];
public:
void read_graph();
void initialize_span_t();
void sort_edges();
void algorithm();
int find_node(int );
void print_min_span_t();
};
void kruskal::read_graph()
cout<<"*************************************************\n"
<<"*************************************************\n";
cout<<"Enter the no. of nodes in the undirected weighted graph ::";
cin>>n;
noe=0;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
int w;
cin>>w;
if(w!=0)
noe++;
graph_edge[noe][1]=i;
graph_edge[noe][2]=j;
graph_edge[noe][3]=w;
<<" , "<<graph_edge[i][2]
<<" > ::"<<graph_edge[i][3]<<endl;
void kruskal::sort_edges()
for(int i=1;i<=noe-1;i++)
for(int j=1;j<=noe-i;j++)
if(graph_edge[j][3]>graph_edge[j+1][3])
int t=graph_edge[j][1];
graph_edge[j][1]=graph_edge[j+1][1];
graph_edge[j+1][1]=t;
t=graph_edge[j][2];
graph_edge[j][2]=graph_edge[j+1][2];
graph_edge[j+1][2]=t;
t=graph_edge[j][3];
graph_edge[j][3]=graph_edge[j+1][3];
graph_edge[j+1][3]=t;
<<" , "<<graph_edge[i][2]
void kruskal::algorithm()
for(int i=1;i<=n;i++)
sets[i][1]=i;
top[i]=1;
for(int i=1;i<=noe;i++)
int p1=find_node(graph_edge[i][1]);
int p2=find_node(graph_edge[i][2]);
if(p1!=p2)
<<graph_edge[i][2]<<" >"<<endl<<endl;
tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3];
tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3];
for(int j=1;j<=top[p2];j++)
top[p1]++;
sets[p1][top[p1]]=sets[p2][j];
top[p2]=0;
else
int kruskal::find_node(int n)
for(int i=1;i<=noe;i++)
for(int j=1;j<=top[i];j++)
if(n==sets[i][j])
return i;
}
return -1;
int main()
kruskal obj;
obj.read_graph();
obj.sort_edges();
obj.algorithm();
return 0;
}
Output:
*************************************************
#include<fstream>
struct student
int rollno;
char name[30];
char address[40];
};
cin>>tempstud.rollno;
cin>>tempstud.name;
cin>>tempstud.address;
cout<<"\n";
int main()
ofstream MCA_studfile_out;
MCA_studfile_out.open("MCA.dat",ios::out | ios::binary | ios::trunc);
if(!MCA_studfile_out.is_open())
char continue1='y';
do
readstudent(MCA_student_out);
if(MCA_studfile_out.fail())
cin>>continue1;
MCA_studfile_out.close();
return 0;
}
Output:
enter name:lara
enter address:srilanka
Dynamiccast- Test application of RTTI:
#include<iostream>
class shape
int linestyle;
int fillcolor;
public:
};
int radius;
int centerpointX;
int centerpointY;
public:
void draw()
cout<<"circle is drawn\n";
};
class dot:public shape
int cX;
int cY;
public:
void draw()
cout<<"dot is drawn\n";
};
int cL;
int cB;
public:
void draw()
cout<<"rectangle is drawn\n";
};
int cS;
public:
void draw()
cout<<"square is drawn\n";
};
int radius;
int centerpointX;
int centerpointY;
public:
void draw()
cout<<"ellipse is drawn\n";
};
int cSIDE;
int cANGLE;
public:
void draw()
cout<<"polygon is drawn\n";
}
};
int main()
ptrshape=˚
ptrcircle=dynamic_cast<circle*>(ptrshape);
if(ptrcircle)cout<<"cast is successful\n";
ptrshape=&someshape;
ptrcircle=dynamic_cast<circle*>(ptrshape);
if(ptrcircle)cout<<"cast is successful\n";
ptrshape=&rod;
ptrdot=dynamic_cast<dot*>(ptrshape);
if(ptrdot)cout<<"cast is successful\n";
ptrshape=&someshape;
ptrdot=dynamic_cast<dot*>(ptrshape);
if(ptrdot)cout<<"cast is successful\n";
ptrshape=&box;
ptrrectangle=dynamic_cast<rectangle*>(ptrshape);
if(ptrrectangle)cout<<"cast is successful\n";
ptrshape=&someshape;
ptrrectangle=dynamic_cast<rectangle*>(ptrshape);
if(ptrrectangle)cout<<"cast is successful\n";
ptrshape=&sq;
ptrsquare=dynamic_cast<square*>(ptrshape);
if(ptrsquare)cout<<"cast is successful\n";
ptrshape=&someshape;
ptrsquare=dynamic_cast<square*>(ptrshape);
if(ptrsquare)cout<<"cast is successful\n";
ptrshape=⪙
ptrellipse=dynamic_cast<ellipse*>(ptrshape);
if(ptrellipse)cout<<"cast is successful\n";
ptrshape=&someshape;
ptrellipse=dynamic_cast<ellipse*>(ptrshape);
if(ptrellipse)cout<<"cast is successful\n";
ptrpolygon=dynamic_cast<polygon*>(ptrshape);
if(ptrpolygon)cout<<"cast is successful\n";
ptrshape=&someshape;
ptrpolygon=dynamic_cast<polygon*>(ptrshape);
if(ptrpolygon)cout<<"cast is successful\n";
}
Output:
cast is successful
cast is successful
cast is successful
cast is successful
cast is successful
cast is successful
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
char name[40];
char dept[20];
char year[10];
int roll;
ofstream inf("stu.txt");
cin>>roll;
inf<<roll<<endl;
cin>>dept;
inf<<dept<<endl;
cin>>name;
inf<<name<<endl;
cin>>year;
inf<<year<<endl;
getch();
}
Output:
Roll: 101
Name:RAM
Year:II
Dept:IT
Write into the file:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
char name[40];
char dept[20];
char year[10];
int roll;
ofstream out("stu.txt");
cin>>roll;
out<<roll<<endl;
cin>>dept;
out<<dept<<endl;
cin>>name;
out<<name<<endl;
cin>>year;
out<<year<<endl;
getch();
}
Output:
STU.TXT
101
RAM
II
IT