Anurag Engineering College: (A53214) Data Structures Lab
Anurag Engineering College: (A53214) Data Structures Lab
PREREQUISITES:
1. Any programming language
CO-REQUISITE :
1. course on “data structures.”
PART- A
1. Program to illustrate string built infunctions
2. Program to evaluate postfixnotations
3. Program to convert infix to postfixnotation
4. Program to illustrate tree traversals
a) Inorder
b) Preorder
c) Postorder
5. Program to illustrate insertion, deletion and searching in Binary SearchTree.
6. Program to illustrate Graphtraversals
a) Breadth FirstSearch
b) Depth FirstSearch
7. Program to illustrate Insertion, deletion and Rotation on AVLTrees.
1. Program to illustrate string built infunctions
STRING COPY
#include<stdio.h>
#include<string.h>
int main(void)
{
char s1[30],s2[30];
clrscr();
printf("\n Enter the string (s1) : ");
gets(s1);
strcpy(s2,s1);
printf("\n Copied string(s2): %s ",s2);
return 0;
}
OUTPUT:
Enter the string (s1): vijay
STRING CONCATENATION
#include<stdio.h>
#include<string.h>
int main(void)
{
char s1[30],s2[30];
clrscr();
printf("\n Enter the string (s1): ");
gets(s1);
printf("\n Enter the string (s2): ");
gets(s2);
strcat(s1,s2);
printf("\n Concatenated string (s2) : %s",s1);
return 0;
}
OUTPUT:
Enter the string (s1): anurag
Enter the string (s2): engineering
STRING REVERSE
#include<stdio.h>
#include<string.h>
int main(void)
{
char s[30];
clrscr();
printf("\n Enter the string(s): ");
gets(s);
strrev(s);
printf("\n Reversed string(s): %s",s);
return 0;
}
OUTPUT:
Enter the string(s): anurag
STRING COMPARE
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[100], b[100];
clrscr();
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if (strcmp(a,b) == 0)
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
getch();
}
OUTPUT:
Enter the first string : anurag
Enter the second string : anurag
Entered strings are equal.
STRING COMPARE(strncmp)
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char line[100];
char line2[100];
clrscr();
printf("Input first word:");
scanf("%s", line);
printf("Input second word:");
scanf("%s", line2);
if (strncmp(line, line2, 2) == 0)
printf ("equal\n");
if (strncmp(line, line2, 2) < 0)
printf("less than equal\n");
if (strncmp(line, line2, 2) > 0)
printf("greater than equal\n");
getch();
}
OUTPUT:
Input First Word: anurag
Input Second Word: anurag
equal
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *s1 ="anurag";
char *s2 ="anuragengineering";
char *s3 ="anurag";
clrscr();
strcmp("Biscuits","Kittens");
strcmp("Kittens","Biscuits");
if(strcmp(s1,s2)==0)
printf("\n This wont get printed because the strings differ");
if(strcmp(s1,s3)==0)
printf("\n This will print because s1 and s3 are the same");
if(!strcmp(s1,s2))
printf("\n The strings are the same");
if(!strncmp(s1,s2,6))
printf("\n The first 6 characters of s1 and s2 are the same");
getch();
}
STRING LENGTH
#include<stdio.h>
#include<string.h>
int main(void)
{
char a[30];
int l;
clrscr();
printf("Enter a string: ");
scanf("%s",&a);
l=strlen(a);
printf("length: %d",l);
return 0;
}
OUTPUT:
Enter a string: vijay
Length:5
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char S1[80]={"COMPUTER"};
char S2[80]={"MARC"};
clrscr();
printf("Length(%s) = %d",S1,strlen(S1));
printf("\nLength(%s) = %d",S2,strlen(S2));
getch();
}
OUTPUT:-
LENGTH (COMPUTER) = 8
LENGTH(MARC) = 4
CONVERSION OF INFIX TO POSTFIX CONVERSION USING ADT
#include<stdio.h>
#include<conio.h>
/*Stack ADT Begining*/
struct stack
{
char* a;
int top;
int size;
};
typedef struct stack Stack;
Stack* initialize_stack()
{
Stack* s;
s = (Stack*)malloc(sizeof(Stack));
s->a = (char*)malloc(sizeof(char)*50);
s->top=0;
s->size=50;
return s;
}
int isempty(Stack* s)
{
return s->top == 0;
}
int isfull(Stack* s)
{
return s->top == s->size;
}
void push(Stack* s,char e)
{
if(!isfull(s))
s->a[s->top++] = e;
else
printf("\nStack Overflow");
}
char pop(Stack* s)
{
char e=0;
if(!isempty(s))
{
e = s->a[s->top-1];
s->top--;
}
return e;
}
char stack_top(Stack* s)
{
char e=0;
if(!isempty(s))
e = s->a[s->top-1];
return e;
}
/*Stack ADT Ends */
int precedence(char ele)
{
int pr;
switch(ele)
{
case '#': pr = 0;break;
case '(': pr = 1;break;
case '+':
case '-': pr = 2;break;
case '*':
case '/':
case '%': pr = 3;break;
}
return pr;
}
void conv_infix_to_postfix(char* infix,char* postfix)
{
Stack* s;
char ch,e;
int i=0,k=0;
s = initialize_stack();
push(s,'#');
while( (ch = infix[i++])!='\0')
{
if(ch == '(')
push(s,ch);
else if(isalnum(ch))
postfix[k++] = ch;
else if(ch ==')')
{
while(stack_top(s) != '(')
postfix[k++] = pop(s);
e=pop(s);
}
else
{
while(precedence(stack_top(s)) >= precedence(ch))
postfix[k++] = pop(s);
push(s,ch);
}
}
while(stack_top(s) != '#')
postfix[k++] = pop(s);
postfix[k] = '\0';
}
int main()
{
char infix_expr[50],postfix_expr[50];
clrscr();
printf("Enter Infix Expression:");
gets(infix_expr);
printf("\nThe Infix Expression :%s",infix_expr);
conv_infix_to_postfix(infix_expr,postfix_expr);
printf("\nPostfix Expression:%s",postfix_expr);
getch();
return 0;
}
OUTPUT:
Enter infix expression : a+b+c+d
Postfix expression:ab+c+d
ADT PROGRAM FOR EVALUATION OF POSTFIX EXPRESSION
#include<stdio.h>
#include<conio.h>
/*Stack ADT Begining*/
struct stack
{
int* a;
int top;
int size;
};
typedef struct stack Stack;
Stack* initialize_stack()
{
Stack* s;
s = (Stack*)malloc(sizeof(Stack));
s->a = (int*)malloc(sizeof(int)*50);
s->top=0;
s->size=50;
return s;
}
int isempty(Stack* s)
{
return s->top == 0;
}
int isfull(Stack* s)
{
return s->top == s->size;
}
void push(Stack* s,int e)
{
if(!isfull(s))
s->a[s->top++] = e;
else
printf("\nStack Overflow");
}
int pop(Stack* s)
{
int e=-9999;
if(!isempty(s))
{
e = s->a[s->top-1];
s->top--;
}
return e;
}
int stack_top(Stack* s)
{
int e=-9999;
if(!isempty(s))
e = s->a[s->top-1];
return e;
}
void delete_stack(Stack* st)
{
free(st->a);
free(st);
}
/*Stack ADT Ends */
int eval_postfix(char* postfix)
{
char ch;
int i=0,op1,op2,result;
Stack *st;
st = initialize_stack();
while( (ch=postfix[i++]) != '\0')
{
if(isdigit(ch))
push(st,ch-'0');
else
{
op1 = pop(st);
op2 = pop(st);
switch(ch)
{
case '+':push (st,op1+op2); break;
case '-':push(st,op1-op2);break;
case '*':push(st,op1 * op2); break;
case '/':push(st,op1 / op2); break;
}
}
}
result = stack_top(st);
delete_stack(st);
return result;
}
int main()
{
char postfix_expr[50];
int result;
clrscr();
printf("\nEnter Postfix Expression:%s",postfix_expr);
gets(postfix_expr);
result = eval_postfix(postfix_expr);
printf("\nResult of Postfix Expression : %d",result);
getch();
return 0;
}
OUTPUT:
OUTPUT
Enter 1:insert, 2:preorder, 3:postorder, 4:inorder, 5:exit
1
Enter data 4
Enter 1:insert, 2:preorder, 3:postorder, 4:inorder, 5:exit
1
Enter data 8
Enter 1:insert, 2:preorder, 3:postorder, 4:inorder, 5:exit
1
Enter data 6
Enter 1:insert, 2:preorder, 3:postorder, 4:inorder, 5:exit
2
Preorder Traversal: 4 8 6
Enter 1:insert, 2:preorder, 3:postorder, 4:inorder, 5:exit
3
Postorder Traversal: 6 8 4
Enter 1:insert, 2:preorder, 3:postorder, 4:inorder, 5:exit
4
Inorder Traversal: 4 6 8
4. Program to illustrate insertion, deletion and searching in Binary Search Tree.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
}*t,*temp;
int element;
void inorder(struct tree*);
struct tree *create(struct tree*, int);
struct tree *find(struct tree*, int);
struct tree *insert(struct tree*, int);
struct tree *del(struct tree*, int);
struct tree *findmin(struct tree*);
struct tree *findmax(struct tree*);
void main()
{
int ch;
printf ("\n\n\t BINARY SEARCH TREE");
do
{
printf ("\nMain Menu\n");
printf ("\n1.Create \n2.Insert \n3.Delete \n4.Find \n5.Findmax \n6.Findmin");
printf ("\n7.Exit");
printf ("\nEnter your choice:");
scanf ("%d", &ch);
switch(ch)
{
case 1: printf("Enter the element\n");
scanf("%d", &element);
t = create(t,element);
inorder(t);
break;
case 2: printf("Enter the element\n");
scanf("%d", &element);
t = insert(t,element);
inorder(t);
break;
case 3: printf("Enter the data");
scanf ("%d",&element);
t = del(t,element);
inorder(t);
break;
case 4: printf ("Enter the data");
scanf("%d",&element);
temp = find(t,element);
if(temp->data==element)
printf ("Element %d is found", element);
else
printf ("Element is not found");
break;
case 5: temp=findmax(t);
printf("Maximum element is %d",temp->data);
break;
case 6: temp = findmin(t);
printf ("Minimum element is %d", temp->data);
break;
}
}
while(ch!= 7);
}
struct tree *create(struct tree* t, int element)
{
t = (struct tree*) malloc (sizeof(struct tree));
t->data= element;
t->lchild = NULL;
t->rchild = NULL;
return t;
}
struct tree *find(struct tree* t, int element)
{
if(t== NULL)
return NULL;
if (element< t->data)
return (find(t->lchild, element));
else if(element>t->data)
return (find(t->rchild,element));
else
return t;
}
struct tree *findmin(struct tree* t)
{
if (t==NULL)
return NULL;
else
if (t->lchild == NULL)
return t;
else
return (findmin (t->lchild));
}
struct tree *findmax (struct tree* t)
{
if(t!=NULL)
{
while (t->rchild != NULL)
t = t->rchild;
}
return t;
}
struct tree *insert (struct tree* t, int element)
{
if (t== NULL)
{
t = (struct tree*) malloc (sizeof(struct tree));
t->data = element;
t->lchild = NULL;
t->rchild = NULL;
return t;
}
else
{
if(element< t->data)
{
t->lchild = insert(t->lchild, element);
}
else
if(element> t->data)
{
t->rchild = insert (t->rchild, element);
}
else if(element == t->data)
{
printf ( "Element already present\n");
}
return t;
}
}
struct tree* del(struct tree* t, int element)
{
if (t == NULL)
printf ("Element not found\n");
else
if(element< t->data)
t->lchild = del(t->lchild, element);
else
if(element> t->data)
t->rchild = del(t->rchild, element);
else
if(t->lchild && t->rchild)
{
temp = findmin(t->rchild);
t->data = temp->data;
t->rchild = del (t->rchild, t->data);
}
else
{
temp = t;
if (t->lchild == NULL)
t = t->rchild;
else
if (t->rchild == NULL)
t = t->lchild;
free (temp);
}
return t;
}
void inorder (struct tree *t)
{
if (t == NULL) return;
else
{
inorder (t->lchild);
printf ("\t%d", t->data);
inorder (t->rchild);
}
}
}
7. Program to illustrate Insertion, deletion and Rotation on AVLTrees.
#include<stdio.h>
int main()
{
node *root=NULL;
int x,n,i,op;
do
{
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:");
printf("\n\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter no. of elements:");
scanf("%d",&n);
printf("\nEnter tree data:");
root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
break;
return 0;
}
T->ht=height(T);
return(T);
}
if(T==NULL)
{
return NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
if(x<T->data)
{
T->left=Delete(T->left,x);
if(BF(T)==-2) //Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
//data to be deleted is found
if(T->right!=NULL)
{ //delete its inorder succesor
p=T->right;
while(p->left!= NULL)
p=p->left;
T->data=p->data;
T->right=Delete(T->right,p->data);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
}
return(y);
}
return(T);
}
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
}
Breadth FirstSearch
#include<stdio.h>
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();
void main()
{
int n,i,s,ch,j;
char c,dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("THE ADJACENCY MATRIX IS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
do
{
for(i=1;i<=n;i++)
vis[i]=0;
printf("\nMENU");
printf("\n1.B.F.S");
printf("\n2.D.F.S");
printf("\nENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);
switch(ch)
{
case 1:bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? ");
scanf("%c",&dummy);
scanf("%c",&c);
}while((c=='y')||(c=='Y'));
}
#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:");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
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);
}