Print DS

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 65

Slip 1 : write a c program to reverse a string using static implementation of stack

/*write a c program to reverse a string using static implementation


of stack*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,len,top;
char str[50],s[50];
clrscr();
printf("\nenter the string:-");
gets(str);
len=strlen(str);
printf("\nthe length of string is :-%d",len);
for(top=0;top<len;top++)
{
s[top]=str[top];
}
top--;
printf("\nthe reverse string is:-");
for(i=top;i>=0;i--)
{
printf("%c",s[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
int eval(int [],int,int);
Slip 2: ‘C’ program to evaluate a given polynomial using function

void main()
{
int a[10],n,x,i,e;
clrscr();
printf(“\n\nENTER THE DEGREE OF POLYNOMIAL\n\n”);
scanf(“%d”,&n);
printf(“\n\nENTER THE CO-EFFICIENT OF POLYNOMIAL\n\n”);
for(i=n;i>=0;i–)
{
printf(“\n\nCO-EFFICIENT OF A[%d]:-\t “,i);
scanf(“%d”,&a[i]);
}
printf(“\n\nENTERED POLYNOMAIL IS \n\n”);
for(i=n;i>0;i–)
{
if(a[i]!=0)
{
printf(“%dX^%d+”,a[i],i);
}
}
printf(“%d”,a[i]);
printf(“\n\nENTER THE VALUE FOR X”);
scanf(“%d”,&x);
e=eval(a,n,x);
printf(“\nEvaluation of Poly is \t %d”,e);
getch();
}
int eval(int a[],int n,int x)
{
int sum=0,i;
for(i=n;i>=0;i–)
{
sum=sum+a[i]*pow(x,i);
}
return sum;
}
Slip 3: ‘C’ program for implementing Linear Search method using function.

#include<stdio.h>
#include<conio.h>
#define MAX 20
int lsearch(int [],int,int);
void main()
{
int a[MAX],k,i,n,m;
clrscr();
printf(“\n\nENTER THE RANGE VALUE”);
scanf(“%d”,&n);
if(n>MAX)
{
printf(“\n\nINVALIED INDEX\n”);
}
else
{
printf(“\n\nENTER THE ARRAY ELEMENTS\n\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“\n\nENTER THE ELEMENT FOR SEARCH\n”);
scanf(“%d”,&k);
m=lsearch(a,n,k);
if(m==-1)
{
printf(“\n\nELEMENT IS NOT FOUND IN LIST”);
}
else
{
printf(“\n\nELEMENT IS FOUND AT LOCATION %d”,m);
}
}
getch();
}

int lsearch(int a[],int n,int k)


{
int i;
for(i=0;i<n;i++)
{
if(a[i]==k)
{
return i;
}
}
if(i==n)
{
return -1;
}
}
Slip 5:C’ program for addition of two polynomials using array.

void main()
{
int a[27],b[27],c[54],m,n,i,j,z,y=0,t,s=0;

printf ("How many terms you want to add in the 1st polynomial ?? : ");
scanf ("%d",&n);

printf ("Enter 1st polynomial : \n");


for (i=0;i<n*3;i=i+3)
{
printf ("Enter coefficient : ");
scanf("%d",&a[i]);
printf ("Enter power of x : ");
scanf("%d",&a[i+1]);
printf ("Enter power of y : ");
scanf("%d",&a[i+2]);
}
printf ("1st polynomial is : ");
for (i=0;i<n*3;i=i+3)
{
printf ("(%dx^%dy^%d) + ",a[i],a[i+1],a[i+2]);

}
printf (" 0 \n");
printf ("How many terms you want to add in the 2nd polynomial ?? : ");
scanf ("%d",&m);

printf ("Enter 2nd polynomial : \n");


for (i=0;i<m*3;i=i+3)
{
printf ("Enter coefficient : ");
scanf("%d",&b[i]);
printf ("Enter power of x : ");
scanf("%d",&b[i+1]);
printf ("Enter power of y : ");
scanf("%d",&b[i+2]);
}
printf ("2nd polynomial is : ");
for (i=0;i<m*3;i=i+3)
{
printf ("(%dx^%dy^%d) + ",b[i],b[i+1],b[i+2]);

}
printf (" 0\n");

printf ("Enter 1 to add : ");


scanf("%d",&z);
switch (z)
{
case 1:
for (i=0;i<n*3;i++)
{c[i]=a[i];}
for (i=n*3,j=0;i<(n+m)*3,j<m*3;i++,j++)
{c[i]=b[j];}
for (i=0;i<(m+n)*3;i=i+3)
{
printf ("(%dx^%dy^%d) + ",c[i],c[i+1],c[i+2]);

}
printf (" 0\n");
}

for (i=1;i<(m+n)*3;i=i+3)
{
for (j=4;j<(m+n)*3;j=j+3)
{
if (c[i]==c[j])
{ if(c[i+1]==c[j+1])
{
c[i-1]=c[i-1]+c[j-1];
c[j-1]=0;

}
}
}
}
printf ("ADDITION \n");
for (i=0;i<(m+n)*3;i=i+3)
if (c[i]!=0)
{
printf ("(%dx^%dy^%d) + ",c[i],c[i+1],c[i+2]);

}
else
printf (" ");
printf (" 0 \n");
}

6.‘C’ program to search given elements into the list using Non-Recursive Binary Search
Method.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, n, key, low, high, mid;
clrscr();
printf(“Enter the array elements in ascending order”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the key element\n”);
scanf(“%d”, &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf(“The key element is found at location %d”, mid + 1);
else
printf(“the key element is not found”);
getch();
}

7.‘C’ program to search given elements into the list using Recursive Binary Search Method.

#include<stdio.h>
#include<conio.h>
#define MAX 20
int bsearch(int [],int,int,int);
void main()
{
int a[10],i,t,f=0,n,l,k;
int bsearch(int[],int,int,int);
clrscr();
printf(“\n\nENTER THE SIZE OF AN ARRAY”);
scanf(“%d”,&n);
if (n>MAX)
{
printf(“\n\nINVALIED INDEX \n\n”);
}
else
{
printf(“\n\nENTER THE ARRAY ELEMENTS\n\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“\n\nENTER THE ELEMENT FOR SEARCH”);
scanf(“%d”,&t);
l=n-1;
k=bsearch(a,f,l,t);
if(k==-1)
{
printf(“\n\nELEMENT IS NOT FOUND\n”);
}
else
{
printf(“\n\nELEMENT IS FOUND AT LOCATION =%d”,k);
}
}
getch();
}
int bsearch(int list[],int f,int l,int t)
{
int m;
if(f<=l)
{
m=(f+l)/2;
if(list[m]==t)
return m;
else if(t<list[m])
bsearch(list,f,m-1,t);
else if(t>list[m])
bsearch(list,m+1,l,t);
}
else
{
return(-1);
}

}
#include<stdio.h>
#include<conio.h>

typedef struct node


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

node *g[20];
int n,visited[20];
int indegree(int i);
int outdegree(int i);
void dfs(int i);

void insert(int vi,int vj)


{
node *p,*q;
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;

if(g[vi]==NULL)
g[vi]=q;
else
{
p=g[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}

void readgraph()
{
int vi,vj,i,j,k,no_of_edges;
for(i=0;i<n;i++)
g[i]=NULL;
printf("\nEnter the no. of Vertices::");
scanf("%d",&n);
printf("\nEnter the no of Edges::");
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("\nEnter the Edge(u,v)::");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
8.‘C’ program to count indegree and outdegree of each node in graph.
void main()
{
int i,j,k;
clrscr();
readgraph();
for(i=0;i<n;i++)
visited[i]=0;

/* printf("\n=====================================================");
printf("\nNode\tIndegree\tOutdegree");
printf("\n=====================================================");
for(i=0;i<n;i++)
{
j=indegree(i);
k=outdegree(i);
printf("\n%2d\t%4d\t\t%5d",i,j,k);
}
printf("\n-----------------------------------------------------");
*/
dfs(0);
getch();
}

int outdegree(int i)
{
int j=0;
node *p;
p=g[i];
while(p!=NULL)
{
p=p->next;
j++;
}
return(j);
}

int indegree(int v)
{
int i,j=0,k;
node *p;
for(i=0;i<n;i++)
{
p=g[i];
while(p!=NULL)
{
if(p->vertex==v)
j++;
p=p->next;
}
}
return(j);
}

void dfs(int i)
{
node *p;
p=g[i];
visited[i]=1;
printf("\nVisit->%d",i);
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
dfs(i);
p=p->next;
}
}

9.‘C’ program to create Circular Singly Link list and display it.

#include<stdio.h>
#include<conio.h>

struct circular
{
int i;
struct circular *next;
};

struct circular *temp;


struct circular *head;
struct circular *p;
struct circular *mid;
struct circular *move;

int cnt=0;

void create(void);
void insert(void);
void display(void);
void del(void);

void main()
{
int ch=0;
clrscr();
while(ch!=5)
{
printf("\n1.CREATE");
printf("\n2.INSERT");
printf("\n3.DELETE");
printf("\n4.DISPLAY");
printf("\n5.EXIT");
scanf("%d",&ch);

if(ch==1)
{
create();
cnt++;
cnt++;
}

if(ch==2)
{
insert();
cnt++;
}
if(ch==3)
{
del();
cnt--;
}

if(ch==4)
{
display();
}

if(ch==5)
{
break;
}
}
getch();
}
void create()
{
head=(struct circular *)malloc(sizeof(struct circular));
head->next=head;
printf("ENETER THE DATA");
scanf("%d",&head->i);
temp=head;

temp->next=(struct circular *)malloc(sizeof(struct circular));


temp=temp->next;
temp->next=head;
printf("ENETER THE DATA");
scanf("%d",&temp->i);

}
void insert()
{
int add,t;

printf("\n\t ENTER ANY NUMBER BETWEEN 1 AND %d",cnt);


scanf("%d",&add);
p=head;
t=1;
while(t<add)
{
p=p->next;
t++;
}
printf("%d",p->i);
clrscr();
mid=(struct circular *)malloc(sizeof(struct circular));
printf("ENETER THE DATA");
scanf("%d",&mid->i);
mid->next=p->next;
p->next=mid;
}

void display()
{
p=head;
printf("%d-->",p->i);
p=p->next;
while(p!=head)
{
printf("%d-->",p->i);
p=p->next;
}
}

void del(void)
{
int add,t;

printf("\n\t ENTER ANY NUMBER BETWEEN 1 AND %d",cnt);


scanf("%d",&add);
p=head;
t=1;
while(t<add-1)
{
p=p->next;
t++;
}
printf("%d",p->i);
clrscr();
mid=p->next;
p->next=mid->next;

}
10.‘C’ program to sort elements of a singly linked list in ascending order and display the
sorted List.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct NUM

{ int data;
struct NUM *link;

}num;
num *createnode()
{
num *nn=NULL;
nn=(num*)malloc(sizeof(num));
if(nn==NULL)
{
printf(“\n insufficient memory”);
exit(0);
}//end if
return(nn) ;
} //end createnode

num *create()
{ num *hn=NULL ,*cn=NULL ,*nn=NULL;
int i,n;
printf(“\n enter no of node “);
scanf(“%d”,&n);
printf(“\n enter data\t”);
for(i=0;i<n;i++)
{
nn=createnode();
scanf(“%d”,&nn->data);
nn->link=NULL;
if(hn==NULL)
{
hn=nn;
cn=nn;
}//end if
else
{
cn->link=nn;
cn=nn;
}
}//end for
return(hn);
}//end create
void display(num *hn)
{
num *cn=NULL;
num *temp,*j,*i;
printf(“\n original data present in link list is “);
for(cn=hn;cn!=NULL;cn=cn->link)
{
printf(” %d”,cn->data);
}//end for
/*following code counts the nonzero,even,odd*/
for(j=hn;j->link!=NULL;j=j->link)
{
for(i=hn;i->link!=NULL;i=i->link)
{
if(i->data > i->link->data)
{
temp->data=i->data;
i->data=i->link->data;
i->link->data=temp->data;

}
}
}

printf(“\n data in ascending order is “);


for(cn=hn;cn!=NULL;cn=cn->link)
{
printf(” %d”,cn->data);
}
}//end display
void main()
{
num *hn,*nn,*pn;
int ch;
clrscr();
hn= create();
display(hn);

getch();
} //end main

11.‘C’ program to sort the element using Quick sort (recursive) method.

#include<stdio.h>

void quicksort(int [10],int,int);

int main(){
int x[20],size,i;

printf("Enter size of the array: ");


scanf("%d",&size);

printf("Enter %d elements: ",size);


for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);

printf("Sorted elements: ");


for(i=0;i<size;i++)
printf(" %d",x[i]);

return 0;
}

void quicksort(int x[10],int first,int last){


int pivot,j,temp,i;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}

12.‘C’ program to read a postfix expression, evaluate it and display the result.

#include<stdio.h>
#include<conio.h>
#include<math.h>
char st[100];
int top=-1;
void push(char c)
{
top++;
st[top]=c;
}
char pop()
{
char c;
c=st[top];
top–;
return c;
}
int main()
{
int i,j,p,a,b,temp;
char s1[30];
clrscr();
printf(“\nEnter the Postfix Expr”);
gets(s1);
j=0;
for(i=0;s1[i]!=”;i++)
{
if(s1[i]<=’9′ && s1[i]>=’0′)
push(s1[i]-48);
else
{
a=pop();
b=pop();
switch(s1[i])
{
case ‘+’:
temp=b+a;
break;
case ‘-‘:
temp=b-a;
break;
case ‘/’: temp=b/a;
break;
case ‘*’:
temp=b*a;
break;
case ‘^’:
temp=pow(b,a);
break;
case ‘%’:
temp=b%a;
break;
}
push(temp);
}
}
j=pop();
printf(“Result is %d “,j);
getch();
return;
}
13.‘C’ program to create two singly linked lists and concatenate one list at the end of
another list.

#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
};
typedef struct list NODE;
NODE *create(NODE *);
void disp(NODE *);
NODE *concat(NODE *,NODE *);
void main()
{
NODE *l1=NULL,*l2=NULL;
clrscr();
l1=create(l1);
printf(“\n\nFIrst Created List Is :\t”);
disp(l1);
l2=create(l2);
printf(“\n\nSecond Created List Is :\t”);
disp(l2);
l1=concat(l1,l2);
printf(“\nconcatenated :”);
disp(l1);
getch();
}
NODE *create(NODE *l1)
{
NODE *tmp,*q;
int n,m,i;
printf(“\n\nHOW MANY NODES U WANT”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the Data”);
scanf(“%d”,&m);
tmp=(NODE *)malloc(sizeof(NODE));
tmp->data=m;
tmp->link=NULL;
if(l1==NULL)
{
l1=tmp;
}
else
{
q=l1;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
return l1;
}
void disp(NODE *l)
{
NODE *q;
if(l==NULL)
{
printf(“\nLIST IS EMPTY”);
}
else
{
q=l;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
printf(“NULL”);
}
}
NODE *concat(NODE *l1,NODE *l2)
{
NODE *q,*p,*tmp;
q=l1;
p=l2;
while(q->link!=NULL)
{
q=q->link;
}
while(p!=NULL)
{
tmp=(NODE *)malloc(sizeof(NODE));
tmp->data=p->data;
tmp->link=NULL;
q->link=tmp;
q=tmp;
p=p->link;
}
return(l1);
}
14.Program to count leaf nodes in a binary tree
#include <stdio.h>
#include <stdlib.h>

/* A binary tree node has data, pointer to left child


and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};

/* Function to get the count of leaf nodes in a binary tree*/


unsigned int getLeafCount(struct node* node)
{
if(node == NULL)
return 0;
if(node->left == NULL && node->right==NULL)
return 1;
else
return getLeafCount(node->left)+
getLeafCount(node->right);
}

/* Helper function that allocates a new node with the


given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

/*Driver program to test above functions*/


int main()
{
/*create a tree*/
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);

/*get leaf count of the above created tree*/


printf("Leaf count of the tree is %d", getLeafCount(root));

getchar();
return 0;
}

15.‘C’ program to count all non-zero elements, odd numbers and even numbers in the
singly linked list.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void main()
{
int ch,n,i,m,a,pos;
clrscr();
start=NULL;
do
{
printf(“\n\nMENU\n\n”);
printf(“\n1.CREATE\n”);
printf(“\n2.DISPLAY\n”);
printf(“\n3.COUNT\n”);
printf(“\n4.EXIT\n”);
printf(“\nENTER UR CHOICE\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n\nHOW MANY NODES U WANT TO CREATE\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nENTER THE DATA”);
scanf(“%d”,&m);
create(m);
}
break;
case 3:
count();
break;
case 2:
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
void count()
{
struct node *q;
int nonz=0,eno=0,ono=0;
q=start;
while(q!=NULL)
{
if(q->data>0)
{
nonz++;
}
if(q->data%2==0)
{
eno++;
}
else
{
ono++;
}

q=q->link;
}
printf(“\n\nPOSITVE NO ARE %d EVEN NO ARE %d ODD NO ARE %d”,nonz,eno,ono);
}

void create(int data)


{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=data;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
void disp()
{
struct node *q;
if(start==NULL)
{
printf(“\n\nLIST IS EMPTY”);
}
else
{
q=start;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
printf(“NULL”);
}
}

16.‘C’ program to swap mth and nth element of singly linked list.
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start=NULL;
void creat(int);
void swap();
void disp();
void main()
{
int ch,i,n,m;
clrscr();
do
{
printf(“\n1.create”);
printf(“\n2.display”);
printf(“\n3.Swap”);
printf(“\n4.exit”);
printf(“\nenter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\nHow many nodes”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the data”);
scanf(“%d”,&m);
creat(m);
}
break;
case 2:
disp();
break;
case 4:
exit(0);
case 3:
swap();
break;
}
}
while(ch!=4);
getch();
}
void creat(int m)
{
struct list *tmp,*q;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
void disp()
{
struct list *q;
if(start==NULL)
{
printf(“list is empty”);
}
else
{
q=start;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
}
}
void swap()
{
int m,n,i,tmp;
struct list *q,*ptr,*ptr1;
printf(“\nEnter the mth and nth position”);
scanf(“%d%d”,&m,&n);
for(i=1,ptr=start;i<m && ptr!=NULL;ptr=ptr->link,i++);
for(i=1,ptr1=start;i<n && ptr1!=NULL;ptr1=ptr1->link,i++);
if(ptr!=NULL && ptr1!=NULL)
{
tmp=ptr->data;
ptr->data=ptr1->data;
ptr1->data=tmp;
}
else
{
printf(“\nPosition Not Found”);
}
}

C Program to Print the Alternate Nodes in a Linked List using Recursion


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

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

void generate(struct node **);


void display(struct node *);
void delete(struct node **);

int main()
{
struct node *head = NULL;

generate(&head);
printf("\nDisplaying the alternate nodes\n");
display(head);
delete(&head);

return 0;
}

void display(struct node *head)


{
static flag = 0;
if(head != NULL)
{
if (!(flag % 2))
{
printf("%d ", head->a);
}
flag++;
display(head->next);
}
}

void generate(struct node **head)


{
int num, i;
struct node *temp;

printf("Enter length of list: ");


scanf("%d", &num);
for (i = num; i > 0; i--)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}

void delete(struct node **head)


{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
17. Write a ‘C’ program using data structure to
accept the details of employees from user and
display it on the screen using Dynamic Memory Allocation.*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int id;
char name[10],city[10];
struct node *next;
};
struct node *list=NULL;
void create()
{
int n,i;
struct node *newnode,*temp;
do
{
printf("ID NAME CITY?\n");
newnode=(struct node *)malloc(sizeof(struct node));
newnode->next=NULL;
scanf("%d %s %s",&newnode->id,&newnode->name,&newnode->city);
if(list==NULL)
list=temp=newnode;
else
{
temp->next=newnode;
temp=newnode;
}
printf("\nDo you want to insert record again ?");
printf("\nIf yes press 1 else 0:");
scanf("%d",&n);
}while(n==1);

}
void display()
{
struct node *temp=list;
printf("\nRecords are\n");
printf("\n ID NAME CITY\n");
printf("------ ---------- ----------");
while(temp!=NULL)
{
printf("\n%3d %10s %10s",temp->id,temp->name,temp->city);
temp=temp->next;
}
}
void main()
{
clrscr();
create();
display();
getch();
}
/***************Output************************
ID NAME CITY?
1 Yasin Pune

Do you want to insert record again ?


If yes press 1 else 0:1
ID NAME CITY?
2 Hamid Pune

Do you want to insert record again ?


If yes press 1 else 0:1
ID NAME CITY?
3 Kapil Delhi

Do you want to insert record again ?


If yes press 1 else 0:1
ID NAME CITY?
4 Lalu Bihar

Do you want to insert record again ?


If yes press 1 else 0:0

Records are

ID NAME CITY
------ ---------- ----------
1 Yasin Pune
2 Hamid Pune
3 Kapil Delhi
4 Lalu Bihar

18. Write a menu driven program using ‘C’ for


singly linked list to perform following operations:
- To create list.
- To display list.
- To add a node at first position of linked list.
To delete a node in between by position of linked list.*/

#define getnode (nodeptr)malloc(sizeof(struct node))


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr list;
void create ()
{
int i,n;
nodeptr newnode,t;

printf("\nNumber of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d]Enter the Data:",i+1);
newnode=getnode;
scanf("%d",&newnode->data);
newnode->next=NULL;
if(list==NULL)
list=t=newnode;
else
{
t->next=newnode;
t=newnode;
}
}
}
void display()
{
nodeptr t;
t=list;
printf("\nNodes are\n");
while(t!=NULL)
{
printf("%3d",t->data);
t=t->next;
}
}
void addfirst()
{
nodeptr newnode;
printf("\nData of the newnode:");
newnode=getnode;
scanf("%d",&newnode->data);
newnode->next=list;
list=newnode;
}
void delmid()
{
nodeptr t1,t2,t3;
int i,pos;
printf("Position of the node to be deleted:");
scanf("%d",&pos);
i=2;
t1=list;
while(i<pos)
{
t1=t1->next;
i++;
}
t2=t1->next;
t3=t2->next;
t1->next=t3;
i=t2->data;
free(t2);
printf("%d is deleted",i);
}
void main()
{
int ch;
clrscr();
up:
printf("\n1 to create");
printf("\n2 to display");
printf("\n3 to add the node at the first position");
printf("\n 4 to delete the node, which is in betwen the list");
printf("\n11 to exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : create(); break;
case 2 : display(); break;
case 3 : addfirst(); break;
case 4 : delmid(); break;
case 11: exit(0);
default: printf("Wrong choice");
}
getch();
goto up;
}
/***********************Output*************

1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:1

Number of nodes:5
1]Enter the Data:1
2]Enter the Data:2
3]Enter the Data:3
4]Enter the Data:4
5]Enter the Data:5

1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:2
Nodes are
1 2 3 4 5
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:3

Data of the newnode:0

1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:2

Nodes are
0 1 2 3 4 5
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:4
Position of the node to be deleted:4
3 is deleted
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:2

Nodes are
0 1 2 4 5
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:11
*************************************/

19. Write a ‘C’ program to reverse a string


using static implementation of stack.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct stack
{
char a[100];
int top;
}s;
void main()
{
int j,i,n;
int l;
char s1[100];
clrscr();
printf("\nEnter the string:");
gets(s1);
l=strlen(s1);
for(i=0;i<l;i++)
{
s.a[s.top]=s1[i];
s.top++;
}
for(i=s.top-1,j=0;i>=0;i--,j++)
{
s1[j]=s.a[i];
}
printf("\nReverse is \n");
puts(s1);
getch();
}
/*******************Output**********************

Enter the string:Poona College is Best

Reverse is
tseB si egelloC anooP
*****************Output2************************

Enter the string:India is My Country.

Reverse is
.yrtnuoC yM si aidnI

**************************************************/

20.Write a ‘C’ program to search givenelements into the list using linear
search method.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],h=0,i,n,f;
clrscr();
printf("\nHow many Elements:");
scanf("%d",&n);
printf("\nEnter the %d Elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Element to find:");
scanf("%d",&f);

for(i=0;i<n;i++)
{
if(a[i]==f)
{
h++;
printf("\n%d is present at location number:%d",f,i+1);
}
}
if(h==0)
printf("%d not Present",f);
getch();
}
************************Output1****************
How many Elements:10
Enter the 10 Elements:
1 5 9 10 11 5 2 0 1 5

Enter the Element to find:5


5 is present at location number:2
5 is present at location number:6
5 is present at location number:10

**********************Output2********************
How many Elements:10
Enter the 10 Elements:
5
2
4
9
10
45
95
11
100
2
Enter the Element to find:3
3 not Present
**************************************************************************
**

21. Write menu driven program using ‘C’ for Static implementation of
Stack. The menu
includes
- push
- pop
- display
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int a[10];
int top;
}s;
int push(int x)
{
s.top++;
s.a[s.top]=x;
return s.a[s.top];
}
int pop()
{
int x;
x=s.a[s.top];
s.top--;
return x;
}
void display()
{
int t;
t=s.top;
while(t>=0)
{
printf("\n%d",s.a[t]);
t--;
}
}
void main()
{
int i,ch;
s.top=-1;
clrscr();
up:
printf("\n1 to push");
printf("\n2 to pop");
printf("\n3 to display");
printf("\n11 to exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: if(s.top==9)
printf("\nStack is full");
else
{
printf("\nEnter the element to insert:");
scanf("%d",&i);
ch=push(i);
printf("%d is inserted",ch);
}
break;
case 2: if(s.top==-1)
printf("\nStack is empty");
else
{
i=pop();
printf("\n%d is removed",i);
}
break;
case 3: if(s.top==-1)
printf("\nNo elements to display");
else
display();
break;
case 11: exit(0);
default: printf("\nWrong choice");
}
getch();
goto up;
}
/*********************OUTPUT*******************

1 to push
2 to pop
3 to display
11 to exit
Enter your choice:1

Enter the element to insert:1


1 is inserted
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:1

Enter the element to insert:2


2 is inserted
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:1

Enter the element to insert:3


3 is inserted
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:1

Enter the element to insert:4


4 is inserted
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:1

Enter the element to insert:5


5 is inserted
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:1

Enter the element to insert:6


6 is inserted
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:
3

6
5
4
3
2
1
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:2

6 is removed
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:2

5 is removed
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:3

4
3
2
1
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:11
***********************************/

22. Write a ‘C’ program to search


given element into the list using Binary search method.*/
#include<stdio.h>
#include<conio.h>
a[20];
int bin_search(int lb,int up,int x)
{
int mid;
mid=(lb+up)/2;
if(a[mid]==x)
return 0;
if(lb<=up)
{
if(x<a[mid])
bin_search(lb,mid-1,x);
else
bin_search(mid+1,up,x);
}
else
return 1;
}
void main()
{
int i,n,x;
clrscr();
printf("\nHow many elements in your array:");
scanf("%d",&n);
printf("\nEnter the elements in sorted order:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEntered Elements are:\n");
for(i=0;i<n;i++)
printf("%3d ",a[i]);
printf("\nEnter the Elements to search:");
scanf("%d",&x);
n=(bin_search(0,n-1,x));
if(n==1)
printf("\nThe Element is not found");
else
printf("\nElement is present ");
getch();
}
/****************Output******************

How many elements in your array:10

Enter the elements in sorted order:1 5 10 25 60 62 70 75 80 81

Entered Elements are:


1 5 10 25 60 62 70 75 80 81
Enter the Elements to search:10

Element is present
******************Output2*******************

How many elements in your array:5

Enter the elements in sorted order: 25 45 60 70 99

Entered Elements are:


25 45 60 70 99
Enter the Elements to search:20

The Element is not found

*/
23. program to create two singly linked lists and perform the union of two
lists and display it.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr q;
q=(nodeptr)malloc(sizeof(struct node));
q->next=NULL;
return q;
}
nodeptr list1=NULL,list2=NULL;
void create1()
{
nodeptr t,nn;
int n,i;
printf("\nHow many nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
nn=getnode();
printf("\nEnter the data:");
scanf("%d",&nn->data);
if(list1==NULL)
list1=t=nn;
else
{
t->next=nn;
t=nn;
}
}
}
void display1()
{
nodeptr t;
t=list1;
while(t!=NULL)
{
printf("%3d",t->data);
t=t->next;
}
}
void create2()
{
nodeptr t,nn;
int n,i;
printf("\nHow many nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
nn=getnode();
printf("\nEnter the data:");
scanf("%d",&nn->data);
if(list2==NULL)
list2=t=nn;
else
{
t->next=nn;
t=nn;
}
}
}
void display2()
{
nodeptr t;
t=list2;
while(t!=NULL)
{
printf("%3d",t->data);
t=t->next;
}
}
void uni()
{
nodeptr t1,t2;
int a[20],flag=0,i=0,j=0,n=0;
for(t1=list1;t1!=NULL;t1=t1->next)
{
a[i]=t1->data;
i++;
}
n=i;
for(t2=list2;t2!=NULL;t2=t2->next)
{
flag=0;
for(i=0;i<n;i++)
{
if(t2->data==a[i])
{
flag=1;
break;
}
}
if(flag==0)
{
a[n++]=t2->data;
}

}
printf("\nThe Union of the two list is\n");
for(i=0;i<n;i++)
printf("%3d",a[i]);
}

void main()
{
int ch;
clrscr();
up:
printf("\n1 to create list1");
printf("\n2 to dislay list1");
printf("\n3 to create list2");
printf("\n4 to display list2");
printf("\n5 to get the union of the lists");
printf("\n11 to exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create1(); break;
case 2: display1(); break;
case 3: create2(); break;
case 4: display2(); break;
case 5: uni(); break;
case 11: exit(0);
default : printf("\nWrong choice");
}
getch();
goto up;
}
/**************OUTPUT****************

1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:1

How many nodes:5

Enter the data:1

Enter the data:2

Enter the data:3

Enter the data:4

Enter the data:5

1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:2
1 2 3 4 5
1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:3

How many nodes:5

Enter the data:4

Enter the data:5

Enter the data:6


Enter the data:7

Enter the data:8

1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:4
4 5 6 7 8
1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:5

The Union of the two list is


1 2 3 4 5 6 7 8
1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:11
*************************************/
24. Write a ‘C’ program to
find given element into the array list
using recursive Binary Search Method.

#include<stdio.h>
#include<conio.h>
struct yasin
{
int a[10];
}s;
int bin_search(int lb,int up,int x)
{
int mid;
if(lb<=up)
{
mid=(lb+up)/2;
if(s.a[mid]==x)
return mid+1;
else if(x<=s.a[mid])
bin_search(lb,mid-1,x);
else
bin_search(mid+1,up,x);
}
else
return -1;
}
void main()
{
int i,n,x;

printf("\nHow many elements:");


scanf("%d",&n);
printf("\nEnter %d Elements in Sorted order:\n",n);
for(i=0;i<n;i++)
scanf("%d",&s.a[i]);
printf("\nEnter the element to search:");
scanf("%d",&x);
if(bin_search(0,n-1,x)==-1)
printf("\nElement not found");
else
printf("%d is found at %d location",x,(bin_search(0,n-1,x)));
getch();
}

/****************************************

How many elements:10

Enter 10 Elements in Sorted order:


2 5 9 10 12 13 15 20 30 45

Enter the element to search:20


20 is found at 8 location
******************Output2*********************

How many elements:5

Enter 5 Elements in Sorted order:


1 3 6 9 15

Enter the element to search:5


Element not found
**********************************************/

25. Write menu driven program using ‘C’ for Static implementation of
Queue. The menu includes
- Insert
- Delete
- Display
- Exit

#include<stdio.h>
#include<conio.h>
#include<process.h>
struct queue
{
int item[10];
int front,rear;
}q;
void ins(int x)
{
q.item[++q.rear]=x;
}
int del()
{
return (q.item[q.front++]);
}
void main()
{
int ch,x,i;
q.rear=-1;
q.front=0;
up:clrscr();
printf("\n ----MENU-----");
printf("\n1)Insert");
printf("\n2)Remove");
printf("\n3)Display");
printf("\n4)Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:if (q.rear==9)
printf("\n queue is full.");
else
{
printf("\n enter element to insert.");
scanf("%d",&x);
ins(x);
}
printf("\n %d is inserted in queue.",x);
break;
case 2:if (q.front > q.rear)
printf("\n queue is empty.");
else
{
x=del();
printf("\n the removed element is %d.",x) ;
}
break;
case 3: if (q.front > q.rear)
printf("\n queue is empty.");
else
{
printf("\n The Queue is-->");
for( i=q.front;i<=q.rear;i++)
printf("\n %d",q.item[i]);
}
break;
case 4:exit(0);
default:printf("\n wrong choice.");
}
getch();
goto up;
}

26. Write a ‘C’ program to sort array


elements using Bubble sort method.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int t,n,i,pass,a[10];
clrscr();
printf("\nHow many element in your array:");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{

scanf("%d",&a[i]);
}
for(pass=1;pass<n-1;pass++)
{
for(i=0;i<n-pass;i++)
{
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
}
printf("\nArray in sorted order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
/****************output*****************

How many element in your array:10

Enter the elements:


1 Element:10
2 Element:25
3 Element:30
4 Element:21
5 Element:5
6 Element:6
7 Element:45
8 Element:60
9 Element:90
10 Element:11

Array in sorted order:


5 6 10 11 21 25 30 45 60 90
***************************************************

27. Write a menu driven program using ‘C’ for static implementation of
Circular Queue for characters. The menu includes
- Insert
- Delete
- Display
- Exit
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct queue
{
int item[10];
int front,rear;
}q;
void ins(int x)
{
q.rear=(q.rear+1)%10;
q.item[q.rear]=x;
}
int del()
{
q.front=(q.front+1)%10;
return (q.item[q.front]);
}

void main()
{
int ch,x,i,j;
q.rear=-1;
q.front=-1;
up:
printf("\n ----MENU----\n");
printf("\n1)Insert");
printf("\n2)Remove");
printf("\n3)Display");
printf("\n4)Exit");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:if ((q.rear+1)%10==q.front)
printf("\n queue is full.");
else
{
printf("\n enter element to insert.");
scanf("%d",&x);
ins(x);

printf("\n %d is inserted in queue.",x);


}
break;
case 2:if (q.front==q.rear)
printf("\n queue is empty.");
else
{
x=del();
printf("\n the removed element is %d.",x) ;

}
break;

case 3:

if (q.front==q.rear)
printf("\n queue is empty.");
else
{
j=(q.front+1)%10;

printf("\n The Queue is-->");


for( i=j;i<=q.rear;i++)
printf("\n %d",q.item[i]);
}
break;
case 4:exit(0);
default:printf("\n wrong choice.");
}
getch();
goto up;
}
/************************Ouput****************

----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:1

enter element to insert.1

1 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:1

enter element to insert.2

2 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:1

enter element to insert.3

3 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:1

enter element to insert.4

4 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:1

enter element to insert.5

5 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:3

The Queue is-->


1
2
3
4
5
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:2

the removed element is 1.


----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:2

the removed element is 2.


----MENU----

1)Insert
2)Remove
3)Display
4)Exit
Enter your choice:1

enter element to insert.


6

6 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:3

The Queue is-->


3
4
5
6
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:1

enter element to insert.7

7 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:1

enter element to insert.8

8 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:1

enter element to insert.9

9 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:1

enter element to insert.10

10 is inserted in queue.
----MENU----

1)Insert
2)Remove
3)Display
4)Exit

Enter your choice:3

The Queue is-->


3
4
5
6
7
8
9
10
***********************************************/

28. Write a ‘C’ program to accept an infix expression,


convert it into its equivalent postfix expression,evaluate
it and display the result.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#define size 50
struct stack
{
char item[size];
int top;
}s;
char isopr(char c)
{
return(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='$');
}
void push(char c)
{
s.item[++s.top]=c;
}
int pop()
{
return (s.item[s.top--]);
}
void main()
{
int ch,i,j=0;
char inf[size],post[size],x;
int op1,op2,p;
char chr[2];
clrscr();
s.top=-1;
printf("\n Enter infix expression:\n");
scanf("%s",inf);
for(i=0;inf[i]!='\0';i++)
{
if (inf[i]=='+'||inf[i]=='-'||inf[i]=='*'||inf[i]=='/'||inf[i]=='(')
push(inf[i]);
else
{
if (inf[i]==')')
{
x=pop();
while (s.top!=-1 && x!='(')

post[j]=x;
j++;
x=pop();
}
}
else
{
post[j]=inf[i];
j++;
}

}
}
while (s.top!=-1)
{
x=pop();
post[j]=x;
j++;
}
post[j]='\0';
printf("\n\n Postfix expression is:\n%s",post);
for(j=0;post[j]!='\0';j++)
{
if (!isopr(post[j]))
{
chr[0]=post[j];
chr[1]='\0';
p=atoi(chr);
push(p);
}
else
{
op1=pop();
op2=pop();
switch(post[j])
{
case '+':push(op1+op2);
break;
case '-':push(op1-op2);
break;
case '*':push(op1*op2);
break;
case '/':push(op1/op2);
break;
case '%':push(op1%op2);
break;
case'$':push(pow(op1,op2));
break;
}
}
}

printf("\n\nPostfix Evaluation result is %d.",pop());

getch();
}

/*Output:

Enter infix expression:


(2+3)*4

Postfix expression is:


23+4*

Postfix Evaluation result is 20.

Enter infix expression:


(3+4)*2

Postfix expression is:


34+2*

Postfix Evaluation result is 14.


Enter infix expression:
(2+3)*4+(3+4)*2

Postfix expression is:


23+434+2*+*

Postfix Evaluation result is 90.


*/

29.Write a ‘C’ program to sort an


array elements using Merge Sort technique.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,h,i,j=0,k=0,a[10],b[10],c[20];
clrscr();
printf("\nHow many elements in array1:");
scanf("%d",&n);
printf("\nEnter the elements in sorted order:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nhow many elements in array 2:\n");
scanf("%d",&m);
printf("\nEnter the array in sorted order:\n");
for(i=0;i<m;i++)
scanf("%d",&b[i]);
h=n+m;
for(i=0;i<h;i++)
{
if(a[j]<=b[k])
{
c[i]=a[j];
j++;
}
else
{
c[i]=b[k];
k++;
}
}
printf("\nArray 3 in sorted order is\n:\n");
for(i=0;i<h;i++)
printf("%3d",c[i]);
getch();
}
/*********************Output****************

How many elements in array1:5

Enter the elements in sorted order:


1
5
9
14
21

how many elements in array 2:


5

Enter the array in sorted order:


2
4
8
16
23

Array 3 in sorted order is


:
1 2 4 5 8 9 14 16 21 23
30. Write a ‘C’ program to create a Binary tree,
traverse it using recursive operations like
inorder, preorder and postorder and display
the result of each one separately.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct stack
{
struct node *arr[30];
int top;
}s;
struct node
{
int data;
struct node *left,*right;
};
typedef struct node *treeptr;
treeptr root=NULL;
treeptr getnode()
{
treeptr newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->right=newnode->left=NULL;
return newnode;
}
void push(treeptr node)
{
s.arr[++s.top]=node;
}
struct node *pop()
{
return (s.arr[s.top--]);
}
void bst()
{
treeptr newnode,temp;
int i,n,c;
printf("\nHow many nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=getnode();
printf("\ndata:");
scanf("%d",&newnode->data);
if(root==NULL)
temp=root=newnode;
else
{
temp=root;
while(temp!=NULL)
{
if(newnode->data<temp->data)
{
if(temp->left==NULL)
{
temp->left=newnode;
break;
}
else
temp=temp->left;
}

else if(newnode->data>temp->data)
{
if(temp->right==NULL)
{
temp->right=newnode;
break;
}
else
temp=temp->right;
}
}
}
//end of while
}
}

void preorder(treeptr root)


{
treeptr temp;
temp=root;
if(temp!=NULL)
{
printf("%3d",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}

void inorder(treeptr root)


{
treeptr temp;
temp=root;
if(temp!=NULL)
{
inorder(temp->left);
printf("%3d",temp->data);
inorder(temp->right);
}
}
void postorder(treeptr root)
{
treeptr temp;
temp=root;
if(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
printf("%3d",temp->data);
}
}
void npreorder(treeptr root)
{
treeptr ptr=root;
push(ptr);
while(s.top!=-1)
{
ptr=pop();
if(ptr)
{
printf("%3d",ptr->data);
push(ptr->right);
push(ptr->left) ;
}
}
}
void ninorder(treeptr root)
{
treeptr ptr=root;
while(s.top!=-1 || ptr!=NULL)
{
if(ptr)
{
push(ptr);
ptr=ptr->left;
}
else
{
ptr=pop();
printf("%3d",ptr->data);
ptr=ptr->right;
}
}
}
void npost(treeptr root)
{
treeptr ptr=root;
int flag=0;
while(s.top!=-1 || ptr!=NULL)
{
if(ptr)
{
flag=1;
push(ptr);
ptr=ptr->left;
}
else

ptr=pop();

if(flag==0)
printf("%3d",ptr->data);
else
{
push(ptr);
ptr=ptr->right;
}
}

void main()

{
int ch;
clrscr();
s.top=-1;
up:
printf("\n1 to create the Binary Search tree");
printf("\n2 to display in preorder");
printf("\n3 to display in inorder:");
printf("\n4 to dislay in postorder");
printf("\n5 to npreorder");
printf("\n6 to ninorder");
printf("\n7 to npost");
printf("\n11 to exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: bst(); break;
case 2: preorder(root); break;
case 3: inorder(root); break;
case 4: postorder(root); break;
case 5: npreorder(root);break;
case 6: ninorder(root); break;
case 7:npost(root);break;
case 11: exit(0);
default: printf("\nWrong choice");
}
getch();
goto up;
}

/****************Output*******************

1 to create the Binary Search tree


2 to display in preorder
3 to display in inorder:
4 to dislay in postorder
11 to exit
Enter your choice:1

How many nodes:8

data:50

data:56

data:2

data:40

data:43

data:30

data:25

data:90

1 to create the Binary Search tree


2 to display in preorder
3 to display in inorder:
4 to dislay in postorder
11 to exit
Enter your choice:2
50 2 40 30 25 43 56 90
1 to create the Binary Search tree
2 to display in preorder
3 to display in inorder:
4 to dislay in postorder
11 to exit
Enter your choice:3
2 25 30 40 43 50 56 90
1 to create the Binary Search tree
2 to display in preorder
3 to display in inorder:
4 to dislay in postorder
11 to exit
Enter your choice:4
25 30 43 40 2 90 56 50
1 to create the Binary Search tree
2 to display in preorder
3 to display in inorder:
4 to dislay in postorder
11 to exit
Enter your choice:11
***************************************/

You might also like