Sie sind auf Seite 1von 56

Data Structure lab programs 2016

1) Design, develop and implement a menu driven program in c for the


following array operations
a) Creating an array of n integer elements
b) Display of array elements with suitable headings
c) Inserting an element(ELEM) at a given valid position (POS)
d) Deleting an element at a given valid position(POS)
e) Exit.
Support the program with functions for each of the above
operations:-

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 1


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 2


Data Structure lab programs 2016

}
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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 3


Data Structure lab programs 2016

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 4


Data Structure lab programs 2016

2. Design, Develop and implement a program in c for the following


operations on Strings:-
a) Read a main string (STR), a pattern String (PAT) and a
replace string (REP)
b) Perform pattern matching operation: Find and replace all
occurrences of PAT in STR with REP if PAT exists in Str.
Report suitable messages in case PAT does not exist in STR

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 5


Data Structure lab programs 2016

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

printf("enter the pattern string p:");


gets(p);
printf("enter the replase string r:");
gets(r);
my_search_replace(p,t,r,d);
printf("the pattern string after replasing %s\n",d);
getch();
}

OUTPUT

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 7


Data Structure lab programs 2016

3. Design, develop and implement a menu driven program in c for the


following array operations on STACK of integers (array
implementation of stack with maximum size MAX)
a) PUSH an element on to stack
b) POP an element from stack
c) Demonstrate OVERFLOW and UNDERFLOW situations on
stack
d) Display the status of stack
e) Exit.

Support the program with appropiate functions for each of the


above operations:-

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 8


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 9


Data Structure lab programs 2016

printf("display is not possible\n");


}
else
{
printf("the stack elements are:\n");
for(i=0;i<=top;i++)
{
printf("%d\t",s[i]);
}
}
}

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 10


Data Structure lab programs 2016

OUTPUT

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 11


Data Structure lab programs 2016

4. Design, develop and implement a menu driven program in c for


converting an INFIX expression to POSTFIX expression. Program
should support for both parenthesized expressions with the
operators: +, -, *, /, %(remainder), ^(power) and alphanumeric
operands:-

#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)
{

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 12


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 13


Data Structure lab programs 2016

OUTPUT

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 14


Data Structure lab programs 2016

5. Design, develop and implement a program in c for the following stack


applications:-
a) Evaluation of suffix expression with single digit operands and
operators: +, -, *, /, %, ^

#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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 16


Data Structure lab programs 2016

b) Solving Tower of Hanoi problem with n disks:-

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 17


Data Structure lab programs 2016

OUTPUT

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 18


Data Structure lab programs 2016

6. Design, develop and implement a menu driven program in c for the


following operations on circular QUEUE of characters (array
implementation of circular queue with maximum size MAX)
a) Insert an element on to circular QUEUE
b) Delete an element from circular QUEUE
c) Demonstrate OVERFLOW and UNDERFLOW situations on
circular QUEUE
d) Display the status of circular QUEUE
e) Exit
Support the program with appropiate functions for each of the
above operations:-

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 20


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 21


Data Structure lab programs 2016

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 22


Data Structure lab programs 2016

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 23


Data Structure lab programs 2016

7. Design, develop and implement a menu driven program in c for the


following operations on singly linked list (SLL) of student data with
the fields: USN, Name, Branch, Sem, PhNo
a) Create a SLL of N students data by using front end.
b) Display the status of SLL and count the number of nodes init.
c) Perform insertion / deletion at end of SLL
d) Perform insertion / deletion at front of SLL (demonstration of
stack)
e) Exit

#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

printf("enter you 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;
default:exit(0);
}
}
}
void ins_front()
{
struct node *p;
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
{
p->link=first;
first=p;
}
}

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 25


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;

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 26


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 27


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 28


Data Structure lab programs 2016

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 29


Data Structure lab programs 2016

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 30


Data Structure lab programs 2016

8. Design, develop and implement a menu driven program in c for the


following operations on doubly linked list (DLL) of employee data
with the fields: SSN, Name, Dept, Design, sal, PhNo
a) Create a DLL of N employees data by using end insertion.
b) Display the status of DLL and count the number of nodes init.
c) Perform insertion / deletion at end of DLL
d) Perform insertion / deletion at front of DLL (demonstration of
stack)
e) Demonstrate how this DLL can be used as Double ended queue
f) Exit

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 32


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 33


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 34


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 35


Data Structure lab programs 2016

OUTPUT

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 36


Data Structure lab programs 2016

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 37


Data Structure lab programs 2016

9. Design, Develop and implement a program in c for the following


operations on Singly circular linked list (SCLL) with header nodes:
a) Represent and evaluate polynomials:-

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 38


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 41


Data Structure lab programs 2016

OUTPUT

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 42


Data Structure lab programs 2016

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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 44


Data Structure lab programs 2016

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

void preorder(struct node*root)


{
if(root==NULL)
return;
printf("%d\t",root->data);
preorder(root->llink);
preorder(root->rlink);
}
void inorder(struct node*root)
{
if(root==NULL)
return;
inorder(root->llink);
printf("%d\t",root->data);
inorder(root->rlink);
}
void postorder(struct node*root)
Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 46
Data Structure lab programs 2016

{
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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 48


Data Structure lab programs 2016

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 49


Data Structure lab programs 2016

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 50


Data Structure lab programs 2016

11. Design, Develop and implement a progam in c for the following


operations on Graphs(G) of cities:-
a) Create a Graph of N cities using adjacency matrix.
b) Print all the nodes reachable from a given starting node in a
diagraph using DFS/BFS method.

#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++)

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 51


Data Structure lab programs 2016

{
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

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 53


Data Structure lab programs 2016

-:Viva Questions:-

1. What is data structure?

2. What are the types of data structure?

3. What is stack?

4. What is queue?

5. What are the types of queue?

6. What is linear queue?

7. What is circular queue?

8. What is pointer?

9. What are the types of memory allocation functions?

10. What is static memory allocation?

11. What is dynamic memory allocation?

12. What are the disadvantages of static memory allocation?

13. What are the functions of dynamic memory allocation functions?

14. What is malloc function?

15. What is calloc function?

16. What is realloc function?

17. What is the recursive function?

18. What are the types of recursive functions?

19. What is performance analysis?

20. What are the types of performance analysis?

21. What is time complexity?

22. What is spall complexity?

23. What is mean by asymptotic notation?

24. What are the types of asymptotic notation?

25. What is an array?

26. What is structure?

27. What are the different ways to the structure declaration?

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 54


Data Structure lab programs 2016

28. What is polynomial?

29. What is the degree of polynomials

30. What are the advantages of polynomial?

31. What are the disadvantages of polynomials?

32. What is sparse matrix?

33. What is transpose of matrix?

34. What are the types of stack operation?

35. What are the applications of stack?

36. What is infix expression?

37. What is prefix expression?

38. What is postfix expression?

39. What are the disadvantages of queue?

40. What is linked list?

41. What are the types of linked list?

42. What is singly linked list?

43. What is doubly linked list?

44. What are the differences between singly linked list and doubly linked

list?
45. What is tree?

46. What is a siblings?

47. What is root node?

48. What is leaf node?

49. What are the types of leaf node?

50. What is binary tree?

51. What are types of binary tree?

52. What is strictly binary tree?

53. What is complete binary tree?

54. What are different traversal techniques of a binary tree?

55. What is almost complete binary tree?

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 55


Data Structure lab programs 2016

56. What is heap?

57. What are the types of heap?

58. What is max heap?

59. What is min heap?

60. What is string?

61. What are operations of string?

62. What is graph?

63. What is union?

64. What are the differences between array and stack?

65. What are the differences between array and queue?

66. What are the differences between structure and union?

Govt engineering clg, KR Pete Mr. Venkatesh Prasad B.S. Page 56

Das könnte Ihnen auch gefallen