Dsa PRGMS
Dsa PRGMS
Dsa PRGMS
3
#include<iostream>
#define Max 6
using namespace std;
class Queue
{
int Q[Max],Front,Rear;
public:
Queue()
{
Front=0;
Rear=-1;
for(int i=0;i<Max;i++)
Q[i]=-1;
}
int is_Queue_Full();
int is_Queue_Empty();
int EnQueue(int);
int DQueue(int&);
void Display();
};
int Queue::is_Queue_Full()
{
if(Rear!=-1 && Front==(Rear+1)%Max)
return 1;
else
return 0;
}
int Queue::is_Queue_Empty()
{
if(Rear==-1)
return 1;
else
return 0;
}
int Queue::EnQueue(int Data)
{
if(is_Queue_Full())
return false;
else
Rear=(Rear+1)%Max;
Q[Rear]=Data;
return true;
}
int Queue::DQueue(int& Data)
{
if(!is_Queue_Empty())
{
Data=Q[Front];
Q[Front]=-1;
Front=(Front+1)%Max;
if(Front==(Rear+1)%Max)
{
Front=0; Rear=-1;
}
return true;
}
return false;
}
void Queue:: Display()
{
for(int i=0;i<Max;i++)
cout<<" "<<Q[i];
}
int main()
{
Queue CQ;
int E,Ch;
char Answer;
do
{ cout<<"\n1:Insert an element\n2:Delete an element ";
cout<<"\nEnter your Choice:";
cin>>Ch;
switch(Ch)
{
case 1:
cout<<"Enter Data";
cin>>E;
if(CQ.EnQueue(E))
cout<<"\n Entered Successfully";
else
cout<<"\nQueue Full Can not insert an element";
break;
case 2:
if(CQ.DQueue(E))
cout<<"\nDeleted Element "<<E;
else
cout<<"Queue is Empty can't Remove element";
break;
}
cout<<"\n";
CQ.Display();
cout<<"\nDo You want to Continue(y/n).: ";
cin>>Answer;
}while(Answer=='y');
cout<<"\nFinal Queue is: ";
CQ.Display();
return 1;
}
ASSIGNMENT NO. 4
#include<iostream>
#include<string.h>
using namespace std;
class tree
{
struct node
{ char data;
node *left,*right;
};
node *root;
public:
tree()
{
root=NULL;
}
void create_post();
void create_pre();
void pre(node *);
void in(node *);
void post(node *);
void preN();
void inN();
void postN();
node *return_root()
{
return root;
}
};
void tree::create_post()
{
node *s[100];
int top=-1;
char post[100],token;
cout<<"\nEnter postfix Expression=>";
cin>>post;
for(int i=0;post[i]!='\0';i++)
{
token=post[i];
node *newnode=new node;
newnode->data=token;
newnode->left=newnode->right=NULL;
if(isalnum(token))
s[++top]=newnode;
else
{
newnode->right=s[top--];
newnode->left=s[top--];
s[++top]=newnode;
}
root=s[top];
}
}
void tree::create_pre()
{
node *s[100];
int top=-1;
char pre[100],token;
cout<<"\nEnter prefix Expression=>";
cin>>pre;
for(int i=strlen(pre)-1;i>=0;i--)
{
token=pre[i];
node *newnode=new node;
newnode->data=token;
newnode->left=newnode->right=NULL;
if(isalnum(token))
s[++top]=newnode;
else
{
newnode->left=s[top--];
newnode->right=s[top--];
s[++top]=newnode;
}
root=s[top];
}
}
}
}
void tree::preN()
{
node *s[100],*temp=root;
int top=-1;
while(temp!=NULL || top>-1)
{
while(temp!=NULL)
{
cout<<"\t"<<temp->data;
s[++top]=temp;
temp=temp->left;
}
temp=s[top--];
temp=temp->right;
}
}
void tree::inN()
{
node *s[100],*temp=root;
int top=-1;
while(temp!=NULL || top>-1)
{
while(temp!=NULL)
{
s[++top]=temp;
temp=temp->left;
}
temp=s[top--];
cout<<"\t"<<temp->data;
temp=temp->right;
}
}
void tree::postN()
{
node *s[100],*temp=root;
char post[100];
int i=0,top=-1;
while(temp!=NULL || top>-1)
{
while(temp!=NULL)
{
post[i++]=temp->data;
s[++top]=temp;
temp=temp->right;
}
temp=s[top--];
temp=temp->left;
}
for(i=strlen(post)-1;i>=0;i--)
cout<<"\t"<<post[i];
}
int main()
{
tree ob;
int ch,choice;
do
{ cout<<"\n1:Create Expression tree using Postfix Expression\n2:Create
Expression tree using Prefix Expression\n3:End the Execution";
cout<<"\nEnter choice from above ";
cin>>ch;
if(ch==1)
ob.create_post();
else if(ch==2)
ob.create_pre();
else
return 0;
cout<<"\n1:Use Recursive Traversals \n2:Use Non Recursive Traversals";
cout<<"\nEnter your choice ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nRecursive Preorder=>";
ob.pre(ob.return_root());
cout<<"\nRecursive Inorder=>";
ob.in(ob.return_root());
cout<<"\nRecursive Postorder=>";
ob.post(ob.return_root());
cout<<endl;
break;
case 2:
cout<<"\nPreorder=>";
ob.preN();
cout<<"\nInorder=>";
ob.inN();
cout<<"\nPostorder=>";
ob.postN();
cout<<endl;
break;
}
}while(ch<3);
}
ASSIGNMENT NO. 9
#include <iostream>
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
largest = l;
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, N, largest);
heapify(arr, N, i);
swap(arr[0], arr[i]);
heapify(arr, i, 0);
int main()
heapSort(arr, N);
printArray(arr, N);
}
ASSIGNMENT NO. 10
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class student
{
int roll,div;
char name[10],address[10];
public:
void getdata()
{
cout<<"\nEnter Rollnumber,Name,Div & Address=\n";
cin>>roll>>name>>div>>address;
}
void putdata()
{
cout<<"\n"<<roll<<"\t"<<name<<"\t"<<div<<"\
t"<<address;
}
int return_Roll(){ return roll;}
};
class seq
{
char fname[15];
public:
void create();
void display();
void Add();
void Remove(int);
void search(int);
};
void seq::create()
{
ofstream fp;//object of class ofstream(write mode)
student s; //class object
char op;
cout<<"\nenter file name=";
cin>>fname;
fp.open(fname);
do
{
s.getdata(); //enter student data
fp.write((char*)&s,sizeof(s));//write student data
in file
cout<<"\nDo u want to add new record(y/n) :";
cin>>op;
}while(op=='y');
cout<<"\nRoll\tName\tDiv\tAddress";
fp.read((char*)&s,sizeof(s)); //read from file
}
fp.close(); //close file
}
void seq::Add()
{
ofstream fp; //write mode
student s; //class object
while(!f1.eof())
{
if(key==s.return_Roll())
{
s.putdata();
flag=1;
}
else
f2.write((char*)&s,sizeof(s));
f1.read((char*)&s,sizeof(s));
}
if(flag==0)
cout<<"\nRecord does not present";
f1.close();
f2.close();
remove(fname);
rename("Temp.Txt",fname);
}
void seq::search(int key)
{
ifstream fp;
student s;
int flag=0;
fp.open(fname); //open a file
fp.read((char*)&s,sizeof(s)); //read
while(!fp.eof())
{
if(key==s.return_Roll()) //compare
{
flag=1;//rollno found
break;
}
fp.read((char*)&s,sizeof(s));//read from file
}//end of while
if(flag==0)
cout<<"Record does not present \n";
else
s.putdata();
fp.close();
}
int main()
{
int ch,R;
seq ob;
do
{
cout<<"\n1: create Database\n2: Display \n3: Add a record\n4:
Delete a record\n5: Search a record";
cout<<"\nEnter your choice: "; cin>>ch;
switch(ch)
{
case 1:
ob.create();
break;
case 2:
ob.display();
break;
case 3:
ob.Add();
ob.display();
break;
case 4:
cout<<"\nEnter Roll No to delete";
cin>>R;
ob.Remove(R);
ob.display();
break;
case 5:
cout<<"\nEnter Roll No to search";
cin>>R;
ob.search(R);
break;
}
}while(ch<6);
}
ASSIGNMENT NO. 1
#include <iostream>
#include<string.h>
using namespace std;
struct student
{
int Rollno;
char Name[10];
float SGPA;
};
void getdata(struct student S[10],int n)
{
for(int i=0;i<n;i++)
{
cout<<"\nEnter Rollno,Name,SGPA of student:"<<i<<"\t";
cin>>S[i].Rollno>>S[i].Name>>S[i].SGPA;
}
}
void putdata(struct student S[10],int n)
{
cout<<"\n Rollno\tName\tSGPA";
for(int i=0;i<n;i++)
cout<<"\n"<<S[i].Rollno<<"\t"<<S[i].Name<<"\t"<<S[i].SGPA;
}
void bubble(struct student a[10],int n)
{
for(int i=0;i<n-1 ;i++)
{
for(int j=0;j<n-i-1;j++)
{ if(a[j].Rollno>a[j+1].Rollno)
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
{
struct student x;
int j;
x=a[i];
for( j=i-1;j>=0;j--)
{
if(strcmp(x.Name,a[j].Name)<0)
a[j+1]=a[j];
else break;
}
a[j+1]=x;
}
{ s[Pos_pivot]=s[right]; s[right]=pivot; }
return right;
}
void quicksort(struct student s[20], int left, int right)
{
int pivot=partition(s,left,right);
}
int linearSearch(struct student values[20], int n,int Key)
{ int flag=0;
cout<<"\nRollno\tName\tSGPA";
for(int i = 0; i < n; i++)
{
if (values[i].SGPA == Key )
{
flag=1;
cout<<"\n"<<values[i]. Rollno<<"\t";
cout<<values[i].Name<<"\t"<<values[i].SGPA;
}
}
return flag;
}
int BinarySearchN(struct student S[20], int N,char value[20])
{
case 2:
cout<<"\n\nList of records alphabetically";
insertion(S,n);
putdata(S,n);
cout<<"\n\n";
break;
case 3:
cout<<"\nList of students according to first ten toppers from a class";
quicksort(S,0,n-1);
putdata(S,n);
cout<<"\n\n";
break;
case 4:
float find;
cout<<"\nEnter SGPA of a student to find record: ";
cin>>find;
int flag;
flag=linearSearch(S,n,find);
if(flag==0) cout<<"\n Record Not present";
cout<<"\n\n";
putdata(S,n);
break;
case 5:
char Name[20];
insertion(S,n);
cout<<"Enter the Name : ";
cin>>Name;
int pos;
pos=BinarySearchN(S,n,Name);
if (pos==-1)
cout<<"\n\nRollno\tName\tSGPA";
for(i = pos; i>=0; i--)
if(strcmp(S[i].Name,Name)!=0)
break;
for(j = pos; j<n; j++)
if(strcmp(S[j].Name,Name)!=0)
break;
for(k = i+1; k<j; k++)
cout<<"\n"<<S[k].Rollno<<"\t"<<S[k].Name<<"\
t"<<S[k].SGPA<<"\n";
break;
case 6:
return 1;
}
}while(ch<7);
}
ASSIGNMENT NO. 2
#include<iostream>
#include<ctype.h>
#include<string.h>
#include<math.h>
using namespace std;
class stack
{
struct node
{
float data;
node *next;
};
node *top;
public:
stack()
{
top=NULL;
}
int empty();
void push(float);
float pop();
int Top();
};
int stack::empty()
{
if(top==NULL)
return 1;
else
return 0;
}
void stack::push(float x)
{ node *newnode;
newnode=new node;
newnode->data=x;
newnode->next=top;
top=newnode;
}
float stack::pop()
{
node *temp=top;
float x;
top=top->next;
x=temp->data;
delete temp;
return x;
}
int stack::Top()
{ return top->data; }
int Priority(char Op)
{
if(Op=='(' || Op==')')
return 0;
if(Op=='+' || Op=='-')
return 1;
else if(Op=='*' || Op=='/')
return 2;
else if(Op=='^')
return 3;
else
return 4;
}
return P;
}
while(!s.empty())
post[j++]=s.pop();
post[j]='\0';
cout<<"\nPostfix Expression=>";
cout<<post;
}
while(!S.empty())
prefix[j++]=S.pop();
prefix[j]='\0';
cout<<"\nPrefix Expression=>";
for(i=strlen(prefix)-1;i>=0;i--)
cout<<prefix[i];
}
int main()
{
int ch;
char infix[20],post[20],prefix[20];
do
{
cout<<"\n\nSELECT THE CHOICE FROM BELOW\n1:Infix to Postfix\n2:Infix to
Prefix\n3:Postfix Evaluation\n4:Prefix Evaluation";
cout<<"\nEnter your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter infix Expression=>";
cin>>infix;
infixtopostfix(infix);
break;
case 2:
cout<<"\nEnter infix Expression=>";
cin>>infix;
infixtoprefix(infix);
break;
case 3:
cout<<"\nEnter Postfix Expression=>";
cin>>post;
cout<<"\nEvaluated Result=> " <<Postfix_Evaluation(post);
break;
case 4:
cout<<"\nEnter Prefix Expression=>";
cin>>prefix;
cout<<"\nEvaluated Result=> "
<<Prefix_Evaluation(prefix);
break;
}
}while(ch!=5);
}
ASSIGNMENT NO. 5
#include<iostream>
using namespace std;
class BST
{
struct node
{
int data;
node *left, *right;
node()
{
left=right=NULL;
}
}*root;
public:
BST()
{ root=NULL; }
node *return_root()
{ return root; }
void insert(int);
void display(node*);
int search(int);
void leveldisp(node*);
void mirroring(node*);
void del(int);
};
else
{
while(1)
{
if(d<temp->data)
{ if(temp->left==NULL)
{ temp->left=Nnode;
cout<<"=>Node Inserted Successfully !\n";
break;
}
else
temp=temp->left;
}
else if(d>temp->data)
{ if(temp->right==NULL)
{ temp->right=Nnode;
cout<<"=>Node Inserted Successfully !\n";
break;
}
else
temp=temp->right;
}
else
{
cout<<"Entry is Repeated...RE-ENTER !\n";
break;
}
}
}
}
int front=0,rear=1,level=0,leaf=0;
if(temp==NULL)
cout<<"Tree is Empty";
else
{
Q[0]=temp, Q[1]=NULL;
cout<<"\n Level "<<level++<<"=>";
while(front<rear)
{
temp=Q[front++];
if(temp==NULL)
{
cout<<"\n Level "<<level++<<"=>";
Q[++rear]=NULL;
}
else
{
cout<<temp->data<<" ";
if(temp->left!=NULL)
Q[++rear]=temp->left;
if(temp->right!=NULL)
Q[++rear]=temp->right;
if(temp->left==NULL&&temp->right==NULL)
leaf++;
}
}
}
cout<<"\n Depth of Tree : "<<level-1;
cout<<"\n Leaf Nodes :"<<leaf;
}
if(temp==NULL)
cout<<"Tree is Empty";
else
{
Q[0]=temp,Q[1]=NULL;
while(front<rear)
{
temp=Q[front++];
if(temp==NULL)
{ cout<<endl;
Q[++rear]=NULL;
}
else
{
cout<<temp->data<<" ";
if(temp->right!=NULL)
Q[++rear]=temp->right;
if(temp->left!=NULL)
Q[++rear]=temp->left;
}
}
}
}
while(temp->data!=num&& temp!=NULL)
{
if(num>temp->data)
{
ptemp=temp;
flag='r';
temp=temp->right;
}
else
{
ptemp=temp;
flag='l';
temp=temp->left;
}
}
if(temp!=NULL)
{
if(temp->left==NULL && temp->right==NULL)
{
if(root==temp)
root==NULL;
if(flag=='r')
ptemp->right=NULL;
else
ptemp->left=NULL;
}
else if(temp->left!=NULL && temp->right!=NULL)
{
prtemp=temp->right;
rtemp=temp->right;
while(rtemp->left!=NULL)
{
prtemp=rtemp;
rtemp=rtemp->left;
}
prtemp->left=rtemp->right;
if(temp==root)
root=rtemp;
else if(flag=='l')
ptemp->left=rtemp;
else if(flag=='r')
ptemp->right=rtemp;
rtemp->left=temp->left;
if(rtemp!=prtemp)
rtemp->right=temp->right;
}
else if(temp->right!=NULL && temp->left==NULL)
{
if(root==temp)
root=root->right;
if(flag=='l')
ptemp->left=temp->right;
else
ptemp->right=temp->right;
}
else if(temp->left!=NULL && temp->right==NULL)
{
if(root==temp)
root=root->left;
if(flag=='l')
ptemp->left=temp->left;
else
ptemp->right=temp->left;
}
}
else
cout<<"\nNode not present to be deleted";
delete temp;
}
int main()
{
BST tree;
int i,val,key,op;
char ans,ANS;
do
{
cout<<"\nEnter the Node value: ";
cin>>val;
tree.insert(val);
cout<<"\nDo you to add more Nodes (Y/N): ";
cin>>ans;
}while(ans=='Y'||ans=='y');
do
{
cout<<"\nSELECT THE OPERATION TO PERFORM";
cout<<"\n1. Insert a new Node";
cout<<"\n2. Display Tree";
cout<<"\n3. Search the Node";
cout<<"\n4. Display level wise Nodes";
cout<<"\n5. Mirror all the Nodes";
cout<<"\n6. Delete a Node";
cout<<"\nEnter your choice : ";
cin>>op;
switch(op)
{
case 1:
cout<<"\n********** Inserting new Node **********\n";
cout<<"\nEnter Value of new Node: ";
cin>>val;
tree.insert(val);
break;
case 2:
cout<<"\n********** Tree Displayed **********\n";
tree.display(tree.return_root());
break;
case 3 :
cout<<"\n********** Searching a node **********\n";
cout<<"\nEnter the node value to search : ";
cin>>key;
i=tree.search(key);
if(i==1)
cout<<"\nThe node with value "<<key<<" is present";
else
cout<<"\nSearched node is absent !";
break;
case 4:
cout<<"\n********* Levelwise Displayed *********\n";
tree.leveldisp(tree.return_root());
break;
case 5:
cout<<"\n********** Mirrored Nodes **********\n";
tree.mirroring(tree.return_root());
break;
case 6:
cout<<"\n********** Deleting a Node **********\n";
cout<<"\nEnter the no. you want to delete : ";
cin>>val;
tree.del(val);
cout<<"\nTree after deletion of node is : \n";
tree.display(tree.return_root());
break;
}
cout<<"\nDo you want to Continue(Y/N) : ";
cin>>ANS;
}while(ANS=='y'||ANS=='Y');
}
ASSIGNMENT NO. 6
#include<iostream>
using namespace std;
class Ttree
{ public:
char Data;
int LFlag,RFlag;
Ttree *Left,*Right;
Ttree(char c='\0')
{ Data=c;
LFlag=RFlag=1;
Left=Right=NULL;
}
};
class Threaded_Tree
{
Ttree *Head;
public:
Threaded_Tree()
{
Head=new Ttree;
Head->Right=Head;
}
void Create(char[]);
void PreTrav();
void InTrav();
};
void Threaded_Tree::InTrav()
{
Ttree *Temp=Head->Left;
do
{
while(Temp->LFlag==0) //follow link to left
Temp=Temp->Left; //move 2 left
cout<<Temp->Data; //print it
Temp=Temp->Right;//follow the thread to right
cout<<Temp->Data; //print it
Temp=Temp->Right;//follow link to right
}
while(Temp!=Head);
}
void Threaded_Tree::PreTrav()
{
Ttree *Temp=Head->Left;
do
{
while(Temp->LFlag==0) //follow link to left
{
cout<<Temp->Data;//print it
Temp=Temp->Left; //move 2 left
}
cout<<Temp->Data;//print
Temp=Temp->Right;//follow the thread 2 right
Temp=Temp->Right;//follow the link 2 right
}
while(Temp!=Head);
}
int main()
{
Threaded_Tree B;
char Estr[25];
cout<<"Enter Postfix Expression: ";
cin>>Estr;
B.Create(Estr);
cout<<" \n----------------------------------\n ";
cout<<" \n Preorder : ";
B.PreTrav();
cout<<" \n----------------------------------\n ";
cout<<" \n Inorder : ";
B.InTrav();
cout<<" \n----------------------------------\n ";
}
ASSIGNMENT NO. 7 A
#include<iostream>
#include<string.h>
using namespace std;
class Graph
{
char Vnames[10][10];
int cost[10][10],n;
public:
Graph();
void creat_graph();
void display();
int Position(char[]);
void prims();
};
Graph::Graph()
{
n=0;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
cost[i][j]=999;
}
}
void Graph::creat_graph()
{
char ans,Start[10],End[10];
int wt,i,j;
cout<<"Enter number of nodes";
cin>>n;
cout<<"\n Enter vertex name:";
for(i=0;i<n;i++)
cin>>Vnames[i];
do
{
cout<<"\nEnter Start and end point of edge";
cin>>Start>>End;
cout<<"Enter weight";
cin>>wt;
i=Position(Start);
j=Position(End);
cost[i][j]=cost[j][i]=wt;
cout<<"\nMore Edges ";
cin>>ans;
}while(ans=='y' || ans=='Y');
}
void Graph::display()
{
int i,j;
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<"\t"<<cost[i][j];
}
}
int Graph::Position(char key[10])
{
for(int i=0;i<n;i++)
if(strcmp(Vnames[i],key)==0)
return i;
return -1;
}
void Graph::prims()
{ int cnt=1,b,i,j,x,y,Total_cost=0,min,visit[10]={0};
char start[10];
cout<<"\n Starting node=";
cin>>start;
x=Position(start);
visit[x]=1;
while(cnt<n)
{
min=999;
for(i=0;i<n;i++)
{
if(visit[i]==1)
{
for(j=0;j<n;j++) // find minimum from adjacent
{ if(cost[i][j]<min && visit[j]==0) //not visited
{ min=cost[i][j];
x=i;
y=j;
}//end of if
}//end of inner for
}//end of if
}//end of outer for
//outer for find minmum from all adjacent that are visited
cout<<"\n"<<Vnames[x]<<"\t"<<Vnames[y]<<"\t"<<min;
Total_cost=Total_cost+min;
cost[x][y]=cost[y][x]=999;
visit[y]=1;
cnt++;
}//end of while
cout<<"\nTotal cost of tree=>"<<Total_cost;
}//end of prims()
int main()
{
Graph G1;
G1.creat_graph();
G1.display();
G1. prims();
}
ASSIGNMENT NO. 7 B
#include<iostream>
#include<string.h>
using namespace std;
class Graph
{
char Vnames[10][10];
int cost[10][10],n;
public:
Graph();
void creat_graph();
void display();
int Position(char[]);
void kru();
};
Graph::Graph()
{
n=0;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
cost[i][j]=999;
}
}
void Graph::creat_graph()
{
char ans,Start[10],End[10];
int wt,i,j;
cout<<"Enter number of nodes";
cin>>n;
cout<<"\n Enter vertex name:";
for(i=0;i<n;i++)
cin>>Vnames[i];
do
{
cout<<"\nEnter Start and end point of edge";
cin>>Start>>End;
cout<<"Enter weight";
cin>>wt;
i=Position(Start);
j=Position(End);
cost[i][j]=cost[j][i]=wt;
cout<<"\nMore Edges ";
cin>>ans;
}while(ans=='y' || ans=='Y');
}
void Graph::display()
{
int i,j;
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<"\t"<<cost[i][j];
}
}
int Graph::Position(char key[10])
{
for(int i=0;i<n;i++)
if(strcmp(Vnames[i],key)==0)
return i;
return -1;
}
void Graph::kru()
{ int i,j,x,y,Total_cost=0,min,gr=1,flag=0,temp, v[10]={0};
while(flag==0)
{ min=999;
for(i=0;i<n;i++) //find the minimum edge
{ for(j=0;j<n;j++)
{ if(cost[i][j]<min)
{ min=cost[i][j];
x=i;
y=j;
}//end of if
}//end inner for
}//end of outer for
int main()
{
Graph ob;
ob.creat_graph();
ob.display();
ob.kru();
}
ASSIGNMENT NO. 8
#include<iostream>
using namespace std;
#include<string.h>
class graph
{
char Vnames[10][10];
int i,j,n,cost[10][10],flag[10];
public:
graph();
int Position(char key[10]);
void creat_graph();
void display();
void Dijkstra();
};
graph::graph()
{
n=0;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(i==j)
cost[i][j]=0;
else
cost[i][j]=999;
}
}
void graph::creat_graph()
{
char ans,Start[10],End[10];
int wt,i,j;
cout<<"Enter number of nodes";
cin>>n;
cout<<"\n Enter vertex name:";
for(i=0;i<n;i++)
{
cin>>Vnames[i];
flag[i]=0;
}
do
{
cout<<"\nEnter Start and end point of edge";
cin>>Start>>End;
cout<<"Enter weight";
cin>>wt;
i=Position(Start);
j=Position(End);
cost[i][j]=cost[j][i]=wt;
//cost[i][j]=wt;
cout<<"\nMore Edges ";
cin>>ans;
}while(ans=='y' || ans=='Y');
}
void graph::display()
{
int i,j;
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<"\t"<<cost[i][j];
}
}
void graph::Dijkstra()
{
int x,dis[10]={0},flag[10]={0};
char visit[10][10]={0};
int i,j,v,sor,min,mnode,k;
char Start[10];
//visit[0]=sor;
strcpy(visit[0],Vnames[sor]);
i=0;
cout<<"\n";
for(x=0;x<=i;x++)
cout<<" "<<visit[x];
for(x=i+1;x<n;x++)
cout<<" -";
cout<<" : ";
for(x=0;x<n;x++)
cout<<" "<<dis[x];
/*main loop*/
for(i=1;i<n;i++)
{ min=999;
for(k=0;k<n;k++)
{
if(flag[k]==0 && dis[k] < min)
{
min=dis[k];
mnode=k;
}
}
flag[mnode]=1;
strcpy(visit[i],Vnames[mnode]);
for(j=0;j<n;j++)
{ if(flag[j]==0 && cost[mnode][j]!=999)
{ if(dis[j]>dis[mnode]+cost[mnode][j])
dis[j]=dis[mnode]+cost[mnode][j];
}
}
cout<<"\n";
for(x=0;x<=i;x++)
cout<<" "<<visit[x];
for(x=i+1;x<n;x++)
cout<<" -";
cout<<" : ";
for(x=0;x<n;x++)
cout<<" "<<dis[x];
//getch();
}
}
int main()
{
graph obj;
obj.creat_graph();
obj.display();
obj.Dijkstra();