0% found this document useful (0 votes)
1 views38 pages

Ds Fair - Final (6) 4

The document is a laboratory record for Joyal John C S at Kuttukaran Polytechnic College for the year 2023-2024, detailing various experiments conducted in the Data Structures laboratory. It includes a list of experiments such as Stack Operations, Infix to Postfix Conversion, Queue Operations, and Singly Linked List, along with C programs and outputs for each experiment. Each experiment aims to implement specific data structure operations using C programming.

Uploaded by

joyaljohn1552004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODG, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views38 pages

Ds Fair - Final (6) 4

The document is a laboratory record for Joyal John C S at Kuttukaran Polytechnic College for the year 2023-2024, detailing various experiments conducted in the Data Structures laboratory. It includes a list of experiments such as Stack Operations, Infix to Postfix Conversion, Queue Operations, and Singly Linked List, along with C programs and outputs for each experiment. Each experiment aims to implement specific data structure operations using C programming.

Uploaded by

joyaljohn1552004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODG, PDF, TXT or read online on Scribd
You are on page 1/ 38

Kuttukaran Polytechnic College

MANAKKAPADY, NORTH PARAVOOR,ERNAKULAM-683511

LABORATORY RECORD

YEAR 2023—2024

NAME:…JOYAL…JOHN…C…S……………………………….

BRANCH:..COMPUTER…ENGINEERING……………………………….…….

SEMESTER:…………S4……………………..………………………….…………

Certified that this is a Bonafide Record of Practical work done in


the…DATA…STRUCTURES.......Laboratory of Kuttukaran Polytechnic
College ,Ernakulum by Mr. /Miss…JOYAL…JOHN…C…S………………

Head of the Department Staff-in-Charge


Date…………………..

Internal Examiner External Examiner


INDEX
SL. PAGE
NO DATE NAME OF EXPERIMENTS NO. REMARK
1 11\12\23 1
STACK OPERATION
2 18\12\23 4
INFIX TO POSTFIX CONVERSION
3 8\1\23 6
QUEUE OPERATIONS
.
4 15\1\24 10
SINGLY LINKED LIST

5 15\1\24 14
STACK USING SINGLY LINKED LIST

6 22\1\24 18
QUEUE USING LINKED LIST

7 5\2\24 22
BINARY SEARCH TREE INORDER
TRAVERSAL

8 12\2\24 24
BINARY SEARCH TREE PREORDER
TRAVERSAL

9 12\2\24 26
BINARY SEARCH TREE POSTORDER
TRAVERSAL

10 11\3\24 28
DEPTH FIRST SEARCH TRAVERSAL

11 18\3\24 30
BREADTH FIRST SEARCH TRAVERSAL

12 25\3\24 32
OPEN ENDED EXPERIMENT
EXPERIMENT NO: 1
STACK OPERATIONS

AIM

Write a C program to implement stack operations.

PROGRAM

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
void main()
{
top = -1;
printf("Enter the size of stack: ");
scanf("%d", &n);
printf("\n \t Stack Operations Using Array");
printf("\n\t 1: PUSH \n\t 2: POP \n\t 3: DISPLAY \n\t 4: EXIT \n");
do{
printf("\n Enter the choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
break;
}
default:
{
printf("\n\t ***ENTER A VALID CHOICE***");
}
}

1
}
while(choice!=4);
}
void push()
{
if(top>=n-1){
printf("\n STACK IS FULL");
}else{
printf("\n Enter a value to be pushed: ");
scanf("%d",&x);
top++;
stack[top] = x;
}
}
void pop()
{
if(top==-1){
printf("\n STACK IS EMPTY");
}else{
printf("THE POPPED ELEMENT IS %d \n", stack[top]);
top--;
}
}
void display()
{
if(top>=0){
printf("THE ELEMENT IN STACK IS \n");
for(i=top;i>=0;i--)
printf("%d \n", stack[i]);
}else{
printf("\n\t STACK IS EMPTY \n");
}
}

OUPUT

Enter the size of stack: 20

Stack Operations Using Array


1: PUSH
2: POP
3: DISPLAY
4: EXIT

Enter the choice: 1

Enter a value to be pushed: 10

Enter the choice: 1

Enter a value to be pushed: 20

2
Enter the choice: 1

Enter a value to be pushed: 30

Enter the choice: 3


THE ELEMENT IN STACK IS
30
20
10

Enter the choice: 2


THE POPPED ELEMENT IS 30

Enter the choice: 2


THE POPPED ELEMENT IS 20

Enter the choice: 2


THE POPPED ELEMENT IS 10

Enter the choice: 2

STACK IS EMPTY
Enter the choice: 3

STACK IS EMPTY

Enter the choice: 4

3
EXPERIMENT NO: 2
INFIX TO POSTFIX CONVERSION

AIM

Write a C program to covert infix expression to postfix expression.

PROGRAM

#include<stdio.h>
#include<ctype.h>
char stack[100];
int top=-1;
void push(char x){
stack[++top]=x;
}
char pop(){
if(top==-1)
return -1;
else
return stack[top--];
}
int priority (char x){
if(x=='(')
return 0;
if(x=='+' || x=='-')
return 1;
if(x=='*' || x=='/')
return 2;
return 0;
}
void main()
{
char exp[100];
char *e,x;
printf("ENTER THE EXPRESSION : ");
scanf("%s",exp);
e=exp;
printf("POSTFIX EXPRESSION : ");
while(*e!='\0'){
if(isalnum(*e))
printf("%c",*e);
else if(*e=='(')
push(*e);
else if(*e==')'){
while((x=pop())!='(')
printf("%c",x);
}else{
while(priority(stack[top])>=priority(*e))
printf("%c",pop());

4
push(*e);
}
e++;
}
while(top!=-1){
printf("%c",pop());
}
}

OUTPUT

ENTER THE EXPRESSION : (a+b)*(c-d)


POSTFIX EXPRESSION : ab+cd-*

5
EXPERIMENT NO: 3
QUEUE OPERATIONS

AIM

Write a C program to implement queue operations.

PROGRAM

#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
void enqueue();
void dequeue();
void show();
int inp_arr[SIZE];
int rear=-1;
int front=-1;
void main()
{
int ch;
while(1){
printf("\n1 : ENQUEUE OPERATION \n");
printf("2 : DEQUEUE OPERATION \n");
printf("3 : DISPLAY THE QUEUE \n");
printf("4 : EXIT \n");
printf("ENTER YOUR CHOICE OF OPERATIONS: ");
scanf("%d",&ch);
switch(ch){
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
show();
break;
case 4:
exit(0);
break;
default:
printf("\n INCORRECT CHOICE \n");
}
}
}
void enqueue()
{
int insert_item;
if(rear==SIZE-1)

6
printf("OVERFLOW \n");
else{
if(front==-1)
front=0;
printf("ELEMENT TO BE INSERTED IN THE QUEUE: \n");
scanf("%d",&insert_item);
rear=rear+1;
inp_arr[rear]=insert_item;
}
}
void dequeue()
{
if(front==-1 || front>rear){
printf("UNDERFLOW \n");
return;
}else{
printf("ELEMENT DELETED FROM THE QUEUE: %d \n", inp_arr[front]);
front=front+1;
}
}
void show()
{
if(front==-1)
printf("EMPTY QUEUE \n");
else{
printf("QUEUE: \n");
for(int i=front; i<=rear; i++)
printf("%d ", inp_arr[i]);
printf("\n");
}
}

OUTPUT
1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION
3 : DISPLAY THE QUEUE
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 1
ELEMENT TO BE INSERTED IN THE QUEUE:
10

1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION
3 : DISPLAY THE QUEUE
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 1
ELEMENT TO BE INSERTED IN THE QUEUE:
20

1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION

7
3 : DISPLAY THE QUEUE
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 1
ELEMENT TO BE INSERTED IN THE QUEUE:
30

1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION
3 : DISPLAY THE QUEUE
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 3
QUEUE:
10 20 30

1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION
3 : DISPLAY THE QUEUE
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 2
ELEMENT DELETED FROM THE QUEUE: 10

1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION
3 : DISPLAY THE QUEUE
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 2
ELEMENT DELETED FROM THE QUEUE: 20

1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION
3 : DISPLAY THE QUEUE
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 2
ELEMENT DELETED FROM THE QUEUE: 30

1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION
3 : DISPLAY THE QUEUE
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 2
UNDERFLOW

1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION
3 : DISPLAY THE QUEUE
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 3
QUEUE:

1 : ENQUEUE OPERATION
2 : DEQUEUE OPERATION
3 : DISPLAY THE QUEUE

8
4 : EXIT
ENTER YOUR CHOICE OF OPERATIONS: 4

9
EXPERIMENT NO: 4
SINGLY LINKED LIST

AIM

Write a C program to implement singly linked list.

PROGRAM

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

void randominsert();
void create(int);
void deleteNode();
void display();

struct node {
int data;
struct node *next;
};

struct node *head = NULL;

int main() {
int choice;
printf("\n\n***** Main Menu *****\n");
printf("\n Choose one option from the following list\n");
printf("================================\n");
printf("\n1: Insert\n2: Display\n3: Delete\n4: Exit\n");

do {
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
randominsert();
break;
case 2:
display();
break;
case 3:
deleteNode();
break;
case 4:
exit(0);
default:
printf("\n Invalid choice\n");
}
} while (choice != 4);

10
return 0;
}

void display() {
struct node *ptr = head;
if (ptr == NULL) {
printf("Nothing to print\n");
} else {
printf("\n Printing values...\n");
while (ptr != NULL) {
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
}

void create(int item) {


struct node *ptr = (struct node*)malloc(sizeof(struct node));
if (ptr == NULL) {
printf("\nOverflow\n");
} else {
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
}

void randominsert() {
struct node *ptr = (struct node*)malloc(sizeof(struct node));
struct node *temp;
int i, loc, item;
printf("Enter the item you want to insert: ");
scanf("%d", &item);
if (ptr == NULL) {
printf("\nOverflow\n");
} else {
printf("Enter the location: ");
scanf("%d", &loc);
if (loc <= 1) {
create(item);
} else {
temp = head;
for (i = 1; i < loc - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("\nCan't insert\n");
} else {
ptr->data = item;
ptr->next = temp->next;

11
temp->next = ptr;
printf("\nNode inserted\n");
}
}
}
}

void deleteNode() {
if (head == NULL) {
printf("\nList is empty\n");
return;
}
int loc, i;
struct node *ptr = head;
struct node *prev = NULL;
printf("\nEnter the location of the node you want to delete: ");
scanf("%d", &loc);
if (loc == 1) {
head = ptr->next;
free(ptr);
printf("\nDeleted node at location %d\n", loc);
return;
}
for (i = 1; i < loc && ptr != NULL; i++) {
prev = ptr;
ptr = ptr->next;
}
if (ptr == NULL) {
printf("\nCan't delete\n");
} else {
prev->next = ptr->next;
free(ptr);
printf("\nDeleted node at location %d\n", loc);
}
}

OUTPUT

***** Main Menu *****


Choose one option from the following list
================================

1: Insert
2: Display
3: Delete
4: Exit

Enter your choice: 1


Enter the item you want to insert: 10
Enter the location: 1

Node inserted

12
Enter your choice: 1
Enter the item you want to insert: 20
Enter the location: 2

Node inserted

Enter your choice: 1


Enter the item you want to insert: 30
Enter the location: 3

Node inserted

Enter your choice: 2

Printing values...
10
20
30

Enter your choice: 3

Enter the location of the node you want to delete: 2

Deleted node at location 2

Enter your choice: 2

Printing values...
10
30

Enter your choice: 3

Enter the location of the node you want to delete: 1

Deleted node at location 1

Enter your choice: 2

Printing values...
30

Enter your choice: 3

Enter the location of the node you want to delete: 1

Deleted node at location 1

Enter your choice: 3

List is empty
Enter your choice: 4

13
EXPERIMENT NO: 5
STACK USING SINGLY LINKED LIST

AIM

Write a C program to implement stack using singly linked list.

PROGRAM

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}
*top=NULL;
void push(int);
void pop();
void display();
void main(){
int choice,value;
printf("\n STACK USING LINKED LIST \n");
while(1){
printf("\n****** MENU ******\n");
printf(" 1: PUSH \n 2: POP \n 3: DISPLAY \n 4: EXIT \n");
printf("ENTER YOUR CHOICE: ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("ENTER THE VALUE TO BE INSERTED: ");
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n WRONG SELECTION !!! PLEASE TRY AGAIN \n");
}
}
}
void push(int value){
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;

14
if(top==NULL)
newnode->next=NULL;
else
newnode->next=top;
top=newnode;
printf("\n INSERTION IS SUCCESS !!! \n");
}
void pop(){
if(top==NULL)
printf("\n STACK IS EMPTY \n");
else{
struct node *temp=top;
printf("\n DELETED ELEMENT: %d", temp->data);
top=temp->next;
free(temp);
}
}
void display(){
if(top==NULL)
printf("\n STACK IS EMPTY \n");
else{
struct node *temp=top;
while(temp->next != NULL){
printf("%d --> ",temp->data);
temp=temp->next;
}
printf("%d --> NULL",temp->data);
}
}

OUTPUT

STACK USING LINKED LIST

****** MENU ******


1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO BE INSERTED: 10

INSERTION IS SUCCESS !!!

****** MENU ******


1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO BE INSERTED: 20

15
INSERTION IS SUCCESS !!!

****** MENU ******


1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 1
ENTER THE VALUE TO BE INSERTED: 30

INSERTION IS SUCCESS !!!

****** MENU ******


1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 3
30 --> 20 --> 10 --> NULL
****** MENU ******
1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 2

DELETED ELEMENT: 30
****** MENU ******
1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 2

DELETED ELEMENT: 20
****** MENU ******
1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 2

DELETED ELEMENT: 10
****** MENU ******
1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 2

STACK IS EMPTY

16
****** MENU ******
1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 3

STACK IS EMPTY

****** MENU ******


1: PUSH
2: POP
3: DISPLAY
4: EXIT
ENTER YOUR CHOICE: 4

17
EXPERIMENT NO: 6
QUEUE USING LINKED LIST

AIM

Write a C program to implement queue using singly linked list.

PROGRAM

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *front=NULL;
struct node *rear=NULL;
void enqueue(int value){
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->next=NULL;
if((front==NULL)&&(rear==NULL))
front=rear=newnode;
else{
rear->next=newnode;
rear=newnode;
}
printf("\nNODE IS INSERTED \n\n");
}
int dequeue(){
if(front==NULL){
printf("\nUNDERFLOW\n");
return -1;
}
else{
struct node *temp=front;
printf("POPPED ELEMENT IS : %d \n",temp->data);
front=front->next;
free(temp);
}
}
void display(){
struct node *temp;
if((front==NULL)&&(rear==NULL))
printf("\nQUEUE IS EMPTY\n\n");
else{
printf("\nTHE QUEUE IS \n");
temp=front;
while(temp){

18
printf("%d-->",temp->data);
temp=temp->next;
}
printf("NULL\n");
}
}
void main(){
int choice, value;
printf("\nIMPLEMENTATION OF QUEUE USING LINKED LIST \n");
while(choice != 4){
printf("\n 1: ENQUEUE\n 2: DEQUEUE\n 3: DISPLAY\n 4: EXIT\n\n");
printf("ENTER YOUR CHOICE: ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("ENTER THE VALUE TO INSERT: ");
scanf("%d",&value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nINVALID CHOICE\n");
}
}
}

OUTPUT

IMPLEMENTATION OF QUEUE USING LINKED LIST

1: ENQUEUE
2: DEQUEUE
3: DISPLAY
4: EXIT

ENTER YOUR CHOICE: 1


ENTER THE VALUE TO INSERT: 10

NODE IS INSERTED

1: ENQUEUE
2: DEQUEUE
3: DISPLAY

19
4: EXIT

ENTER YOUR CHOICE: 1


ENTER THE VALUE TO INSERT: 20

NODE IS INSERTED

1: ENQUEUE
2: DEQUEUE
3: DISPLAY
4: EXIT

ENTER YOUR CHOICE: 1


ENTER THE VALUE TO INSERT: 30

NODE IS INSERTED

1: ENQUEUE
2: DEQUEUE
3: DISPLAY
4: EXIT

ENTER YOUR CHOICE: 3

THE QUEUE IS
10-->20-->30-->NULL

1: ENQUEUE
2: DEQUEUE
3: DISPLAY
4: EXIT

ENTER YOUR CHOICE: 2


POPPED ELEMENT IS : 10

1: ENQUEUE
2: DEQUEUE
3: DISPLAY
4: EXIT

ENTER YOUR CHOICE: 2


POPPED ELEMENT IS : 20

1: ENQUEUE
2: DEQUEUE
3: DISPLAY
4: EXIT

ENTER YOUR CHOICE: 2


POPPED ELEMENT IS : 30

20
1: ENQUEUE
2: DEQUEUE
3: DISPLAY
4: EXIT

ENTER YOUR CHOICE: 2

UNDERFLOW

1: ENQUEUE
2: DEQUEUE
3: DISPLAY
4: EXIT

ENTER YOUR CHOICE: 3

THE QUEUE IS
NULL

1: ENQUEUE
2: DEQUEUE
3: DISPLAY
4: EXIT

ENTER YOUR CHOICE: 4

21
EXPERIMENT NO: 7
BINARY SEARCH TREE INORDER TRAVERSAL

AIM

Write a C program to implement Binary Search Tree in Inorder Traversal.

PROGRAM

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *right_child;
struct node *left_child;
};
struct node *new_node(int x){
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->left_child=NULL;
temp->right_child=NULL;
return temp;
}
struct node* insert(struct node *root,int x){
if(root==NULL)
return new_node(x);
else if(x>root->data)
root->right_child=insert(root->right_child, x);
else
root->left_child=insert(root->left_child, x);
return root;
}
void inorder(struct node *root){
if(root != NULL){
inorder(root->left_child);
printf("%d \t", root->data);
inorder(root->right_child);
}
}
int main(){
int i,m,n,r;
printf("Enter Root node : ");
scanf("%d",&n);
printf("How many nodes you want : ");
scanf("%d",&m);
struct node *root;
root = new_node(n);
printf("Enter the node values : ");
for(i=1;i<=m;i++){

22
scanf("%d",&r);
insert(root,r);
}
printf("\nTree nodes in inorder Traversal are : \n");
inorder(root);
}

OUTPUT

Enter Root node : 20


How many nodes you want : 11
Enter the node values : 5
1
15
9
7
12
30
25
40
45
42

Tree nodes in inorder Traversal are :


1 5 7 9 12 15 20 25 30 40 42 45

23
EXPERIMENT NO: 8
BINARY SEARCH TREE PREORDER TRAVERSAL

AIM

Write a C program to implement Binary Search Tree in Preorder Traversal.

PROGRAM

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *right_child;
struct node *left_child;
};
struct node *new_node(int x){
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->left_child=NULL;
temp->right_child=NULL;
return temp;
}
struct node* insert(struct node *root,int x){
if(root==NULL)
return new_node(x);
else if(x>root->data)
root->right_child=insert(root->right_child, x);
else
root->left_child=insert(root->left_child, x);
return root;
}
void preorder(struct node *root){
if(root != NULL){
printf("%d \t", root->data);
preorder(root->left_child);
preorder(root->right_child);
}
}
int main(){
int i,m,n,r;
printf("Enter Root node : ");
scanf("%d",&n);
printf("How many nodes you want : ");
scanf("%d",&m);
struct node *root;
root = new_node(n);
printf("Enter the node values : ");
for(i=1;i<=m;i++){

24
scanf("%d",&r);
insert(root,r);
}
printf("\nTree nodes in preorder Traversal are : \n");
preorder(root);
}

OUTPUT

Enter Root node : 20


How many nodes you want : 11
Enter the node values : 5
1
15
9
7
12
30
25
40
45
42

Tree nodes in preorder Traversal are :


20 5 1 15 9 7 12 30 25 40 45 42

25
EXPERIMENT NO: 9
BINARY SEARCH TREE POSTORDER TRAVERSAL

AIM

Write a C program to implement Binary Search Tree in Postorder Traversal.

PROGRAM

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *right_child;
struct node *left_child;
};
struct node *new_node(int x){
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->left_child=NULL;
temp->right_child=NULL;
return temp;
}
struct node* insert(struct node *root,int x){
if(root==NULL)
return new_node(x);
else if(x>root->data)
root->right_child=insert(root->right_child, x);
else
root->left_child=insert(root->left_child, x);
return root;
}
void postorder(struct node *root){
if(root != NULL){
postorder(root->left_child);
postorder(root->right_child);
printf("%d \t", root->data);
}
}
int main(){
int i,m,n,r;
printf("Enter Root node : ");
scanf("%d",&n);
printf("How many nodes you want : ");
scanf("%d",&m);
struct node *root;
root = new_node(n);
printf("Enter the node values : ");
for(i=1;i<=m;i++){

26
scanf("%d",&r);
insert(root,r);
}
printf("\nTree nodes in postorder Traversal are : \n");
postorder(root);
}

OUTPUT

Enter Root node : 20


How many nodes you want : 11
Enter the node values : 5
1
15
9
7
12
30
25
40
45
42

Tree nodes in postorder Traversal are :


1 7 12 9 15 5 25 42 45 40 30 20

27
EXPERIMENT NO: 10
DEPTH FIRST SEARCH TRAVERSAL

AIM

Write a C program to implementation of graph traversal using BFS.

PROGRAM

#include<stdio.h>
int a[20][20],reach[20],n;
void dfs(int v){
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i]){
printf("\n%d -> %d",v,i);
dfs(i);
}
}
void main(){
int i, j, count=0;
printf("\nENTER NUMBER OF VERTICES: ");
scanf("%d", &n);
for(i=1;i<=n;i++){
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("\nENTER THE ADJACENCY MATRIX: \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for(i=1;i<=n;i++){
if(reach[i])
count++;
}
if(count==n)
printf("\nGRAPH IS CONNECTED");
else
printf("\nGRAPH IS NOT CONNECTED");
}

28
OUTPUT

ENTER NUMBER OF VERTICES: 4

ENTER THE ADJACENCY MATRIX:


0111
1011
1101
0110

1 -> 2
2 -> 3
3 -> 4

GRAPH IS CONNECTED

29
EXPERIMENT NO: 11
BREADTH FIRST SEARCH TRAVERSAL

AIM

Write a C program to implementation of graph traversal using DFS.

PROGRAM

#include<stdio.h>
int n,i,j,visited[10],queue[10],front=-1,rear=-1;
int adj[10][10];
void bfs(int v){
for(i=1;i<=n;i++)
if(adj[v][i] && !visited[i])
queue[++rear]=i;
if(front<=rear){
visited[queue[front]]=1;
bfs(queue[front++]);
}
}
int main(){
int v;
printf("ENTER THE NUMBER OF VERTICES: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
queue[i]=0;
visited[i]=0;
}
printf("ENTER GRAPH DATA IN MATRIX FORM: \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
printf("ENTER THE STARTING VERTEX: ");
scanf("%d",&v);
bfs(v);
printf("THE NODE WHICH ARE REACHABLE ARE: \n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("BFS IS NOT POSSIBLE. NOT ALL NODES ARE REACHABLE.");
return 0;
}

30
OUTPUT

ENTER THE NUMBER OF VERTICES: 4


ENTER GRAPH DATA IN MATRIX FORM:
0111
1010
1101
1110
ENTER THE STARTING VERTEX: 2
THE NODE WHICH ARE REACHABLE ARE:
1 2 3 4

31
EXPERIMENT NO:12
OPENEND EXPERIMENT

AIM

Write the a program to using Queue operation

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

struct library {
char book_name[50];
char author[50];
int pages;
float price;
};

int main()
{
struct library lib[100];

char ar_nm[30], bk_nm[30];

int i, input, count;

i = input = count = 0;

while (input != 5) {

printf("\n\n********######"
"WELCOME TO E-LIBRARY "
"#####********\n");
printf("\n\n1. Add book infor"
"mation\n2. Display "
"book information\n");
printf("3. List all books of "
"given author\n");
printf(
"4. List the count of books"
" in the library\n");
printf("5. Exit");

printf("\n\nEnter one of "


"the above: ");
scanf("%d", &input);

32
switch (input) {

case 1:

printf("Enter book name = ");


scanf("%s", lib[i].book_name);

printf("Enter author name = ");


scanf("%s", lib[i].author);

printf("Enter pages = ");


scanf("%d", &lib[i].pages);

printf("Enter price = ");


scanf("%f", &lib[i].price);
count++;
break;
case 2:
printf("you have entered"
" the following "
"information\n");
for (i = 0; i < count; i++) {

printf("\t book name = %s \n",


lib[i].book_name);

printf("\t author name = %s \n",


lib[i].author);

printf("\t pages = %d \n",


lib[i].pages);

printf("\t price = %f \n",


lib[i].price);
}
break;

case 3:
printf("Enter author name : ");
scanf("%s", ar_nm);
for (i = 0; i < count; i++) {

if (strcmp(ar_nm,
lib[i].author)
== 0)
printf("%s %s %d %f",
lib[i].book_name,
lib[i].author,
lib[i].pages,
lib[i].price);
}

33
break;

case 4:
printf("\n Number of books in library : %d",
count);
break;
case 5:
exit(0);
}
}
return 0;
}

OUTPUT

********######WELCOME TO E-LIBRARY #####********

1. Add book information


2. Display book information
3. List all books of given author
4. List the count of books in the library
5. Exit

Enter one of the above: 1


Enter book name = ram_c/o_anandhi
Enter author name = renjith
Enter pages = 500
Enter price = 350

********######WELCOME TO E-LIBRARY #####********

1. Add book information


2. Display book information
3. List all books of given author
4. List the count of books in the library
5. Exit

Enter one of the above: 1


Enter book name = neermathalam
Enter author name = aami

34
Enter pages = 250
Enter price = 300.000000

********######WELCOME TO E-LIBRARY #####********

1. Add book information


2. Display book information
3. List all books of given author
4. List the count of books in the library
5. Exit

Enter one of the above: 2


you have entered the following information
book name = neermathalam
author name = aami
pages = 250
price = 300.000000

********######WELCOME TO E-LIBRARY #####********

1. Add book information


2. Display book information
3. List all books of given author
4. List the count of books in the library
5. Exit

Enter one of the above: 4

Number of books in library : 2

********######WELCOME TO E-LIBRARY #####********

1. Add book information


2. Display book information
3. List all books of given author
4. List the count of books in the library

35
5. Exit

Enter one of the above: 3


Enter author name : aami
neermathalam aami 250 300.000000

36

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy