Beruflich Dokumente
Kultur Dokumente
#include<stdio.h>
#include<conio.h>
int n,i,a[10];
int element,position;
void create();
void display();
void insert();
void delet();
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n enter you choice:");
printf("\n 1.create\n 2.display\n 3.insert\n 4.delete\n 5.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:create();
break;
case 2:display();
break;
case 3:insert();
break;
case 4:delet();
break;
default:exit(0);
}
getch();
}
}
void create()
{
printf("enter the number of elements:\n");
scanf("%d",&n);
printf("enter the array elements:\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
}
void display()
{
printf("the array elements are:\n");
for(i=0;i<=n-1;i++)
{
printf("%d\t",a[i]);
}
}
void insert()
{
printf("enter the element tobe inserted:\n");
scanf("%d",&element);
printf("enter the position:\n");
scanf("%d",&position);
i=n-1;
while(position<=i)
{
a[i+1]=a[i];
i--;
}
a[position]=element;
n++;
}
void delet()
{
printf("enter the postion tobe deleted:\n");
scanf("%d",&position);
i=position+1;
while(i<=n-1)
{
a[i-1]=a[i];
i++;
}
n--;
}
OUTPUT
Support the program with functions for each of the above the
operations. Don’t use built-in functions.
#include<stdio.h>
#include<string.h>
int str_cmp(char p[],char t[],int i)
{
int j,m;
j=0;
m=strlen(p);
while(j<m&&p[j]==t[i+j])
j++;
if(j==m)
return 1;
return-1;
}
void my_search_replace(char p[],char t[],char r[],char d[])
{
int i,j,k,m,n,flag=0;
i=j=k=0;
n=strlen(t);
m=strlen(p);
while(i<=n-m)
{
flag=str_cmp(p,t,i);
if(flag==1)
{
while(j<i)
d[k++]=t[j++];
j=0;
while(j<strlen(r))
d[k++]=r[j++];
i=i+strlen(p);
j=i;
}
else
i++;
}
while(j<n)
d[k++]=t[j++];
d[k]='\0';
}
void main()
{
char t[40],p[10],r[10],d[40];
clrscr();
printf("enter the string t:");
gets(t);
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 6
Data Structure lab programs 2016
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<process.h>
void push();
void pop();
void display();
int s[5];
int i,top=-1,size=5;
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n enter your choice:");
printf("\n 1.push\n 2.pop\n 3.display\n 4.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default:exit(0);
}
getch();
}
}
void push()
{
int elem;
if(top==size-1)
{
printf("stack is overflow\n");
}
else
{
printf("enter an element tobe pushed:\n");
scanf("%d",&elem);
top=top+1;
s[top]=elem;
}
}
void pop()
{
if(top==-1)
{
printf("stack is underflow\n");
}
else
{
printf("element deleted is: %d\t",s[top]);
top=top-1;
}
}
void display()
{
if(top==-1)
{
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<string.h>
int ir(char symbol);
int sr(char symbol);
void infix_to_postfix(char infix[],char postfix[]);
void main()
{
char infix[20],postfix[20];
clrscr();
printf("enter infix expression is :");
scanf("%s",infix);
infix_to_postfix(infix,postfix);
printf("postfix expression is: %s",postfix);
getch();
}
int ir(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '$':
case '^':return 6;
case '(':return 9;
case ')':return 0;
default:return 7;
}
}
int sr(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '$':
case '^':return 5;
case '(':return 0;
case '#':return -1;
default:return 8;
}
}
void infix_to_postfix(char infix[],char postfix[])
{
int i,j=0;
int top=-1;
char symbol;
char s[10];
s[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(sr(s[top])>ir(symbol))
{
postfix[j++]=s[top--];
}
if(sr(s[top])<ir(symbol))
{
s[++top]=symbol;
}
if(sr(s[top])==ir(symbol))
{
top--;
}
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
int compute(int oprd1,char symbol,char oprd2)
{
switch(symbol)
{
case '+':return oprd1+oprd2;
case '-':return oprd1-oprd2;
case '*':return oprd1*oprd2;
case '/':return oprd1/oprd2;
case '$':
case '^':return pow(oprd1,oprd2);
}
}
void evalpost(char postfix[])
{
int oprd1,oprd2,res,s[20],i;
int top=-1;
char symbol;
for(i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if(isdigit(symbol))
{
s[++top]=symbol -'0';
}
else
{
oprd2=s[top--];
oprd1=s[top--];
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 15
Data Structure lab programs 2016
res=compute(oprd1,symbol,oprd2);
s[++top]=res;
}
}
printf("result is:%d\n",res);
}
void main()
{
char postfix[20];
clrscr();
printf("enter postfix expression:\n");
scanf("%s",postfix);
evalpost(postfix);
getch();
}
OUTPUT
#include<stdio.h>
#include<conio.h>
void TOWER();
void main()
{
int n;
clrscr();
printf("enter the number of disc\n");
scanf("%d",&n);
TOWER(n,'S','T','D');
getch();
}
void TOWER(int n,char source,char temp,char destination)
{
if(n==0)
return;
TOWER(n-1,source,destination,temp);
printf("move disc %d from %c to %c \n",n,source,destination);
TOWER(n-1,temp,source,destination);
}
OUTPUT
#include<stdio.h>
#include<conio.h>
void cq_insert();
void cq_delet();
void cq_display();
int q[5];
int size=5;
int front=0,rear=-1;
int count=0;
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n enter your choice:");
printf("\n 1.cq_insert\n 2.cq_delete\n 3.cq_display\n 4.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:cq_insert();
break;
case 2:cq_delet();
break;
case 3:cq_display();
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 19
Data Structure lab programs 2016
break;
default:exit(0);
}
getch();
}
}
void cq_insert()
{
int elem;
if(count==size)
{
printf("insertion is not possible\n");
}
else
{
printf("enter an element tobe inserted:\n");
scanf("%d",&elem);
rear=(rear+1)%size;
q[rear]=elem;
if(front==1)
{
front=0;
}
count++;
}
}
void cq_delet()
{
if(count==0)
{
printf("deletion is not possible\n");
}
else
{
printf("element deleted is: %d\t",q[front]);
front=(front+1)%size;
count--;
}
}
void cq_display()
{
int i,f=front;
if(count==0)
{
printf("circular queue is empty\n");
}
else
{
printf("the queue elements are:\n");
for(i=1;i<=count;i++)
{
printf("%d\t",q[f]);
f=(f+1)%size;
}
}
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int usn;
char name[20];
char branch[20];
int sem;
char phone[20];
struct node *link;
};
struct node *first=NULL;
void ins_front();
void del_front();
void ins_rear();
void del_rear();
void display();
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n 1.insert_front\n 2.delete_front\n 3.insert_rear\n
4.delete_rear\n 5.display\n 6.exit\n");
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 24
Data Structure lab programs 2016
void del_front()
{
if(first==NULL)
{
printf("student list is empty deletion not possible\n");
}
else if(first->link==NULL)
{
printf("element deleted is %d\n",first->usn);
first=NULL;
}
else
{
printf("element deleted is %d\n",first->usn);
first=first->link;
}
}
void ins_rear()
{
struct node *p,*temp;
p=malloc(sizeof(struct node));
printf("enter Usn Name Branch Sem Phone\n");
scanf("%d %s %s %d %s",&p->usn,p->name,p->branch,&p-
>sem,p->phone);
p->link=NULL;
if(first==NULL)
{
first=p;
}
else
{
temp=first;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=p;
}
}
void del_rear()
{
struct node *prev,*cur;
if(first==NULL)
{
printf("student list is empty deletion is not possible\n");
}
else if(first->link==NULL)
{
printf("element deleted is %d\n",first->usn);
first=NULL;
}
else
{
cur=first;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
printf("element deleted is %d\n",cur->usn);
prev->link=NULL;
}
}
void display()
{
struct node *temp;
int count=0;
if(first==NULL)
{
printf("list is empty display not possible\n");
}
else
{
temp=first;
while(temp!=NULL)
{
printf("%d %s %s %d %s",temp->usn,temp-
>name,temp->branch,temp->sem,temp->phone);
temp=temp->link;
count=count++;
}
printf("\n number of students is:%d\n",count);
}
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct employ
{
int ssn;
char name[20];
char dept[20];
char desgn[20];
float salary;
char phone[20];
struct employ *llink;
struct employ *rlink;
};
struct employ *first=NULL;
void ins_front();
void del_front();
void ins_rear();
void del_rear();
void display();
void main()
{
int choice;
clrscr();
while(1)
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 31
Data Structure lab programs 2016
{
printf("\n 1.ins_front\n 2.del_front\n 3.ins_rear\n 4.del_rear\n
5.display\n 6.exit\n");
printf("enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:ins_front();
break;
case 2:del_front();
break;
case 3:ins_rear();
break;
case 4:del_rear();
break;
case 5:display();
break;
case 6:exit(0);
break;
}
}
}
void ins_front()
{
struct employ *p;
float sal;
p=malloc(sizeof(struct employ));
printf("enter ssn,name,dept,desgn,salary,phone\n");
scanf("%d%s%s%s%f%s",&p->ssn,p->name,p->dept,p->desgn,&sal,p-
>phone);
p->salary=sal;
p->llink=NULL;
p->rlink=NULL;
if(first==NULL)
{
first=p;
}
else
{
p->rlink=first;
first->llink=p;
first=p;
}
}
void del_front()
{
if(first==NULL)
{
printf("list is empty deletion not possible\n");
}
else if(first->rlink==NULL)
{
printf("element deleted is: %d",first->ssn);
first=NULL;
}
else
{
printf("element deleted is: %d",first->ssn);
first=first->rlink;
first->llink=NULL;
}
}
void ins_rear()
{
struct employ *p,*temp;
float sal;
p=malloc(sizeof(struct employ));
printf("enter ssn,name,dept,desgn,salary,phone\n");
scanf("%d%s%s%s%f%s",&p->ssn,p->name,p->dept,p->desgn,&sal,p-
>phone);
p->salary=sal;
p->llink=NULL;
p->rlink=NULL;
if(first==NULL)
{
first=p;
}
else
{
temp=first;
while(temp->rlink!=NULL)
{
temp=temp->rlink;
}
temp->rlink=p;
p->llink=temp;
}
}
void del_rear()
{
struct employ *prev,*cur;
if(first==NULL)
{
printf("list is empty deletion not possible\n");
}
else if(first->rlink==NULL)
{
printf("employ record deleted is: %d",first->ssn);
first=NULL;
}
else
{
cur=first;
while(cur->rlink!=NULL)
{
prev=cur;
cur=cur->rlink;
}
printf("employ record deleted is: %d",cur->ssn);
free(cur);
prev->rlink=NULL;
}
}
void display()
{
struct employ *p1;
float sal;
int count=0;
if(first==NULL)
{
printf("list is empty display not possible\n");
}
else
{
p1=first;
while(p1!=NULL)
{ sal=p1->salary;
printf("%d %s %s %s %f %s\t",p1->ssn,p1->name,p1-
>dept,p1->desgn,sal,p1->phone);
p1=p1->rlink;
count=count+1;
}
printf("\n number of employees is: %d\n",count);
}
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int cf,px,py,pz;
struct node *link;
};
typedef struct node *NODE;
NODE insert_rear(int cf,int px,int py,int pz,NODE head)
{
NODE temp,cur;
temp=malloc(sizeof(struct node));
temp->cf=cf;
temp->px=px;
temp->py=py;
temp->pz=pz;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
NODE readpoly(NODE head)
{
int i,n;
int cf,px,py,pz;
printf("enter the number of terms in the poly\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter the term: %d\n",i);
printf("cf,px,py,pz=");
scanf("%d%d%d%d",&cf,&px,&py,&pz);
head=insert_rear(cf,px,py,pz,head);
}
return head;
}
void display(NODE head)
{
NODE temp;
if(head->link==head)
{
printf("polynomial does not exist\n");
return;
}
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 39
Data Structure lab programs 2016
temp=head->link;
while(temp!=head)
{
if(temp->cf<0)
printf("%d",temp->cf);
else
printf("+%d",temp->cf);
if(temp->px!=0)
printf("x^%d",temp->px);
if(temp->py!=0)
printf("y^%d",temp->py);
if(temp->pz!=0)
printf("z^%d",temp->pz);
temp=temp->link;
}
printf("\n");
}
float evaluate(NODE head)
{
int x,y,z;
float sum=0;
NODE p;
printf("enter the value of x,y,z\n");
scanf("%d%d%d",&x,&y,&z);
p=head->link;
while(p!=head)
{
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 40
Data Structure lab programs 2016
sum=sum+p->cf*pow(x,p->px)*pow(y,p->py)
*pow(z,p->pz);
p=p->link;
}
return sum;
}
void main()
{
NODE head;
float result;
clrscr();
head=malloc(sizeof(struct node));
head->link=head;
printf("enter the polynomial\n");
head=readpoly(head);
result=evaluate(head);
printf("the given polynomial is\n");
display(head);
printf("the result is=%f\n",result);
getch();
}
OUTPUT
10. Design, Develop and implement a menu driven program in c for the
following operations on Binary Search tree (BST) of integers:-
a) Create a BST of N integers: 6, 9, 5, 2, 8, 1, 24, 14, 7,8, 5, 2
b) Traverse the BST in In order, preorder and Post order.
c) Search the ST for a given element (KEY) and report the
appropriate message
d) Exit
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
struct node*llink;
int data;
struct node*rlink;
};
struct node *root=NULL;
void insert();
void preorder(struct node*root);
void inorder(struct node*root);
void postorder(struct node*root);
void search();
void main()
{
int choice;
clrscr();
while(1)
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 43
Data Structure lab programs 2016
{
printf("\n1.insert\n 2.preorder\n 3.inorder\n 4.postorder\n 5.search\n
6.exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:preorder(root);
break;
case 3:inorder(root);
break;
case 4:postorder(root);
break;
case 5:search();
break;
default:exit(0);
}
}
}
void insert()
{
int elem;
struct node*p,*cur,*prev;
p=malloc(sizeof(struct node));
printf("enter the element to be inserted\n");
scanf("%d",&elem);
p->llink=NULL;
p->data=elem;
p->rlink=NULL;
if(root==NULL)
{
root=p;
}
else
{
prev=NULL;
cur=root;
while(cur!=NULL)
{
if(elem<cur->data)
{
prev=cur;
cur=cur->llink;
}
else
{
prev=cur;
cur=cur->rlink;
}
}
if(elem<prev->data)
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 45
Data Structure lab programs 2016
{
prev->llink=p;
}
else
{
prev->rlink=p;
}
}
}
{
if(root==NULL)
return;
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->data);
}
void search()
{
int key;
struct node *cur;
printf("enter the key element to be searched\n");
scanf("%d",&key);
cur=root;
while(cur!=NULL)
{
if(key==cur->data)
{
printf("element found\n");
getch();
exit(0);
return;
}
if(key<cur->data)
{
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 47
Data Structure lab programs 2016
cur=cur->llink;
}
else
{
cur=cur->rlink;
}
}
printf("element not found\n");
}
OUTPUT
#include<stdio.h>
#include<conio.h>
int n,a[10][10],vis[10];
int i,j,src;
void bfs(int);
void main()
{
clrscr();
printf("enter the number of vertices\n");
scanf("%d",&n);
printf("enter the adjancency matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the source node\n");
scanf("%d",&src);
bfs(src);
printf("reachability is given below \n");
for(i=1;i<=n;i++)
{
if(vis[i]==1)
{
printf("%d is reachable\n",i);
}
else
{
printf("%d is not reachable\n",i);
}
}
getch();
}
void bfs(int src)
{
int f=0,r=-1,q[10];
for(i=1;i<=n;i++)
{
vis[i]=0;
}
vis[src]=1;
r=r+1;
q[r]=src;
while(f<=r)
{
i=q[f];
f=f+1;
for(j=1;j<=n;j++)
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 52
Data Structure lab programs 2016
{
if(a[i][j]==1&&vis[j]==0)
{
vis[j]=1;
r=r++;
q[r]=j;
}
}
}
}
OUTPUT
-:Viva Questions:-
3. What is stack?
4. What is queue?
8. What is pointer?
44. What are the differences between singly linked list and doubly linked
list?
45. What is tree?