Sie sind auf Seite 1von 123

S.Y.B.C.A.

Sem-III Practical Slip Oct/April 2014 - 2015


Data Structure Using ‘C’
Q. 2) Write a ‘C’ program to reverse a string using Static implementation of Stack.

[Marks 15]
#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();
}

Q. 3) Write a menu driven program using ‘C’ for singly linked list-
- To create linked list.
- To display linked list
- To insert node at last position of linked list.
- To delete node from specific position of linked list.
[Marks 25]
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start,*q,*temp;
int cnt;
void create();
void display();
void first();

Slip 1
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
void del();
void count();
void main()
{
char ch='y';
int choice;
clrscr();
while(ch=='y'||ch=='Y')
{
printf("\n1 :to create list");
printf("\n2 :to display list");
printf("\n3 :add node at first position:");
printf("\n4 :delete a node in between by position of list");
printf("\n\nenter your choice:-");
scanf("%d",&choice);
switch(choice)
{
case 1:while(ch=='y'||ch=='Y')
{
create ();
printf("\ndo you want to insert element(Y|N):-");
scanf("\n%s",&ch);
}
break;
case 2:
display();
break;
case 3:first();
break;
case 4:if(start==NULL)
printf("\nlinked list is empty");
else
del();
break;
}
printf("\n\ndo you want to enter another choice(Y|N):-");
scanf("\n%s",&ch);
}
getch();
}
void create()
{
temp=malloc(sizeof(struct node));
printf("\nenter the number:-");
scanf("%d",&temp->data);
temp->next=NULL;

Slip 2
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
if(start==NULL)
start=temp;
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
}
void display()
{
if(start==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in singly linked list are:-\n");
q=start;
while(q!=NULL)
{
printf("%d\n",q->data);
q=q->next;
}
}
}
void first()
{
temp=malloc(sizeof(struct node));
printf("\nenter the number:-");
scanf("%d",&temp->data);
temp->next=start;
start=temp;
printf("\n\n");
}
void del()
{
int pos,i;
printf("\nenter the position which you want to delete:-");
scanf("%d",&pos);
count();
if(pos==1||pos==cnt||pos>cnt)
{
printf("\nyou enter wrong position");
}

Slip 3
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
else
{
q=start;
for(i=0;i<pos-2;i++)
{
q=q->next;
}
temp=q->next;
q->next=temp->next;
free(temp);
q=q->next;
printf("%d",pos);
}
}
void count()
{
q=start;
while(q!=NULL)
{
q=q->next;
cnt++;
}
}
Q. 2) Write a ‘C’ program to evaluate a given polynomial using function. (Use array).
[Marks 15]
#include<stdio.h>
#include<conio.h>
#include<math.h>
int eval(int [],int,int);
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)
{

Slip 4
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
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;
}

Q. 3) Write a ‘C’ program to accept an infix expression, convert it into its equivalent postfix
expression and display the result.
#include<stdio.h>
#include<conio.h>
char st[100];
int top=-1;
int prec(char c)
{
switch(c)
{
case ‘(‘:
return 0;
case ‘+’:
case ‘-‘:
return 1;
case ‘*’:
case ‘/’:
case ‘%’:
return 2;
case ‘^’:
return 3;
}
return 0;
}
void push(char c)

Slip 5
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘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 ch,s1[30],s2[30];
clrscr();
printf(“\nEnter the Infix Expr”);
gets(s1);
j=0;
for(i=0;s1[i]!=”;i++)
{
switch(s1[i])
{
case ‘(‘:
case ‘+’:
case ‘-‘:
case ‘/’:
case ‘*’:
case ‘^’:
p=prec(s1[i]);
while(top!=-1 && p<=prec(st[top]))
s2[j++]=pop();
push(s1[i]);
break;
case ‘)’:
do
{
ch=pop();
if(ch!='(‘)
{
s2[j++]=ch;
}
}
while(ch!='(‘);
break;
default:

Slip 6
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
s2[j++]=s1[i];
}
}
while(top!=-1)
{
s2[j++]=pop();
}
s2[j]=”;
printf(“\nPostFix Expr %s”,s2);
// getch();
// return 0;
// }
printf(“\n\nEval of PostFix Expr is \n\n”);
j=0;
for(i=0;s2[i]!=”;i++)
{
if(s2[i]<=’9′ && s2[i]>=’0’)
push(s2[i]-48);
else
{
a=pop();
b=pop();
switch(s2[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();

Slip 7
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf(“Result is %d “,j);
getch();
return;
}

Q. 2) Write a ‘C’ program for implementing Linear Search method using function.
[Marks 15]
#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++)

Slip 8
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
if(a[i]==k)
{
return i;
}
}
if(i==n)
{
return -1;
}
}

Q. 3) Write a ‘C’ program to create linked list with given number in which data part of each
node contains individual digit of the number.
(Ex. Suppose the number is 584 then the nodes of linked list should contain 5, 8, 4)

[Marks 25]
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void del(int);
void main()
{
int ch,n,i,k,r=0,m;
clrscr();
start=NULL;
do
{
printf(“\n\nMENU\n\n”);
printf(“\n1.CREATE\n”);
printf(“\n2.DISPLAY\n”);
printf(“\n3.DELETE\n”);
printf(“\n4.EXIT\n”);
printf(“\nENTER UR CHOICE\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n\nENTER THE NUMBER\n”);
scanf(“%d”,&n);

Slip 9
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
while(n>0)
{
k=n%10;
n=n/10;
r=r*10+k;
}
while(r>0)
{
k=r%10;
create(k);
r=r/10;
}
break;
case 2:
disp();
break;
case 4:
exit(0);
case 3:
printf(“\n\nENTER THE DATA FOR DELETE”);
scanf(“%d”,&m);
del(m);
break;
}
}
while(ch!=4);
getch();
}
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;
}
}

Slip 10
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
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”);
}
}
void addbeg(int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node ));
tmp->data=data;
tmp->link=start;
start=tmp;
}
void addlast(int data)
{
struct node *q,*tmp;
tmp=(struct node *) malloc(sizeof(struct node));
tmp->data=data;
tmp->link=NULL;
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
void addpos(int data,int pos)
{
int i;
struct node *q,*tmp;
tmp=(struct node *) malloc(sizeof(struct node));
tmp->data=data;
q=start;
for(i=0;i<pos-1;i++)
{

Slip 11
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
q=q->link;
}
tmp->link=q->link;
q->link=tmp;
}
void del(int m)
{
struct node *q,*tmp;
if(start->data==m)
{
tmp=start;
start=start->link;
free(tmp);
}
else
{
q=start;
while(q->link->link!=NULL)
{
if(q->link->data==m)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
}
q=q->link;
}
}
if(q->link->data==m)
{
tmp=q->link;
q->link=NULL;
free(tmp);
}
}

Q. 2) Write a ‘C’ program that create a 2-D table of integers whose size will be specified at run
time. (Dynamic Memory Allocation)
[Marks 15]
Q. 3) Write a menu driven program in ‘C’ for static implementation of Circular Queue for
integers. The menu includes
- Insert
- Delete
- Display
- Exit

Slip 12
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

[Marks 25]
#include<stdio.h>
#include<conio.h>
#define MAX 3
int a[MAX];
int front=-1;
int rear=-1;
void insert();
void del();
void disp();
void main()
{
int ch;
clrscr();
do
{
printf(“\nMenu \n”);
printf(“\n1.INSERT”);
printf(“\n2.del”);
printf(“\n3.disp”);
printf(“\n4.exit”);
printf(“\nEnter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
void insert()
{
int n;
if((front==0 && rear==MAX-1)||(front==rear+1 && front>0))

Slip 13
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
printf(“Q is Full”);
}
else
{
if(front==-1)
{
front=0;
}
else
{
if(rear==MAX-1 && front>0)
{
rear=0;
printf(“\n\nENTER THE DATA”);
scanf(“%d”,&n);
a[rear]=n;
}
else
{
rear++;
printf(“\nEnter the Element”);
scanf(“%d”,&n);
a[rear]=n;
}
}
}
}
void del()
{
if(front==-1)
{
printf(“\nQ is Empty”);
}
else
{
printf(“Deleted Element is %d”, a[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
if(front==MAX-1)
{

Slip 14
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
front=0;
}
else
{
front++;
}
}
}
}
void disp()
{
int i;
if(front==-1)
{
printf(“\nQ is Empty”);
}
else
{
if(front<=rear)
{
for(i=front;i<=rear;i++)
{
printf(“%d->”,a[i]);
}
}
else
{
for(i=front;i<=MAX-1;i++)
{
printf(“%d->”,a[i]);
}
for(i=0;i<=rear;i++)
{
printf(“%d->”,a[i]);
}
}
}
}

Q. 2) Write a ‘C’ program for addition of two polynomials using array.

[Marks 15]
#include<stdio.h>
#include<conio.h>

main()

Slip 15
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
int a[10], b[10], c[10],m,n,k,k1,i,j,x;

clrscr();

printf("\n\tPolynomial Addition\n");
printf("\t===================\n");

printf("\n\tEnter the no. of terms of the polynomial:");


scanf("%d", &m);

printf("\n\tEnter the degrees and coefficients:");


for (i=0;i<2*m;i++)
scanf("%d", &a[i]);

printf("\n\tFirst polynomial is:");

k1=0;
if(a[k1+1]==1)
printf("x^%d", a[k1]);
else
printf("%dx^%d", a[k1+1],a[k1]);
k1+=2;

while (k1<i)
{
printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;
}

printf("\n\n\n\tEnter the no. of terms of 2nd polynomial:");


scanf("%d", &n);

printf("\n\tEnter the degrees and co-efficients:");

for(j=0;j<2*n;j++)
scanf("%d", &b[j]);
printf("\n\tSecond polynomial is:");

k1=0;
if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);

Slip 16
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
k1+=2;
while (k1<2*n)
{
printf("+%dx^%d", b[k1+1],b[k1]);
k1+=2;
}

i=0;
j=0;
k=0;

while (m>0 && n>0)


{
if (a[i]==b[j])
{
c[k+1]=a[i+1]+b[j+1];
c[k]=a[i];
m--;
n--;
i+=2;
j+=2;
}
else if (a[i]>b[j])
{
c[k+1]=a[i+1];
c[k]=a[i];
m--;
i+=2;
}
else
{
c[k+1]=b[j+1];
c[k]=b[j];
n--;
j+=2;
}
k+=2;
}

while (m>0)
{
c[k+1]=a[i+1];
c[k]=a[i];
k+=2;
i+=2;

Slip 17
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
m--;
}

while (n>0)
{
c[k+1]=b[j+1];
c[k]=b[j];
k+=2;
j+=2;
n--;
}

printf("\n\n\n\n\tSum of the two polynomials is:");


k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]);
else
printf("%dx^%d", c[k1+1],c[k1]);
k1+=2;

while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]);
else
printf("+%dx^%d", c[k1+1], c[k1]);
k1+=2;
}

getch();
return 0;

}
OR
#include<stdio.h>
void main()
{
int poly1[6][2],poly2[6][2],term1,term2,match,proceed,i,j;
printf("Enter the number of terms in first polynomial : ");
scanf("%d",&term1);
printf("Enter the number of terms in second polynomial : ");
scanf("%d",&term2);
printf("Enter the coeff and expo of the first polynomial:\n");
for(i=0;i<term1;i++)

Slip 18
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
scanf("%d %d",&poly1[i][0],&poly1[i][1]);
}
printf("Enter the coeff and expo of the second polynomial:\n");
for(i=0;i<term2;i++)
{
scanf("%d %d",&poly2[i][0],&poly2[i][1]);
}
printf("The resultant polynomial after addition :\n");
for(i=0;i<term1;i++)
{
match=0;
for(j=0;j<term2;j++)
{
if(match==0)
if(poly1[i][1]==poly2[j][1])
{
printf("%d %d\n",(poly1[i][0]+poly2[j][0]), poly1[i][1]);
match=1;
}
}
}
for(i=0;i<term1;i++)
{
proceed=1;
for(j=0;j<term2;j++)
{
if(proceed==1)
if(poly1[i][1]!=poly2[j][1])
proceed=1;
else
proceed=0;
}
if(proceed==1)
printf("%d %d\n",poly1[i][0],poly1[i][1]);
}
for(i=0;i<term2;i++)
{
proceed=1;
for(j=0;j<term1;j++)
{
if(proceed==1)
if(poly2[i][1]!=poly1[j][1])
proceed=1;
else
proceed=0;

Slip 19
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}
if(proceed==1)
printf("%d %d",poly2[i][0],poly2[i][1]);
}
getch();
}

Q. 3) Write a ‘C’ program to create a singly linked list and count total number of nodes in it
and display the result.
[Marks 25]
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void ser(int);
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.SEARCH \n”);
printf(“\n5.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);

Slip 20
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}
break;
case 3:
count();
break;
case 2:
disp();
break;
case 4:
printf(“\nENTER THE ELEMENT FOR SEARCH”);
scanf(“%d”,&m);
ser(m);
break;
case 5:
exit(0);
}
}
while(ch!=7);
getch();
}
void count()
{
struct node *q;
int cnt=0;
q=start;
while(q!=NULL)
{
cnt++;
q=q->link;
}
printf(“\n\No Of Nodes Are\t %d”,cnt);
}
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)

Slip 21
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
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”);
}
}
void ser(int data)
{
struct node *q,*tmp;
q=start;
while(q!=NULL)
{
if(q->data==data)
{
printf(“\nElement Is Found”);
break;
}
else
{
q=q->link;
}
}
if(q==NULL)
{
printf(“\nElement is Not Found”);
}
}

Q. 2) Write a ‘C’ program to search given elements into the list using Non-Recursive Binary
Search Method.

Slip 22
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
[Marks 15]
#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();
}

Q. 3) Write a menu driven program using ‘C’ for singly linked list -
- To create linked list.
- To display linked list
- To search node in linked list.
- Insert at last position
[Marks 25]
#include<stdio.h>
#include<conio.h>
struct node
{

Slip 23
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void ser(int);
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.SEARCH \n”);
printf(“\n5.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:
printf(“\nENTER THE ELEMENT FOR SEARCH”);
scanf(“%d”,&m);
ser(m);
break;
case 5:

Slip 24
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
exit(0);
}
}
while(ch!=7);
getch();
}
void count()
{
struct node *q;
int cnt=0;
q=start;
while(q!=NULL)
{
cnt++;
q=q->link;
}
printf(“\n\No Of Nodes Are\t %d”,cnt);
}
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;

Slip 25
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
printf(“NULL”);
}
}
void ser(int data)
{
struct node *q,*tmp;
q=start;
while(q!=NULL)
{
if(q->data==data)
{
printf(“\nElement Is Found”);
break;
}
else
{
q=q->link;
}
}
if(q==NULL)
{
printf(“\nElement is Not Found”);
}
}

Q. 2) Write a ‘C’ program to search given element into the list using Recursive Binary search
method.
[Marks 15]
#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)

Slip 26
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
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);
}
}

Q. 3) Write a ‘C’ program to create two singly linked lists and perform the union of two lists
and display it.

Slip 27
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
[Marks 25]
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *alloc(int);
NODE *create();
NODE *findlast(NODE *);
NODE *unionlist(NODE *,NODE *);
NODE *search(NODE *,int);
void display(NODE *);
NODE *getnode();
void main()
{
NODE *list1=NULL,*list2=NULL,*list3=NULL;
clrscr();
printf(“\nCreate first list.”);
list1=create();
printf(“\nCreate second list.”);
list2=create();
printf(“\n Data in first list: “);
display(list1);
printf(“\n Data in second list: “);
display(list2);
printf(“\n\n Union of two list is: “);
list3=unionlist(list1,list2);
if(list3==NULL)
printf(“\nList of union is empty”);
else
display(list3);
getch();
}
NODE *alloc(int val)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=NULL;
return temp;
}
NODE *getnode()
{
NODE *temp;

Slip 28
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
temp=(NODE *)malloc(sizeof(NODE));
printf(“\nEnter the data: “);
scanf(“%d”,&temp->data);
temp->next=NULL;
return temp;
}
NODE *search(NODE *list,int val)
{
NODE *ptr;
for(ptr=list;ptr!=NULL && ptr->data!=val;ptr=ptr->next);
return(ptr);
}
NODE *create()
{
NODE *ptr,*temp,*list=NULL;
int n,i;
printf(“\n Enter how many nodes : “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
temp=getnode();
if(list==NULL)
{
list=temp;
}
else
{
ptr=findlast(list);
ptr->next=temp;
}
}
return(list);
}
NODE *findlast(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr->next!=NULL;ptr=ptr->next);
return(ptr);
}
NODE *unionlist(NODE *list1,NODE *list2)
{
NODE *temp,*ptr1,*ptr2,*list3=NULL;
int i,val;
for(ptr1=list1;ptr1!=NULL;ptr1=ptr1->next)
{
temp=alloc(ptr1->data);

Slip 29
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
if(list3==NULL)
list3=temp;
else
{
ptr2=findlast(list3);
ptr2->next=temp;
}
}
for(ptr1=list2;ptr1!=NULL;ptr1=ptr1->next)
{
ptr2=search(list1,ptr1->data);
if(ptr2==NULL)
{
temp=alloc(ptr1->data);
ptr2=findlast(list3);
ptr2->next=temp;
}
}
return(list3);
}
void display(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr!=NULL;ptr=ptr->next)
printf(“%d->”,ptr->data);
printf(“NULL”);
}

Q. 2) Write a C program that create a 1-D table of integers whose size will be specified at run
time. (Dynamic Memory Allocation)
[Marks 15]
#include <stdio.h>
#include <stdlib.h>

int main()
{
int num, i, *ptr, sum = 0;

printf("Enter number of elements: ");


scanf("%d", &num);

ptr = (int*) malloc(num * sizeof(int)); //memory allocated using malloc


if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);

Slip 30
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
}
printf("Elements of array are : ");
for(i = 0; i < num; ++i)
{
printf("Element = %d", ptr + i);
free(ptr);
return 0;
}

Q. 3) Write a ‘C’ program to create a singly linked list, reverse it and display both the list.
[Marks 25]
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start;
void createlist(int);
void disp();
void main()
{
struct list *p1,*p2,*p3;
int i,n,m;
clrscr();
printf(“\nHow many nodes u Want to Created”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\n\nEnter the Data”);
scanf(“%d”,&m);
createlist(m);
}
printf(“\n\nCreated List Is\n\n”);
disp();
printf(“\n\nReverses List is\n\n”);
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;

Slip 31
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
disp();
getch();
}
void createlist(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;
q=start;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
printf(“NULL”);
}
Q. 2) Write a ‘C’ program to sort array elements using Bubble sort method.
[Marks 15]
#include<stdio.h>
#include<conio.h>
void main()
{

Slip 32
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
int a[10],i,n,j,k,tmp;
clrscr();
printf(“\n\nHOW MANY ELEMENTS U WANT”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nENTER THE ELEMENT a[%d]\t”,i);
scanf(“%d”,&a[i]);
}
printf(“\nUNSORTED LIST IS \n\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
printf(“\n\nELEMENTS AFTER PASS:-%d \t\n”,i);
for(k=0;k<n;k++)
{
printf(“%d\t”,a[k]);
}
}
printf(“\nSorted List is \n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}
Q. 3) Write a ‘C’ program to create two singly linked lists and perform the intersection
operations on two lists and display the resultant list.
[Marks 25]
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct node
{

Slip 33
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
int data;
struct node* next;
};
/* A utility function to insert a node at the begining of a linked list*/
void push (struct node** head_ref, int new_data);
/* A utilty function to chec if given data is present in a list */
int isPresent (struct node *head, int data);
/* Function to get union of two linked lists head1 and head2 */
/* Function to get intersection of two linked lists head1 and head2 */
struct node *getIntersection (struct node *head1, struct node *head2)
{
struct node *result = NULL;
struct node *t1 = head1;
// Traverse list1 and search each element of it in list2. If the element
// is present in list 2, then insert the element to result
while (t1 != NULL)
{
if (isPresent(head2, t1->data))
push (&result, t1->data);
t1 = t1->next;
}
return result;
}
/* A utility function to insert a node at the begining of a linked list*/
void push (struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* A utility function to print a linked list*/
void printList (struct node *node)
{
while (node != NULL)
{
printf (“%d “, node->data);
node = node->next;
}
}

Slip 34
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
/* A utilty function that returns true if data is present in linked list
else return false */
int isPresent (struct node *head, int data)
{
struct node *t = head;
while (t != NULL)
{
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}
void main()
{
/* Start with the empty list */
struct node* head1 = NULL;
struct node* head2 = NULL;
struct node* intersecn = NULL;
int n,num,i;
clrscr();
/*create a linked lits 10->15->5->20 */
printf(“\nEnter total number elements of first list : \n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nenter %d element : “,i+1);
scanf(“%d”,&num);
push( &head1, num) ;
}
printf(“\nEnter total number elements of second list : \n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nenter %d element : “,i+1);
scanf(“%d”,&num);
push( &head2, num) ;
}

intersecn = getIntersection (head1, head2);


printf (“\n First list is \n”);
printList (head1);
printf (“\n Second list is \n”);
printList (head2);
printf (“\n Intersection list is \n”);
printList (intersecn);

Slip 35
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
getch();
}

Q. 2) Write a ‘C’ program to count indegree and outdegree of each node in graph.

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

Slip 36
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
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);
}
}

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

Slip 37
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

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;
}
}
Q. 3) Write a menu driven program using ‘C’ for Dynamic implementation of Queue for
integers. The menu includes
- Insert
- Delete
- DisplayExit [Marks 25]
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*front=NULL,*rear=NULL;
void create(int m)
{

Slip 38
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
struct list *tmp;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(front==NULL)
front=tmp;
else
rear->link=tmp;
rear=tmp;
}
void del()
{
struct list *tmp;
if(front==NULL)
printf(“\n\nQUEUE IS FULL”);
else
{
tmp=front;
printf(“\n\nDELETED IS ELEMENT %d”,tmp->data);
front=front->link;
free(tmp);
}
}
void disp()
{
struct list *q;
if(front==NULL)
printf(“\n\nQUEUE IS EMPTY”);
else
{
q=front;
while(q!=NULL)
{
printf(“%d==>”,q->data);
q=q->link;
}
}
}
void main()
{
int i,n,ch;
clrscr();
do
{
printf(“\n\nMENU”);
printf(“\n\n1.INSERT “);

Slip 39
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf(“\n\n2.DELETE “);
printf(“\n\n3.DISPLAT “);
printf(“\n\n4.EXIT”);
printf(“\n\nENTER UR CHOICE”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n\nENTER THE NUMBER”);
scanf(“%d”,&n);
create(n);
break;
case 2:
del();
break;
case 3:
printf(“\n\nQUEUE ELEMENTS ARE \n\n”);
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
Q. 2) Write a ‘C’ program which accept the string and reverse each word of the string using
stack.
[Marks 15]
#include<stdio.h>
#include<conio.h>
#define MAX 50
char a[MAX];
int top=-1;
void push(char c)
{
top++;
a[top]=c;
}
char pop()
{
char c;
c=a[top];
top–;
return c;
}

Slip 40
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
void main()
{
int i;
clrscr();
printf(“\n\nENTER THE STRING”);
gets(a);
for(i=0;a[i]!=”; i++)
{
if(a[i]==’ ‘)
{
while(top!=-1)
{
printf(“%c”,pop());
}
printf(” “);
}
else
{
if(top==MAX-1)
{
printf(“\n\nSTACK IS FULL”);
}
else
{
push(a[i]);
}
}
}
while(top!=-1)
{
printf(“%c”,pop());
}
getch();
}
Q. 3) Write a ‘C’ program to read the adjacency matrix of directed graph and convert it into
adjacency list.
[Marks 25]
#include<stdio.h>
#include<conio.h>
struct n
{
int data;
struct n *link;
};
typedef struct n NODE;
NODE *getnode(int);

Slip 41
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
NODE *findlast(NODE *);
void display(NODE *[],int);
void main()
{
NODE *ptr,*temp,*h[10];
int n,a[10][10],i,j;
clrscr();
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;
}
}
}
}

Slip 42
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf(“\n The Adjacency list looks like … \n”);
display(h,n);
getch();
}
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(“\n 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”);
}
}
Q. 2) Write a ‘C’ program to create Circular Singly Link list and display it.
[Marks 15]
#include <stdio.h>
#include <stdlib.h>

/*
* Basic structure of Node
*/
struct node {

Slip 43
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
int data;
struct node * next;
}*head;

/*
* Functions used in this program
*/
void createList(int n);
void displayList();

int main()
{
int n, data, choice=1;

head = NULL;

/*
* Runs forever until user chooses 0
*/
while(choice != 0)
{
printf("============================================\n");
printf("CIRCULAR LINKED LIST PROGRAM\n");
printf("============================================\n");
printf("1. Create List\n");
printf("2. Display list\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");
printf("Enter your choice : ");

scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter the total number of nodes in list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
displayList();
break;

Slip 44
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
case 0:
break;
default:
printf("Error! Invalid choice. Please choose between 0-2");
}

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

return 0;
}

/**
* Creates a circular linked list of n nodes.
*
* @n Number of nodes to be created
*/
void createList(int n)
{
int i, data;
struct node *prevNode, *newNode;

if(n >= 1)
{
/*
* Creates and links the head node
*/
head = (struct node *)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");


scanf("%d", &data);

head->data = data;
head->next = NULL;

prevNode = head;

/*
* Creates and links rest of the n-1 nodes
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

Slip 45
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf("Enter data of %d node: ", i);
scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;

//Links the previous node with newly created node


prevNode->next = newNode;

//Moves the previous node ahead


prevNode = newNode;
}

//Links the last node with first node


prevNode->next = head;

printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n");


}
}

/**
* Displays the content of the list
*/
void displayList()
{
struct node *current;
int n = 1;

if(head == NULL)
{
printf("List is empty.\n");
}
else
{
current = head;
printf("DATA IN THE LIST:\n");

do {
printf("Data %d = %d\n", n, current->data);

current = current->next;
n++;
}while(current != head);
}

Slip 46
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}

Q. 3) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes
- Create a BST
- Insert element in a BST
- Display [Marks 25]

#include <stdio.h>

#include <stdlib.h>

struct btnode

int value;

struct btnode *l;

struct btnode *r;

}*root = NULL, *temp = NULL, *t2, *t1;

void create();

void search1(struct btnode *t,int data);

int smallest(struct btnode *t);

int largest(struct btnode *t);


int flag = 1;
void main()

{
int ch;

printf("\nOPERATIONS ---");

Slip 47
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

printf("\n1 - Create\n");

printf("2 - Display\n");
printf(" 3 - Exit\n");

while(1)

printf("\nEnter your choice : ");

scanf("%d", &ch);

switch (ch)

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

inorder(root);

break;

case 4:

preorder(root);

break;

case 5:

postorder(root);

Slip 48
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
break;

case 6:

exit(0);

default :

printf("Wrong choice, Please enter correct choice ");

break;

/* To insert a node in the tree */

void insert()

create();

if (root == NULL)

root = temp;

else

search(root);

/* To create a node */

void create()

Slip 49
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
int data;

printf("Enter data of node to be inserted : ");

scanf("%d", &data);

temp = (struct btnode *)malloc(1*sizeof(struct btnode));

temp->value = data;

temp->l = temp->r = NULL;

/* Function to search the appropriate position to insert the new node */

void search(struct btnode *t)

if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node
value insert at right */

search(t->r);

else if ((temp->value > t->value) && (t->r == NULL))

t->r = temp;

else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node
value insert at left */

search(t->l);

else if ((temp->value < t->value) && (t->l == NULL))

t->l = temp;

Slip 50
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
/* recursive function to perform inorder traversal of tree */

void inorder(struct btnode *t)

if (root == NULL)

printf("No elements in a tree to display");

return;

if (t->l != NULL)

inorder(t->l);

printf("%d -> ", t->value);

if (t->r != NULL)

inorder(t->r);

/* To check for the deleted node */

void delete()

int data;

if (root == NULL)

printf("No elements in a tree to delete");

Slip 51
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
return;

printf("Enter the data to be deleted : ");

scanf("%d", &data);

t1 = root;

t2 = root;

search1(root, data);

/* To find the preorder traversal */

void preorder(struct btnode *t)

if (root == NULL)

printf("No elements in a tree to display");

return;

printf("%d -> ", t->value);

if (t->l != NULL)

preorder(t->l);

if (t->r != NULL)

preorder(t->r);

Slip 52
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

/* To find the postorder traversal */

void postorder(struct btnode *t)

if (root == NULL)

printf("No elements in a tree to display ");

return;

if (t->l != NULL)

postorder(t->l);

if (t->r != NULL)

postorder(t->r);

printf("%d -> ", t->value);

/* Search for the appropriate position to insert the new node */

void search1(struct btnode *t, int data)

if ((data>t->value))

t1 = t;

search1(t->r, data);

Slip 53
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}

else if ((data < t->value))

t1 = t;

search1(t->l, data);

else if ((data==t->value))

delete1(t);

/* To delete a node */

void delete1(struct btnode *t)

int k;

/* To delete leaf node */

if ((t->l == NULL) && (t->r == NULL))

if (t1->l == t)

t1->l = NULL;

Slip 54
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}

else

t1->r = NULL;

t = NULL;

free(t);

return;

/* To delete node having one left hand child */

else if ((t->r == NULL))

if (t1 == t)

root = t->l;

t1 = root;

else if (t1->l == t)

t1->l = t->l;

Slip 55
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
else

t1->r = t->l;

t = NULL;

free(t);

return;

/* To delete node having right hand child */

else if (t->l == NULL)

if (t1 == t)

root = t->r;

t1 = root;

else if (t1->r == t)

t1->r = t->r;

else

t1->l = t->r;

t == NULL;

free(t);

Slip 56
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
return;

/* To delete node having two child */

else if ((t->l != NULL) && (t->r != NULL))

t2 = root;

if (t->r != NULL)

k = smallest(t->r);

flag = 1;

else

k =largest(t->l);

flag = 2;

search1(root, k);

t->value = k;

Slip 57
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
/* To find the smallest element in the right sub tree */

int smallest(struct btnode *t)

t2 = t;

if (t->l != NULL)

t2 = t;

return(smallest(t->l));

else

return (t->value);

/* To find the largest element in the left sub tree */

int largest(struct btnode *t)

if (t->r != NULL)

t2 = t;

return(largest(t->r));

else

return(t->value);

Slip 58
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}
Q. 2) Write a ‘C’ program to sort array elements using Insertion sort method. [Marks 15]
#include<stdio.h>
#include<conio.h>
#define MAX 20
void insertsort(int [MAX],int);
void display(int [MAX],int);
void main()
{
int a[20],i,n;
clrscr();
printf(“\nHow many numbers:”);
scanf(“%d”,&n);
printf(“\nEnter the unsorted element: “);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
insertsort(a,n);
printf(“\n\nThe Sorted list is: “);
display(a,n);
getch();
}
void insertsort(int a[MAX],int n)
{
int i,j,key;
printf(“\nstep \t Sorted list”);
for(i=1;i<n;i++)
{
key=a[i];
printf(“\n\n%d”,i);
display(a,i);
for(j=i-1;j>=0 && a[j]>key;j–)
// j=i-1;
// while((j>=0) && (a[j]>key))
{
a[j+1]=a[j];
// j=j-1;
}
a[j+1]=key;
}
}
void display(int a[MAX],int n)
{
int i;
for(i=0;i<n;i++)
printf(“\t%d”,a[i]);
}

Slip 59
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
Q. 3) Write a menu driven program using ‘C’ for implementation of Singly linked list. Menu
includes –
- Create.
- Display
- Insert node at specific position
- Search a given element in list

[Marks 15]
- #include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void ser(int);
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.SEARCH \n”);
printf(“\n5.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;

Slip 60
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
case 3:
count();
break;
case 2:
disp();
break;
case 4:
printf(“\nENTER THE ELEMENT FOR SEARCH”);
scanf(“%d”,&m);
ser(m);
break;
case 5:
exit(0);
}
}
while(ch!=7);
getch();
}
void count()
{
struct node *q;
int cnt=0;
q=start;
while(q!=NULL)
{
cnt++;
q=q->link;
}
printf(“\n\No Of Nodes Are\t %d”,cnt);
}
- 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;

Slip 61
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}
}
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”);
}
}
void ser(int data)
{
struct node *q,*tmp;
q=start;
while(q!=NULL)
{
if(q->data==data)
{
printf(“\nElement Is Found”);
break;
}
else
{
q=q->link;
}
}
if(q==NULL)
{
printf(“\nElement is Not Found”);
}
}

Q. 2) Write a ‘C’ program to sort array elements in ascending order using Selection sort
method.
[Marks 15]

Slip 62
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
#include <stdio.h>
int main()
{
int data[100],i,n,steps,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(steps=0;steps<n;++steps)
for(i=steps+1;i<n;++i)
{
if(data[steps]>data[i])
/* To sort in descending order, change > to <. */
{
temp=data[steps];
data[steps]=data[i];
data[i]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}

Q. 3) Write a ‘C’ program for Binary Search Tree. The menu includes
- Create a BST
- Display
- Delete a given element from BST

[Marks 25]
Q. 2) Write a ‘C’ program to accept n student names from user and store it in an array. Write a
function to search given student name into the array using Linear search method.
[Marks 15]
Q. 3) Write a ‘C’ program to read an adjacency matrix of a directed graph and traverse it using
DFS. [Marks 25]
#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
void main()
{
int i,j;
printf("Enter number of vertices:");

Slip 63
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

scanf("%d",&n);
//read the adjecency matrix
printf("\nEnter adjecency matrix of the graph:");

for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
//visited is initialized to zero
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1;

for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}

Q. 2) Write a ‘C’ program to accept two polynomials and add these two polynomials using
function. Display the result (Use array).
[Marks 15]
#include < stdio.h >
#include < conio.h >
#define MAX 20

struct addpolynomial {
int exp, coef;
};

void main() {
struct addpolynomial p1[MAX], p2[MAX], p3[MAX];
int max1, max2, max3;
clrscr();
printf("\nEnter first addpolynomial : ");
max1 = read_addpolynomial(p1);
printf("\nEnter second addpolynomial : ");
max2 = read_addpolynomial(p2);
max3 = add_addpolynomial(p1, p2, p3, max1, max2);
printf("\nFirst addpolynomial is ");
print_addpolynomial(p1, max1);

Slip 64
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf("\nSecond addpolynomial is ");
print_addpolynomial(p2, max2);
printf("\n The resultant addpolynomial after addition is");
print_addpolynomial(p3, max3);
}

//function to read polynomial


int read_addpolynomial(struct addpolynomial p[]) {
int i, texp;
i = 0;
printf("\nEnter exp ( use -1 to exit) : ");
scanf("%d", &texp);
while (texp != -1) {
p[i].exp = texp;
printf("\nEnter coef : ");
scanf("%d", &p[i].coef);
i++;
printf("\nEnter exp ( use -1 to exit) : ");
scanf("%d", &texp);
}
return (i);
}

//function to print polynomial


int print_addpolynomial(struct addpolynomial p[], intMAX1) {
int i;
for (i = 0; i < max1; i++)
printf("%+dX%d ", p[i].coef, p[i].exp);
return;
}

//function to ad polynomials
int add_addpolynomial( p1, p2, p3, max1, max2)
struct addpolynomial p1[], p2[], p3[];
intMAX1, max2;
{
int i,j,k;
i = j = k = 0;
while ( i <max1 && j <max2)
{
if( p1[i].exp > p2[j].exp)
{
p3[k] = p1[i];
k++;
i++;
}

Slip 65
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
else
if( p1[i].exp < p2[j].exp)
{
p3[k] = p2[j];
k++;
j++;
}
else
{
p3[k].exp = p1[i].exp;
p3[k].coef = p1[i].coef + p2[j].coef;
i++;
j++;
k++;
}
}
while( i <max1 )
{
p3[k] = p1[i];
k++;
i++;
}
while( j <max2 )
{
p3[k] = p2[j];
k++;
j++;
}
return(k);
}

Q. 3) Write a menu driven program using ‘C’ for implementation of Singly linked list. Menu
includes –
- Create
- Display
- Insert node at first position

[Marks 25]
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;

Slip 66
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
void create(int);
void disp();
void addbeg(int);
void del(int);
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.ADDBEG\n”);
printf(“\n4.DELETE NODE BY POSITION\n”);
printf(“\n5.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 2:
disp();
break;
case 5:
exit(0);
case 3:
printf(“\nENTER THE VALUE FOR NODE”);
scanf(“%d”,&a);
addbeg(a);
break;
case 4:
printf(“\n\nENTER THE POSITION”);
scanf(“%d”,&pos);
del(m);
break;

Slip 67
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}
}
while(ch!=5);
getch();
}
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”);
}
}
void addbeg(int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node ));
tmp->data=data;

Slip 68
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
tmp->link=start;
start=tmp;
}
void del(int pos)
{
struct node *q,*tmp;
int i;
q=start;
for(i=1;i<pos-1;i++)
{
q=q->link;
}
tmp=q->link;
q->link=tmp->link;
free(tmp);
}

Q. 2) Write a ‘C’ program to sort elements of a singly linked list in ascending order and display
the sorted List.
[Marks 15]
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start;
void create(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;

Slip 69
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
if(start==NULL)
printf(“\n\n LIST IS EMPTY”);
else
{
q=start;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
}
}
void sort()
{
struct list *q,*p;
int data;
q=start;
while(q!=NULL)
{
p=q->link;
while(p!=NULL)
{
if(q->data>p->data)
{
data=q->data;
q->data=p->data;
p->data=data;
}
p=p->link;
}
q=q->link;
}
}
void main()
{
int ch,m,i,n;
clrscr();
do
{
printf(“\n\n1.create \n\n2.display \n\n3.sort \n\n4.Exit\n\n”);
printf(“\n\nENTER UR CHOICE”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n\nHOW MANY NODES”);

Slip 70
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\n\nENTER THE DATA”);
scanf(“%d”,&m);
create(m);
}
break;
case 2:
disp();
break;
case 3:
sort();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
Q. 3) Write a ‘C’ program to create a random array of n integers. Sort the array using bubble
sort. Accept a value x from user and use linear search algorithm to check whether the
number is present in array or not.
[Marks 25]
#include <stdio.h>

int main()
{
int array[100], n, c, d, swap, search;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];

Slip 71
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

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


{printf("%d\n", array[c]);}

printf("Enter the number to search\n");


scanf("%d", &search);

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


{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
{printf("%d is not present in array.\n", search);}
return 0;
}

Q.2) Write a ‘C’ program to sort the element using Quick sort (recursive) method.

[Marks 15]
#include<stdio.h>
#include<conio.h>

//quick Sort function to Sort Integer array list


void quicksort(int array[], int firstIndex, int lastIndex)
{
//declaaring index variables
int pivotIndex, temp, index1, index2;

if(firstIndex < lastIndex)


{
//assigninh first element index as pivot element
pivotIndex = firstIndex;
index1 = firstIndex;

Slip 72
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
index2 = lastIndex;

//Sorting in Ascending order with quick sort


while(index1 < index2)
{
while(array[index1] <= array[pivotIndex] && index1 < lastIndex)
{
index1++;
}
while(array[index2]>array[pivotIndex])
{
index2--;
}

if(index1<index2)
{
//Swapping opertation
temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}

//At the end of first iteration, swap pivot element with index2 element
temp = array[pivotIndex];
array[pivotIndex] = array[index2];
array[index2] = temp;

//Recursive call for quick sort, with partiontioning


quicksort(array, firstIndex, index2-1);
quicksort(array, index2+1, lastIndex);
}
}

int main()
{
//Declaring variables
int array[100],n,i;

//Number of elements in array form user input


printf("Enter the number of element you want to Sort : ");
scanf("%d",&n);

//code to ask to enter elements from user equal to n


printf("Enter Elements in the list : ");
for(i = 0; i < n; i++)

Slip 73
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
scanf("%d",&array[i]);
}

//calling quickSort function defined above


quicksort(array,0,n-1);

//print sorted array


printf("Sorted elements: ");
for(i=0;i<n;i++)
printf(" %d",array[i]);

getch();
return 0;
}

Q. 3) Write a menu driven program using ‘C’ for implementation of Singly linked list. Menu
includes –
- Create
- Display
- Insert Node at particular position

[Marks 25]

Q. 2) Write a ‘C’ program to sort the array elements in ascending order using Merge sort
method.
[Marks 15]
#include <stdio.h>

void merge(int [], int, int [], int, int []);

int main() {
int a[100], b[100], m, n, c, sorted[200];

printf("Input number of elements in first array\n");


scanf("%d", &m);

printf("Input %d integers\n", m);


for (c = 0; c < m; c++) {
scanf("%d", &a[c]);
}

printf("Input number of elements in second array\n");


scanf("%d", &n);

Slip 74
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf("Input %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &b[c]);
}

merge(a, m, b, n, sorted);

printf("Sorted array:\n");

for (c = 0; c < m + n; c++) {


printf("%d\n", sorted[c]);
}

return 0;
}

void merge(int a[], int m, int b[], int n, int sorted[]) {


int i, j, k;

j = k = 0;

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


if (j < m && k < n) {
if (a[j] < b[k]) {
sorted[i] = a[j];
j++;
}
else {
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m) {
for (; i < m + n;) {
sorted[i] = b[k];
k++;
i++;
}
}
else {
for (; i < m + n;) {
sorted[i] = a[j];
j++;
i++;
}

Slip 75
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}
}
}

Q. 3) Write a ‘C’ program to read n integers and create two lists such that all positive numbers
are in one list and negative numbers are in another list. Display both the lists.
[Marks 25]
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
/* structure containing a data part and link part */
struct node
{
int data ;
struct node *link ;
};
void add ( struct node **, int ) ;
void display ( struct node * ) ;

void main( )
{
struct node *first, *second, *third ;
int n,i,num;
clrscr();
first = second = NULL ; /* empty linked lists */
printf(“\nEnter total number elements of list : \n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nenter %d element(positive or negative number) : “,i+1);
scanf(“%d”,&num);
if(num<0)
add ( &first, num) ;
else
add ( &second, num) ;
}
printf ( “\n—–first linked list is —–\n : “) ;
display ( first ) ;
printf ( “\n\nSecond linked list : “) ;
display ( second ) ;
getch();
}
/* adds node to an ascending order linked list */
void add ( struct node **q, int num )

Slip 76
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
struct node *r, *temp = *q ;
r = malloc ( sizeof ( struct node ) ) ;
r-> data = num ;
/* if list is empty or if new node is to be inserted before the first node */
if ( *q == NULL || ( *q ) -> data > num )
{
*q = r ;
( *q ) -> link = temp ;
}
else
{
/* traverse the entire linked list to search the position to insert the new node */
while ( temp != NULL )
{
if ( temp -> data < num && ( temp -> link -> data > num ||
temp -> link == NULL ))
{
r -> link = temp -> link ;
temp -> link = r ;
return ;
}
temp = temp -> link ; /*go to next node */
}
r -> link = NULL ;
temp -> link = r ;
}
}
/* displays the contents of the linked list */
void display ( struct node *q )
{
printf ( “\n” ) ;
/* traverse the entire linked list */
while ( q != NULL )
{
printf ( “%d “, q -> data ) ;
q = q -> link ;
}
}

Q. 2) Write a ‘C’ program to read a postfix expression, evaluate it and display the result.
(Using two variable) [Marks 15]
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
int s[SIZE];
int top=-1; /* Global declarations */

Slip 77
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

push(int elem)
{ /* Function for PUSH operation */
s[++top]=elem;
}

int pop()
{ /* Function for POP operation */
return(s[top--]);
}

main()
{ /* Main Program */
char pofx[50],ch;
int i=0,op1,op2;
printf("\n\nRead the Postfix Expression ? ");
scanf("%s",pofx);
while( (ch=pofx[i++]) != '\0')
{
if(isdigit(ch)) push(ch-'0'); /* Push the operand */
else
{ /* Operator,pop two operands */
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
}
}
}
printf("\n Given Postfix Expn: %s\n",pofx);
printf("\n Result after Evaluation: %d\n",s[top]);
}

Q. 3) 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.
[Marks 25]
#include<stdio.h>
struct BT
{
int data;

Slip 78
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
struct BT *right, *left;
};
void insert( struct BT ** ptr, int d )
{
if ( ( *ptr ) == NULL )
{
( *ptr ) = ( struct BT* ) malloc( sizeof( struct BT ) );
( *ptr ) ->data = d;
( *ptr ) ->left = ( *ptr ) ->right = NULL;
}
else
{
if ( ( *ptr ) ->data > d )
insert( &( ( *ptr ) ->left ), d );
else
insert( &( ( *ptr ) ->right ), d );
}
return ;
}
int search( struct BT *ptr, int no )
{
if ( ptr == NULL )
return ( 0 );
if ( ptr->data == no )
return ( 1 );
if ( ptr->data > no )
return ( search( ptr->left, no ) );
else
return ( search( ptr->right, no ) );
}
void inorder( struct BT *ptr )
{
if ( ptr == NULL )
return ;
else
{
inorder( ptr->left );
printf( “\t%d”, ptr->data );
inorder( ptr->right );
}
}
void preorder( struct BT*ptr )
{
if ( ptr == NULL )
return ;
else

Slip 79
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
printf( “\t%d”, ptr->data );
preorder( ptr->left );
preorder( ptr->right );
}
}
void postorder( struct BT*ptr )
{
if ( ptr == NULL )
return ;
else
{
postorder( ptr->left );
postorder( ptr->right );
printf( “\t%d”, ptr->data );
}
}
main()
{
struct BT * root;
int ch, d, no, f;
root = NULL;
while ( ch != 6 )
{
printf( “\n 1.Insert\n 2.Search\n 3.Inorder\n 4.Preorder\n 5.Postorder\n 6.Exit\n” );
printf( “\n Enter the choice:” );
scanf( “%d”, &ch );
switch ( ch )
{
case 1:
printf( “Enter the data:” );
scanf( “%d”, &d );
insert( &root, d );
break;
case 2:
printf( “Enter the node:” );
scanf( “%d”, &no );
f = search( root, no );
if ( f == 0 )
printf( “Node is not present” );
else
printf( “Node is present” );
break;
case 3:
inorder( root );
break;

Slip 80
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
case 4:
preorder( root );
break;
case 5:
postorder( root );
break;
case 6:
break;
}
}
}

Q. 2) Write a ‘C’ program to create two singly linked lists and concatenate one list at the
end of another list.
[Marks 15]
#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”);

Slip 81
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
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;

Slip 82
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
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);
}

Q. 3) Write a ‘C’ program to read ‘n’ integers and store them in a binary tree structure and
count the following and display it.
- Number of nodes
- Degree of tree
- Leaf nodes

[Marks 25]
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct bintree
{
int info;
struct bintree *left;
struct bintree *right;
};
int count=0;
int flag = 0;
void main()
{
struct bintree *root,*p;
int ch,x,key;
char ans;
struct bintree * insert(struct bintree *,int);
void traverse(struct bintree *);
void count_leaf(struct bintree *);
void interior_nodes(struct bintree *);
clrscr();
root = NULL;

Slip 83
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
do
{
printf(“\n1.Insert a Node”);
printf(“\n2.Traverse\n3.No of Nodes “);
printf(“\n4.Leaf Nodes of Tree\n5.Interior Nodes”);
printf(“\n6.Exit”);
printf(“\nEnter your choice::”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n\n\t\tEnter the information::”);scanf(“%d”,&x);
root = insert(root,x);
break;
case 2:
traverse(root);
break;
case 3:
count = 0;
traverse(root);
printf(“\n\n\t\tThe no of nodes = %d”,count);
break;
case 4:
count = 0;
count_leaf(root);
printf(“\n\n\t\tThe no of leaf nodes are = %d”,count);
break;
case 5 :
count = 0;flag = 0;
interior_nodes(root);
printf(“\n\n\t\tThe no of interior nodes are = %d”,count);
break;
case 6:
break;
default :
printf(“\n\n\t\tInvalid choice “);
}
}while(ch != 6);
getch();
}
struct bintree * insert(struct bintree *root,int x)
{
struct bintree *p,*q,*curr;
curr = (struct bintree *)malloc(sizeof(struct bintree));
curr->info = x;
curr->left=NULL;

Slip 84
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
curr->right=NULL;
p=q=root;
if(root == NULL)
{
root = curr;
printf(“\n\n\t\t%d Is inserted at root position “,curr->info);
return root;
}
while(q != NULL)
{
p = q;
if(x < p->info)
q = p->left;
else
q = p->right;
}
if(x < p->info)
{
printf(“\n\n\t\tThe node is inserted at %d’s left”,p->info);
p->left = curr;
}
else
{
printf(“\n\n\t\tThe node is inserted at %d’s right”,p->info);
p->right = curr;
}
return root;
}
void traverse(struct bintree *root)
{
if(root != NULL)
{
traverse(root->left);
printf(“ %d”,root->info);
count++;
traverse(root->right);
}
}
void count_leaf(struct bintree *root)
{
if(root != NULL)
{
if(root->left == NULL && root->right == NULL)
{
count++;
printf(“ %d”,root->info);

Slip 85
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}
count_leaf(root->left);
count_leaf(root->right);
}
}
void interior_nodes(struct bintree *root)
{
if(root != NULL)
{
if((root->left != NULL || root->right != NULL) && flag == 1)
{
count++;
printf(“ %d”,root->info);
}
flag = 1;
interior_nodes(root->left);
interior_nodes(root->right);
}
}

Q. 2) Write a ‘C’ program to remove last node of the singly linked list and insert it at the
beginning of list.

[Marks 15]
#include<stdio.h>
#include<stdlib.h>

/* A linked list node */


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

/* We are using a double pointer head_ref here because we change


head of the linked list inside this function.*/
void moveToFront(struct node **head_ref)
{
/* If linked list is empty, or it contains only one node,
then nothing needs to be done, simply return */
if (*head_ref == NULL || (*head_ref)->next == NULL)
return;

/* Initialize second last and last pointers */


struct node *secLast = NULL;
struct node *last = *head_ref;

Slip 86
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

/*After this loop secLast contains address of second last


node and last contains address of last node in Linked List */
while (last->next != NULL)
{
secLast = last;
last = last->next;
}

/* Set the next of second last as NULL */


secLast->next = NULL;

/* Set next of last as head node */


last->next = *head_ref;

/* Change the head pointer to point to last node now */


*head_ref = last;
}

/* UTILITY FUNCTIONS */
/* Function to add a node at the begining of Linked List */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));

/* put in the data */


new_node->data = new_data;

/* link the old list off the new node */


new_node->next = (*head_ref);

/* move the head to point to the new node */


(*head_ref) = new_node;
}

/* Function to print nodes in a given linked list */


void printList(struct node *node)
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}

Slip 87
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}

/* Druver program to test above function */


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

/* The constructed linked list is:


1->2->3->4->5 */
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);

printf("\n Linked list before moving last to front\n");


printList(start);

moveToFront(&start);

printf("\n Linked list after removing last to front\n");


printList(start);

return 0;
}

Q. 3) Write a ‘C’ program to read an adjacency matrix of a directed graph and traverse using
BFS. [Marks 25]

#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{
int v;

Slip 88
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n Bfs is not possible");
getch();
}

Q. 2) Write a ‘C’ program to create binary search tree and display its leaf nodes.
[Marks 15]
#include<stdlib.h>
#include<stdio.h>

struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;

void insert(node ** tree, int val)


{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;

Slip 89
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
}

if(val < (*tree)->data)


{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}

void print_preorder(node * tree)


{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}

void print_inorder(node * tree)


{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}

void print_postorder(node * tree)


{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}

void deltree(node * tree)


{

Slip 90
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}

node* search(node ** tree, int val)


{
if(!(*tree))
{
return NULL;
}

if(val < (*tree)->data)


{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}

void main()
{
node *root;
node *tmp;
//int i;

root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);

/* Printing nodes of tree */

Slip 91
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf("Pre Order Display\n");
print_preorder(root);

printf("In Order Display\n");


print_inorder(root);

printf("Post Order Display\n");


print_postorder(root);

/* Search node into tree */


tmp = search(&root, 4);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}

/* Deleting all nodes of tree */


deltree(root);
}

Q. 3) Write a ‘C’ program to accept an infix expression, convert it into its equivalent prefix
expression and display the result.
[Marks 25]
#include<stdio.h>

#include<conio.h>

//Stack precedence function

int F(char symbol)

switch(symbol)

case ‘+’ :

case ‘-‘ :

Slip 92
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
return 1;

case ‘*’:

case ‘^’:

return 6:

case ‘)’:

return 0:;

case ‘#’:

return -1;

default:

return 8;

//Input precedence function

int G(char symbol)

switch(symbol)

case ‘+’ :

case ‘-‘ :

return 2;

case ‘*’:

return 4;

case ‘^’:

Slip 93
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
return 5:

case ‘(‘:

return 0;

case ‘)’:

return 9:;

case ‘#’:

return -1;

default:

return 7;

Void infix_prefix(char infix[], char prefix[])

int top, j, i;

char symbol, s[40];

top = -1;

s[++top] = ‘#’;

J = 0;

strrev(infix);

for(i = 0;i < strlen(infix); i++)

symbol= infix[i];

while(F(s[top]) > G(symbol))

Slip 94
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{

prefix[j] = s[top--];

j++;

if(F(s[top]) != G(symbol))

s[++top] = symbol;

else

top--;

while(s[top != ‘#’)

prefix[j++] = s[top--];

prefix[j] = ‘\0’;

strrev(prefix);

void main()

char infix[20];

char prefix[20];

printf(“/nEnter a valid infix expression\n”);

scanf(“%s”,infix);

infix_prefix(infix, prefix);

Slip 95
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf(“\n\nThe prefix expression is\n”);

printf(“%s\n”,prefix);

Q. 2) Write a ‘C’ program to count all non-zero elements, odd numbers and even numbers in
the singly linked list.
[Marks 15]
#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;

Slip 96
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
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;
}

Slip 97
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
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”);
}
}

Q. 3) Write menu driven program using ‘C’ for Circular doubly linked list. The menu
includes
- Create
- Display
- Exit
[Marks 25]
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#define NULL 0
struct linkedlist
{
int item;
struct linkedlist *right,*left;
};
typedef struct linkedlist node;
void main()
{

Slip 98
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
node *start,*end;
int choice;
int menu(void);
node *create(node **lastnode);
void display(node *first,node *last);
void insert(node **first,node **last);
void del(node **first,node **last);
clrscr();
printf(“\n DOUBLY LINKED LIST”);
printf(“\n ******************”);
do
{
printf(“\n\nMain menu”);
printf(“\n\n1.Create \n2.Insert \n3.Delete \n4.Display \n5.Exit”);
choice =menu();
switch(choice)
{
case 1:
printf(“\n Enter the data(-999 to stop):”);
start=create(&end);
continue;
case 2:
insert(&start,&end);
printf(“\n”);
continue;
case 3:
del(&start,&end);
printf(“\n”);
continue;
case 4:
display(start,end);
printf(“\n”);
continue;
case 5:
exit(0);
default:
printf(“\n\nINVALID CHOICE…”);
}
}while(1);
}
int menu()
{
int choice;
do
{
printf(“\n Enter your choice:”);

Slip 99
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
scanf(“%d”,&choice);
if(choice<1||choice>5)
printf(“\n Wrong choice”);
}while(choice<1||choice>5);
printf(“\n”);
return(choice);
}
node *create(node **lastnode)
{
node *temp,*firstnode;
int info;
*lastnode=NULL;
firstnode=NULL;
scanf(“%d”,&info);
while(info!=-999)
{
temp=(node *)malloc(sizeof(node));
temp->item=info;
temp->right=NULL;
if(firstnode==NULL)
{
temp->left=NULL;
firstnode=temp;
}
else
{
temp->left=(*lastnode);
(*lastnode)->right=temp;
}
(*lastnode)=temp;
scanf(“%d”,&info);
}
if(firstnode!=NULL)
(*lastnode)=temp;
return(firstnode);
}
void display(node *first,node *last)
{
printf(“\n Forward traversal\n”);
while(first!=NULL)
{
printf(“%d\t”,first->item);
first=first->right;
}
printf(“\n Backward traversal\n”);
while(last!=NULL)
Slip
100
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
printf(“%d\t”,last->item);
last=last->left;
}
return;
}
void insert(node **first,node **last)
{
node *newnode;
int newitem;
int position;
node *temp;
int i;
printf(“\n New data item:”);
scanf(“%d”,&newitem);
do
{
printf(“\n Position of insertion:”);
scanf(“%d”,&position);
}while(position<=0);
if(((*first)==NULL)||(position==1))
{
newnode=(node *)malloc(sizeof(node));
newnode->item=newitem;
newnode->right=*first;
newnode->left=NULL;
if((*first)!=NULL)
(*first)->left=newnode;
else
(*last)=newnode;
*first=newnode;
}
else
{
i=1;
temp=*first;
while((i<position-1)&&(temp->right!=NULL))
{
i++;
temp=temp->right;
}
newnode=(node *)malloc(sizeof(node));
newnode->item=newitem;
newnode->right=temp->right;
if(temp->right!=NULL)
temp->right->left=newnode;
Slip
101
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
newnode->left=temp;
temp->right=newnode;
}
if(newnode->right==NULL)
*last=newnode;
}
void del(node **first,node **last)
{
node *temp,*prev;
int target;
printf(“\n Enter the data to be deleted:”);
scanf(“%d”,&target);
if(*first==NULL)
printf(“\n List is empty”);
else if((*first)->item==target)
{
if((*first)->right==NULL)
*first=*last=NULL;
else
{
*first=(*first)->right;
(*first)->left=NULL;
}
}
else
{
temp=*first;
prev=NULL;
while((temp->right!=NULL)&&(temp->item!=target))
{
prev=temp;
temp=temp->right;
}
if(temp->item!=target)
printf(“\n Element not found”);
else
{
if(temp==*last)
*last=prev;
else
temp->right->left=temp->left;
prev->right=temp->right;
}
}
}

Slip
102
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

Q. 2) Write a ‘C’ program to create doubly link list and display nodes having even value.
[Marks 15]
Q. 3) Write menu driven program using ‘C’ for Dynamic implementation of Stack. The menu
includes following operations:
- push
- pop
- display
- exit
[Marks 25]

#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*top=NULL;
void push(int m)
{
struct list *tmp;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
tmp->link=top;
top=tmp;
}
void pop()
{
struct list *tmp;
if(top==NULL)
printf(“\n\nSTACK IS EMPTY”);
else
{
tmp=top;
printf(“\n\nDELETED IS ELEMENT %d”,tmp->data);
top=top->link;
free(tmp);
}
}
void disp()
{
struct list *q;
if(top==NULL)
printf(“\n\nSTACK IS EMPTY”);
else
Slip
103
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
q=top;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
}
}
void main()
{
int i,n,ch;
clrscr();
do
{
printf(“\n\nMENU”);
printf(“\n\n1.PUSH “);
printf(“\n\n2.POP “);
printf(“\n\n3.DISPLAY “);
printf(“\n\n4.EXIT”);
printf(“\n\nENTER UR CHOICE”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n\nENTER THE NUMBER”);
scanf(“%d”,&n);
push(n);
break;
case 2:
pop();
break;
case 3:
printf(“\n\nSTACK ELEMENTS ARE \n\n”);
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}

Q. 2) Write a ‘C’ program to create a Circular doubly Link list and display it.
[Marks 15]
Slip
104
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
#include<stdio.h>
#include<conio.h>

struct circular
{
int i;
struct circular *front;
struct circular *back;
};

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

Slip
105
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
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->back=head;
head->front=head;

printf("ENETER THE DATA");


scanf("%d",&head->i);
temp=head;

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


temp=temp->back;
temp->back=head;
head->front=temp;

printf("ENETER THE DATA");


scanf("%d",&temp->i);

}
void insert()
{
int add,t;
Slip
106
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

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


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

mid->back=p->back;
p->back=mid;
p->back->front=mid;
mid->front=p;

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

void del(void)
{
int add,t;

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


scanf("%d",&add);
p=head;
t=1;
while(t<add-1)
{
p=p->back;
t++;
}
Slip
107
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
clrscr();
mid=p->back;
p->back=mid->back;
mid->back->front=p;
free(mid);
}

Q. 3) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes -
- Create a BST
- Traverse it by using Inorder traversing technique
- Search a given value in BST
[Marks 25]
th th
Q. 2) Write a ‘C’ program to swap m and n element of singly linked list.
[Marks 15]
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start,*q,*temp,*t;
int cnt,num1,num2,m,n;
void create();
void display();
void swap();
int count();
void main()
{
int ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
create();
printf("\ndo you want to enter(Y|N):-");
scanf("%s",&ch);
}
display();
swap();
count();
display();
getch();
}
void create()
{
temp=malloc(sizeof(struct node));
Slip
108
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf("\nenter the number:-");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
}
void display()
{
if(start==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in singly linked list are:-\n");
q=start;
while(q!=NULL)
{
printf("%d\n",q->data);
q=q->next;
}
}
}
void swap()
{
int i;
printf("\nenter two position of a element which you want to swap");
scanf("%d%d",&m,&n);
count();
if(m>0&&m<=cnt&&n>0&&n<=cnt)
{
q=start;
i=0;
printf("\n\nelement after swapping");
while(q!=NULL)
{
i++;
if(m==i)
q->data=num2;
Slip
109
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
if(n==i)
q->data=num1;
q=q->next;
}
}
else
printf("\nyou entered wrong position please tyr again");
}
int count()
{
cnt=0;
q=start;
while(q!=NULL)
{
cnt++;
if(cnt==m)
num1=q->data;
if(cnt==n)
num2=q->data;
q=q->next;
}
return cnt;
}

Q. 3) Write a ‘C’ program to read ‘n’ integers and store them in a Binary search tree and
display the nodes level wise.

[Marks 25]
#include <stdio.h>
typedef struct node
{
int value;
struct node *right;
struct node *left;
}mynode;
mynode *root;
void add_node(int value);
void levelOrderTraversal(mynode *root);
void main()
{
int num;
char ch=’y';
clrscr();
root = NULL;
do
{
Slip
110
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
printf(“\nenter the node value”);
scanf(“%d”,&num);
add_node(num);
printf(“\ndo you want to continue (y/n)”);
flushall();
scanf(“%c”,&ch);
}while(ch!=’n');
printf(“\n\n\nLEVEL ORDER TRAVERSAL\n\n”);
levelOrderTraversal(root);
getch();
}

// Function to add a new node…


void add_node(int value)
{
mynode *prev, *cur, *temp;
temp = (mynode *) malloc(sizeof(mynode));
temp->value = value;
temp->right = NULL;
temp->left = NULL;
if(root==NULL)
{
printf(“\nCreating the root..\n”);
root = temp;
return;
}
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
cur=(value<cur->value)?cur->left:cur->right;
}
if(value < prev->value)
prev->left=temp;
else
prev->right=temp;
}

// Level order traversal..


void levelOrderTraversal(mynode *root)
{
mynode *queue[100] = {(mynode *)0}; // Important to initialize!
int size = 0;
int queue_pointer = 0;

Slip
111
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
while(root)
{
printf(“[%d] “, root->value);
if(root->left)
{
queue[size++] = root->left;
}
if(root->right)
{
queue[size++] = root->right;
}
root = queue[queue_pointer++];
}
}

Q. 2) Write a ‘C’ program to create a singly Link list and display its alternative nodes.
[Marks 15]
#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)


{
int flag = 0;

Slip
112
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
while(head != NULL)
{
if (!(flag % 2))
{
printf("%d ", head->a);
}
flag++;
head = 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);
}
}}

Slip
113
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’

Q. 3) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes
- Create a BST
- Traverse it by using Preorder traversing technique
- Search a given value in BST
[Marks 25]
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>

typedef struct BST {


int data;
struct BST *lchild, *rchild;
} node;

void insert(node *, node *);


void preorder(node *);
node *search(node *, int, node **);

void main() {
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
clrscr();

printf("\nProgram For Binary Search Tree ");


do {
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);

switch (choice) {
case 1:
do {
new_node = get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);

if (root == NULL) /* Tree is not Created */


root = new_node;
Slip
114
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
else
insert(root, new_node);

printf("\nWant To enter More Elements?(y/n)");


ans = getch();
} while (ans == 'y');
break;

case 2:
printf("\nEnter Element to be searched :");
scanf("%d", &key);

tmp = search(root, key, &parent);


printf("\nParent of node %d is %d", tmp->data, parent->data);
break;

case 3:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
}
} while (choice != 4);
}
/*
Get new Node
*/
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
Slip
115
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}

if (new_node->data > root->data) {


if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
/*
This function is for searching the node from
binary Search Tree
*/
node *search(node *root, int key, node **parent) {
node *temp;
temp = root;
while (temp != NULL) {
if (temp->data == key) {
printf("\nThe %d Element is Present", temp->data);
return temp;
}
*parent = temp;

if (temp->data > key)


temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp) {
if (temp != NULL) {
printf("%d", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}

Slip
116
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
Q. 2) Write a ‘C’ program to create to a Singly linked list. Accept the number from user, search
the number in the list .If the number is present delete the node from the list and display
the list .If node not present print the message “Node not Found”.

[Marks 15]
Q. 3) Write a ‘C’ program to read ‘n’ integers and store them in binary search tree. Display
mirror image of tree. (using recursive function)
[Marks 25]
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node* lchild;
struct node* rchild;
};
struct node* create(struct node*);
void inorder(struct node* );
void preorder(struct node* );
void postorder(struct node* );
void mirror(struct node *);
void main()
{
struct node* root=NULL;
root=(struct node*)create(root);
printf(“display”);
printf(“\n inorder is”);
inorder(root);
printf(“\n preorder is”);
preorder(root);
printf(“\n postorder is”);
postorder(root);
printf(“\nmirror of tree is\n”);
mirror(root);
inorder(root);
getch();
}
struct node* create(struct node* root)
{
struct node* temp,*newn;
int i,size;
clrscr();
printf(“enter size”);
scanf(“%d”,&size);
for(i=0;i<size;i++)
{
Slip
117
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
newn=(struct node*)malloc(sizeof(struct node));
newn->lchild=newn->rchild=NULL;
printf(“enter data”);
scanf(“%d”,&newn->data);
if(root==NULL)
{
root=newn;
}
else
{
temp=root;
while(temp!=NULL)
{
if(temp->data>newn->data)
{
if(temp->lchild!=NULL)
{
temp=temp->lchild;
}
else
{
temp->lchild=newn;
break;
}
}
else
{
if(temp->rchild!=NULL)
{
temp=temp->rchild;
}
else
{
temp->rchild=newn;
break;
}
}
} //while
}//else
}//for
return root;
}
void inorder(struct node* root)
{
if(root!=NULL)
{
Slip
118
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
inorder(root->lchild);
printf(“%d “,root->data);
inorder(root->rchild);
}
}
void preorder(struct node* root)
{
if(root!=NULL)
{
printf(“%d “,root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void postorder(struct node* root)
{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
printf(“%d “,root->data);
}
}
void mirror(struct node * root)
{
struct node * temp=root,*temp1;
if(temp)
{
if(temp->lchild)
mirror(temp->lchild);
if(temp->rchild)
mirror(temp->rchild);
temp1=temp->lchild;
temp->lchild=temp->rchild;
temp->rchild=temp1;
}
}

Q. 2) Write a ‘C’ program to accept the details of employees from user and display it on the
screen using Dynamic Memory Allocation.
[Marks 15]
#include<stdio.h>
#include<conio.h>
struct emp
{
int eno;
Slip
119
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
char ename[10];
float sal;
};
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
void main()
{
struct emp *e;
int n,i;
clrscr();
printf(“\nEnter the Size \n”);
scanf(“%d”,&n);
e=(struct emp *)calloc(n,sizeof(struct emp));
printf(“\nEnter the Eno,EName, ESal \n”);
for(i=0;i<n;i++)
{
scanf(“%d%s%f”,&e[i].eno,&e[i].ename,&e[i].sal);
}
printf(“\n\nDetails of Employee \n\n”);
for(i=0;i<n;i++)
{
printf(“\n\n%d\t%s\t%f”,e[i].eno,e[i].ename,e[i].sal);
}
getch();
}
Q. 3) Write menu driven program using ‘C’ for Singly linked list. The menu includes
- Create
- Display
- Search particular node in list and display its position.
[Marks 25]
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void ser(int);
void main()
Slip
120
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
{
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.SEARCH \n”);
printf(“\n5.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:
printf(“\nENTER THE ELEMENT FOR SEARCH”);
scanf(“%d”,&m);
ser(m);
break;
case 5:
exit(0);
}
}
while(ch!=7);
getch();
}
void count()
{
Slip
121
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
struct node *q;
int cnt=0;
q=start;
while(q!=NULL)
{
cnt++;
q=q->link;
}
printf(“\n\No Of Nodes Are\t %d”,cnt);
}
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”);
}
}
Slip
122
S.Y.B.C.A. Sem-III Practical Slip Oct/April 2014 - 2015
Data Structure Using ‘C’
void ser(int data)
{
struct node *q,*tmp;
q=start;
while(q!=NULL)
{
if(q->data==data)
{
printf(“\nElement Is Found”);
break;
}
else
{
q=q->link;
}
}
if(q==NULL)
{
printf(“\nElement is Not Found”);
}
}

Slip
123

Das könnte Ihnen auch gefallen