TEMPLATE OF LINKED LIST

:
#include <iostream> template <typename T> class double_linked { struct node { T data; node* prev; node* next; node(T t, node* p, node* n) : data(t), prev(p), next(n) {} }; node* head; node* tail; public: double_linked() : head( NULL ), tail ( NULL ) {} template<int N> double_linked( T (&arr) [N]) : head( NULL ), tail ( NULL ) { for( int i(0); i != N; ++i) push_back(arr[i]); } bool empty() const { return ( !head || !tail ); } operator bool() const { return !empty(); } 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; } } }; template <typename T> void double_linked<T>::push_back(T data) { tail = new node(data, tail, NULL); if( tail->prev ) tail->prev->next = tail; if( empty() ) head = tail; } template <typename T> void double_linked<T>::push_front(T data) {

if( tail ) tail->next = NULL. return data.head = new node(data. if( empty() ) tail = head. head). NULL. else head = NULL . } template<typename T> T double_linked<T>::pop_front() { if( empty() ) . delete temp. node* temp(tail). } template<typename T> T double_linked<T>::pop_back() { if( empty() ) throw("double_linked : list empty"). T data( tail->data ). tail = tail->prev . if( head->next ) head->next->prev = head.

push_back(81. 19 } . } .push_front( 100 ). 6.2).pop_back() << " "<<"\n". } int main() { int arr[] = { 4. head = head->next . d1list. return data.0). delete temp. 58. dlist. T data( head->data ). dlist. if( head ) head->prev = NULL. double_linked<double>d1list (array).2. double_linked<int> dlist ( arr ).push_front(12. while (d1list) std::cout<<d1list.push_back( 11 ). d1list. 56. else tail = NULL.pop_back()<<""<<"\n".2.throw("double_linked : list empty"). 8. 32.1. node* temp(head). 23. double array[]={ 41. while( dlist ) std::cout << dlist.4 }.

1 58.2 23.OUTPUT: 11 19 32 8 6 4 100 81.4 56.2 41.2 12 .

int top[100]. void kruskal::read_graph() { cout<<"*************************************************\n" <<"This program implements the kruskal algorithm\n" <<"*************************************************\n". int find_node(int ). }. class kruskal { private: int n. int sets[100][10]. void print_min_span_t(). . int tree[10][10]. public: void read_graph(). //no edges in the graph int graph_edge[100][4]. void sort_edges().Minimum spanning tree using kruskal algorithm: #include<iostream> using namespace std. //no of nodes int noe. void algorithm(). void initialize_span_t().

cin>>w. graph_edge[noe][2]=j. "<<graph_edge[i][2] . graph_edge[noe][1]=i.j<=n.i++) { for(int j=i+1.cout<<"Enter the no.i<=noe.i++) cout<<" < "<<graph_edge[i][1] <<" . cout<<"Enter the weights for the following edges ::\n". graph_edge[noe][3]=w. int w. for(int i=1. cin>>n.i<=n. noe=0. of nodes in the undirected weighted graph ::". } } } // print the graph edges cout<<"\n\nThe edges in the given graph are::\n". for( int i=1. "<<j<<" > ::".j++) { cout<<" < "<<i<<" . if(w!=0) { noe++.

i++) { for(int j=1. } void kruskal::sort_edges() { /**** Sort the edges using bubble sort in increasing order**************/ for(int i=1. } } } // print the graph edges cout<<"\n\nAfter sorting the edges in the given graph are::\n". graph_edge[j][2]=graph_edge[j+1][2].i<=noe-1. graph_edge[j+1][1]=t. graph_edge[j+1][2]=t. . t=graph_edge[j][2]. graph_edge[j+1][3]=t. graph_edge[j][1]=graph_edge[j+1][1].<<" > ::"<<graph_edge[i][3]<<endl.j++) { if(graph_edge[j][3]>graph_edge[j+1][3]) { int t=graph_edge[j][1].j<=noe-i. t=graph_edge[j][3]. graph_edge[j][3]=graph_edge[j+1][3].

" <<graph_edge[i][2]<<" >"<<endl<<endl.for(int i=1. } void kruskal::algorithm() { // ->make a set for each node for(int i=1.i++) cout<< "< "<<graph_edge[i][1] <<" .i<=noe.i<=noe. int p2=find_node(graph_edge[i][2]). tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3].i++) { sets[i][1]=i. top[i]=1.i<=n. for(int i=1. tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3].i++) { int p1=find_node(graph_edge[i][1]). "<<graph_edge[i][2] <<" > ::"<<graph_edge[i][3]<<endl. . } cout<<"\nThe algorithm starts ::\n\n". if(p1!=p2) { cout<<"The edge included in the tree is ::" <<" < "<<graph_edge[i][1]<<" .

} else { cout<<"Inclusion of the edge " <<" < "<<graph_edge[i][1]<<" .j<=top[i]. " <<graph_edge[i][2]<<" > "<<"forms a cycle so it is removed\n\n".i++) { for(int j=1.j++) { if(n==sets[i][j]) return i. } } } int kruskal::find_node(int n) { for(int i=1.j<=top[p2].j++) { top[p1]++. } top[p2]=0. sets[p1][top[p1]]=sets[p2][j]. } } .i<=noe.for(int j=1.

algorithm().sort_edges().read_graph(). obj. return 0. } int main() { kruskal obj. obj. } . obj.return -1.

of nodes in the undirected weighted graph ::4 Enter the weights for the following edges :: < 1 . 3 > ::4 < 1 . 2 > ::3 < 1 . 4 > ::2 < 2 . 4 > ::3 The edges in the given graph are:: < 1 . 4 > ::1 < 1 . 3 > ::5 < 2 . 4 > ::3 < 1 . 4 > ::2 < 1 . 4 > ::1 < 3 .Output: This program implements the kruskal algorithm ************************************************* Enter the no. 4 > ::3 After sorting the edges in the given graph are:: < 2 . 3 > ::4 < 1 . 3 > ::5 < 2 . 4 > ::1 < 3 . 2 > ::3 < 3 . 3 > ::4 . 2 > ::3 < 1 . 4 > ::2 < 2 .

< 2 . 2 > forms a cycle so it is removed The edge included in the tree is :: < 3 . 4 > Inclusion of the edge < 1 . 4 > The edge included in the tree is :: < 1 . 4 > Inclusion of the edge < 1 . 3 > forms a cycle so it is removed Inclusion of the edge < 2 . 3 > forms a cycle so it is removed . 3 > ::5 The algorithm starts :: The edge included in the tree is :: < 2 .

cin>>tempstud.Dealing with binary files: #include<iostream> #include<fstream> using namespace std. cin>>tempstud. char address[40]. cout<<"\n enter name:". ofstream MCA_studfile_out. . cout<<"\n". struct student { int rollno.address. char name[30]. cout<<"\n enter address:". cin>>tempstud.rollno. } int main() { struct student MCA_student_out. }.name. void readstudent(student & tempstud) { cout<<"\n enter the roll no: ".

fail()) cout<<"do you want to continue y/n :".close(). if(!MCA_studfile_out. } .MCA_studfile_out.is_open()) cout<<"file cannot be opened \n".ios::out | ios::binary | ios::trunc). MCA_studfile_out. MCA_studfile_out.open("MCA. if(MCA_studfile_out. cin>>continue1. } while (continue1 != 'n'). return 0. char continue1='y'.dat".write((char*) &MCA_student_out. do { readstudent(MCA_student_out).sizeof (struct student)).

Output: enter the roll no: 1 enter name:lara enter address:srilanka .

int centerpointY. class circle:public shape { int radius. public: virtual void draw() { cout<<"shape is draw \n". } }. class shape { int linestyle. int fillcolor.Test application of RTTI: #include<iostream> using namespace std. int centerpointX. public: void draw() { cout<<"circle is drawn\n". } }. .Dynamiccast.

public: . public: void draw() { cout<<"dot is drawn\n". } }. class square:public shape { int cS. class rectangle:public shape { int cL. int cB. } }. int cY. public: void draw() { cout<<"rectangle is drawn\n".class dot:public shape { int cX.

class ellipse:public shape { int radius. } }. int centerpointY.void draw() { cout<<"square is drawn\n". public: void draw() { cout<<"ellipse is drawn\n". int cANGLE. } }. int centerpointX. public: void draw() { cout<<"polygon is drawn\n". class polygon:public shape { int cSIDE. } .

. *ptrcircle. rectangle box. *ptrrectangle. if(ptrcircle)cout<<"cast is successful\n". ptrshape=&someshape. *ptrshape. else cout<<"test is not successful\n". *ptrdot. ptrshape=&box. ptrrectangle=dynamic_cast<rectangle*>(ptrshape). if(ptrdot)cout<<"cast is successful\n". ptrdot=dynamic_cast<dot*>(ptrshape). if(ptrdot)cout<<"cast is successful\n". else cout<<"test is not successful\n". dot rod. else cout<<"test is not successful\n". ptrcircle=dynamic_cast<circle*>(ptrshape). if(ptrcircle)cout<<"cast is successful\n". int main() { shape someshape. ptrcircle=dynamic_cast<circle*>(ptrshape). ptrdot=dynamic_cast<dot*>(ptrshape). else cout<<"test is not successful\n". ptrshape=&someshape. ptrshape=&rod.}. circle ring. ptrshape=&ring.

else cout<<"test is not successful\n". if(ptrellipse)cout<<"cast is successful\n". *ptrsquare . ptrsquare=dynamic_cast<square*>(ptrshape). . else cout<<"test is not successful\n". ptrellipse=dynamic_cast<ellipse*>(ptrshape). polygon po. ptrellipse=dynamic_cast<ellipse*>(ptrshape). square sq. ptrshape=&el. *ptrpolygon. else cout<<"test is not successful\n". ptrshape=&someshape. *ptrellipse. ptrrectangle=dynamic_cast<rectangle*>(ptrshape). ptrshape=&someshape. if(ptrellipse)cout<<"cast is successful\n". if(ptrrectangle)cout<<"cast is successful\n". ptrsquare=dynamic_cast<square*>(ptrshape). else cout<<"test is not successful\n". else cout<<"test is not successful\n". ptrshape=&someshape. if(ptrsquare)cout<<"cast is successful\n". else cout<<"test is not successful\n".if(ptrrectangle)cout<<"cast is successful\n". if(ptrsquare)cout<<"cast is successful\n". ptrshape=&sq. ellipse el.

ptrpolygon=dynamic_cast<polygon*>(ptrshape). if(ptrpolygon)cout<<"cast is successful\n".ptrshape=&po. } . else cout<<"test is not successful\n". ptrshape=&someshape. if(ptrpolygon)cout<<"cast is successful\n". else cout<<"test is not successful\n". ptrpolygon=dynamic_cast<polygon*>(ptrshape).

Output: cast is successful test is not successful cast is successful test is not successful cast is successful test is not successful cast is successful test is not successful cast is successful test is not successful cast is successful test is not successful .

ofstream inf("stu.Read the content from the file: #include<iostream.h> #include<conio. inf<<name<<endl. getch(). inf<<dept<<endl. cout<<´\n Enter the name:´. int roll. cin>>name. cout<<´\n Enter the dept:´. inf<<roll<<endl. cin>>year. } . inf<<year<<endl. cout<<´\n Enter the roll:´. char year[10].h> #include<fstream. cin>>dept.txt").h> void main() { char name[40]. cin>>roll. cout<<´\n Enter the year:´. char dept[20].

Output: Roll: 101 Name:RAM Year:II Dept:IT .

char dept[20]. cout<<´\n Enter the year:´.h> void main() { char name[40]. cin>>year. out<<roll<<endl. getch(). out<<dept<<endl. char year[10].txt"). ofstream out("stu. cout<<´\n Enter the roll:´.Write into the file: #include<iostream. cin>>roll.h> #include<conio. } . cout<<´\n Enter the dept:´. out<<year<<endl. int roll. out<<name<<endl.h> #include<fstream. cin>>name. cin>>dept. cout<<´\n Enter the name:´.

TXT 101 RAM II IT .Output: STU.

Sign up to vote on this title
UsefulNot useful