Ds 1
Ds 1
Ds 1
LAB PROGRAMS
INDEX
Page
Sl No. Topic
Number
1a Tower of Hanoi 3
2
1a.Program to Implement Tower of Hanoi
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;clrscr();
printf("Enter no of disks\n");
scanf("%d",&n);
tower(n,'s','d','t');
getch();
}
Output:
3
1b. Program to Implement Stack using Arrays
Program:
#include<stdio.h>
#include<conio.h>
#define SIZE 10
int top=-1,stack[20];
void main()
{
int ch=1,option;
clrscr();
while(ch==1)
{
printf("Stack Operations\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your Choice\n");
scanf("%d",&option);
switch(option)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(0);
default:printf("Wrong Choice\n");
}
printf("Do you want to continue 1-YES, 0-NO\n");
scanf("%d",&ch);
}
}
push()
{
int num;
if(top==(SIZE-1))
{
printf("Stack is Full\n");
return;
}
4
else
{
printf("Enter element to insert\n");
scanf("%d",&num);
stack[++top]=num;
return;
}
}
pop()
{
if(top==-1)
{
printf("Stack is Empty\n");
return;
}
else
{
printf("Deleted element=%d\n",stack[top]);
top--;
return;
}
}
display()
{
int i;
if(top==-1)
{
printf("Stack is Empty\n");
return;
}
else
{
printf("Status of Stack is\n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
return;
}
}
5
Output:
6
2. Program to convert Infix to Postfix
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int top=-1,i=0,j=0,length;
char symbol,infix[20],postfix[20],stack[20];
void main()
{
clrscr();
printf("Enter infix Expression\n");
scanf("%s",infix);
infix_to_postfix(infix,postfix);
printf("Postfix Expression is %s\n",postfix);
getch();
}
void push()
{
stack[++top]=symbol;
return;
}
char pop()
{
char temp;
temp=stack[top--];
return(temp);
}
7
while(i<length)
{
symbol=infix[i];
switch(symbol)
{
case '(':push(symbol);break;
case ')':temp=pop();
while(temp!='(')
{
postfix[j++]=temp;
temp=pop();
}
break;
case '/':
case '*':
case '^':
case '+':
case '-':while(preceed(stack[top])==preceed(symbol))
{
temp=pop();
postfix[j++]=temp;
}
push(symbol);
break;
default: postfix[j++]=symbol;
}
i++;
}
while(top>0)
{
postfix[j++]=pop();
return;
}
}
preceed(char symbol)
{
int p;
8
switch(symbol)
{
case '^':p=3;break;
case '/':
case '*':p=2;break;
case '+':
case '-':p=1;break;
case '(':
case ')':p=0;break;
case '#':p=-1;break;
}
return(p);
}
Output:
9
3.Program to convert Infix to Prefix
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void push();
void in_prefix();
char prefix[20],infix[20],stack[20],symbol,temp;
int top=-1,length,i=0,j=0,k=0;
void main()
{
clrscr();
printf("Enter infix Expression\n");
gets(infix);
in_prefix(infix,prefix);
printf("Prefix Expression is %s",prefix);
getch();
}
10
while(temp!=')')
{
prefix[j--]=temp;
temp=pop();
}
break;
case '^':
case '/':
case '*':
case '+':
case '-': while(ISP(stack[top])>=ICP(symbol))
{
temp=pop();
prefix[j--]=temp;
}
push(symbol);break;
default :prefix[j--]=symbol;break;
}
length--;
}
while(top>0)
{
temp=pop();
prefix[j--]=temp;
}
}
ISP(char symbol)
11
{
int p;
switch(symbol)
{
case '^': p=6;break;
case '*':
case '/': p=3;break;
case '+':
case '-': p=1;break;
case ')': p=0;break;
case '#': p=-1;break;
}
return(p);
}
ICP(char symbol)
{
int q;
switch(symbol)
{
case '^': q=5;break;
case '*':
case '/': q=4;break;
case '+':
case '-': q=2;break;
case ')': q=0;break;
case '(': q=9;break;
}
return q;
}
Output:
12
4a. Program to Implement Insertion Sort
Program:
#include<stdio.h>
#include<conio.h>
14
4b. Implementation of Queue using Arrays
Program:
#include<stdio.h>
#include<conio.h>
#define SIZE 2
int rear=-1,front=-1,queue[10];
void main()
{
int ch=1,option;
clrscr();
while(ch==1)
{
printf("Queue Operations\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice\n");
scanf("%d",&option);
switch(option)
{
case 1:qinsert();break;
case 2:qdelete();break;
case 3:qdisplay();break;
case 4:exit(0);
default :printf("Wrong Choice\n");
}
printf("Do you want to continue\n");
scanf("%d",&ch);
}
}
qinsert()
{
int num;
if(rear==(SIZE-1))
{
printf("Queue is full\n");
return;
}
printf("Enter element to insert\n");
15
scanf("%d",&num);
queue[++rear]=num;
if(front==-1)
front++;
return;
}
qdelete()
{
if(front==-1)
{
printf("Queue is Empty\n");
return;
}
if(front==rear)
{
printf("Deleted element = %d\n",queue[front]);
front=-1;
rear=-1;
return;
}
printf("Deleted element = %d\n",queue[front]);
front++;
return;
}
qdisplay()
{
int i;
if(front==-1)
{
printf("Queue is Empty\n");
return;
}
printf("Status of Stack is\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
printf("\n");
return;
}
16
Output:
17
5. Singly linked list
Program:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
typedef struct node *NODE;
NODE first=0;
int i,choice,ch1=1,choice1,ch2=1,choice2,ch3=1;
NODE insert_front(NODE);
NODE insert_pos(NODE);
NODE delete_front(NODE);
NODE delete_pos(NODE);
void display (NODE);
void main()
{
clrscr();
while(ch1==1)
{
printf("Singly linked list Implementation\n");
printf("1 Insert\n2 Delete\n3 Display\n4 Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("insert implementation\n");
ch2=1;
while(ch2==1)
{
printf("1 insert front\n2 insert pos\n");
printf("Enter your choice\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:first=insert_front(first);break;
18
case 2:first=insert_pos(first);break;
default:printf("wrong choice\n");
}
printf("Do you wanto insert again\n");
scanf("%d",&ch2);
}
break;
case 2:printf("delete implementation\n");
ch3=1;
while(ch3==1)
{
printf("1 delete front\n2 delete position\n");
printf("enter your choice\n");
scanf("%d",&choice2);
switch(choice2)
{
case 1:first=delete_front(first);break;
case 2:first=delete_pos(first);break;
default: printf("wrong choice\n");
}
printf("do you wanto delete again\n");
scanf("%d",&ch3);
}
break;
case 3:display (first);break;
case 4:exit(0);
default:printf("wrong choice\n");
}
printf("do you wanto continue\n");
scanf("%d",&ch1);
}
}
19
if(first==0)
{
first=newnode;
return(first);
}
newnode->link=first;
first=newnode;
return(first);
}
if(pos==1)
{
newnode->link=first;
first=newnode;
return(first);
}
for(i=1;i<pos;i++)
{
temp1=temp;
temp=temp->link;
}
if(temp==0)
{
printf("invalid pos\n");
return(first);
}
20
temp1->link=newnode;
newnode->link=temp;
return(first);
}
21
}
printf("deleted data %d\n",temp->data);
temp1->link=temp->link;
free(temp);
return(first);
}
if(temp!=0)
{
22
printf("In list are\n");
}
while(temp!=0)
{
printf("%d\t",temp->data);
temp=temp->link;
printf("\n");
}
}
Output:
23
24
6. Stack implementation using linked list
Program:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
};
typedef struct node *NODE;
NODE first=0;
int i, choice, ch=1;
NODE push(NODE);
NODE pop(NODE);
void display(NODE);
void main()
{
clrscr();
while(ch==1)
{
printf("Stack implementation\n");
printf("1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("Enter your choice: \n");
scanf("%d",&choice);
switch(choice)
{
case 1: first = push(first); break;
case 2: first = pop(first); break;
case 3: display(first);break;
case 4: exit(0);
default : printf("Wrong Choice\n"); break;
}
printf("Do you want to continue : \n");
scanf("%d",&ch);
}
25
}
newnode->link=first;
first=newnode;
return(first);
}
temp=first;
if(temp->link==0)
{
printf("Deleted data = %d\n",temp->data);
free(temp);
first=0;
return(first);
}
printf("Deleted data=%d\n",temp->data);
first=first->link;
26
free(temp);
return(first);
}
while(temp!=0)
{
printf("%d=>",temp->data);
temp=temp->link;
}
return;
}
27
Output:
28
7. Queue using singly linked list
Program:
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
};
typedef struct node *NODE;
NODE first=0;
int i, choice, ch=1;
NODE insert_end(NODE);
NODE delete_front(NODE);
void display(NODE);
void main()
{
clrscr();
while(ch==1)
{
printf("Queue implementation: \n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter your choice: \n");
scanf("%d",&choice);
switch(choice)
{
case 1: first = insert_end(first); break;
case 2: first = delete_front(first); break;
case 3: display(first);break;
case 4: exit(0);
default : printf("Wrong Choice\n"); break;
}
printf("Do you want to continue : \n");
scanf("%d",&ch);
}
29
{
NODE newnode,temp;
newnode=(NODE)malloc(sizeof(struct node));
newnode->link=0;
printf("Enter data: \n");
scanf("%d",&newnode->data);
if(first==0)
{
first = newnode;
return(first);
}
temp=first;
while(temp->link!=0)
temp=temp->link;
temp->link=newnode;
return(first);
}
temp=first;
if(temp->link==0)
{
printf("Deleted data = %d\n",temp->data);
free(temp);
first=0;
return(first);
}
printf("Deleted data=%d\n",temp->data);
first=first->link;
free(temp);
30
return(first);
}
while(temp!=0)
{
printf("%d=>\n",temp->data);
temp=(NODE)temp->data;
}
return;
}
31
Output:
32
8. Circular Singly Linked List Implementation
Programs:
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *rlink;
};
typedef struct node *NODE;
int i, choice, ch1=1, choice1, ch2=1, choice2, ch3=1;
NODE insert_front(NODE);
NODE insert_end(NODE);
NODE delete_front(NODE);
NODE delete_end(NODE);
void display(NODE);
main()
{
NODE head = 0;
head = (NODE)malloc(sizeof(struct node));
head->rlink=head;
clrscr();
while(ch1==1)
{
printf("Circular Singly Linked List Implementation:\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Insert Implementation: \n");
ch2=1;
while(ch2==1)
{
printf("1.Insert Front\n2.Insert End\n");
printf("Enter your choice:\n");
scanf("%d",&choice1);
switch(choice1)
33
{
case 1 : head = insert_front(head); break;
case 2 : head = insert_end(head); break;
default : printf("Wrong Choice\n"); break;
}
printf("Do you want to insert again\n");
scanf("%d",&ch2);
}
break;
switch(choice2)
{
case 1 : head = delete_front(head); break;
case 2 : head = delete_end(head); break;
default : printf("Wrong Choice\n"); break;
}
return 0;
}
34
NODE insert_front(NODE head)
{
NODE newnode,first;
newnode=(NODE)malloc(sizeof(struct node));
newnode->rlink=0;
printf("Enter Data:\n");
scanf("%d",&newnode->data);
if(head->rlink==head)
{
head->rlink=newnode;
newnode->rlink=head;
return head;
}
first=head->rlink;
head->rlink=newnode;
newnode->rlink=first;
return (head);
}
next=head->rlink;
while(next->rlink!=head)
next=next->rlink;
35
next->rlink=newnode;
newnode->rlink=head;
return(head);
}
temp=head->rlink;
if(temp->rlink==0)
{
printf("Deleted Data=%d\n",temp->data);
head->rlink=head;
free(temp);
return (head);
}
first=temp->rlink;
printf("Deleted Data=%d\n",temp->data);
head->rlink=first;
free(temp);
return(head);
}
temp=head->rlink;
36
if(temp->rlink==head)
{
printf("Deleted data is %d\n",temp->data);
head->rlink=head;
free(temp);
return(head);
}
while(temp->rlink!=head)
{
prev=temp;
temp=temp->rlink;
}
37
Output:
38
39
9. Doubly Linked List
Program:
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
};
main()
{
int ch=1;
clrscr();
while(ch==1)
{
printf("Doubly Linked List Implementation:\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Insert Implementation:\n");
ch2=1;
while(ch2==1)
{
40
printf("1.Insert Front\n2.Insert End\n");
printf("Enter your choice:\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1 : first = insert_front(first); break;
case 2 : first = insert_end(first); break;
default : printf("Wrong Choice\n"); break;
}
switch(choice2)
{
case 1 : first = delete_front(first); break;
case 2 : first = delete_end(first); break;
default : printf("Wrong Choice\n"); break;
}
case 4 : exit(0);
default : printf("Wrong Choice\n");
41
break;
}
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->llink=newnode->rlink=0;
printf("Enter Data:\n");
scanf("%d",&newnode->data);
if(first==0)
{
first=newnode;
return first;
}
newnode->rlink=first;
first->llink=newnode;
first=newnode;
return (first);
}
{
NODE newnode, temp;
newnode=(NODE)malloc(sizeof(struct node));
newnode->rlink=newnode->llink=0;
printf("Enter data:\n");
42
scanf("%d",&newnode->data);
if(first==0)
{
first=newnode;
return(first);
}
temp=first;
while(temp->rlink!=0)
temp=temp->rlink;
temp->rlink=newnode;
newnode->llink=temp;
return(first);
}
temp=first;
if(temp->rlink==0)
{
printf("Deleted Data=%d\n",temp->data);
free(temp);
return (first);
}
printf("Deleted Data=%d\n",temp->data);
first=first->rlink;
first->llink=0;
free(temp);
return(first);
}
43
{
NODE temp, temp1;
if(first==0)
{
printf("No nodes in the list\n");
return(first);
}
temp=first;
if(temp->rlink==0)
{
printf("Deleted data = %d\n",temp->data);
free(temp);
first=0;
return(first);
}
while(temp->rlink!=0)
temp=temp->rlink;
printf("Deleted data = %d\n",temp->data);
temp1=temp->llink;
free(temp);
temp1->rlink=0;
return(first);
}
44
45
10. Doubly Linked List
Program:
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;
NODE first=0;
int i, choice, ch1=1, choice1, ch2=1, choice2, ch3=1;
NODE insert_front(NODE);
NODE insert_pos(NODE);
NODE delete_front(NODE);
NODE delete_pos(NODE);
void display(NODE);
main()
{
clrscr();
while(ch1==1)
{
printf("Doubly Linked List Implementation:\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Insert Implementation:\n");
while(ch2==1)
{
printf("1.Insert Front\n2.Insert Position\n");
printf("Enter your choice:\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1 : first = insert_front(first); break;
46
case 2 : first = insert_pos(first); break;
default : printf("Wrong Choice\n"); break;
}
printf("Do you want to insert again?\n");
scanf("%d",&ch2);
}
break;
case 2 : printf("Delete Implementation:\n");
ch3=1;
while(ch3==1)
{
printf("1.Delete Front\n2.Delete Position\n");
printf("Enter your choice:\n");
scanf("%d",&choice2);
switch(choice2)
{
case 1 : first = delete_front(first);break;
case 2 : first = delete_pos(first); break;
default : printf("Wrong Choice\n"); break;
}
47
scanf("%d",&newnode->data);
if(first==0)
{
first=newnode;
return first;
}
newnode->rlink=first;
first->llink=newnode;
first=newnode;
return (first);
}
48
if(temp==0)
{
printf("Invalid position\n");
return(first);
}
temp1->rlink=newnode;
newnode->llink=temp1;
newnode->rlink=temp;
temp->llink=newnode;
return(first);
}
49
{
printf("No nodes in the list\n");
return(first);
}
printf("Enter the position:\n");
scanf("%d",&pos);
temp=first;
if((pos==1)&&(first->rlink==0))
{
printf("Deleted data = %d\n",temp->data);
free(temp);
first=0;
return(first);
}
if(pos==1)
{
printf("Deleted data = %d\n",temp->data);
first=first->rlink;
first->llink=0;
free(temp);
return(first);
}
for(i=0;i<pos;i++)
temp=temp->rlink;
if(temp==0)
{
printf("Invalid position\n");
return(first);
}
temp1=temp->llink;
temp2=temp->rlink;
printf("Deleted data = %d\n",temp->data);
temp1->rlink=temp2;
temp2->llink=temp1;
free(temp);
return(first);
}
50
NODE temp;
temp=first;
if(temp==0)
{
printf("No nodes in the list\n");
}
while(temp!=0)
{
printf("%d=>",temp->data);
temp=temp->rlink;
}
}
Output:
51
52
11. Circular Doubly Linked List
Programs:
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *rlink;
struct node *llink;
};
main()
{
NODE head = 0;
head = (NODE)malloc(sizeof(struct node));
head->rlink=head->llink=head;
clrscr();
while(ch1==1)
{
printf("Circular Singly Linked List Implementation:\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Insert Implementation:\n");
ch2=1;
while(ch2==1)
{
printf("1.Insert Front\n2.Insert End\n");
printf("Enter your choice:\n");
53
scanf("%d",&choice1);
switch(choice1)
{
case 1 : head = insert_front(head); break;
case 2 : head = insert_end(head); break;
default : printf("\Wrong Choice\n"); break;
}
printf("Do you want to insert again?\n");
scanf("%d",&ch2);
}break;
case 2 : printf("Delete Implementation:\n ");
while(ch3==1)
{
printf("1.Delete Front\n2.Delete End\n");
printf("Enter your choice:\n");
scanf("%d",&choice2);
switch(choice2)
{
case 1 : head = delete_front(head); break;
case 2 : head = delete_end(head); break;
default : printf("Wrong Choice\n"); break;
}
printf("Do you want to delete again?\n");
scanf("%d",&ch3);
}break;
case 3 : display(head); break;
case 4 : exit(0);
default : printf("Wrong Choice\n"); break;
}
printf("Do you want to continue:\n");
scanf("%d",&ch1);
}
return 0;
}
54
printf("Enter Data:\n");
scanf("%d",&newnode->data);
if(head->rlink==head)
{
head->rlink=newnode;
newnode->llink=head;
newnode->rlink=head;
head->llink=newnode;
}
first=head->rlink;
head->rlink=newnode;
newnode->llink=head;
newnode->rlink=first;
first->llink=newnode;
return (head);
}
55
NODE delete_front(NODE head)
{
NODE first,next;
if(head->rlink==head)
{
printf("No nodes in the list\n");
return(head);
}
first=head->rlink;
if(first->rlink==head)
{
printf("Deleted Data=%d\n",first->data);
head->rlink=head->llink=head;
free(first);
return (head);
}
next=first->rlink;
printf("Deleted Data=%d\n",first->data);
head->rlink=next;
next->llink=head;
free(first);
return(head);
}
56
last=head->llink;
prev=last->llink;
printf("Deleted data = %d\n",last->data);
prev->rlink=head;
head->llink=prev;
free(last);
return(head);
}
void display(NODE head)
{
NODE next;
next=head->rlink;
if(head->rlink==head)
{
printf("No nodes in the list\n");
}
else
{
while(next->rlink!=head)
{
printf("%d=>",next->data);
next=next->rlink;
}
printf("%d=>",next->data);
}
}
Output:
57
58
12. Binary Tree
Program:
#include<stdio.h>
struct node
{
int data;
struct node *lchild;
struct node *rchild;
};
typedef struct node *NODE;
NODE root=0;
void create(NODE *);
void inorder(NODE);
void main()
{
clrscr();
printf("Create Tree\n");
root=(NODE)malloc(sizeof(struct node));
printf("Enter data for root node:\n");
scanf("%d",&root->data);
create(&root);
printf("Inorder Traversal:\n");
inorder(root);
}
if(is_rchild(&(*root1)))
{
(*root1)->rchild=(NODE)malloc(sizeof(struct node));
temp1=(*root1)->rchild;
printf("Enter data for right child:\n");
scanf("%d",&temp1->data);
create(&temp1);
}
else
(*root1)->rchild=0;
return;
}
int is_lchild(NODE *root2)
{
int ch;
printf("Create lchild of %d 1-YES:\n",(*root2)->data);
scanf("%d",&ch);
if(ch==1)
return(1);
else
return(0);
}
int is_rchild(NODE *root2)
{
int ch1;
printf("Create rchild of %d:\n",(*root2)->data);
scanf("%d",&ch1);
if(ch1==1)
return(1);
else
return(0);
}
void inorder(NODE root4)
{
if(root4!=0)
{
inorder(root4->lchild);
printf("%d==>",root4->data);
60
inorder(root4->rchild);
}}
Output:
61
13. Tree Traversals
Program:
#include<stdio.h>
struct node
{
int data;
struct node *lchild;
struct node *rchild;
};
typedef struct node *NODE;
NODE root=0;
void create(NODE *);
void inorder(NODE);
void preorder(NODE);
void postorder(NODE);
int is_lchild(NODE *);
int is_rchild(NODE *);
void main()
{
clrscr();
printf("Create Tree\n");
root=(NODE)malloc(sizeof(struct node));
printf("Enter the data for root node:\n");
scanf("%d",&root->data);
create(&root);
printf("\nPreorder Traversal: \n");
preorder(root);
printf("\nInorder Traversal: \n");
inorder(root);
printf("\nPostorder Traversal: \n");
postorder(root);
}
if(is_rchild(&(*root1)))
{
(*root1)->rchild=(NODE)malloc(sizeof(struct node));
temp1=(*root1)->rchild;
printf("\nEnter data for right child:");
scanf("%d",&temp1->data);
create(&temp1);
}
else
(*root1)->rchild=0;
return;
}
63
else
return(0);
}
64
Output:
65
14. Binary Search Tree
Program:
#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
struct node
{
int data;
struct node *lchild;
struct node *rchild;
};
typedef struct node *NODE;
int i=1,num,req;
void insert(NODE*,int) ;
void node_delete(NODE*);
search(NODE*,int,NODE*,NODE*,int*);
void inorder(NODE root4);
void main()
{
NODE root=0;
clrscr();
printf("enter the num of nodes\n");
scanf("%d",&req);
while(i++<=req)
{
printf("enter the data\n");
scanf("%d",&num);
insert(&root,num);
}
printf("nodes before deletion\n");
inorder(root);
printf("nodes after deletion\n");
node_delete(&root);
inorder(root);
getch();
}
void insert(NODE *(root1),int num)
{
66
if((*root1)==0)
{
(*root1)=(NODE)malloc(sizeof(struct node));
(*root1)->lchild=(*root1)->rchild=0;
(*root1)->data=num;
}
else
{
if(num<((*root1)->data))
insert(&((*root1)->lchild),num);
else
insert(&((*root1)->rchild),num);
}
return;
}
void inorder(NODE root4)
{
if(root4!=0)
{
inorder(root4->lchild);
printf("%d=>",root4->data);
inorder(root4->rchild);
}
}
search(NODE *root3,int num2,NODE *par,NODE *x,int *found)
{
NODE q;
q=*root3;
*found=FALSE;
*par=0;
while(q!=0)
{
if(num2==q->data)
{
*found=TRUE;
*x=q; return;
}
*par=q;
if(num2<q->data)
q=q->lchild;
67
else
q=q->rchild;
}
return;
}
void node_delete(NODE *root2)
{
int num,found;
NODE parent,x,xsucc;
parent=x=0;
if(*root2==0)
{
printf("tree is empty\n");
return;
}
printf("enter data to be deleted\n");
scanf("%d",&num);
search(&(*root2),num,&parent,&x,&found);
if(found==FALSE)
{
printf("data to be deleted is not found\n",num);
return;
}
printf("data to be deleted is found\n",num);
if(x->lchild!=0&&x->rchild!=0)
{
parent=x;
xsucc=x->rchild;
while(xsucc->lchild!=0)
{
parent=xsucc;
xsucc=x->lchild;
}
x->data=xsucc->data;
x=xsucc;
}
if(x->lchild!=0&&x->rchild!=0)
{
if(parent->lchild==x)
parent->lchild=x->lchild;
68
else
parent->rchild=x->lchild;
free(x);
return;
}
if(x->rchild!=0&&x->lchild==0)
{
if(parent->lchild==x)
parent->lchild=x->rchild;
else
parent->rchild=x->rchild;
free(x);
return;
}
if(x->lchild==0&&x->rchild==0)
{
if(parent->rchild==x)
parent->lchild=0;
else
parent->lchild=0;
free(x);
return;
}
}
Output:
69
15. Evaluation of Expression
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct node
{
int data;
struct node *lchild;
struct node *rchild;
};
typedef struct node *NODE;
NODE root=0;
NODE create_tree(char postfix[]);
float eval(NODE root);
void main()
{
char postfix[20];
float result;
clrscr();
printf("enter the postfix\n");
scanf("%s",postfix);
root=create_tree(postfix);
result=eval(root);
printf("result=%f\n",result);
getch();
}
NODE create_tree(char postfix[])
{
NODE temp,stack[20];
int i=0,j=0;
char symbol;
for(i=0;(symbol=postfix[i])!=0;i++)
{
temp=(NODE)malloc(sizeof(struct node));
temp->lchild=temp->rchild=0;
temp->data=symbol;
if(isalnum(symbol))
stack[j++]=temp;
70
else
{
temp->rchild=stack[--j];
temp->lchild=stack[--j];
stack[j++]=temp;
}}
return(stack[--j]);
}
float eval(NODE root)
{
float num;
switch(root->data)
{
case '+':return eval(root->lchild)+eval(root->rchild);
case '-':return eval(root->lchild)-eval(root->rchild);
case '/':return eval(root->lchild)/eval(root->rchild);
case '*':return eval(root->lchild)*eval(root->rchild);
case '^':return pow(eval(root->lchild),eval(root->rchild));
default :if(isalpha(root->data))
{
printf("enter the value of %c\n",root->data);
scanf("%f",&num);return(num);
}
else
return(root->data-'\0');
}}
Output:
71
16. Right Threaded Binary Tree
Program:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
int RT;
};
typedef struct node *NODE;
NODE head=0;
NODE create(int,NODE);
void insert_left(int,NODE);
void insert_right(int,NODE);
void inorder(NODE);
NODE inorder_successor(NODE);
int ch,i,n,item,choice;
void main()
{
NODE head=0;
clrscr();
head=(NODE)malloc(sizeof(struct node));
head->right=head;
head->left=0;
head->RT=0;
while(1)
{
printf("\n1.create tree\n2.inorder\n3.exit\n");
printf("enter the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter num of nodes to create\n");
scanf("%d",&n);
for(i=1;i<n+1;i++)
72
{
printf("enter %d data\n",i);
scanf("%d",&item);
head=create(item,head);
}
break;
case 2:inorder(head);
break;
case 3:exit(0);
73
{
if(curptr->RT==0)
curptr=curptr->right;
else
break;
}
}
}
if(item<(curptr->data))
{
insert_left(item,ptr);
return(head);
}
else
{
if(item>(curptr->data)&&curptr->RT==1)
insert_right(item,ptr);
}
return(head);
}
74
ptr->right=newnode;
newnode->right=temp;
ptr->RT=0;
newnode->RT=1;
}
Output:
75
76