0% found this document useful (0 votes)
53 views44 pages

DS Complete File, SEM-3

The document discusses implementing two stacks using a single array. Functions are defined to push and pop from each stack by manipulating the top indexes. The top index of the first stack starts at -1 while the second stack starts at the size of the array. Values can be pushed until either stack is full and popped until empty.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views44 pages

DS Complete File, SEM-3

The document discusses implementing two stacks using a single array. Functions are defined to push and pop from each stack by manipulating the top indexes. The top index of the first stack starts at -1 while the second stack starts at the size of the array. Values can be pushed until either stack is full and popped until empty.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

DATA STRUCTURES PRACTICAL FILE

(CIC-255)

Submitted to Guru Gobind Singh Indraprastha University, Delhi (India)


In partial fulfillment of the requirement for the award of the degree of

Bachelor of Technology in
Information Technology

Under the guidance of

Mr. Akshay Singh

Assistant Professor

Information Technology Department

Submitted by:
Kshitij Kumar
09415003121

MAHARAJA SURAJMAL INSTITUTE OF TECHNOLOGY


C4, JANAKPURI, NEW DELHI – 110058
INDEX

S. Experiment Name Da Teache


No te r’s
Signatu
re
1

10

11

12

13

14

15

16

17

18

19

20
1. WAP to search an element in an array using Linear Search

#include<stdio.h>
void linearsearch(int* arr,int target){

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


{
if(arr[i]==target)
{
printf("Element found at index %d",i);
break;
}
}

int main(){
int arr[]={1,2,3,4,5,6,7,8,9};
linearsearch(arr,9);
return 0;
}

Output
2. WAP to search an element in an array using Binary Search

#include <stdio.h>
void binarySearch(int *arr, int start, int end, int target)
{
if (end >= start) {
int mid = start + (end - start) / 2;

if (arr[mid] == target)
{
printf("Element found at index %d",mid);
return ;
}
if (arr[mid] > target)
return binarySearch(arr, start, mid - 1, target);

return binarySearch(arr, mid + 1, end, target);


}

printf("Element not found in the array");


}

int main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
binarySearch(arr, 0, n - 1, 10);
return 0;
}

Output
3. WAP to check whether a given matrix is sparse matrix or not

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main ( )
{
int n, m;
printf ( "Enter rows and columns of a matrix : " );
scanf ( "%d %d", &n, &m );
int* arr[n];
for ( int i = 0; i < n; i++ )
{
arr[i] = malloc ( m * sizeof ( int ) );
}

int count = 0;
printf ( "Enter elements of matrix\n" );
for ( int i = 0; i < n; i++ )
{
for ( int j = 0; j < m; j++ )
{
printf ( "Enter a[%d][%d] element : ", i + 1, j + 1 );
scanf ( "%d", &arr[i][j] );
if ( arr[i][j] == 0 )
{
count++;
}
}
}

if ( count >= round ( (n*m) / 2.0 ) )


{
printf ( "Given matrix is a sparse matrix\n" );
}
else
{
printf ( "Given matrix is not a sparse matrix\n" );
}

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


{
free(arr[i]);
}

return 0;
}

Output
4. WAP to perform following operations on a Singly Linked List
a) Insert a new node at specified position
b) Deletion of a node from a specified position
c) Reversal of that linked list

#include<stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *get_Input();
void printLL(struct Node *head);
struct Node *get_Ati(struct Node *head, int data, int i);
struct Node *del_NodeAti(struct Node *head, int i);
struct Node *reverseLL(struct Node *head);
int main()
{
struct Node *head = get_Input();
printLL(head);
printf("\n");
int data, position;
printf("Enter data you want to insert : ");
scanf("%d", &data);
printf("Enter position : ");
scanf("%d", &position);
head = get_Ati(head, data, position);
printLL(head);
printf("\n");
int del_Pos;
printf("Enter position you want to delete : ");
scanf("%d", &del_Pos);
head = del_NodeAti(head, del_Pos);
printLL(head);
printf("\n");
printf("Reversed Linked List is\n");
head = reverseLL(head);
printLL(head);
return 0;
}
struct Node *get_Input()
{
struct Node *head = NULL;
struct Node *tail = NULL;
int num;
printf("Enter number of nodes you want to enter : ");
scanf("%d", &num);
int i = 0;
while (i < num)
{
int data;
printf("Enter data of %d nodes : ", i + 1);
scanf("%d", &data);
if (head == NULL)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
head = newNode;
tail = newNode;
}
else
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
}
i++;
}
return head;
}
void printLL(struct Node *head)
{
struct Node *temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
struct Node *get_Ati(struct Node *head, int data, int i)
{
if (head == NULL)
return NULL;
struct Node *temp = head;
if (i == 0)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->next = head;
newNode->data = data;
head = newNode;
return head;
}
for (int position = 0; position < i - 1 && temp != NULL; position++)
temp = temp->next;
if (temp != NULL)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = temp->next;
temp->next = newNode;
}
return head;
}
struct Node *del_NodeAti(struct Node *head, int i)
{
if (head == NULL)
return NULL;
if (i == 0)
{
struct Node *temp = head;
head = head->next;
temp->next = NULL;
free(temp);
return head;
}
struct Node *temp = head;
for (int position = 0; position < i - 1 && temp != NULL; position++)
temp = temp->next;
if (temp != NULL && temp->next != NULL)
{
struct Node *deletingNode = temp->next;
temp->next = temp->next->next;
deletingNode->next = NULL;
free(deletingNode);
}
return head;
}
struct Node *reverseLL(struct Node *head)
{
if (head == NULL || head->next == NULL)
return head;
struct Node *temp = reverseLL(head->next);
head->next->next = head;
head->next = NULL;
head = temp;
return head;
}

Output
5. WAP to perform following operations on a Doubly Linked List
a) Insert a new node at beginning of linked list
b) Deletion of a node from end of the linked list

#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
struct Node* previous;
};
struct Node* takeInput()
{
struct Node* head = NULL;
struct Node* tail = NULL;
struct Node* pre = NULL;

int n;
printf ( "Enter number of nodes you want to enter : " );
scanf ( "%d", &n );
int i = 0;
while ( i < n )
{
int nodeData;
printf ( "Enter data of %d node : ", i+1 );
scanf ( "%d", &nodeData );
struct Node* newNode = ( struct Node* ) malloc( sizeof ( struct Node ) );
newNode -> data = nodeData;
newNode -> next = NULL;
newNode -> previous = NULL;

if( head == NULL )


{
head = newNode;
tail = newNode;
pre = newNode;
}

else
{
tail -> next = newNode;
tail = newNode;
tail -> previous = pre;
pre = newNode;
}
i++;
}
return head;
}

void printDLL ( struct Node* head )


{
if ( head == NULL )
{
printf ( "Doubly Linked List is empty\n" );
return;
}

struct Node* temp = head;


while ( temp != NULL )
{
printf ( "%d\t",temp -> data );
temp = temp -> next;
}
printf ( "\n" );
}
struct Node* insertAtStart ( struct Node* head )
{
int nodeData;
printf ( "Enter node data : " );
scanf ( "%d", &nodeData );
struct Node* newNode = ( struct Node* ) malloc ( sizeof ( struct Node ) );
newNode -> data = nodeData;

if ( head == NULL )
{
newNode -> next = NULL;
newNode -> previous = NULL;
head = newNode;
return head;
}
newNode -> next = head;
newNode -> previous = NULL;
head -> previous = newNode;
head = newNode;
return head;
}
struct Node* deleteAtEnd ( struct Node* head )
{
if ( head == NULL )
{
printf ( "Doubly Linked List is empty\n" );
return NULL;
}

if ( head -> next == NULL )


{
free ( head );
return NULL;
}

struct Node* temp = head;


while ( temp -> next != NULL )
{
temp = temp -> next;
}

struct Node* prevNode = temp -> previous;


prevNode -> next = NULL;
free ( temp );
return head;
}

int main()
{
struct Node* head = takeInput();
printDLL ( head );
printf ( "Inserting a node at beginning\n" );
head = insertAtStart ( head );
printDLL ( head );
printf ( "Deleting node from end\n" );
head = deleteAtEnd ( head );
printDLL ( head );
return 0;
}
Output
6. Implement two stacks using a single array.

#include <stdio.h>

#define SIZE 10

int ar[SIZE];

int top1 = -1;

int top2 = SIZE;

// Functions to push data

void push_stack1(int data)

if (top1 < top2 - 1)

ar[++top1] = data;

else

printf("Stack Full! Cannot Push\n");

void push_stack2(int data)

if (top1 < top2 - 1)

ar[--top2] = data;

else

printf("Stack Full! Cannot Push\n");

// Functions to pop data

void pop_stack1()

if (top1 >= 0)

int popped_value = ar[top1--];


printf("%d is being popped from Stack 1\n", popped_value);

else

printf("Stack Empty! Cannot Pop\n");

void pop_stack2()

if (top2 < SIZE)

int popped_value = ar[top2++];

printf("%d is being popped from Stack 2\n", popped_value);

else

printf("Stack Empty! Cannot Pop\n");

// Functions to Print Stack 1 and Stack 2

void print_stack1()

int i;

for (i = top1; i >= 0; --i)

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

printf("\n");

void print_stack2()

{
int i;

for (i = top2; i < SIZE; ++i)

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

printf("\n");

int main()

int ar[SIZE];

int i;

int num_of_ele;

printf("We can push a total of 10 values\n");

for (i = 1; i <= 6; ++i)

push_stack1(i);

printf("Value Pushed in Stack 1 is %d\n", i);

for (i = 1; i <= 4; ++i)

push_stack2(i);

printf("Value Pushed in Stack 2 is %d\n", i);

// Print Both Stacks

print_stack1();

print_stack2();

printf("Pushing Value in Stack 1 is %d\n", 11);


push_stack1(11);

num_of_ele = top1 + 1;

while (num_of_ele)

pop_stack1();

--num_of_ele;

pop_stack1();

return 0;

Output

7. Create a circular linked list having information about a college and


perform insertion at front and deletion at end.
#include <stdio.h>
#include <stdlib.h>

struct listNode {

int eno;

struct listNode* next;

};

typedef struct listNode node;

node* head = NULL;

void insertionAtFront(int info){

node* n;

n = (node*)malloc(sizeof(node));

if (n){

n->eno = info;

if (!head){

n->next = n;

head = n;

return;

node* temp = head;

do {

temp = temp->next;

} while (temp->next != head);

temp->next = n;

n->next = head;

head = n;

else {

printf("Overflow\n");
}

return;

void deletionAtEnd(){

if (!head){

printf("List Empty\n");

return;

node* temp = head;

if (head->next == head){

free(temp);

head = NULL;

return;

do {

temp = temp->next;

} while (temp->next->next != head);

node* temp2 = temp->next;

temp->next = head;

free(temp2);

return;

void print(){

if (!head){

printf("List Empty\n");

return;

}
node* temp = head;

do {

printf("%d ", temp->eno);

temp = temp->next;

} while (temp != head);

printf("\n");

return;

int main(){

int c,e;

while (1){

printf("Choice (1 - Inserion at Front, 2 - Deletion at End, 3 - Print, 4 - Exit) : ");

scanf("%d",&c);

if (c==4){

printf("Fin!\n");

break;

switch (c){

case 1:

printf("Enter the eno of new student : ");

scanf("%d",&e);

insertionAtFront(e);

printf("Element Inserted!\n");

break;

case 2:

deletionAtEnd();

printf("Element Deleted!\n");
break;

case 3:

print();

break;

return 0;

Output:

8. Create a linear queue using linked list and implement different different
operations such as insert ,delete and display the queue element

#include <stdio.h>
#include <stdlib.h>

struct queue{

int data;

struct queue *next;

};

struct queue *enqueue(struct queue *top, int value){

struct queue *ptr = top;

while (ptr->next != NULL){

ptr = ptr->next;

struct queue *temp = (struct queue *)malloc(sizeof(struct queue));

temp->data = value;

temp->next = NULL;

ptr->next = temp;

return top;

struct queue *dequeue(struct queue *top){

top = top->next;

return top;

void print(struct queue *top){

while (top != NULL) {

printf("%d\n", top->data);

top = top->next;

int main(){
struct queue *top;

top = (struct queue *)malloc(sizeof(struct queue));

top->data = 14;

top->next = NULL;

struct queue *tail;

tail = (struct queue *)malloc(sizeof(struct queue));

tail = top;

top = enqueue(top, 15);

top = enqueue(top, 37);

top = enqueue(top, 42);

print(top);

top = dequeue(top);

printf("Elements after dequeue from top \n");

print(top);

Output:

9. Create a circular linked list having information about a college and


perform insertion at front and deletion at end.

#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *top = NULL;
void linkedListTraversal(struct Node *ptr)
{
while (ptr != NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
int isEmpty(struct Node *top)
{
if (top == NULL)
return 1;
else
return 0;
}
int isFull(struct Node *top)
{
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
if (p == NULL)
return 1;
else
return 0;
}
struct Node *push(struct Node *top, int x)
{
if (isFull(top)){
printf("Stack Overflow\n");
}
else{
struct Node *n = (struct Node *)malloc(sizeof(struct Node));
n->data = x;
n->next = top;
top = n;
return top;
}
}
int pop(struct Node *tp)
{
if (isEmpty(tp)){
printf("Stack Underflow\n");
}
else{
struct Node *n = tp;
top = (tp)->next;
int x = n->data;
free(n);
return x;
}
}
int peek(int pos)
{
struct Node *ptr = top;
for (int i = 0; (i < pos - 1 && ptr != NULL); i++){
ptr = ptr->next;
}
if (ptr != NULL)
return ptr->data;
else
return -1;
}
int main()
{
int choice, a, b;
printf("1 : To push\n");
printf("2 : To peek\n");
printf("3 : To traverse\n");
printf("4 : To pop\n");
printf("5 : To Exit\n");
do
{
printf("Enter the choice (1/2/3/4/5): ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("Enter the element to push : ");
scanf("%d", &a);
top = push(top, a);
break;
}
case 2:
{
printf("Enter the index whose element you want to find : ");
scanf("%d", &b);
printf("Value at position %d is : %d\n", b, peek(b));
break;
}
case 3:
{
linkedListTraversal(top);
break;
}
case 4:
{
pop(top);
break;
}
case 5:
{
printf(" EXIT POINT ");
break;
}
default:
{
printf("Please Enter a Valid Choice(1/2/3/4)");
}
}
} while (choice != 5);
return 0;
}

Output:

10. Create a binary tree and perform tree tranval (preorder,


postorder,inorder) using the concept of recursion.

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
// creating node
struct node *create(int data)
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
p->data = data;
p->left = NULL;
p->right = NULL;
return p;
}
void preorder(struct node *p)
{
if (p != NULL)
{
printf("%d\n", p->data);
preorder(p->left);
preorder(p->right);
}
}
void postorder(struct node *p)
{
if (p != NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d\n", p->data);
}
}
void inorder(struct node *p)
{
if (p != NULL)
{
inorder(p->left);
printf("%d\n", p->data);
inorder(p->right);
}
}
int main()
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
struct node *p1 = create(38);
struct node *p2 = create(67);
struct node *p3 = create(2);
struct node *p4 = create(48);
struct node *p5 = create(59);
p1->left = p2;
p1->right = p3;
p2->left = p4;
p2->right = p5;
printf("Preorder\n");
preorder(p1);
printf("Inorder\n");
inorder(p1);
printf("Postorder\n");
postorder(p1);
return 0;
}

Output:

11. Implement Selection sort , Insertion sort , Bubble sort , Merge sort ,
Quick sort and Heap sort using array as a data structure .

#include <stdio.h>

void swap(int *a, int *b){


int temp = *a;

*a = *b;

*b = temp;

// bubble sort function

void bubbleSort(int array[], int n){

int i, j;

for (i = 0; i < n - 1; i++)

for (j = 0; j < n - i - 1; j++)

if (array[j] > array[j + 1])

swap(&array[j], &array[j + 1]);

// Insertion Sort Function

void insertionSort(int array[], int n){

int i, element, j;

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

element = array[i];

j = i - 1;

while (j >= 0 && array[j] > element) {

array[j + 1] = array[j];

j = j - 1;

array[j + 1] = element;

// Selection Sort
void selectionSort(int array[], int n){

int i, j, min_element;

for (i = 0; i < n - 1; i++){

min_element = i;

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

if (array[j] < array[min_element])

min_element = j;

swap(&array[min_element], &array[i]);

// quick sort

int partition(int arr[], int lowIndex, int highIndex)

int pivotElement = arr[highIndex];

int i = (lowIndex - 1);

for (int j = lowIndex; j <= highIndex - 1; j++) {

if (arr[j] <= pivotElement)

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[highIndex]);

return (i + 1);

// QuickSort Function

void quickSort(int arr[], int lowIndex, int highIndex){


if (lowIndex < highIndex)

int pivot = partition(arr, lowIndex, highIndex);

// Separately sort elements before & after partition

quickSort(arr, lowIndex, pivot - 1);

quickSort(arr, pivot + 1, highIndex);

// MergeSort Function

void merge(int arr[], int l, int m, int r){

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

else {
arr[k] = R[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];

j++;

k++;

void mergeSort(int arr[], int l, int r){

if (l < r) {

int m = l + (r - l) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

// printing an array

void printArray(int array[], int size){

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

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

// program for implementation of Heap Sort

void solveHeap(int arr[], int N, int i){

int largest = i;

int l = 2 * i + 1;

int r = 2 * i + 2;

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

largest = l;

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

largest = r;

if (largest != i) {

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

solveHeap(arr, N, largest);

void heapSort(int arr[], int N){

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

solveHeap(arr, N, i);

for (int i = N - 1; i > 0; i--) {

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

solveHeap(arr, i, 0);

int main() {

printf("BUBBLE SORT\n ");


int array[] = {4,3,2,1};

int size = sizeof(array) / sizeof(array[0]);

bubbleSort(array, size);

printArray(array, size);

printf("\n");

printf("INSERTION SORT\n ");

int array1[] = {29,65,44,10};

int n = sizeof(array) / sizeof(array[0]);

insertionSort(array1, n);

printArray(array1, n);

printf("\n");

printf("SELECTION SORT\n ");

int array2[] = {15, 99, 53, 10, 2, 36};

selectionSort(array2, size);

printArray(array2, size);

printf("\n");

printf("QUICK SORT\n ");

int arr[] = {81, 2, 99, 5};

quickSort(arr, 0, n - 1);

printArray(arr, n);

printf("\n");

printf("MERGE SORT\n ");

int arr1[] = {85, 24, 63, 45, 17, 31, 96, 50};

int arr_size = sizeof(arr1) / sizeof(arr1[0]);

mergeSort(arr1, 0, arr_size - 1);

printArray(arr1, arr_size);

printf("\n");
printf("HEAP SORT\n ");

int arr2[] = {12, 11, 13, 5, 6, 7};

int N = sizeof(arr2) / sizeof(arr2[0]);

heapSort(arr2, N);

printArray(arr2, N);

return 0;

Output:

12. Implement the searching using hashing method

#include <stdio.h>

#include <string.h>

#include <stdlib.h>
#include <stdbool.h>

struct DataItem

int data;

int key;

};

struct DataItem *hashArray[20];

struct DataItem *dummyItem;

struct DataItem *item;

int hashCode(int key)

return key % 20;

struct DataItem *search(int key)

int hashIndex = hashCode(key);

while (hashArray[hashIndex] != NULL)

if (hashArray[hashIndex]->key == key)

return hashArray[hashIndex];

++hashIndex;

hashIndex %= 20;

return NULL;

void insert(int key, int data)

{
struct DataItem *item = (struct DataItem *)malloc(sizeof(struct DataItem));

item->data = data;

item->key = key;

int hashIndex = hashCode(key);

while (hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1)

++hashIndex;

hashIndex %= 20;

hashArray[hashIndex] = item;

void display()

int i = 0;

for (i = 0; i < 20; i++)

if (hashArray[i] != NULL)

printf(" (%d,%d)", hashArray[i]->key, hashArray[i]->data);

printf("\n");

int main()

dummyItem = (struct DataItem *)malloc(sizeof(struct DataItem));

dummyItem->data = -1;

dummyItem->key = -1;

insert(1, 44);
insert(2, 7);

insert(42, 49);

insert(4, 86);

insert(12, 65);

insert(14, 18);

insert(17, 11);

insert(13, 8);

insert(37, 16);

display();

item = search(17);

if (item != NULL)

printf("Element found: %d\n", item->data);

else

printf("Element not found\n");

return 0;

Output:

13. Create a graph and perform DFS and BFS

#include <stdio.h>

int q[20], top = -1, front = -1, rear = -1, a[20][20], vis[20], stack[20];

int del(){
int k;

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

return (0);

else {

k = q[front++];

return (k);

void push(int item) {

if (top == 19)

printf("Stack overflow ");

else

stack[++top] = item;

int pop(){

int k;

if (top == -1)

return (0);

else {

k = stack[top--];

return (k);

void add(int item) {

if (rear == 19)

printf("QUEUE FULL");

else {
if (rear == -1) {

q[++rear] = item;

front++;

else

q[++rear] = item;

void bfs(int s, int n) {

int p, i;

add(s);

vis[s] = 1;

p = del();

if (p != 0)

printf(" %d", p);

while (p != 0) {

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

if ((a[p][i] != 0) && (vis[i] == 0)) {

add(i);

vis[i] = 1;

p = del ();

if (p != 0)

printf(" %d ", p);

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

if (vis[i] == 0)
bfs(i, n);

void dfs(int s, int n) {

int i, k;

push(s);

vis[s] = 1;

k = pop();

if (k != 0)

printf(" %d ", k);

while (k != 0) {

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

if ((a[k][i] != 0) && (vis[i] == 0)) {

push(i);

vis[i] = 1;

k = pop();

if (k != 0)

printf(" %d ", k);

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

if (vis[i] == 0)

dfs(i, n);

int main() {

int n, i, s, ch, j;

char c, dummy;
printf("ENTER THE NUMBER VERTICES ");

scanf("%d", &n);

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

for (j = 1; j <= n; j++) {

printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ", i, j);

scanf("%d", &a[i][j]);

printf("THE ADJACENCY MATRIX IS\n");

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

for (j = 1; j <= n; j++) {

printf(" %d", a[i][j]); }

printf("\n");

do {

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

vis[i] = 0;

printf("\n1.B.F.S");

printf("\n2.D.F.S");

printf("\nENTER YOUR CHOICE ");

scanf("%d", &ch);

printf("ENTER THE SOURCE VERTEX: ");

scanf("%d", &s);

switch (ch) {

case 1:

bfs(s, n);

break;
case 2:

dfs(s, n);

break;

printf("DO U WANT TO CONTINUE(Y/N) ? ");

scanf("%c", &dummy);

scanf("%c", &c);

} while ((c == 'y') || (c == 'Y'));

Output:

You might also like