Sei sulla pagina 1di 64

Program 5(A)

// AIMWrite a program to a implement stack using array

// Language C++

#include <bits/stdc++.h>

using namespace std;

class stac

int stk[5];

int top;

public:

stac()

top=-1;

void push(int x)

if(top > 4)

cout <<"stack over flow";

return;

stk[++top]=x;

cout <<"inserted" <<x;

void pop()
{

if(top <0)

cout <<"stack under flow";

return;

cout <<"deleted" <<stk[top--];

void display()

if(top<0)

cout <<" stack empty";

return;

for(int i=top;i>=0;i--)

cout <<stk[i] <<" ";

};

int main()

ios_base::sync_with_stdio(0);

cin.tie(0);

int ch;

stac st;

do

cout <<"\n1.Push"<<endl<<"2.Pop"<<endl <<"3.Display"<<endl<< "4.Exit"<<endl<<"Enter ur choice"<<endl;

cin >> ch;


switch(ch)

case 1: cout <<"Enter the element"<<endl;

cin >> ch;

st.push(ch);

break;

case 2: st.pop(); break;

case 3: st.display();break;

case 4: exit(0);

}while(ch!=4);

return 0;

Output:

! !
Program 5(B)
// AIMWrite a program to a implement stack using Linked List.

// Language C++

#include<iostream>

using namespace std;

// Creating a NODE Structure

struct node

int data;

struct node *next;

};

class stack

struct node *top;

public:

stack() // constructure

top=NULL;

void push(); // to insert an element

void pop(); // to delete an element

void show(); // to show the stack

};
// PUSH Operation

void stack::push()

int value;

struct node *ptr;

cout<<"\nPUSH Operationn";

cout<<"Enter a number to insert: ";

cin>>value;

ptr=new node;

ptr->data=value;

ptr->next=NULL;

if(top!=NULL)

ptr->next=top;

top=ptr;

cout<<"\nNew item is inserted to the stack!!!";

// POP Operation

void stack::pop()

struct node *temp;

if(top==NULL)

cout<<"\nThe stack is empty!!!";

temp=top;

top=top->next;

cout<<"\nPOP Operation........\nPoped value is "<<temp->data;


delete temp;

// Show stack

void stack::show()

struct node *ptr1=top;

cout<<"\nThe stack is\n";

while(ptr1!=NULL)

cout<<ptr1->data<<" ->";

ptr1=ptr1->next;

cout<<"NULL\n";

// Main function

int main()

stack s;

int choice;

while(1)

cout<<"\n\t\tSTACK USING LINKED LIST\n\n";

cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";

cout<<"\nEnter your choice(1-4): ";

cin>>choice;

switch(choice)

{
case 1:

s.push();

break;

case 2:

s.pop();

break;

case 3:

s.show();

break;

case 4:

return 0;

break;

default:

cout<<"\nPlease enter correct choice(1-4)!!";

break;

return 0;

}
Output:
!

Program 6
// AIMWrite a program to convert infix to postfix notation.
// Language C++

#include<bits/stdc++.h>

using namespace std;

int prec(char c)

if(c == '^')

return 3;

else if(c == '*' || c == '/')

return 2;

else if(c == '+' || c == '-')

return 1;

else

return -1;

void infixToPostfix(string s)

std::stack<char> st;

st.push('N');

int l = s.length();

string ns;

for(int i = 0; i < l; i++)

if((s[i] >= 'a' && s[i] <= 'z')||(s[i] >= 'A' && s[i] <= 'Z'))

ns+=s[i];

else if(s[i] == '(')

st.push('(');

else if(s[i] == ')')

{
while(st.top() != 'N' && st.top() != '(')

char c = st.top();

st.pop();

ns += c;

if(st.top() == '(')

char c = st.top();

st.pop();

else{

while(st.top() != 'N' && prec(s[i]) <= prec(st.top()))

char c = st.top();

st.pop();

ns += c;

st.push(s[i]);

while(st.top() != 'N')

char c = st.top();

st.pop();

ns += c;
}

cout << ns << endl;

int main()

string exp = "a+b*(c^d-e)^(f+g*h)-i";

cout<<"The given expression is:\t"<<endl<<exp<<endl;

cout<<"The postfix of the given expression is:"<<endl;

infixToPostfix(exp);

return 0;

Output:

Program 7
// AIMWrite a program to implement doubly linked list.

// Language C
#include <stdio.h>

#include <stdlib.h>

struct node

struct node *prev;

int n;

struct node *next;

}*h,*temp,*temp1,*temp2,*temp4;

void insert1();

void insert2();

void insert3();

void traversebeg();

void traverseend(int);

void sort();

void search();

void update();

void delete();

int count = 0;

void main()

int ch;

h = NULL;

temp = temp1 = NULL;


printf("\n 1 - Insert at beginning");

printf("\n 2 - Insert at end");

printf("\n 3 - Insert at position i");

printf("\n 4 - Delete at i");

printf("\n 5 - Display from beginning");

printf("\n 6 - Display from end");

printf("\n 7 - Search for element");

printf("\n 8 - Sort the list");

printf("\n 9 - Update an element");

printf("\n 10 - Exit");

while (1)

printf("\n Enter choice : ");

scanf("%d", &ch);

switch (ch)

case 1:

insert1();

break;

case 2:

insert2();

break;

case 3:

insert3();

break;

case 4:

delete();
break;

case 5:

traversebeg();

break;

case 6:

temp2 = h;

if (temp2 == NULL)

printf("\n Error : List empty to display ");

else

printf("\n Reverse order of linked list is : ");

traverseend(temp2->n);

break;

case 7:

search();

break;

case 8:

sort();

break;

case 9:

update();

break;

case 10:

exit(0);

default:

printf("\n Wrong choice menu");

}
}

/* TO create an empty node */

void create()

int data;

temp =(struct node *)malloc(1*sizeof(struct node));

temp->prev = NULL;

temp->next = NULL;

printf("\n Enter value to node : ");

scanf("%d", &data);

temp->n = data;

count++;

/* TO insert at beginning */

void insert1()

if (h == NULL)

create();

h = temp;

temp1 = h;

else

create();

temp->next = h;
h->prev = temp;

h = temp;

/* To insert at end */

void insert2()

if (h == NULL)

create();

h = temp;

temp1 = h;

else

create();

temp1->next = temp;

temp->prev = temp1;

temp1 = temp;

/* To insert at any position */

void insert3()

int pos, i = 2;

printf("\n Enter position to be inserted : ");


scanf("%d", &pos);

temp2 = h;

if ((pos < 1) || (pos >= count + 1))

printf("\n Position out of range to insert");

return;

if ((h == NULL) && (pos != 1))

printf("\n Empty list cannot insert other than 1st position");

return;

if ((h == NULL) && (pos == 1))

create();

h = temp;

temp1 = h;

return;

else

while (i < pos)

temp2 = temp2->next;

i++;

create();

temp->prev = temp2;
temp->next = temp2->next;

temp2->next->prev = temp;

temp2->next = temp;

/* To delete an element */

void delete()

int i = 1, pos;

printf("\n Enter position to be deleted : ");

scanf("%d", &pos);

temp2 = h;

if ((pos < 1) || (pos >= count + 1))

printf("\n Error : Position out of range to delete");

return;

if (h == NULL)

printf("\n Error : Empty list no elements to delete");

return;

else

while (i < pos)

{
temp2 = temp2->next;

i++;

if (i == 1)

if (temp2->next == NULL)

printf("Node deleted from list");

free(temp2);

temp2 = h = NULL;

return;

if (temp2->next == NULL)

temp2->prev->next = NULL;

free(temp2);

printf("Node deleted from list");

return;

temp2->next->prev = temp2->prev;

if (i != 1)

temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */

if (i == 1)

h = temp2->next;

printf("\n Node deleted");

free(temp2);

count--;
}

/* Traverse from beginning */

void traversebeg()

temp2 = h;

if (temp2 == NULL)

printf("List empty to display \n");

return;

printf("\n Linked list elements from begining : ");

while (temp2->next != NULL)

printf(" %d ", temp2->n);

temp2 = temp2->next;

printf(" %d ", temp2->n);

/* To traverse from end recursively */

void traverseend(int i)

if (temp2 != NULL)

i = temp2->n;

temp2 = temp2->next;
traverseend(i);

printf(" %d ", i);

/* To search for an element in the list */

void search()

int data, count = 0;

temp2 = h;

if (temp2 == NULL)

printf("\n Error : List empty to search for data");

return;

printf("\n Enter value to search : ");

scanf("%d", &data);

while (temp2 != NULL)

if (temp2->n == data)

printf("\n Data found in %d position",count + 1);

return;

else

temp2 = temp2->next;

count++;

}
printf("\n Error : %d not found in list", data);

/* To update a node value in the list */

void update()

int data, data1;

printf("\n Enter node data to be updated : ");

scanf("%d", &data);

printf("\n Enter new data : ");

scanf("%d", &data1);

temp2 = h;

if (temp2 == NULL)

printf("\n Error : List empty no node to update");

return;

while (temp2 != NULL)

if (temp2->n == data)

temp2->n = data1;

traversebeg();

return;

else

temp2 = temp2->next;
}

printf("\n Error : %d not found in list to update", data);

/* To sort the linked list */

void sort()

int i, j, x;

temp2 = h;

temp4 = h;

if (temp2 == NULL)

printf("\n List empty to sort");

return;

for (temp2 = h; temp2 != NULL; temp2 = temp2->next)

for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)

if (temp2->n > temp4->n)

x = temp2->n;

temp2->n = temp4->n;

temp4->n = x;

}
}

traversebeg();

Output:

!
Program 8(A)
// AIMWrite a program to implement simple queue using array.

// Language C

#include <stdio.h>

#define MAX 50

int queue_array[MAX];

int rear = - 1;

int front = - 1;

main()

int choice;

while (1)

{
printf("1.Insert element to queue \n");

printf("2.Delete element from queue \n");

printf("3.Display all elements of queue \n");

printf("4.Quit \n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch (choice)

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(1);

default:

printf("Wrong choice \n");

} /*End of switch*/

} /*End of while*/

} /*End of main()*/

insert()

int add_item;

if (rear == MAX - 1)

printf("Queue Overflow \n");


else

if (front == - 1)

/*If queue is initially empty */

front = 0;

printf("Inset the element in queue : ");

scanf("%d", &add_item);

rear = rear + 1;

queue_array[rear] = add_item;

} /*End of insert()*/

delete()

if (front == - 1 || front > rear)

printf("Queue Underflow \n");

return ;

else

printf("Element deleted from queue is : %d\n", queue_array[front]);

front = front + 1;

} /*End of delete() */

display()

int i;

if (front == - 1)
printf("Queue is empty \n");

else

printf("Queue is : \n");

for (i = front; i <= rear; i++)

printf("%d ", queue_array[i]);

printf("\n");

Output:

!
Program 8(B)
// AIMWrite a program to implement simple queue using linked list.

// Language C

#include <stdio.h>

#include <stdlib.h>

struct node

int info;

struct node *ptr;

*front,*rear,*temp,*front1;

int frontelement();

void enq(int data);

void deq();

void empty();

void display();
void create();

void queuesize();

int count = 0;

void main()

int no, ch, e;

printf("\n 1 - Enque");

printf("\n 2 - Deque");

printf("\n 3 - Front element");

printf("\n 4 - Empty");

printf("\n 5 - Exit");

printf("\n 6 - Display");

printf("\n 7 - Queue size");

create();

while (1)

printf("\n Enter choice : ");

scanf("%d", &ch);

switch (ch)

case 1:

printf("Enter data : ");

scanf("%d", &no);

enq(no);

break;

case 2:
deq();

break;

case 3:

e = frontelement();

if (e != 0)

printf("Front element : %d", e);

else

printf("\n No front element in Queue as queue is empty");

break;

case 4:

empty();

break;

case 5:

exit(0);

case 6:

display();

break;

case 7:

queuesize();

break;

default:

printf("Wrong choice, Please enter correct choice ");

break;

/* Create an empty queue */

void create()
{

front = rear = NULL;

/* Returns queue size */

void queuesize()

printf("\n Queue size : %d", count);

/* Enqueing the queue */

void enq(int data)

if (rear == NULL)

rear = (struct node *)malloc(1*sizeof(struct node));

rear->ptr = NULL;

rear->info = data;

front = rear;

else

temp=(struct node *)malloc(1*sizeof(struct node));

rear->ptr = temp;

temp->info = data;

temp->ptr = NULL;

rear = temp;

}
count++;

/* Displaying the queue elements */

void display()

front1 = front;

if ((front1 == NULL) && (rear == NULL))

printf("Queue is empty");

return;

while (front1 != rear)

printf("%d ", front1->info);

front1 = front1->ptr;

if (front1 == rear)

printf("%d", front1->info);

/* Dequeing the queue */

void deq()

front1 = front;

if (front1 == NULL)

{
printf("\n Error: Trying to display elements from empty queue");

return;

else

if (front1->ptr != NULL)

front1 = front1->ptr;

printf("\n Dequed value : %d", front->info);

free(front);

front = front1;

else

printf("\n Dequed value : %d", front->info);

free(front);

front = NULL;

rear = NULL;

count--;

/* Returns the front element of queue */

int frontelement()

if ((front != NULL) && (rear != NULL))

return(front->info);

else

return 0;

}
/* Display if queue is empty or not */

void empty()

if ((front == NULL) && (rear == NULL))

printf("\n Queue empty");

else

printf("Queue not empty");

Output:

!
Program 8(C)
// AIMWrite a program to implement priority queue using linked list.

// Language C++

#include<stdio.h>

#include<stdlib.h>

struct node

int priority;

int info;

struct node *link;

}*front=NULL;

void insert(int item, int item_priority);

int del();

void display();

int isEmpty();

main()

int choice,item,item_priority;

while(1)

printf("1.Insert\n");

printf("2.Delete\n");

printf("3.Display\n");

printf("4.Quit\n");
printf("Enter your choice : ");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Input the item to be added in the queue : ");

scanf("%d",&item);

printf("Enter its priority : ");

scanf("%d",&item_priority);

insert(item, item_priority);

break;

case 2:

printf("Deleted item is %d\n",del());

break;

case 3:

display();

break;

case 4:

exit(1);

default :

printf("Wrong choice\n");

void insert(int item,int item_priority)

struct node *tmp,*p;


tmp=(struct node *)malloc(sizeof(struct node));

if(tmp==NULL)

printf("Memory not available\n");

return;

tmp->info=item;

tmp->priority=item_priority;

if( isEmpty() || item_priority < front->priority )

tmp->link=front;

front=tmp;

else

p = front;

while( p->link!=NULL && p->link->priority<=item_priority )

p=p->link;

tmp->link=p->link;

p->link=tmp;

}/*End of insert()*/

int del()

struct node *tmp;

int item;

if( isEmpty() )

printf("Queue Underflow\n");
exit(1);

else

tmp=front;

item=tmp->info;

front=front->link;

free(tmp);

return item;

}/*End of del()*/

int isEmpty()

if( front == NULL )

return 1;

else

return 0;

}/*End of isEmpty()*/

void display()

struct node *ptr;

ptr=front;

if( isEmpty() )

printf("Queue is empty\n");

else

{ printf("Queue is :\n");

printf("Priority Item\n");

while(ptr!=NULL)

{
printf("%5d %5d\n",ptr->priority,ptr->info);

ptr=ptr->link;

Output:

Program 9
// AIMWrite a program to implement (perform insertion, deletion and modification of a node
and // transversal) a binary search tree.

// Language C++
#include <stdio.h>

#include <stdlib.h>

struct btnode

int value;

struct btnode *l;

struct btnode *r;

}*root = NULL, *temp = NULL, *t2, *t1;

void delete1();

void insert();

void delete();

void inorder(struct btnode *t);

void create();

void search(struct btnode *t);

void preorder(struct btnode *t);

void postorder(struct btnode *t);

void search1(struct btnode *t,int data);

int smallest(struct btnode *t);

int largest(struct btnode *t);

int flag = 1;

void main()

int ch;
printf("\nOPERATIONS ---");

printf("\n1 - Insert an element into tree\n");

printf("2 - Delete an element from the tree\n");

printf("3 - Inorder Traversal\n");

printf("4 - Preorder Traversal\n");

printf("5 - Postorder Traversal\n");

printf("6 - Exit\n");

while(1)

printf("\nEnter your choice : ");

scanf("%d", &ch);

switch (ch)

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

inorder(root);

break;

case 4:

preorder(root);

break;

case 5:

postorder(root);

break;
case 6:

exit(0);

default :

printf("Wrong choice, Please enter correct choice ");

break;

/* To insert a node in the tree */

void insert()

create();

if (root == NULL)

root = temp;

else

search(root);

/* To create a node */

void create()

int data;

printf("Enter data of node to be inserted : ");

scanf("%d", &data);

temp = (struct btnode *)malloc(1*sizeof(struct btnode));

temp->value = data;

temp->l = temp->r = NULL;


}

/* Function to search the appropriate position to insert the new node */

void search(struct btnode *t)

if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */

search(t->r);

else if ((temp->value > t->value) && (t->r == NULL))

t->r = temp;

else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */

search(t->l);

else if ((temp->value < t->value) && (t->l == NULL))

t->l = temp;

/* recursive function to perform inorder traversal of tree */

void inorder(struct btnode *t)

if (root == NULL)

printf("No elements in a tree to display");

return;

if (t->l != NULL)

inorder(t->l);

printf("%d -> ", t->value);

if (t->r != NULL)

inorder(t->r);

}
/* To check for the deleted node */

void delete()

int data;

if (root == NULL)

printf("No elements in a tree to delete");

return;

printf("Enter the data to be deleted : ");

scanf("%d", &data);

t1 = root;

t2 = root;

search1(root, data);

/* To find the preorder traversal */

void preorder(struct btnode *t)

if (root == NULL)

printf("No elements in a tree to display");

return;

printf("%d -> ", t->value);

if (t->l != NULL)

preorder(t->l);
if (t->r != NULL)

preorder(t->r);

/* To find the postorder traversal */

void postorder(struct btnode *t)

if (root == NULL)

printf("No elements in a tree to display ");

return;

if (t->l != NULL)

postorder(t->l);

if (t->r != NULL)

postorder(t->r);

printf("%d -> ", t->value);

/* Search for the appropriate position to insert the new node */

void search1(struct btnode *t, int data)

if ((data>t->value))

t1 = t;

search1(t->r, data);

else if ((data < t->value))

{
t1 = t;

search1(t->l, data);

else if ((data==t->value))

delete1(t);

/* To delete a node */

void delete1(struct btnode *t)

int k;

/* To delete leaf node */

if ((t->l == NULL) && (t->r == NULL))

if (t1->l == t)

t1->l = NULL;

else

t1->r = NULL;

t = NULL;

free(t);

return;

}
/* To delete node having one left hand child */

else if ((t->r == NULL))

if (t1 == t)

root = t->l;

t1 = root;

else if (t1->l == t)

t1->l = t->l;

else

t1->r = t->l;

t = NULL;

free(t);

return;

/* To delete node having right hand child */

else if (t->l == NULL)

if (t1 == t)

root = t->r;
t1 = root;

else if (t1->r == t)

t1->r = t->r;

else

t1->l = t->r;

t == NULL;

free(t);

return;

/* To delete node having two child */

else if ((t->l != NULL) && (t->r != NULL))

t2 = root;

if (t->r != NULL)

k = smallest(t->r);

flag = 1;

else

k =largest(t->l);

flag = 2;

search1(root, k);

t->value = k;

}
}

/* To find the smallest element in the right sub tree */

int smallest(struct btnode *t)

t2 = t;

if (t->l != NULL)

t2 = t;

return(smallest(t->l));

else

return (t->value);

/* To find the largest element in the left sub tree */

int largest(struct btnode *t)

if (t->r != NULL)

t2 = t;

return(largest(t->r));

else

return(t->value);

Output:
!

Program 10
// AIMWrite a program to create the mirror image of a binary tree.

// Language C++

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node* left;

struct node* right;


};

struct node* newNode(int data)

struct node* node = (struct node*)

malloc(sizeof(struct node));

node->data = data;

node->left = NULL;

node->right = NULL;

return(node);

void mirror(struct node* node)

if (node==NULL)

return;

else

struct node* temp;

/* do the subtrees */

mirror(node->left);

mirror(node->right);

/* swap the pointers in this node */

temp = node->left;

node->left = node->right;

node->right = temp;

}
void inOrder(struct node* node)

if (node == NULL)

return;

inOrder(node->left);

printf("%d ", node->data);

inOrder(node->right);

int main()

struct node *root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

printf("\n Inorder traversal of the constructed tree is \n");

inOrder(root);

mirror(root);

printf("\n Inorder traversal of the mirror tree is \n");

inOrder(root);

getchar();

return 0;

Output:
!

Program 11
// AIMWrite a program to implement heap sort.

// Language C++

#include <bits/stdc++.h>

using namespace std;

void heapify(int arr[], int n, int i)

int largest = i; // Initialize largest as root


int l = 2*i + 1; // left = 2*i + 1

int r = 2*i + 2; // right = 2*i + 2

if (l < n && arr[l] > arr[largest])

largest = l;

if (r < n && arr[r] > arr[largest])

largest = r;

// If largest is not root

if (largest != i)

swap(arr[i], arr[largest]);

heapify(arr, n, largest);

void heapSort(int arr[], int n)

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);

for (int i=n-1; i>=0; i--)

// Move current root to end

swap(arr[0], arr[i]);

// call max heapify on the reduced heap

heapify(arr, i, 0);

}
/* A utility function to print array of size n */

void printArray(int arr[], int n)

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

cout << arr[i] << " ";

cout << "\n";

// Driver program

int main()

int n;

cout<<"Enter the size of the data:"<<endl;

cin>>n;

int arr[n];

cout<<"Enter the data elements:"<<endl;

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

cin>>arr[i];

heapSort(arr, n);

cout << "Sorted array is \n";

printArray(arr, n);

Output:
!

Program 12(A)
// AIMWrite a program to perform a depth first search in a graph.

// Language C++

#include<bits/stdc++.h>

using namespace std;

int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
int main()

int m;

cout <<"Enter no of vertices"<<endl;

cin >> n;

cout <<"Enter no of edges"<<endl;

cin >> m;

cout <<"\nEDGES \n";

for(k=1;k<=m;k++)

cin >>i>>j;

cost[i][j]=1;

cout <<"Enter initial vertex"<<endl;

cin >>v;

cout <<"ORDER OF VISITED VERTICES";

cout << v <<" ";

visited[v]=1;

k=1;

while(k<n)

for(j=n;j>=1;j--)

if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)

visit[j]=1;

stk[top]=j;

top++;
}

v=stk[--top];

cout<<v << " ";

k++;

visit[v]=0; visited[v]=1;

Output:

Program 12(B)
// AIMWrite a program to perform a breadth first search in a graph.

// Language C++

#include <bits/stdc++.h>

using namespace std;


void breadthFirstSearch(vector< list< int > > adjacencyList, int parent[], int level[])

list<int>::iterator itr;

int i, par, lev;

bool flag = true;

lev = 0;

level[1] = lev;

while (flag) {

flag = false;

for (i = 1; i < adjacencyList.size(); ++i) {

if (level[i] == lev) {

flag = true;

itr = adjacencyList[i].begin();

par = i;

while (itr != adjacencyList[i].end()) {

if (level[*itr] != -1) {

++itr;

continue;

level[*itr] = lev + 1;

parent[*itr] = par;

++itr;

}
++lev;

int main()

int vertices, edges, v1, v2, weight;

cout<<"Enter the Number of Vertices -\n";

scanf("%d", &vertices);

cout<<"Enter the Number of Edges -\n";

scanf("%d", &edges);

vector< list<int> > adjacencyList(vertices + 1);

cout<<"Enter the Edges V1 -> V2"<<endl;

for (int i = 1; i <= edges; ++i) {

scanf("%d%d", &v1, &v2);

adjacencyList[v1].push_back(v2);

adjacencyList[v2].push_back(v1);

cout<<"\nThe Adjacency List-\n";

// Printing Adjacency List

for (int i = 1; i < adjacencyList.size(); ++i) {


cout<<"adjacencyList"<< i;

list<int>::iterator itr = adjacencyList[i].begin();

while (itr != adjacencyList[i].end()) {

cout<<" -> "<<i;

++itr;

cout<<endl;

int parent[vertices + 1];

//Each element of Parent Array holds the Node value of its parent

int level[vertices + 1];

//Each element of Level Array holds the Level value of that node

for (int i = 0; i <= vertices; ++i) {

//Initialising our arrays

parent[i] = 0;

level[i] = -1;

breadthFirstSearch(adjacencyList, parent, level);

//Level Array

cout<<"\nLevel and Parent Arrays -\n";

for (int i = 1; i <= vertices; ++i) {

cout<<"Level of Node "<<i<<" is"<<level[i]<<", Parent is"<<parent[i]<<endl;

}
return 0;

Output:

Potrebbero piacerti anche