Escolar Documentos
Profissional Documentos
Cultura Documentos
SL.N
O
1.
2.
3.
4.
5.
6.
7.
NAME OF THE
PROGRAM
a) Write a C Program to stimulate the
tower of Hanoi problem.
b) Write a C Program to implement
stack using arrays.
Write a C Program to convert an infix
expression to postfix form.
Write a C Program to convert an infix
expression to prefix form.
a) Write a C Program to sort elements
using Insertion sort.
b) Write a C Program to implement
queue using arrays.
Write a C Program to create a singly
linked list to perform the
following operations.
a)Insert front
b)Insert specified position
c)Delete front
d)Delete specified position
e)Display
Write a C Program to stimulate stack
using singly linked list.
Write a C Program to stimulate queue
using singly linked list.
1
8.
9.
10.
11.
12.
13.
14.
15.
16.
#include<stdio.h>
#include<conio.h>
void tower(char src,char temp,char dst,int n);
int main(void)
{
int n;
clrscr();
scanf("%d",&n);
tower('a','b','c',n);
return(0);
}
void tower(char src,char temp,char dst,int n)
{
if(n>0)
{
tower(src,dst,temp,n-1);
printf("Move disk %d from %c to %c\n",n,src,dst);
tower(temp,src,dst,n-1);
}}
OUTPUT:
4
3
Move
Move
Move
Move
Move
Move
Move
disk
disk
disk
disk
disk
disk
disk
1
2
1
3
1
2
1
from
from
from
from
from
from
from
a to c
a to b
c to b
a to c
b to a
b to c
a to c
printf(Stack operations\n);
while(ch)
{
printf(Array implementation\n);
printf(1.Push\n);
printf(2.Pop\n);
printf(3.Display\n);
printf(4.Exit\n);
printf(Enter the choice\n);
scanf(%d,&choice);
switch(choice)
{
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 go array implementation again\n);
scanf(%d,&ch);
} }
push()
{
if(top==(size-1))
{
printf(Stack is full\n);
return;
}
printf(Enter the element\n);
scanf(%d,&num);
stack[++top]=num;
return;
}
pop()
{
if(top==-1)
{
printf(No elements in the stack\n);
return;
}
printf(Popped element is %d\n,stack[top]);
6
top--;
return;
}
display()
{
int i;
if(top==-1)
{
printf(No elements in the stack\n);
return;
}
printf(Stack elements are\n);
for(i=top;i>=0;i--)
printf(%d\n,stack[i]);
return;
}
OUTPUT:
Stack operations
Array implementation
1.Push
2.Pop
3.Display
4.Exit
Enter the choice
1
Enter the element
23
Do you want to go array implementation again
1
Array implementation
1.Push
2.Pop
3.Display
4.Exit
Enter the choice
45
Wrong choice
Do you want to go array implementation again
1
Array implementation
1.Push
2.Pop
3.Display
7
4.Exit
Enter the choice
1
Enter the element
45
Do you want to go array implementation again
1
Array implementation
1.Push
2.Pop
3.Display
4.Exit
Enter the choice
2
Popped element is 45
Do you want to go array implementation again
1
Array implementation
1.Push
2.Pop
3.Display
4.Exit
Enter the choice
3
Stack elements are
23
Do you want to go array implementation again
4
Array implementation
1.Push
2.Pop
3.Display
4.Exit
Enter the choice
4
}
push(symbol);
break;
default : postfix[j++]=symbol;
break;
}i++;
}
while(top>0)
{
postfix [j++]=pop ();
}
return;
}
push(char symbol)
{
stack[++top]=symbol;
return;
}
pop ()
{
char symbol;
symbol=stack[top--];
return(symbol);
}
preced (char symbol)
{
int p;
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;
10
}
return(p);
}
Output :
Enter infix expression
(a+b)
Postfix expression=ab+
length=strlen (infix);
push ('#');
length=length-1;
j=length;
while(infix[i]!='\0')
{
if (infix[i]=='('||infix[i]==')')
J--;
i++;
}
while(length>=k)
{
symbol=infix[length];
switch (symbol)
{
case ')': push(symbol);
break;
case '(': temp=pop();
while(temp!=')')
{
prefix[j--]=temp;
temp=pop();
}
break;
case '^':
case '*':
case '/':
case '+':
case '-': if (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;
}
12
return;
}
push (char symbol)
{
stack [++top]=symbol;
return;
}
pop ()
{
char symbol;
symbol=stack[top];
top--;
return(symbol);
}
ISP (char symbol)
{
switch (symbol)
{
case '^: return 6;
case '*':
case '/': return 3;
case '+':
case '-': return 1;
case '#': return -1;
case ')': return 0;
}
return (symbol);
}
ICP (char op)
{
switch (op)
{
case '^': return 5;
case '+':
case '-': return 4;
case '*':
case '/': return 2;
case ')': return 0;
case '(': return 9;
}
return (op);
}
Ouput:Enter infix expression
13
(a+b)
Infix=(a+b)
Prefix=+ab
4.a) C-PROGRAM TO SORT ELEMENTS USING INSERTION
SORT
#include<stdio.h>
#include<conio.h>
int n,a[10],i,j,num;
void main ()
{
clrscr();
printf("ENTER THE SIZE OF THE ARRAY\n");
scanf ("%d", &n);
printf("enter the elements\n");
for (i=0; i<n; i++)
scanf ("%d",&a[i]);
insert_sort (n, a);
printf ("the sorted array is \n");
for (i=0; i<n;i++)
printf ("%d\t",a[i]);
getch ();
}
insert_sort (int n, int a[])
{
for (i=0; i<n;i++)
{
num=a[i];
for (j=i-1; j>=0&&num<a[j]; j--)
a[j+1] =a[j];
a[j+1]=num;
}
return;
}
14
Output:
Enter the size of the array
5
Enter the elements
5 3 12 11 6
Sorted array is
3
5
6
11
12
break;
}
Qinsert()
{
if(rear=(size-1))
{
printf("stack is full\n");
return;
}
else
{
int num;
printf("enter the element to be inserted\n");
scanf("%d",&num);
q[rear++]=num;
if(front==-1) front++; return;
}}
Qdelete()
{
if(front==-1)
{
printf("no elements/q empty\n");
return;
}
else if(front==rear)
{
printf("element to be deleted is %d\n",q[front]);
front=rear=-1;
return;
}
else
{
printf("Element to be deleted=%d\n",q[front]);
front++;
return;
}
Qdisplay()
{
int i;
if(front==-1)
{
printf("no element\n");
return;
}
else
16
{
for(i=front;i>=rear;i++)
{
printf("%d\t",q[i]);
}
return;
}
OUTPUT:
QUEUE Implementation
1.Insert
2.delete
3.display
4.exit
Enter you choice
1
Enter the data
23
QUEUE Implementation
1.Insert
2.delete
3.display
4.exit
Enter your choice
3
23
QUEUE Implementation
1.Insert
2.delete
3.display
4.exit
Enter your choice
2
Deleted node=23
QUEUE Implementation
1.Insert
2.delete
17
3.display
4.exit
Enter your choice
4
default:printf("Wrong choice\n");
break;
}
printf("Do you want to continue\n");
scanf("%d",&ch1);
}}
NODE insert_front(NODE first)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(NODE));
newnode->link=NULL;
printf("Enter the data\n");
scanf("%d",&newnode->data);
if(first==0)
{
return(newnode);
}
else
{
newnode->link=first;
return(newnode);
}}
NODE insert_pos(NODE first)
{
NODE newnode,temp,temp1;
int pos=0;
newnode=(NODE)malloc(sizeof(NODE));
newnode->link=NULL;
printf("Enter the data to be stored in the node\n");
scanf("%d",&newnode->data);
printf("Enter the position where the node is to be
inserted\n");
scanf("%d",&pos);
if(pos==1 && first==0)
return(newnode);
if(pos==1)
{
newnode->link=first;
first=newnode;
return(first);
}
temp=first;
for(i=1;i<pos;i++)
{
if(temp==NULL)
20
{
printf("Invalid Position\n");
return(first);
}
temp1=temp;
temp=temp->link;
}
newnode->link=temp;
temp1->link=newnode;
return(first);
}
NODE delete_front(NODE first)
{
NODE temp;
if(first==0)
{
printf("No nodes in the list\n");
return(first);
}
temp=first;
printf("Deleted node is %d\n",temp->data);
first=first->link;
free(temp);
return(first);
}
NODE delete_pos(NODE first)
{
NODE temp,temp1;
int i,pos=0;
if(first==0)
{
printf("No nodes in the list\n");
return(first);
}
printf("Enter the position of the node to be deleted\n");
scanf("%d",&pos);
temp=first;
if(pos==1 && first->link==0)
{
printf("Deleted node=%d\n",temp->data);
free(temp);
first=0;
return(first);
}
if(pos==1)
21
{
printf("Deleted node=%d\n",first->data);
first=first->link;
free(first);
return(first);
}
temp1=0;
for(i=1;i<pos;i++)
{
temp1=temp;
temp=temp->link;
}
if(temp==0);
{
printf("Invalid position\n");
return(first);
}
printf("Deleted node =%d\n",temp->data);
temp1->link=temp->link;
free(temp);
return(first);
}
Display(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("No nodes in the list\n");
return;
}
temp=first;
while(temp!=0)
{
printf("%d=>",temp->data);
temp=temp->link;
}
return;
}
Ouput:Singly linked list operation
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
22
1
Insert implemenetation
1.Insert Front
2.Insert specified position
Enter your choice
1
Enter the data
23
Do you want to go to insert implementation again
1
1.Insert Front
2.Insert specified position
Enter your choice
1
Enter the data
34
Do you want to go to insert implementation again
1
1.Insert Front
2.Insert specified position
Enter your choice
2
Enter the data to be stored in the node
46
Enter the position where the node is to be inserted
2
Do you want to go to insert implementation again
0
Do you want to continue
1
Singly linked list operation
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
3
34=>46=>23=>Do you want to continue
0
23
{
printf("%d=>",temp->data);
temp=temp->link;
}
return;
}
Output:Stack operation
Linked list implementation
1.Push
2.Pop
3.Display
4.Exit
Enter your choice
1
Enter the data
23
Do you want to go to linked list implementation again
1
Linked list implementation
1.Push
2.Pop
3.Display
4.Exit
Enter your choice
1
Enter the data
45
Do you want to go to linked list implementation again
1
Linked list implementation
1.Push
2.Pop
3.Display
4.Exit
Enter your choice
2
Deleted node is 45
Do you want to go to linked list implementation again
1
Linked list implementation
1.Push
2.Pop
3.Display
4.Exit
Enter your choice
3
23=>Do you want to go to linked list implementation again
1
Linked list implementation
1.Push
2.Pop
3.Display
4.Exit
Enter your choice
4
26
NODE newnode,temp;
newnode=(NODE)malloc(sizeof(NODE));
newnode->link=NULL;
printf("Enter the data\n");
scanf("%d",&newnode->data);
if(first==0)
{
return(newnode);
}
temp=first;
while(temp->link!=NULL)
temp=temp->link;
temp->link=newnode;
return(first);
}
NODE delete_front(NODE first)
{
NODE temp;
if(first==0)
{
printf("No nodes in the list\n");
return(first);
}
temp=first;
printf("Deleted node is %d\n",temp->data);
first=first->link;
free(temp);
return(first);
}
Display(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("No nodes in the list\n");
return;
}
temp=first;
while(temp!=0)
{
printf("%d=>",temp->data);
temp=temp->link;
}
return;
}
28
OUTPUT:
Queue operation
Linked list implementation
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
1
Enter the data
23
Do you want to go to linked list implementation again
1
Linked list implementation
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
1
Enter the data
45
Do you want to go to linked list implementation again
1
Linked list implementation
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
2
Deleted node is 23
Do you want to go to linked list implementation again
1
Linked list implementation
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
3
45=>Do you want to go to linked list implementation
again
0
29
scanf("%d",&ch2);
}
break;
case 2:printf("Delete implemenetation\n");
ch3=1;
while(ch3==1)
{
printf("1.Delete Front\n");
printf("2.Delete Rear\n");
printf("Enter your choice\n");
scanf("%d",&choice2);
switch(choice2)
{
case 1:Delete_front(&first,&last);
break;
case 2:Delete_last(&first,&last);
break;
default:printf("Wrong choice\n");
}
printf("Do you want to go to delete implementation
again\n");
scanf("%d",&ch3);
}
break;
case 3:Display(&first,&last);
break;
case 4:exit(0);
default:printf("Wrong choice\n");
break;
}
printf("Do you want to continue\n");
scanf("%d",&ch1);
}}
Insert_front(NODE *first,NODE *last)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(NODE));
newnode->link=newnode;
printf("Enter the data\n");
scanf("%d",&newnode->data);
if(*first==0)
{
*first=*last=newnode;
return;
}
31
newnode->link=*first;
(*last)->link=newnode;
*first=newnode;
return;
}
Insert_last(NODE *first2,NODE *last2)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->link=newnode;
printf("Enter the data to be stored in the node\n");
scanf("%d",&newnode->data);
if(*first2==0)
{
*first2=*last2=newnode;
return;
}
(*last2)->link=newnode;
newnode->link=*first2;
*last2=newnode;
return;
}
Delete_front(NODE *first3,NODE *last3)
{
if(*first3==0)
{
printf("No nodes in the list\n");
return;
}
printf("Deleted node is %d\n",(*first3)->data);
if((*first3)->link==*first3)
{
free(*first3);
*first3=*last3=0;
return;
}
(*last3)->link=(*first3)->link;
free(*first3);
*first3=(*last3)->link;
return;
}
Delete_last(NODE *first4,NODE *last4)
{
NODE temp;
if(*first4==0)
32
{
printf("No nodes in the list\n");
return;
}
printf("Deleted node=%d\n",(*last4)->data);
if((*first4)->link==*first4)
{
free(*first4);
*first4=*last4=0;
return;
}
temp=*first4;
while(temp->link!=*last4)
temp=temp->link;
temp->link=*first4;
free(*last4);
*last4=temp;
return;
}
Display(NODE *first5,NODE *last5)
{
NODE temp;
if(*first5==0)
{
printf("No nodes in the list\n");
return;
}
temp=*first5;
while(temp->link!=*first5)
{
printf("%d=>",temp->data);
temp=temp->link;
}
printf("%d ",(*last5)->data);
return;}
OUTPUT:
Circular linked list operation
1.Insert
2.Delete
3.Display
4.Exit
33
#include<stdio.h>
#include<conio.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 first);
NODE Insert_end(NODE first);
NODE Delete_front(NODE first);
NODE Delete_end(NODE first);
void main()
{
clrscr();
while(ch1==1)
{
printf("Doubly linked list operation\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.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\n");
printf("2.Insert rear\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");
}
35
newnode->rlink=first;
first->llink=newnode;
first=newnode;
return(first);
}
NODE Insert_end(NODE first)
{
NODE newnode,temp;
newnode=(NODE)malloc(sizeof(NODE));
newnode->rlink=newnode->llink=0;
printf("Enter the data to be stored in the node\n");
scanf("%d",&newnode->data);
if(first==0)
return(newnode);
temp=first;
while(temp->rlink!=0)
temp=temp->rlink;
temp->rlink=newnode;
newnode->llink=temp;
return(first);
}
NODE Delete_front(NODE first)
{
NODE temp;
if(first==0)
{
printf("No nodes in the list\n");
return(first);
}
temp=first;
printf("Deleted node is %d\n",temp->data);
first=first->rlink;
first->llink=0;
free(temp);
return(first);
}
NODE Delete_end(NODE first)
{
NODE temp;
if(first==0)
{
printf("No nodes in the list\n");
return(first);
}
temp=first;
37
if(temp->rlink==0)
{
printf("Node to be deleted=%d\n",temp->data);
free(temp);
first=0;
return(first);
}
while(temp->rlink!=0)
temp=temp->rlink;
printf("Deleted node=%d\n",temp->data);
temp->llink->rlink=0;
free(temp);
return(first);
}
Display(NODE first)
{
NODE temp;
if(first==0)
{
printf("No nodes in the list\n");
return;
}
temp=first;
while(temp!=0)
{
printf("%d=>",temp->data);
temp=temp->rlink;
}
return;
}
Doubly linked list operation
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice
1
Insert implementation
1.Insert front
2.Insert rear
Enter your choice
1
Enter the data to be stored
23
38
NODE first=0,last=0;
int ch=1,choice,ch1=1,choice1,ch2=1,choice2;
void main()
{
clrscr();
while(ch==1)
{
printf("Doubly Linked List implementation \n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Insert implementataion\n");
while(ch1==1)
{
printf("1.insert front\n2.insert position\n");
printf("Enter ur choice\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:insert_front(&first,&last);
break;
case 2:insert_pos(&first,&last);
break;
default:printf("wrong choice\n");
}
printf("Do u want to go to insert implementation
again\n");
scanf("%d",&ch1);
}
break;
case 2:printf("Delete implementataion\n");
while(ch2==1)
{
printf("1.Delete front\n2.Delete position\n");
printf("Enter ur choice\n");
scanf("%d",&choice2);
switch(choice2)
{
case 1:delete_front(&first,&last);
break;
case 2:delete_pos(&first,&last);
break;
default:printf("wrong choice\n");
40
}
printf("Do u want to go to delete implementation
again\n");
scanf("%d",&ch2);
}
break;
case 3:display(&first,&last);
break;
case 4:exit(0);
}
printf("Do u want to continue\n");
scanf("%d",&ch);
}}
insert_front(NODE *first,NODE *last)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->llink=newnode->rlink=newnode;
printf("Enter the data to be stored\n");
scanf("%d",&newnode->data);
if(*first==0)
{
*first=newnode;
*last=newnode;
return;
}
newnode->rlink=*first;
(*first)->llink=newnode;
(*last)->rlink=newnode;
newnode->llink=*last;
*first=newnode;
return;
}
insert_pos(NODE *first,NODE *last)
{
NODE newnode,prev,temp;
int pos,i;
newnode=(NODE)malloc(sizeof(struct node));
newnode->rlink=newnode->llink=newnode;
printf("Enter the data to be stored\n");
scanf("%d",&newnode->data);
printf("Enter the position node to be stored\n");
scanf("%d",&pos);
if(pos==1&&*first==0)
41
{
*first=*last=newnode;
return;
}
if(pos==1)
{
newnode->rlink=*first;
(*first)->llink=newnode;
newnode->llink=*last;
(*last)->rlink=newnode;
*first=newnode;
return;
}
temp=*first;
for(i=1;i<pos;i++)
{
temp=temp->rlink;
if(temp==*first)
{
printf("invalid posititon\n");
return;
}
}
prev=temp->llink;
prev->rlink=newnode;
newnode->llink=prev;
newnode->rlink=temp;
temp->llink=newnode;
return;
}
delete_front(NODE *first,NODE *last)
{
NODE temp;
if(*first==0)
{
printf("No nodes in the list\n");
return;
}
if((*first)->rlink==*first)
{
printf("Node to be deleted=%d\n",(*first)->data);
free(*first);
*first=*last=0;
return;
}
42
temp=(*first)->rlink;
(*last)->rlink=*last;
printf("Node to be deleted=%d\n",(*first)->data);
free(*first);
*first=temp;
return;
}
delete_pos(NODE *first,NODE *last)
{
NODE temp,prev;
int pos,i;
if(*first==0)
{
printf("No nodes in the list\n");
return;
}
printf("Enter the postion where node to be deleted\n");
scanf("%d",&pos);
if(pos==1 && (*first)->rlink==*first)
{
printf("Node to be deleted=%d\n",(*first)->data);
free(*first);
*first=*last=0;
return;
}
if(pos==1)
{
temp=(*first)->rlink;
printf("Node to be deleted=%d\n",(*first)->data);
(*last)->rlink=temp;
temp->llink=*last;
free(*first);
*first=temp;
return;
}
temp=*first;
for(i=1;i<pos;i++)
{
temp=temp->rlink;
if(temp==*first)
{
printf("Invalid position\n");
return;
}
43
prev=temp->llink;
printf("Node to be deleted=%d\n",temp->data);
prev->rlink=temp->rlink;
temp->rlink->llink=prev;
free(temp);
return;
}
}
display(NODE *first,NODE *last)
{
NODE temp;
if(*first==0)
{
printf("No nodes in the list\n");
return;
}
temp=*first;
while(temp!=*last)
{
printf("%d=>",temp->data);
temp=temp->rlink;
}
printf("%d=>",(*last)->data);
}
Output:Doubly Linked List implementation
1.Insert
2.Delete
3.Display
4.Exit
Enter ur choice
1
Insert implementataion
1.insert front
2.insert position
Enter ur choice
2
Enter the data to be stored
23
Enter the position node to be stored
1
Do u want to go to insert implementation again
1
1.insert front
2.insert position
44
Enter ur choice
2
Enter the data to be stored
23
Enter the position node to be stored
3
invalid posititon
Do u want to go to insert implementation again
0
Do u want to continue
1
Doubly Linked List implementation
1.Insert
2.Delete
3.Display
4.Exit
Enter ur choice
2
Delete implementataion
1.Delete front
2.Delete position
Enter ur choice
2
Enter the postion where node to be deleted
1
Node to be deleted=23
Do u want to go to delete implementation again
1
1.Delete front
2.Delete position
Enter ur choice
1
No nodes in the list
Do u want to go to delete implementation again
0
Do u want to continue
0
45
void main()
{
clrscr();
while(ch==1)
{
printf("Doubly Linked List implementation \n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Insert implementataion\n");
while(ch1==1)
{
printf("1.Insert front\n2.Insert position\n");
printf("Enter ur choice\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:insert_front(&first,&last);
break;
case 2:insert_pos(&first,&last);
break;
default:printf("wrong choice\n");
}
printf("Do u want to go to insert implementation
again\n");
scanf("%d",&ch1);
}
break;
case 2:printf("Delete implementataion\n");
while(ch2==1)
{
printf("1.Delete front\n2.Delete position\n");
printf("Enter ur choice\n");
scanf("%d",&choice2);
switch(choice2)
{
case 1:delete_front(&first,&last);
break;
case 2:delete_pos(&first,&last);
break;
default:printf("wrong choice\n");
}
47
*first=*last=newnode;
return;
}
if(pos==1)
{
newnode->rlink=*first;
(*first)->llink=newnode;
newnode->llink=*last;
(*last)->rlink=newnode;
*first=newnode;
return;
}
temp=*first;
for(i=1;i<pos;i++)
{
temp=temp->rlink;
if(temp==*first)
{
printf("Invalid posititon\n");
return;
}
}
prev=temp->llink;
prev->rlink=newnode;
newnode->llink=prev;
newnode->rlink=temp;
temp->llink=newnode;
return;
}
delete_front(NODE *first,NODE *last)
{
NODE temp;
if(*first==0)
{
printf("No nodes in the list\n");
return;
}
if((*first)->rlink==*first)
{
printf("Node to be deleted=%d\n",(*first)->data);
free(*first);
*first=*last=0;
return;
}
temp=(*first)->rlink;
49
(*last)->rlink=*last;
printf("Node to be deleted=%d\n",(*first)->data);
free(*first);
*first=temp;
return;
}
delete_pos(NODE *first,NODE *last)
{
NODE temp,prev;
int pos,i;
if(*first==0)
{
printf("No nodes in the list\n");
return;
}
printf("Enter the postion where node to be deleted\n");
scanf("%d",&pos);
if(pos==1 && (*first)->rlink==*first)
{
printf("Node to be deleted=%d\n",(*first)->data);
free(*first);
*first=*last=0;
return;
}
if(pos==1)
{
temp=(*first)->rlink;
printf("Node to be deleted=%d\n",(*first)->data);
(*last)->rlink=temp;
temp->llink=*last;
free(*first);
*first=temp;
return;
}
temp=*first;
for(i=1;i<pos;i++)
{
temp=temp->rlink;
if(temp==*first)
{
printf("Invalid position\n");
return;
}
prev=temp->llink;
50
printf("Node to be deleted=%d\n",temp->data);
prev->rlink=temp->rlink;
temp->rlink->llink=prev;
free(temp);
return;
}
}
display(NODE *first,NODE *last)
{
NODE temp;
if(*first==0)
{
printf("No nodse in the list\n");
return;
}
temp=*first;
while(temp!=*last)
{
printf("%d=>",temp->data);
temp=temp->rlink;
}
printf("%d=>",(*last)->data);
}
Output:Doubly Linked List implementation
1.Insert
2.Delete
3.Display
4.Exit
Enter ur choice
1
Insert implementataion
1.Insert front
2.Insert position
Enter ur choice
1
Enter the data to be stored
34
Do u want to go to insert implementation again
1
1.Insert front
2.Insert position
Enter ur choice
1
Enter the data to be stored
51
23
Do u want to go to insert implementation again
1
1.Insert front
2.Insert position
Enter ur choice
2
Enter the data to be stored
34
Enter the position node to be stored
2
Do u want to go to insert implementation again
0
Do u want to continue
1
Doubly Linked List implementation
1.Insert
2.Delete
3.Display
4.Exit
Enter ur choice
3
23=>34=>34=>Do u want to continue
1
Doubly Linked List implementation
1.Insert
2.Delete
3.Display
4.Exit
Enter ur choice
2
Delete implementataion
1.Delete front
2.Delete position
Enter ur choice
2
Enter the postion where node to be deleted
2
Node to be deleted=34
Do u want to go to delete implementation again
0
Do u want to continue
1
Doubly Linked List implementation
1.Insert
52
2.Delete
3.Display
4.Exit
Enter ur choice
3
23=>34=>Do u want to continue
0
12. C-PROGRAM TO CREATE A BINARY TREE
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *lchild;
struct node *rchild;
};
typedef struct node *NODE;
NODE root=0;
void inorder(NODE root4);
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("Inorder traversal\n");
inorder(root);
getch();
}
create(NODE *root1)
{
NODE temp,temp1;
if(is_lchild(&(*root1)))
{
(*root1)->lchild=(NODE)malloc(sizeof(struct node));
temp=(*root1)->lchild;
printf("Enter the data for left child\n");
scanf("%d",&temp->data);
create(&temp);
}
53
else
(*root1)->lchild=0;
if(is_rchild(&(*root1)))
{
(*root1)->rchild=(NODE)malloc(sizeof(struct node));
temp1=(*root1)->rchild;
printf("Enter the data for right child\n");
scanf("%d",&temp1->data);
create(&temp1);
}
else
(*root1)->rchild=0;
return;
}
is_lchild(NODE *root2)
{
int ch;
printf("Do you want to create lchild of(%d),type 1 means yes;0
means no\n",(*root2)->data);
scanf("%d",&ch);
if(ch==1)
return(1);
else
return(0);
}
is_rchild(NODE *root3)
{
int ch;
printf("Do you want to create rchild of(%d),type 1 means yes;0
means no\n",(*root3)->data);
scanf("%d",&ch);
if(ch==1)
return(1);
else
return(0);
}
void inorder(NODE root4)
{
if(root4!=0)
{
inorder(root4->lchild);
printf("%d=>",root4->data);
inorder(root4->rchild);
}}
54
OUTPUT:
Create tree
Enter the data for root node
23
Do you want to create lchild of(23),type 1 means yes;0 means no
1
Enter the data for left child
45
Do you want to create lchild of(45),type 1 means yes;0 means no
1
Enter the data for left child
10
Do you want to create lchild of(10),type 1 means yes;0 means no
0
Do you want to create rchild of(10),type 1 means yes;0 means no
0
Do you want to create rchild of(45),type 1 means yes;0 means no
1
Enter the data for right child
23
Do you want to create lchild of(23),type 1 means yes;0 means no
0
Do you want to create rchild of(23),type 1 means yes;0 means no
0
Do you want to create rchild of(23),type 1 means yes;0 means no
0
Inorder traversal
10=>45=>23=>23=>
55
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *lchild;
struct node *rchild;
};
typedef struct node *NODE;
NODE root=0;
void preorder(NODE root4);
void inorder(NODE root5);
void postorder(NODE root6);
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("Preorder traversal\n");
preorder(root);
printf("\nInorder traversal\n");
inorder(root);
printf("\nPostorder traversal\n");
postorder(root);
getch();
}
create(NODE *root1)
{
NODE temp,temp1;
if(is_lchild(&(*root1)))
{
56
(*root1)->lchild=(NODE)malloc(sizeof(struct node));
temp=(*root1)->lchild;
printf("Enter the data for left child\n");
scanf("%d",&temp->data);
create(&temp);
}
else
(*root1)->lchild=0;
if(is_rchild(&(*root1)))
{
(*root1)->rchild=(NODE)malloc(sizeof(struct node));
temp1=(*root1)->rchild;
printf("Enter the data for right child\n");
scanf("%d",&temp1->data);
create(&temp1);
}
else
(*root1)->rchild=0;
return;
}
is_lchild(NODE *root2)
{
int ch;
printf("Do you want to create lchild of (%d),type 1 means yes;0
means no\n",(*root2)->data);
scanf("%d",&ch);
if(ch==1)
return(1);
else
return(0);
}
is_rchild(NODE *root3)
{
int ch1;
printf("Do you want to create rchild of (%d),type 1 means
yes;0 means no\n",(*root3)->data);
scanf("%d",&ch1);
if(ch1==1)
return(1);
else
return(0);
}
void preorder(NODE root4)
{
if(root4!=0)
57
{
printf("%d=>",root4->data);
preorder(root4->lchild);
preorder(root4->rchild);
}}
void inorder(NODE root5)
{
if(root5!=0)
{
inorder(root5->lchild);
printf("%d=>",root5->data);
inorder(root5->rchild);
}}
void postorder(NODE root6)
{
if(root6!=0)
{
postorder(root6->lchild);
postorder(root6->rchild);
printf("%d=>",root6->data);
}}
OUTPUT:Create tree
Enter the data for root node
2
Do you want to create lchild of (2),type 1 means yes;0
means no
1
Enter the data for left child
4
Do you want to create lchild of (4),type 1 means yes;0
means no
0
Do you want to create rchild of (4),type 1 means yes;0
means no
0
Do you want to create rchild of (2),type 1 means yes;0
means no
1
Enter the data for right child
56
Do you want to create lchild of (56),type 1 means yes;0
means no
0
58
int i=1,num,req;
clrscr();
printf("ENTER THE NUMBER OF NODES TO BE CREATED\N");
scanf("%d",&req);
while(i++<=req)
{
printf("Enetr the data\n");
scanf("%d",&num);
insert(&root,num);
}
printf("inorder traversal before deletion\n");
inorder(root);
node_delete(&root);
printf("inorder traversal after deletion\n");
inorder(root);
getch();
}
void insert(NODE *root1,int num)
{
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);
}
}
void node_delete(NODE *root2)
{
NODE x,parent,xsucc;
int num1,found,item;
if(*root2==0)
{
printf("true is empty\n");
return;
}
parent=x=0;
printf("Enter the node to be deleted\n");
60
scanf("%d",&num1);
search(&(*root2), &parent, &x, &found,num1);
if(found==FALSE)
{
printf("%d to be deleted is not found\n",num1);
return;
}
printf("%d to be deleted is found\n",num1);
if(x->lchild!=0 && x->rchild!=0)
{
parent=x;
xsucc=x->rchild;
while(xsucc->lchild!=0)
{
parent=xsucc;
xsucc=xsucc->lchild;
}
x->data=xsucc->data;
x=xsucc;
}
if(x->lchild!=0&&x->rchild==0)
{
if(parent->lchild==x)
parent->lchild=x->lchild;
else
parent->rchild=x->lchild;
free(x);
return;
}
if(x->lchild==0 && x->rchild!=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->lchild==x)
parent->lchild=0;
else
parent->rchild=0;
61
free(x);
return;
}
}
search(NODE *root3,NODE *par,NODE *x,int *found,int num2)
{
NODE q;
*found=FALSE;
*par=0;
q=*root3;
while(q!=0)
{
if(q->data==num2)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(num2<q->data)
q=q->lchild;
else
q=q->rchild;
}
return;
}
void inorder(NODE root4)
{
if(root4!=0)
{
inorder(root4->lchild);
printf("%d=>",root4->data);
inorder(root4->rchild);
}
}
Output:ENTER THE NUMBER OF NODES TO BE CREATEDN3
Enetr the data
34
Enetr the data
45
Enetr the data
56
62
#include<conio.h>
#include<math.h>
struct node
{
char data;
struct node *lchild;
struct node *rchild;
};
typedef struct node *NODE;
NODE root=0;
NODE create_tree(char postfix[20]);
float eval_post(NODE root);
void main()
{
char postfix[20];
float result;
clrscr();
printf("Enter the postfix expression\n");
scanf("%s",postfix);
root=create_tree(postfix);
result=eval_post(root);
printf("result=%f",result);
getch();
}
NODE create_tree(char postfix[20])
{
NODE temp,stack[20];
char symbol;
int i,j=0;
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;
else
{
temp->rchild=stack[--j];
temp->lchild=stack[--j];
stack[j++]=temp;
}}
return(stack[--j]);
}
float eval_post(NODE root)
64
{
float num;
switch(root->data)
{
case '+':return eval_post(root->lchild)+eval_post(root->rchild);
case '-':return eval_post(root->lchild)-eval_post(root->rchild);
case '*':return eval_post(root->lchild)*eval_post(root->rchild);
case '/':return eval_post(root->lchild)/eval_post(root->rchild);
case '^':return pow(eval_post(root->lchild),eval_post(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);
}
}
OUTPUT:
Enter the postfix expression
ab+
Enter the value of a
4
Enter the value of b
3
result=7.000000
65
case 3:exit(0);
default:printf("Wrong choice\n");
break;
}}}
NODE create(int item,NODE head)
{
NODE curptr,ptr;
if(head->left==NULL)
{
insert_left(item,head);
return head;
}
curptr=head->left;
while(curptr!=head)
{
ptr=curptr;
if(item<curptr->data)
{
if(curptr->left!=NULL)
curptr=curptr->left;
else
break;
}
else
{
if(item>curptr->data)
{
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;
}
void insert_left(int item,NODE ptr)
67
{
NODE temp,newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->left=NULL;
newnode->data=item;
ptr->left=newnode;
newnode->right=ptr;
newnode->RT=1;
}
void insert_right(int item,NODE ptr)
{
NODE temp,newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->left=NULL;
newnode->data=item;
temp=ptr->right;
ptr->right=newnode;
ptr->RT=0;
newnode->right=temp;
newnode->RT=1;
}
void inorder(NODE head)
{
NODE temp;
if(head->left==NULL)
{
printf("No nodes in the list\n");
return;
}
temp=head;
while(1)
{
temp=inorder_successor(temp);
if(temp==head)
return;
printf("%d\n",temp->data);
}}
NODE inorder_successor(NODE ptr)
{
NODE temp;
temp=ptr->right;
if(ptr->RT==1)
return temp;
while(temp->left!=NULL)
temp=temp->left;
68
return(temp);
}
Output:1.Create
2.Inorder
3.Exit
Enter the
1
enter the
3
Enter the
34
Enter the
45
Enter the
47
1.Create
2.Inorder
3.Exit
Enter the
2
34
45
47
1.Create
2.Inorder
3.Exit
Enter the
3
choice
number of nodes to be created
1 data
2 data
3 data
choice
choice
69
70