0% found this document useful (0 votes)
12 views

Extra Programs

The document contains three separate programs: one for converting infix expressions to postfix expressions, another for implementing tree traversal methods (inorder, preorder, postorder), and a third for implementing graph traversal methods (DFS and BFS). Each program includes code snippets, function definitions, and sample outputs demonstrating their functionality. The programs utilize data structures such as stacks, trees, and linked lists to perform their respective operations.

Uploaded by

vinita sharma
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)
12 views

Extra Programs

The document contains three separate programs: one for converting infix expressions to postfix expressions, another for implementing tree traversal methods (inorder, preorder, postorder), and a third for implementing graph traversal methods (DFS and BFS). Each program includes code snippets, function definitions, and sample outputs demonstrating their functionality. The programs utilize data structures such as stacks, trees, and linked lists to perform their respective operations.

Uploaded by

vinita sharma
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/ 9

1.

Write a program to convert an infix expression into its equivalent


postfix expression.
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
char s[SIZE];
int top = -1; /* Global declarations */
main()
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
clrscr();
printf("\n\n Enter the Infix Expression \n ");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '\0')
{
if (ch == '(')
push(ch);
else
if (isalnum(ch)) /* Alphabet or number */
pofx[k++] = ch;
else
if (ch == ')')
{
while ( s[top] != '(' )
{
pofx[k++] = pop();
}
elem = pop(); /* To Remove ( */
}
else
{ /* symbol is Operator */
while (pr(s[top]) >= pr(ch))
{
pofx[k++] = pop();
}
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = '\0'; /* Make pofx as valid string */
printf("\n\nGiven Infix Expn: %s \n\nPostfix Expn: %s\n", infx, pofx);
getch();
}
push(char elem)
{ /* Function for PUSH operation */
s[++top] = elem;
}
char pop()
{ /* Function for POP operation */
return (s[top--]);
}
int pr(char elem)
{ /* Function for precedence */
switch(elem)
{
case '#' : return 0;
case '(' : return 1;
case '+' : return 2;
case '-' : return 2;
case '*' : return 3;
case '/' : return 3;
}
}

Output:-
Infix A + B * C – D / E * H
Postfix A B C * + D E / H * -

2. Write a program to implement the tree traversal methods.


#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
void inorder(struct node* root){
if(root == NULL) return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}
void preorder(struct node* root){
if(root == NULL) return;
printf("%d ->", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node* root) {
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}
struct node* createNode(value){
struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node *root, int value) {
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node *root, int value){
root->right = createNode(value);
return root->right;
}
int main(){
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
printf("Inorder traversal \n");
inorder(root);
printf("\nPreorder traversal \n");
preorder(root);
printf("\nPostorder traversal \n");
postorder(root);
}

Output:
Inorder traversal
5 ->12 ->6 ->1 ->9 ->
Preorder traversal
1 ->12 ->5 ->6 ->9 ->
Postorder traversal
5 ->6 ->12 ->9 ->1 ->

3. Write a program to implement the graph traversal methods.


#include<stdio.h>
#include<stdlib.h>
void dfs(); // For Deapth First Search(DFS) Traversal
void bfs(); // For Breadth First Search(BFS) Traversal
struct link // Structure for adjacency list
{
struct node *next;
struct link *adj;
};
struct node // Structure for elements in the graph
{
int data,status;
struct node *next;
struct link *adj;
};
struct node *start,*p,*q; //Declaring pointer variable for structure node
struct link *l,*k; //Declaring pointer variable for structure node
void create() // Function to Create the graph
{
int dat,flag=0;dat=1; //Initializing 'flag' & 'dat' with a value of '0' & //'start'
pointing to
NULL, as no value is being input till nowprintf("Enter the nodes in the graph(0
to end): ");
while(dat)
{
scanf("%d",&dat); //Getting the data from user
if(dat==0) //If data entered is '0' then assume its the end of inputting elements
break;
p=(struct node*)malloc(sizeof(struct node)); //reserving memory space for nodal
element p-
>data=dat; //storing the input data into node's data element
p->status=0;
p- >next=NULL; //next element is set to NULL p->adj=NULL; //previous
element is set to NULL
if(flag==0) //If flag's value is zero then follow below procedure
{
start=p;
q=p;
flag++;
} //If flag's value is not zero then follow below methodelse
{
q- >next=p;
q=p;
}
} //Finishing the data entry
loopp=start; //Assigning the pointer 'p' the starting locationwhile(p!=NULL)
{
printf("Enter the links to %d (0 to end) : ",p->data);
flag=0; //Setting the initial value of 'flag' be '0'
while(1)
{
scanf("%d",&dat);
if(dat==0)
break;
k=(struct link*)malloc(sizeof(struct link)); //Allocating memory space for "link"
element k->adj=NULL;
if(flag==0)
{
p->adj=k;
l=k;
flag++;
}
else
{
l->adj=k;
l=k;
}
q=start;
while(q!=NULL)
{
if(q->data==dat)
k->next=q;
q=q->next;
}
}
p=p->next;
}
}
void bfs() //Function for Breadth First Traversal of Graph
{
int q[20],i=1,j=0;
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
q[0]=p->data;
p->status=1;
while(1)
{
if(q[j]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==q[j])
break;
p=p->next;
}
k=p->adj;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
q[i]=q->data;
q->status=1;
q[i+1]=0;
i++;
}
k=k->adj;
}
j++;
}
j=0;
printf("Breadth First Search Results\n");
while(q[j]!=0) //For printing the BFS result array
{
printf("%d ",q[j]);
Data structures Marri Laxman Reddy of Institute of Technology and Management
j++;
}
getche();
}//Function for Depth First Search(BFS) Traversal.
void dfs()
{
int stack[20],top=1;
printf("Deapth First Search Results");
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
stack[0]=0;
stack[top]=p->data;
p->status=1;
while(1)
{
if(stack[top]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==stack[top])
break;
p=p->next;
}
printf("%d ",stack[top]); //Printing the DFS result
top--;
k=p->adj;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
top++;
stack[top]=q->data;
q->status=1;
}
k=k->adj;
}
}
}
int main()
{
int ch;
create(); //Invoking the create function
while(1)
{
printf("1: DFS\n2: BSF\n0: Exit\nEnter your choice: ");
scanf("%d",&ch); //User enters his choice
switch(ch)
{
case 1:
dfs(); //For Depth First Traversal
break;
case 2:
bfs(); //For Breadth First Traversal
break;
case 0:
exit(0); //If Zero then exit the program (Omit this line if you don't want that)
break;
default:
printf("Incorrect choice!");
}
}
return 0;
} //End of program.

You might also like