You are on page 1of 72

1.

Slip1_2 : Write a C program which uses Binary search tree library and implements following Functions:

a) Create Binary Tree [10]

b) Insert a node in a binary tree [5]

c) Search a node in binary tree [5]

d) Display a binary tree [5]

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *r;

struct node *l;

};

#define newnode (struct node*)malloc(sizeof(struct node))

struct node *insert(struct node *,int);

struct node *create()

int n,x,i;

struct node *root;

root=NULL;

printf("\n Enter no of nodes");

scanf("%d",&n);
printf("\nEnter tree values");

for(i=0;i<n;i++)

scanf("%d",&x);

root=insert(root,x);

return root;

struct node *insert(struct node *rt,int x)

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);

void postorder(struct node *tmp)

if(tmp!=NULL)

postorder(tmp->l);

postorder(tmp->r);

printf("%d->",tmp->data);

void preorder(struct node *tmp)

if(tmp!=NULL)
{

printf("%d->",tmp->data);

preorder(tmp->l);

preorder(tmp->r);

struct node *search(struct node *tmp,int x)

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;

struct node *rt;

rt=create();

printf("\nInorder\n");
inorder(rt);

printf("\npostorder\n");

postorder(rt);

printf("\npreorder\n");

preorder(rt);

printf("\nEnter element to insert : ");

scanf("%d",&no);

rt=insert(rt,no);

inorder(rt);

printf("\nEnter the node to be searched \n");

scanf("%d",&y);

rt=search(rt,y);

if(rt!=NULL)

printf("\nNode is found in tree \n");

else

printf("\nNode is not found in tree \n");

***************************

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.

i. Search, Display operation carries 5 marks each

ii. Validate at appropriate position


iii. Creation of singly linked list in sorted order

#include<stdio.h>

#include<stdlib.h>

#define newnode (struct node *)malloc(sizeof(struct node));

struct node

int data;

struct node *next;

};

struct node* create(int n)

struct node *f,*s;

int i;

f = newnode;

printf("\n Enter the data :");

scanf("%d",&f->data);

s = f;

for(i=2;i<=n;i++)

s->next = newnode;

s = s->next;

printf("\n Enter the data :");

scanf("%d",&s->data);
}

s->next=NULL;

return f;

void display(struct node* f)

struct node *t;

printf("\n Link list is : \n");

for(t=f; t!=NULL; t=t->next)

printf("\t %d",t->data);

struct node *insert(struct node *f,int p)

int cnt=0,i;

struct node *t,*s;

for(t=f; t!=NULL; t=t->next)

cnt++;

if(p>=cnt)

printf("\n Invalid position");


else

s=newnode;

printf("\n Enter the data :");

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;

void sort(struct node *f)

struct node *p,*q;


int temp;

for(p=f;p!=NULL;p=p->next)

for(q=p->next;q!=NULL;q=q->next)

if(p->data > q->data)

temp=p->data;

p->data=q->data;

q->data=temp;

main()

int n,p,p1,ch;

struct node *l;

do

printf("\n\n 1.create link list \n 2. dispaly \n 3.insert into link list \n 4.sorted order \n
0.exit ");

printf("\n \n Enter Your choice");


scanf("%d",&ch);

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;

case 3 :printf("\n Enter the position to enter data");

scanf("%d",&p);

l=insert(l,p);

break;

case 4:sort(l);

break;

case 0 :exit(0);

default : printf("\n Invalid choice");

}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

1.Read a graph as adjacency Matrix [5]

2.Creation of adjacency List [10]

3.alculate out degree & In degree from adjacency list [10]

#include<stdio.h>

#include<malloc.h>

struct n

int data;

struct n *link;

};

typedef struct n NODE;

NODE *getnode(int);

NODE *findlast(NODE *);

void display(NODE *[],int);

void main()

NODE *ptr,*temp,*h[10];

int n,a[10][10],i,j;

printf("\n Enter total number of vertices : ");

scanf("%d",&n);
printf("\n Enter entries of an adjacency matrix :\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

printf("\n Enter a[%d][%d] : ",i+1,j+1);

scanf("%d",&a[i][j]);

printf("\n Entered Adjacency matrix is … \n");

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;

printf("\n The Adjacency list looks like … \n");

display(h,n);

NODE *getnode(int j)

NODE * temp;

temp=(NODE *)malloc(sizeof(NODE));

temp->data=j;

temp->link=NULL;

return(temp);

NODE *findlast(NODE *h)

{
NODE *ptr;

for(ptr=h;ptr->link!=NULL;ptr=ptr->link);

return(ptr);

void display(NODE *h[10],int n)

NODE *ptr;

int i;

for(i=0;i<n;i++)

printf(" V%d ",i+1);

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]

Creation of adjacency List [10]

Calculate out degree [10]

#include<stdio.h>

#include<malloc.h>

struct n

int data;

struct n *link;

};

typedef struct n NODE;

NODE *getnode(int);

NODE *findlast(NODE *);

void display(NODE *[],int);

void main()

NODE *ptr,*temp,*h[10];

int n,a[10][10],i,j;

printf("\n Enter total number of vertices : ");

scanf("%d",&n);

printf("\n Enter entries of an adjacency matrix :\n");

for(i=0;i<n;i++)

{
for(j=0;j<n;j++)

printf("\n Enter a[%d][%d] : ",i+1,j+1);

scanf("%d",&a[i][j]);

printf("\n Entered Adjacency matrix is … \n");

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;

printf("\n The Adjacency list looks like … \n");

display(h,n);

NODE *getnode(int j)

NODE * temp;

temp=(NODE *)malloc(sizeof(NODE));

temp->data=j;

temp->link=NULL;

return(temp);

NODE *findlast(NODE *h)

NODE *ptr;

for(ptr=h;ptr->link!=NULL;ptr=ptr->link);

return(ptr);
}

void display(NODE *h[10],int n)

NODE *ptr;

int i;

for(i=0;i<n;i++)

printf(" V%d ",i+1);

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.

1.Read a graph as adjacency Matrix [5]

2.Creation of adjacency List [10]

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;

printf("\n Enter total number of vertices : ");

scanf("%d",&n);

printf("\n Enter entries of an adjacency matrix :\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

printf("\n Enter a[%d][%d] : ",i+1,j+1);

scanf("%d",&g[i][j]);

printf("\n Entered Adjacency matrix is … \n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

printf("\t %d ",g[i][j]);

}
printf("\n");

printf("\n Enter vertex to find indegree : ");

scanf("%d",&in);

for(i=0;i<n;i++)

for(j=0;j<n;j++)

if(i==in-1 && g[j][i]==1)

indegree++;

printf("node=%d\t indegree=%d",in,indegree);

//printf("Total Degree of graph =%d\n ",td);

*******************************

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

Read a graph as adjacency Matrix


[5]

Check the matrix is symmetric or not [10]


Calculate in degree of all vertices
[10]

#include

#include

struct n

int data;

struct n *link;

};

typedef struct n NODE;

NODE *getnode(int);

NODE *findlast(NODE *);

void display(NODE *[],int);

void main()

NODE *ptr,*temp,*h[10];

int n,a[10][10],i,j;

printf("\n Enter total number of vertices : ");

scanf("%d",&n);

printf("\n Enter entries of an adjacency matrix :\n");

for(i=0;i

for(j=0;j

printf("\n Enter a[%d][%d] : ",i+1,j+1);


scanf("%d",&a[i][j]);

printf("\n Entered Adjacency matrix is … \n");

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;

printf("\n The Adjacency list looks like … \n");

display(h,n);

int g[10][10],outdegree=0,td=0;

printf("Enter How many nodes");

scanf("%d",&n);

printf("Enter Matrix elements");

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("\n Node\t%d \tindegree=%d\t Outdegree=%d\n",i,indegree,outdegree);

printf("%d",td);

NODE *getnode(int j)

NODE * temp;

temp=(NODE *)malloc(sizeof(NODE));

temp->data=j;

temp->link=NULL;
return(temp);

NODE *findlast(NODE *h)

NODE *ptr;

for(ptr=h;ptr->link!=NULL;ptr=ptr->link);

return(ptr);

void display(NODE *h[10],int n)

NODE *ptr;

int i;

for(i=0;i

printf(" V%d ",i+1);

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 :

Create Binary search Tree [10]

Insert a node in a binary tree [10]

Display a binary tree [5]

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *r;

struct node *l;

};

#define newnode (struct node*)malloc(sizeof(struct node));

struct node *insert(struct node *,int);

struct node *create()

int n,x,i;

struct node *root;

root=NULL;
printf("\n Enter no of nodes");

scanf("%d",&n);

printf("\nEnter tree values");

for(i=0;i<n;i++)

scanf("%d",&x);

root=insert(root,x);

return root;

struct node *insert(struct node *rt,int x)

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);

void preorder(struct node *tmp)

if(tmp!=NULL)

printf("%d->",tmp->data);

preorder(tmp->l);

preorder(tmp->r);

int count(struct node *tmp)

{
static int i=0;

if(tmp==NULL)

return 0;

else if(tmp->l==NULL && tmp->r==NULL)

return 1;

i=count(tmp->l)+count(tmp->r);

return i;

main()

int no,y,cnt;

struct node *rt;

rt=create();

printf("\nInorder\n");

inorder(rt);

printf("\npreorder\n");

preorder(rt);

cnt=count(rt);

printf("\n No of child nodes are : %d",cnt);

*******************************
8. Slip8_2 : Write a C program which uses Binary search tree library and implements following Functions
(use

Recursion).

a) Create Binary search Tree [10]

b) In order [5]

c) Pre order [5]

d) Post order [5]

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *r;

struct node *l;

};

#define newnode (struct node*)malloc(sizeof(struct node));

struct node *insert(struct node *,int);

struct node *create()

int n,x,i;

struct node *root;

root=NULL;

printf("\n Enter no of nodes");

scanf("%d",&n);
printf("\nEnter tree values");

for(i=0;i<n;i++)

scanf("%d",&x);

root=insert(root,x);

return root;

struct node *insert(struct node *rt,int x)

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);

void preorder(struct node *tmp)

if(tmp!=NULL)

printf("%d->",tmp->data);

preorder(tmp->l);

preorder(tmp->r);

int count(struct node *tmp)

static int i=0;

if(tmp==NULL)
return 0;

else if(tmp->l==NULL && tmp->r==NULL)

return 1;

i=count(tmp->l)+count(tmp->r);

return i;

main()

int no,y,cnt;

struct node *rt;

rt=create();

printf("\nInorder\n");

inorder(rt);

printf("\npreorder\n");

preorder(rt);

cnt=count(rt);

printf("\n No of child nodes are : %d",cnt);

******************************

9. Slip9_2 : Write a C program which uses Binary search tree library and implements following functions

Create Binary search Tree [10]


In order [5]

Search a node in binary tree [5]

Count a node in binary tree [5]

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *r;

struct node *l;

};

#define newnode (struct node*)malloc(sizeof(struct node))

struct node *insert(struct node *,int);

struct node *create()

int n,x,i;

struct node *root;

root=NULL;

printf("\n Enter no of nodes");

scanf("%d",&n);

printf("\nEnter tree values");

for(i=0;i<n;i++)
{

scanf("%d",&x);

root=insert(root,x);

return root;

struct node *insert(struct node *rt,int x)

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);

void postorder(struct node *tmp)

if(tmp!=NULL)

postorder(tmp->l);

postorder(tmp->r);

printf("%d->",tmp->data);

void preorder(struct node *tmp)

if(tmp!=NULL)

printf("%d->",tmp->data);
preorder(tmp->l);

preorder(tmp->r);

struct node *search(struct node *tmp,int x)

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;

struct node *rt;

rt=create();

printf("\nInorder\n");

inorder(rt);

printf("\npostorder\n");
postorder(rt);

printf("\npreorder\n");

preorder(rt);

printf("\nEnter element to insert : ");

scanf("%d",&no);

rt=insert(rt,no);

inorder(rt);

printf("\nEnter the node to be searched \n");

scanf("%d",&y);

rt=search(rt,y);

if(rt!=NULL)

printf("\nNode is found in tree \n");

else

printf("\nNode is not found in tree \n");

***************************

10. Slip10_2 : Write a C program which uses Binary search tree library and implements following
functions

a) Create Binary search Tree [10]

b) Insert a node in a binary tree [5]

c) Search a node in binary tree [5]

d) Display a binary tree [5]


#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *r;

struct node *l;

};

#define newnode (struct node*)malloc(sizeof(struct node))

struct node *insert(struct node *,int);

struct node *create()

int n,x,i;

struct node *root;

root=NULL;

printf("\n Enter no of nodes");

scanf("%d",&n);

printf("\nEnter tree values");

for(i=0;i<n;i++)

scanf("%d",&x);
root=insert(root,x);

return root;

struct node *insert(struct node *rt,int x)

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);

void postorder(struct node *tmp)

if(tmp!=NULL)

postorder(tmp->l);

postorder(tmp->r);

printf("%d->",tmp->data);

void preorder(struct node *tmp)

if(tmp!=NULL)

{
printf("%d->",tmp->data);

preorder(tmp->l);

preorder(tmp->r);

struct node *search(struct node *tmp,int x)

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;

struct node *rt;

rt=create();

printf("\nInorder\n");

inorder(rt);
printf("\npostorder\n");

postorder(rt);

printf("\npreorder\n");

preorder(rt);

printf("\nEnter element to insert : ");

scanf("%d",&no);

rt=insert(rt,no);

inorder(rt);

printf("\nEnter the node to be searched \n");

scanf("%d",&y);

rt=search(rt,y);

if(rt!=NULL)

printf("\nNode is found in tree \n");

else

printf("\nNode is not found in tree \n");

**********************

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

that includes stack library and calls different stack operations.

Push & Pop operation carries 8 mark each [16]

Isempty & Peek carries 4 mark each [8]


Init function [1]

#include<stdio.h>

#include<stdlib.h>

#include"dynamic_stack.h"

main()

int n,i=0,ch;

struct node *s;

init();

do

printf("\nchoices are:\n1:push into stack\n2:pop from stack\n3:check whether stack is


empty or not\n4:display \n5:Peek of stack \n6:exit\n");

printf("enter your choice: ");

scanf("%d",&ch);

switch(ch)

case 1:printf("\nenter element:");

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

printf("\nstack is not empty");

break;

case 4: display();

break;

case 5: printf("\n peek of stack is %d ",peek());

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>

#define newnode (struct snode*)malloc(sizeof(struct snode))

struct snode

int data;

struct snode *next;

};

struct snode *create(int no)

struct snode *f,*s;

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;
}

void display(struct snode *f)

struct snode *s;

s=f;

do

{printf("\t%d",s->data);

s=s->next;

}while(s!=f);

struct node * append(struct snode *f,int n)

{ struct snode *t,*s;

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;

struct snode *l1,*l2;

printf("enter no of nodes for 1st link");

scanf("%d",&no);

l1=create(no);

display(l1);

printf("\nenter data to be append : ");

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.

Insert, Delete carries 6 marks each [12]

Display Function [4]

Search Function [5]

Creation of Singly link list [4]

#include<stdio.h>

#include<stdlib.h>
struct node

int data;

struct node *next;

};

struct node* create(int n)

int i=0;

struct node *s,*f;

f=(struct node *)malloc(sizeof(struct node));

printf("\n Enter node 1: ");

scanf("%d",&f->data);

f->next=NULL;

s=f;

for(i=2;i<=n;i++)

s->next=(struct node *)malloc(sizeof(struct node));

s=s->next;

printf("\n Enter node %d: ",i);

scanf("%d",&s->data);

s->next=NULL;

return f;
}

void display(struct node* f)

struct node *t;

for(t=f;t!=NULL;t=t->next)

printf("\t %d ->",t->data);

printf("NULL");

struct node * insert(struct node * f,int pos)

struct node *t,*s;int i,cnt=0;

for(t=f;t!=NULL;t=t->next)

{ cnt++;

if(pos>cnt)

printf("\n Invalid position ");

else

{ s=(struct node *)malloc(sizeof(struct node));

printf("\n Enter data : ");


scanf("%d",&s->data);

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;

struct node* delete(struct node *f,int pos)

struct node *t,*s;int i,cnt=0;

for(t=f;t!=NULL;t=t->next)
{ cnt++;

if(pos>cnt)

{ printf("\n Invalid position ");

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;

void search(struct node* f,int sr)

struct node *t;

int flag=0;

for(t=f;t!=NULL;t=t->next)

if(sr==t->data)

flag=1;

break;

if(flag==1)

printf("\nELEMENT IS FOUND ");

else

printf("\nELEMENT IS NOT PRESENT ");

}
main()

int ch,n,i,m,a,pos,sr,d;

struct node *H;

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:

printf("\nHow many nodes you want to \n");

scanf("%d",&n);

H=create(n);

break;

case 2:printf("\n Link list is :\n");


display(H);

break;

case 3:

printf("\n Enter data to be search : ");

scanf("%d",&sr);

search(H,sr);

break;

case 4: printf("\n Enter position to be data added :");

scanf("%d",&d);

H=insert(H,d);

break;

case 5:printf("\n Enter position to be data deleted :");

scanf("%d",&d);

H=delete(H,d);

break;

case 6: exit(0);

break;

default :printf("\n Invalid choice ");

} 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.

Insert, Delete carries 6 marks each.

#include<stdio.h>

#include<stdlib.h>

#include "doublylist.h"

void main()

int n,ch,sr,d;

struct dnode *H;

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:

printf("\nHow many nodes you want to \n");

scanf("%d",&n);

H=create(n);

break;

case 2:printf("\n Link list is :\n");

display(H);

break;

case 3:

printf("\n Enter data to be search : ");

scanf("%d",&sr);

search(H,sr);

break;

case 4: printf("\n Enter position to be data added :");

scanf("%d",&d);

H=insert(H,d);

break;

case 5:printf("\n Enter position to be data deleted :");


scanf("%d",&d);

H=delete(H,d);

break;

case 6: exit(0);

break;

default :printf("\n Invalid choice ");

}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
{

printf("\nchoices are:\n1:push into stack\n2:pop from stack\n3:check whether stack is empty or


not\n4:check whether stack is full or not\n5:peek(top) element of stack\n6:display elements of stack\
n7:exit\n");

printf("enter your choice: ");

scanf("%d",&ch);

switch(ch)

case 1: printf("\nenter element:");

scanf("%d",&n);

push(n);

break;

case 2:

printf("\ndeleted elements is:%d",pop());

break;

case 3: i=isempty();

if(i==1)

printf("\nstack is empty");

else

printf("\nstack is not empty");

break;

case 4: i=isfull();

if(i==1)

printf("\nstack is full");

else
printf("\nstack is not full");

break;

case 5: printf("\ntop element of stack is:%d",peek());

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

printf("\nchoices are:\n1:push/insert into queue\n2:pop/delete from queue\n3:check whether queue is


empty or not\n4:check whether queue is full or not\n5:peek(top) element of queue\n6:exit\n");

printf("enter your choice: ");

scanf("%d",&ch);

switch(ch)

case 1:printf("\nenter element:");

scanf("%d",&n);

add(&p,n);

break;

case 2:printf("deleted elements is:%d",del(&p));

break;

case 3:i=isempty(&p);

if(i==1)

printf("\nqueue is empty");

else

printf("\nqueue is not empty");

break;

case 4:i=isfull(&p);

if(i==1)
printf("\nqueue is full");

else

printf("queue is not full");

break;

case 5://n1=peek();

printf("top element of queue is:%d",peek(&p));

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

a) Create Binary Search Tree [10]

b) Count a node in binary tree [5]

c) Search a node in binary tree [5]

d) Display a binary tree [5]

#include<stdio.h>

#include<stdlib.h>

struct node

{
int data;

struct node *r;

struct node *l;

};

#define newnode (struct node*)malloc(sizeof(struct node))

struct node *insert(struct node *,int);

struct node *create()

int n,x,i;

struct node *root;

root=NULL;

printf("\n Enter no of nodes");

scanf("%d",&n);

printf("\nEnter tree values");

for(i=0;i<n;i++)

scanf("%d",&x);

root=insert(root,x);

return root;

void inorder(struct node *tmp)

if(tmp!=NULL)
{

inorder(tmp->l);

printf("%d->",tmp->data);

inorder(tmp->r);

struct node *search(struct node *tmp,int x)

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));

int count(struct node *tmp)

static int i=0;

if(tmp==NULL)

return 0;

else if(tmp->l==NULL && tmp->r==NULL)

return 1;

i=count(tmp->l)+count(tmp->r);
return i;

main()

int no,y;

struct node *rt;

rt=create();

printf("\nInorder\n");

inorder(rt);

printf("\nEnter the node to be searched \n");

scanf("%d",&y);

rt=search(rt,y);

if(rt!=NULL)

printf("\nNode is found in tree \n");

else

printf("\nNode is not found in tree \n");

cnt=count(rt);

printf("\n No of child nodes are : %d",cnt);


}

**************************

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

printf("\nchoices are:\n1:push/insert into queue\n2:pop/delete from queue\n3:check whether queue is


empty or not\n4:check whether queue is full or not\n5:peek(top) element of queue\n6:exit\n");

printf("enter your choice: ");

scanf("%d",&ch);

switch(ch)

{
case 1:printf("\nenter element:");

scanf("%d",&n);

add(&p,n);

break;

case 2:printf("deleted elements is:%d",del(&p));

break;

case 3:i=isempty(&p);

if(i==1)

printf("\nqueue is empty");

else

printf("\nqueue is not empty");

break;

case 4:i=isfull(&p);

if(i==1)

printf("\nqueue is full");

else

printf("queue is not full");

break;

case 5://n1=peek();

printf("top element of queue is:%d",peek(&p));

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;

printf("\nenter the string:");

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)

printf("\ngiven string is palindron");

else

printf("\ngiven string is not palindrom");

************************

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.

same as Slip no : 24_2

#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]);

printf("\nstring in reversed order is:");

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.

Slip no 18_1, 19-1, 22_1, 24_1, 14_2, 21_2, 26_2 */

#include<stdio.h>

#include<stdlib.h>

#include "doublylist.h"

void main()

{
int n,ch,sr,d;

struct dnode *H;

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:

printf("\nHow many nodes you want to \n");

scanf("%d",&n);

H=create(n);

break;

case 2:printf("\n Link list is :\n");

display(H);

break;
case 3:

printf("\n Enter data to be search : ");

scanf("%d",&sr);

search(H,sr);

break;

case 4: printf("\n Enter position to be data added :");

scanf("%d",&d);

H=insert(H,d);

break;

case 5:printf("\n Enter position to be data deleted :");

scanf("%d",&d);

H=delete(H,d);

break;

case 6: exit(0);

break;

default :printf("\n Invalid choice ");

}while(ch!=6);

***********************

You might also like