Professional Documents
Culture Documents
Slip1_2 : Write a C program which uses Binary search tree library and implements following Functions:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
int n,x,i;
root=NULL;
scanf("%d",&n);
printf("\nEnter tree values");
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return root;
if(rt==NULL)
rt=newnode;
rt->data=x;
rt->l=NULL;
rt->r=NULL;
return rt;
else if(x>rt->data)
rt->r=insert(rt->r,x);
return rt;
else
rt->l=insert(rt->l,x);
return rt;
if(tmp!=NULL)
inorder(tmp->l);
printf("%d->",tmp->data);
inorder(tmp->r);
if(tmp!=NULL)
postorder(tmp->l);
postorder(tmp->r);
printf("%d->",tmp->data);
if(tmp!=NULL)
{
printf("%d->",tmp->data);
preorder(tmp->l);
preorder(tmp->r);
if(tmp==NULL)
return (NULL);
if(tmp->data==x)
return (tmp);
if(x>tmp->data)
return(search((tmp->r),x));
return(search((tmp->l),x));
main()
int no,y;
rt=create();
printf("\nInorder\n");
inorder(rt);
printf("\npostorder\n");
postorder(rt);
printf("\npreorder\n");
preorder(rt);
scanf("%d",&no);
rt=insert(rt,no);
inorder(rt);
scanf("%d",&y);
rt=search(rt,y);
if(rt!=NULL)
else
***************************
2. slip2_2 : There are lists where insertion should ensure the ordering of data elements. Since the
elements are in ascending order the search can terminate once equal or greater element is found.
Implement a singly linked list of ordered integers (ascending/descending) with insert, search and display
operations.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
int i;
f = newnode;
scanf("%d",&f->data);
s = f;
for(i=2;i<=n;i++)
s->next = newnode;
s = s->next;
scanf("%d",&s->data);
}
s->next=NULL;
return f;
printf("\t %d",t->data);
int cnt=0,i;
cnt++;
if(p>=cnt)
s=newnode;
scanf("%d",&s->data);
if(p==1)
s->next = f;
f=s;
else
for(i=1,t=f;i<=p-2;i++)
t=t->next;
s->next = t->next;
t->next = s;
return f;
for(p=f;p!=NULL;p=p->next)
for(q=p->next;q!=NULL;q=q->next)
temp=p->data;
p->data=q->data;
q->data=temp;
main()
int n,p,p1,ch;
do
printf("\n\n 1.create link list \n 2. dispaly \n 3.insert into link list \n 4.sorted order \n
0.exit ");
switch(ch)
case 1 :printf("\n Enter how manu elements you wnt to enter :");
scanf("%d",&n);
l=create(n);
break;
case 2 : display(l);
break;
scanf("%d",&p);
l=insert(l,p);
break;
case 4:sort(l);
break;
case 0 :exit(0);
}while(ch!=0);
}
*******************************
3. Slip3_2. Write a C program that accepts the graph as an adjacency matrix and converts it to adjacency
list representation Implement functions to print in degree, out degree and to Display the adjacency
matrix
#include<stdio.h>
#include<malloc.h>
struct n
int data;
struct n *link;
};
NODE *getnode(int);
void main()
NODE *ptr,*temp,*h[10];
int n,a[10][10],i,j;
scanf("%d",&n);
printf("\n Enter entries of an adjacency matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("\t %d ",a[i][j]);
printf("\n");
for(i=0;i<n;i++)
h[i]=NULL;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i][j]==1)
{
temp=getnode(j+1);
if(h[i]==NULL)
h[i]=temp;
else
ptr=findlast(h[i]);
ptr->link=temp;
display(h,n);
NODE *getnode(int j)
NODE * temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=j;
temp->link=NULL;
return(temp);
{
NODE *ptr;
for(ptr=h;ptr->link!=NULL;ptr=ptr->link);
return(ptr);
NODE *ptr;
int i;
for(i=0;i<n;i++)
ptr=h[i];
if(ptr==NULL)
printf(" NULL");
while(ptr!=NULL)
printf(" ->V%d",ptr->data);
ptr=ptr->link;
printf("\n");
*******************************
4. Slip4_2 : Write a C program that accepts the graph as an adjacency matrix and converts it to
adjacency list representation. Implement functions to print out degree of any vertex i (take i as
parameter to the function) from adjacency list
Read a graph as adjacency Matrix [5]
#include<stdio.h>
#include<malloc.h>
struct n
int data;
struct n *link;
};
NODE *getnode(int);
void main()
NODE *ptr,*temp,*h[10];
int n,a[10][10],i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("\t %d ",a[i][j]);
printf("\n");
for(i=0;i<n;i++)
h[i]=NULL;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i][j]==1)
temp=getnode(j+1);
if(h[i]==NULL)
h[i]=temp;
else
ptr=findlast(h[i]);
ptr->link=temp;
display(h,n);
NODE *getnode(int j)
NODE * temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=j;
temp->link=NULL;
return(temp);
NODE *ptr;
for(ptr=h;ptr->link!=NULL;ptr=ptr->link);
return(ptr);
}
NODE *ptr;
int i;
for(i=0;i<n;i++)
ptr=h[i];
if(ptr==NULL)
printf(" NULL");
while(ptr!=NULL)
printf(" ->V%d",ptr->data);
ptr=ptr->link;
printf("\n");
*********************************
5. Slip5_2 : Write a C program that accepts the graph as an adjacency matrix and converts it to adjacency
list representation. Write a function to display the graph in adjacency list form.
3.Display adjacency List & In degree of any vertex i (take I as parameter to the function) each carries 5
marks [10]
#include<stdio.h>
#include<malloc.h>
int n,g[10][10],v[10];
main()
{ int in,td=0,indegree=0,outdegree=0,n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&g[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("\t %d ",g[i][j]);
}
printf("\n");
scanf("%d",&in);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
indegree++;
printf("node=%d\t indegree=%d",in,indegree);
*******************************
Slip6_2 . Write a C program that accepts the graph as an adjacency matrix and checks if the graph is
undirected. The matrix for undirected graph is symmetric. Also calculate in degree of all vertices
#include
#include
struct n
int data;
struct n *link;
};
NODE *getnode(int);
void main()
NODE *ptr,*temp,*h[10];
int n,a[10][10],i,j;
scanf("%d",&n);
for(i=0;i
for(j=0;j
for(i=0;i
for(j=0;j
printf("\t %d ",a[i][j]);
printf("\n");
for(i=0;i
h[i]=NULL;
for(i=0;i
for(j=0;j
if(a[i][j]==1)
temp=getnode(j+1);
if(h[i]==NULL)
h[i]=temp;
else
{
ptr=findlast(h[i]);
ptr->link=temp;
display(h,n);
int g[10][10],outdegree=0,td=0;
scanf("%d",&n);
for(i=0;i
for(j=0;j
scanf("%d",&g[i][j]);
for(i=0;i
outdegree=0;
for(j=0;j
if(g[i][j]==1)
outdegree++;
td+=1;
}
if(g[j][i]==1)
indegree++;
td+=1;
printf("%d",td);
NODE *getnode(int j)
NODE * temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=j;
temp->link=NULL;
return(temp);
NODE *ptr;
for(ptr=h;ptr->link!=NULL;ptr=ptr->link);
return(ptr);
NODE *ptr;
int i;
for(i=0;i
ptr=h[i];
if(ptr==NULL)
printf(" NULL");
while(ptr!=NULL)
printf(" ->V%d",ptr->data);
ptr=ptr->link;
printf("\n");
}
}
**********************************
7. Slip7_2 : Write a C program which uses Binary search tree library and implements following functions :
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
int n,x,i;
root=NULL;
printf("\n Enter no of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return root;
if(rt==NULL)
rt=newnode;
rt->data=x;
rt->l=NULL;
rt->r=NULL;
return rt;
else if(x>rt->data)
rt->r=insert(rt->r,x);
return rt;
}
else
rt->l=insert(rt->l,x);
return rt;
if(tmp!=NULL)
inorder(tmp->l);
printf("%d->",tmp->data);
inorder(tmp->r);
if(tmp!=NULL)
printf("%d->",tmp->data);
preorder(tmp->l);
preorder(tmp->r);
{
static int i=0;
if(tmp==NULL)
return 0;
return 1;
i=count(tmp->l)+count(tmp->r);
return i;
main()
int no,y,cnt;
rt=create();
printf("\nInorder\n");
inorder(rt);
printf("\npreorder\n");
preorder(rt);
cnt=count(rt);
*******************************
8. Slip8_2 : Write a C program which uses Binary search tree library and implements following Functions
(use
Recursion).
b) In order [5]
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
int n,x,i;
root=NULL;
scanf("%d",&n);
printf("\nEnter tree values");
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return root;
if(rt==NULL)
rt=newnode;
rt->data=x;
rt->l=NULL;
rt->r=NULL;
return rt;
else if(x>rt->data)
rt->r=insert(rt->r,x);
return rt;
else
rt->l=insert(rt->l,x);
return rt;
if(tmp!=NULL)
inorder(tmp->l);
printf("%d->",tmp->data);
inorder(tmp->r);
if(tmp!=NULL)
printf("%d->",tmp->data);
preorder(tmp->l);
preorder(tmp->r);
if(tmp==NULL)
return 0;
return 1;
i=count(tmp->l)+count(tmp->r);
return i;
main()
int no,y,cnt;
rt=create();
printf("\nInorder\n");
inorder(rt);
printf("\npreorder\n");
preorder(rt);
cnt=count(rt);
******************************
9. Slip9_2 : Write a C program which uses Binary search tree library and implements following functions
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
int n,x,i;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
return root;
if(rt==NULL)
rt=newnode;
rt->data=x;
rt->l=NULL;
rt->r=NULL;
return rt;
else if(x>rt->data)
rt->r=insert(rt->r,x);
return rt;
else
rt->l=insert(rt->l,x);
return rt;
}
void inorder(struct node *tmp)
if(tmp!=NULL)
inorder(tmp->l);
printf("%d->",tmp->data);
inorder(tmp->r);
if(tmp!=NULL)
postorder(tmp->l);
postorder(tmp->r);
printf("%d->",tmp->data);
if(tmp!=NULL)
printf("%d->",tmp->data);
preorder(tmp->l);
preorder(tmp->r);
if(tmp==NULL)
return (NULL);
if(tmp->data==x)
return (tmp);
if(x>tmp->data)
return(search((tmp->r),x));
return(search((tmp->l),x));
main()
int no,y;
rt=create();
printf("\nInorder\n");
inorder(rt);
printf("\npostorder\n");
postorder(rt);
printf("\npreorder\n");
preorder(rt);
scanf("%d",&no);
rt=insert(rt,no);
inorder(rt);
scanf("%d",&y);
rt=search(rt,y);
if(rt!=NULL)
else
***************************
10. Slip10_2 : Write a C program which uses Binary search tree library and implements following
functions
#include<stdlib.h>
struct node
int data;
};
int n,x,i;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return root;
if(rt==NULL)
rt=newnode;
rt->data=x;
rt->l=NULL;
rt->r=NULL;
return rt;
else if(x>rt->data)
rt->r=insert(rt->r,x);
return rt;
else
rt->l=insert(rt->l,x);
return rt;
}
void inorder(struct node *tmp)
if(tmp!=NULL)
inorder(tmp->l);
printf("%d->",tmp->data);
inorder(tmp->r);
if(tmp!=NULL)
postorder(tmp->l);
postorder(tmp->r);
printf("%d->",tmp->data);
if(tmp!=NULL)
{
printf("%d->",tmp->data);
preorder(tmp->l);
preorder(tmp->r);
if(tmp==NULL)
return (NULL);
if(tmp->data==x)
return (tmp);
if(x>tmp->data)
return(search((tmp->r),x));
return(search((tmp->l),x));
main()
int no,y;
rt=create();
printf("\nInorder\n");
inorder(rt);
printf("\npostorder\n");
postorder(rt);
printf("\npreorder\n");
preorder(rt);
scanf("%d",&no);
rt=insert(rt,no);
inorder(rt);
scanf("%d",&y);
rt=search(rt,y);
if(rt!=NULL)
else
**********************
11. Slip11_2 : Implement a stack library (dystack.h) of integers using a dynamic (linked list)
implementation of
the stack and implementing the Push,Pop,IsEmpty,Init,Peek operations. Write a menu driven program
#include<stdio.h>
#include<stdlib.h>
#include"dynamic_stack.h"
main()
int n,i=0,ch;
init();
do
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
push(n);
break;
case 2:printf("deleted elements is:%d",pop());
break;
case 3:i=isempty();
if(i==1)
printf("\nstack is empty");
else
break;
case 4: display();
break;
break;
case 6: exit(0);
}while(ch!=6);
**************************
12. Slip12_2 : There are lists where new elements are always appended at the end of the list. The list can
be implemented as a circular list with the external pointer pointing to the last element of the
list.Implement singly linked circular list of integers with append and display operations. The operation
append(L, n), appends to the end of the list, n integers either accepted from user or randomly generated
Creation of circular Link List & Append Function each carrires 10 marks. [20]
Display Function
[5]
#include<stdio.h>
#include<stdlib.h>
struct snode
int data;
};
int i;
f=newnode;
printf("enter data");
scanf("%d",&f->data);
f->next=NULL;
s=f;
for(i=2;i<=no;i++)
s->next=newnode;
s=s->next;
scanf("%d",&s->data);
s->next=NULL;
s->next=f;
return f;
}
s=f;
do
{printf("\t%d",s->data);
s=s->next;
}while(s!=f);
s=newnode;
s->data=n;
t=f;
int i,count=0;
do
{ count++;
t=t->next;
} while(t->next!=f);
t->next=s;
t=t->next;
t->next=f;
return f;
}
main()
{ int no,num;
scanf("%d",&no);
l1=create(no);
display(l1);
scanf("%d",&num);
l1=append(l1,num);
display(l1);
*********************************
13. Slip13_2 : Implement a list library (singlylist.h) for a singly linked list with the insert, delete, search,
display operations. Write a menu driven program to call the operations.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
int i=0;
scanf("%d",&f->data);
f->next=NULL;
s=f;
for(i=2;i<=n;i++)
s=s->next;
scanf("%d",&s->data);
s->next=NULL;
return f;
}
for(t=f;t!=NULL;t=t->next)
printf("\t %d ->",t->data);
printf("NULL");
for(t=f;t!=NULL;t=t->next)
{ cnt++;
if(pos>cnt)
else
if(pos==1)
{ s->next=f;
f=s;
else if(pos==cnt)
{ for(t=f;t->next!=NULL;t=t->next);
t->next=s;
t=t->next;
t->next=NULL;
else
{ for(i=1,t=f;i<pos-1;i++)
t=t->next;
s->next=t->next;
t->next=s;
} //end of else
return f;
for(t=f;t!=NULL;t=t->next)
{ cnt++;
if(pos>cnt)
else
{ if(pos==1)
{ s=f;
f=f->next;
free(s);
else if(pos==cnt)
{ for(i=1,t=f;i<cnt-1;i++)
t=t->next;
s=t->next;
t->next=NULL;
free(s);
else
for(i=1,t=f;i<pos-1;i++)
t=t->next;
s=t->next;
t->next=s->next;
free(s);
}
} //end of else
return f;
int flag=0;
for(t=f;t!=NULL;t=t->next)
if(sr==t->data)
flag=1;
break;
if(flag==1)
else
}
main()
int ch,n,i,m,a,pos,sr,d;
do
printf("\nMENU\n");
printf("\n1.Create");
printf("\n2.Display");
printf("\n3.Search");
printf("\n4.Insert");
printf("\n5.Delete");
printf("\n6.Exit\n");
printf("\nEnter Ur Choice\n");
scanf("%d",&ch);
switch(ch)
case 1:
scanf("%d",&n);
H=create(n);
break;
break;
case 3:
scanf("%d",&sr);
search(H,sr);
break;
scanf("%d",&d);
H=insert(H,d);
break;
scanf("%d",&d);
H=delete(H,d);
break;
case 6: exit(0);
break;
} while(ch!=6);
}
****************************
14. Slip14_2 : Implement a list library (doublylist.h) for a doubly linked list with the insert, delete search,
display operations. Write a menu driven program to call the operations.
#include<stdio.h>
#include<stdlib.h>
#include "doublylist.h"
void main()
int n,ch,sr,d;
do
printf("\nMENU\n");
printf("\n1.Create");
printf("\n2.Display");
printf("\n3.Search");
printf("\n4.Insert");
printf("\n5.Delete");
printf("\n6.Exit\n");
printf("\nEnter Ur Choice\n");
scanf("%d",&ch);
switch(ch)
case 1:
scanf("%d",&n);
H=create(n);
break;
display(H);
break;
case 3:
scanf("%d",&sr);
search(H,sr);
break;
scanf("%d",&d);
H=insert(H,d);
break;
H=delete(H,d);
break;
case 6: exit(0);
break;
}while(ch!=6);
*******************
Slip15_2 : Implement a stack library (dystack.h) of integers using a dynamic (linked list) implementation
of the stack and implementing the init,push,pop,peek & isempty operations. Write a menu driven
program that includes stack library and calls different stack operations. Push & Pop operation carries 8
marks each. [16]
#include<stdio.h>
#include"dystack.h"
main()
int n,i=0,ch;
init();
do
{
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
push(n);
break;
case 2:
break;
case 3: i=isempty();
if(i==1)
printf("\nstack is empty");
else
break;
case 4: i=isfull();
if(i==1)
printf("\nstack is full");
else
printf("\nstack is not full");
break;
break;
case 6: display();
break;
case 7:exit(0);
}while(ch!=7);
*****************************
Slip16_2 : Implement a queue library (dyqueue.h) of integers using a dynamic (circular linked list)
implementation of the queue and implementing the above Insert,Delete,Is Empty operations. Write a
menu driven program that includes queue library and calls different queue
operations.insert,Delete,Create ,IsEmpty,Disaplay operation carries 5 marks each.
#include<stdio.h>
#include"dyqueue.h"
main()
int n,i=0,ch;
struct node p;
init(&p);
do
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
add(&p,n);
break;
break;
case 3:i=isempty(&p);
if(i==1)
printf("\nqueue is empty");
else
break;
case 4:i=isfull(&p);
if(i==1)
printf("\nqueue is full");
else
break;
case 5://n1=peek();
break;
case 6:exit(0);
while(ch!=6);
*************************
17. Slip17_2 : Write a C program which uses Binary search tree library and implements following
functions
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
};
int n,x,i;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return root;
if(tmp!=NULL)
{
inorder(tmp->l);
printf("%d->",tmp->data);
inorder(tmp->r);
if(tmp==NULL)
return (NULL);
if(tmp->data==x)
return (tmp);
if(x>tmp->data)
return(search((tmp->r),x));
return(search((tmp->l),x));
if(tmp==NULL)
return 0;
return 1;
i=count(tmp->l)+count(tmp->r);
return i;
main()
int no,y;
rt=create();
printf("\nInorder\n");
inorder(rt);
scanf("%d",&y);
rt=search(rt,y);
if(rt!=NULL)
else
cnt=count(rt);
**************************
18. Slip18_2 : Implement a queue library (stqueue.h) of integers using a static implementation of the
queue and implementing the Insert,Delete,IsEmpty,IsFull,Init,Peek operations. Write a Menu driven
program that includes queue library and calls different queue operations.Insert, Delete operation carries
8 mark each.IsEmpty,IsFull function carries 3 mark each.Init ,Peek function.
#include<stdio.h>
#include"stqueue.h"
main()
int n,i=0,ch;
struct node p;
init(&p);
do
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nenter element:");
scanf("%d",&n);
add(&p,n);
break;
break;
case 3:i=isempty(&p);
if(i==1)
printf("\nqueue is empty");
else
break;
case 4:i=isfull(&p);
if(i==1)
printf("\nqueue is full");
else
break;
case 5://n1=peek();
break;
case 6:exit(0);
} while(ch!=6);
**********************
19. Slip19_2 : Write a function that checks whether a string of characters is palindrome or not. The
function should use a stack library (cststack.h) of stack of characters using a static implementation of the
stack.
#include<stdio.h>
#include"cststack.h"
#include<string.h>
main()
char str[20],s1[20],item;
int i,j=0;
gets(str);
strcpy(s1,str);
init();
for(i=0;str[i]!='\0';i++)
push(str[i]);
}
while(!isempty())
item=pop();
str[j]=item;
j++;
str[j]='\0';
if(strcmp(s1,str)==0)
else
************************
Slip20_2: Write a function that reverses a string of characters. The function should use a stack library
(cststack.h) of stack of characters using a static implementation of the stack.
#include<stdio.h>
#include"cststack.h"
main()
char str[20],item;
int i;
printf("\nenter the string:");
gets(str);
init();
for(i=0;str[i]!='\0';i++)
push(str[i]);
while(!isempty())
item=pop();
printf("%c",item);
printf("\n");
**********************
21.Slip21_2 : Implement a list library (singlylist.h) for a singly linked list with create() ,display(), insert(),
delete() operations. Write a menu driven driver program to call the operations.
#include<stdio.h>
#include<stdlib.h>
#include "doublylist.h"
void main()
{
int n,ch,sr,d;
do
printf("\nMENU\n");
printf("\n1.Create");
printf("\n2.Display");
printf("\n3.Search");
printf("\n4.Insert");
printf("\n5.Delete");
printf("\n6.Exit\n");
printf("\nEnter Ur Choice\n");
scanf("%d",&ch);
switch(ch)
case 1:
scanf("%d",&n);
H=create(n);
break;
display(H);
break;
case 3:
scanf("%d",&sr);
search(H,sr);
break;
scanf("%d",&d);
H=insert(H,d);
break;
scanf("%d",&d);
H=delete(H,d);
break;
case 6: exit(0);
break;
}while(ch!=6);
***********************