Template of Linked List

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

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)

{
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() )

throw("double_linked : list 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()

int arr[] = { 4, 6, 8, 32, 19 } ;

double_linked<int> dlist ( arr );

dlist.push_back( 11 );

dlist.push_front( 100 );

while( dlist )

std::cout << dlist.pop_back() << " "<<"\n";

double array[]={ 41.2, 58.2, 56.1, 23.4 };

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>

using namespace std;

class kruskal

private:

int n; //no of nodes

int noe; //no edges in the graph

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"

<<"This program implements the kruskal algorithm\n"

<<"*************************************************\n";
cout<<"Enter the no. of nodes in the undirected weighted graph ::";

cin>>n;

noe=0;

cout<<"Enter the weights for the following edges ::\n";

for(int i=1;i<=n;i++)

for(int j=i+1;j<=n;j++)

cout<<" < "<<i<<" , "<<j<<" > ::";

int w;

cin>>w;

if(w!=0)

noe++;

graph_edge[noe][1]=i;

graph_edge[noe][2]=j;

graph_edge[noe][3]=w;

// print the graph edges

cout<<"\n\nThe edges in the given graph are::\n";

for( int i=1;i<=noe;i++)

cout<<" < "<<graph_edge[i][1]

<<" , "<<graph_edge[i][2]
<<" > ::"<<graph_edge[i][3]<<endl;

void kruskal::sort_edges()

/**** Sort the edges using bubble sort in increasing order**************/

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;

// print the graph edges

cout<<"\n\nAfter sorting the edges in the given graph are::\n";


for(int i=1;i<=noe;i++)

cout<< "< "<<graph_edge[i][1]

<<" , "<<graph_edge[i][2]

<<" > ::"<<graph_edge[i][3]<<endl;

void kruskal::algorithm()

// ->make a set for each node

for(int i=1;i<=n;i++)

sets[i][1]=i;

top[i]=1;

cout<<"\nThe algorithm starts ::\n\n";

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)

cout<<"The edge included in the tree is ::"

<<" < "<<graph_edge[i][1]<<" , "

<<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

cout<<"Inclusion of the edge "

<<" < "<<graph_edge[i][1]<<" , "

<<graph_edge[i][2]<<" > "<<"forms a cycle so it is removed\n\n";

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:

This program implements the kruskal algorithm

*************************************************

Enter the no. of nodes in the undirected weighted graph ::4

Enter the weights for the following edges ::

< 1 , 2 > ::3

< 1 , 3 > ::4

< 1 , 4 > ::2

< 2 , 3 > ::5

< 2 , 4 > ::1

< 3 , 4 > ::3

The edges in the given graph are::

< 1 , 2 > ::3

< 1 , 3 > ::4

< 1 , 4 > ::2

< 2 , 3 > ::5

< 2 , 4 > ::1

< 3 , 4 > ::3

After sorting the edges in the given graph are::

< 2 , 4 > ::1

< 1 , 4 > ::2

< 1 , 2 > ::3

< 3 , 4 > ::3

< 1 , 3 > ::4


< 2 , 3 > ::5

The algorithm starts ::

The edge included in the tree is :: < 2 , 4 >

The edge included in the tree is :: < 1 , 4 >

Inclusion of the edge < 1 , 2 > forms a cycle so it is removed

The edge included in the tree is :: < 3 , 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


Dealing with binary files:
#include<iostream>

#include<fstream>

using namespace std;

struct student

int rollno;

char name[30];

char address[40];

};

void readstudent(student & tempstud)

cout<<"\n enter the roll no: ";

cin>>tempstud.rollno;

cout<<"\n enter name:";

cin>>tempstud.name;

cout<<"\n enter address:";

cin>>tempstud.address;

cout<<"\n";

int main()

struct student MCA_student_out;

ofstream MCA_studfile_out;
MCA_studfile_out.open("MCA.dat",ios::out | ios::binary | ios::trunc);

if(!MCA_studfile_out.is_open())

cout<<"file cannot be opened \n";

char continue1='y';

do

readstudent(MCA_student_out);

MCA_studfile_out.write((char*) &MCA_student_out,sizeof (struct student));

if(MCA_studfile_out.fail())

cout<<"do you want to continue y/n :";

cin>>continue1;

while (continue1 != 'n');

MCA_studfile_out.close();

return 0;

}
Output:

enter the roll no: 1

enter name:lara

enter address:srilanka
Dynamiccast- Test application of RTTI:
#include<iostream>

using namespace std;

class shape

int linestyle;

int fillcolor;

public:

virtual void draw()

cout<<"shape is draw \n";

};

class circle:public shape

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";

};

class rectangle:public shape

int cL;

int cB;

public:

void draw()

cout<<"rectangle is drawn\n";

};

class square:public shape

int cS;

public:
void draw()

cout<<"square is drawn\n";

};

class ellipse:public shape

int radius;

int centerpointX;

int centerpointY;

public:

void draw()

cout<<"ellipse is drawn\n";

};

class polygon:public shape

int cSIDE;

int cANGLE;

public:

void draw()

cout<<"polygon is drawn\n";

}
};

int main()

shape someshape, *ptrshape;

circle ring, *ptrcircle;

ptrshape=&ring;

ptrcircle=dynamic_cast<circle*>(ptrshape);

if(ptrcircle)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

ptrshape=&someshape;

ptrcircle=dynamic_cast<circle*>(ptrshape);

if(ptrcircle)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

dot rod, *ptrdot;

ptrshape=&rod;

ptrdot=dynamic_cast<dot*>(ptrshape);

if(ptrdot)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

ptrshape=&someshape;

ptrdot=dynamic_cast<dot*>(ptrshape);

if(ptrdot)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

rectangle box, *ptrrectangle;

ptrshape=&box;

ptrrectangle=dynamic_cast<rectangle*>(ptrshape);
if(ptrrectangle)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

ptrshape=&someshape;

ptrrectangle=dynamic_cast<rectangle*>(ptrshape);

if(ptrrectangle)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

square sq, *ptrsquare ;

ptrshape=&sq;

ptrsquare=dynamic_cast<square*>(ptrshape);

if(ptrsquare)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

ptrshape=&someshape;

ptrsquare=dynamic_cast<square*>(ptrshape);

if(ptrsquare)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

ellipse el, *ptrellipse;

ptrshape=&el;

ptrellipse=dynamic_cast<ellipse*>(ptrshape);

if(ptrellipse)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

ptrshape=&someshape;

ptrellipse=dynamic_cast<ellipse*>(ptrshape);

if(ptrellipse)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

polygon po, *ptrpolygon;


ptrshape=&po;

ptrpolygon=dynamic_cast<polygon*>(ptrshape);

if(ptrpolygon)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

ptrshape=&someshape;

ptrpolygon=dynamic_cast<polygon*>(ptrshape);

if(ptrpolygon)cout<<"cast is successful\n";

else cout<<"test is not successful\n";

}
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


Read the content from 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 inf("stu.txt");

cout<<”\n Enter the roll:”;

cin>>roll;

inf<<roll<<endl;

cout<<”\n Enter the dept:”;

cin>>dept;

inf<<dept<<endl;

cout<<”\n Enter the name:”;

cin>>name;

inf<<name<<endl;

cout<<”\n Enter the year:”;

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");

cout<<”\n Enter the roll:”;

cin>>roll;

out<<roll<<endl;

cout<<”\n Enter the dept:”;

cin>>dept;

out<<dept<<endl;

cout<<”\n Enter the name:”;

cin>>name;

out<<name<<endl;

cout<<”\n Enter the year:”;

cin>>year;

out<<year<<endl;

getch();

}
Output:

STU.TXT

101

RAM

II

IT

You might also like