Data Structures Lab Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

DATA STRUCTURES LABORATORY

Course Code CIE Marks 50


Hours/Week (L: T: P) 0:0:2 SEE Marks 50
No. of Credits 1 Examination Hours 3 Hours

1. Develop a menu driven Program in C for the following 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 appropriate functions for each of the above
operations

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
struct stack
{
int top;
int data[SIZE];
};
typedef struct stack STACK;

void push(STACK *s,int item)


{
if(s->top==SIZE-1)
printf("\n Stack Overflow");
else
{
s->top=s->top+1;
s->data[s->top]=item;
}
}

void pop(STACK *s)


{
if(s->top==-1)
printf("\n Stack Underflow");
else
{
printf("\n Element popped is %d",s->data[s->top]);
s->top=s->top-1;
}
}

void display(STACK s)
{
int i;
if(s.top ==-1)
printf("\n Stack Empty");
else
{
printf("\ Stack content are\n");
for(i=s.top;i>=0;i--)
printf("%d\n",s.data[i]);
}
}
int main()
{
int ch,item;
STACK s;
s.top=-1;
for(;;)
{
printf("\n1. Push\n2. Pop\n3. Display\n4.Exit");
printf("\nRead Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Read element to be pushed :");
scanf("%d",&item);
push(&s,item);
break;
case 2:pop(&s);
break;
case 3:display(s);
break;
default:exit(0);
}
}
return 0;

2. Develop a Program in C for converting an Infix Expression to Postfix Expression.


Program should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and
alphanumeric operands
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 20
struct stack
{
int top;
char data[SIZE];
};
typedef struct stack STACK;
void push(STACK *s,char item)
{
s->data[++(s->top)]=item;
}

char pop(STACK *s)


{
return s->data[(s->top)--];
}

int preced(char symbol)


{
switch(symbol)
{
case '^':return 5;
case '*':
case '/':return 3;
case '+':
case '-':return 1;
}
}

void infixtopostfix(STACK *s,char infix[SIZE])


{
int i,j=0;
char postfix[SIZE],temp,symbol;
for(i=0;infix[i]!='\0';i++)
{
symbol=infix[i];
if(isalnum(symbol))
postfix[j++]=symbol;
else
{
switch(symbol)
{
case '(':push(s,symbol);
break;
case ')':temp=pop(s);
while(temp!='(')
{
postfix[j++]=temp;
temp=pop(s);
}
break;
case '+':
case '-':
case '*':
case '/':
case '^': if (s->top ==-1 || s->data[s->top]=='(')
push(s,symbol);
else
{
while(preced(s->data[s->top])>= preced(symbol) && s->top!=-1 &&s->data[s-
>top]!='(')
postfix[j++]=pop(s);
push(s,symbol);
}
break;
default :printf("\n Invalid!!!!!");
exit(0);

}
}
}
while(s->top!=-1)
postfix[j++]=pop(s);
postfix[j]='\0';
printf("\n The postfix expression is %s\n",postfix);
}
int main()
{
STACK s;
s.top=-1;
char infix[SIZE];
printf("\n Read Infix expression\n");
scanf("%s",infix);
infixtopostfix(&s,infix);
return 0;
}

3. Develop and Implement a Program for evaluation of Stack Suffix


expression with single digit operands and operators: +, -, *, /, %, ^.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define SIZE 20
struct stack
{
int top;
float data[SIZE];
};
typedef struct stack STACK;
void push(STACK *s,float item)
{
s->data[++(s->top)]=item;
}

float pop(STACK *s)


{
return s->data[(s->top)--];
}

float operate(float op1,float op2,char symbol)


{
switch(symbol)
{
case '+':return op1+op2;
case '-':return op1-op2;
case '*':return op1*op2;
case '/':return op1/op2;
case '^':return pow(op1,op2);
}
}

float eval(STACK *s,char postfix[SIZE])


{
int i;
char symbol;
float res,op1,op2;
for(i=0;postfix[i]!='\0';i++)
{
symbol=postfix[i];
if(isdigit(symbol))
push(s,symbol-'0');
else
{
op2=pop(s);
op1=pop(s);
res=operate(op1,op2,symbol);
push(s,res);
}
}
return pop(s);
}

int main()
{
char postfix[SIZE];
STACK s;
float ans;
s.top=-1;
printf("\n Read postfix expr\n");
scanf("%s",postfix);
ans=eval(&s,postfix);
printf("\n The final answer is %f\n",ans);
return 0;
}

4. Develop recursive program in C to


i) To Find GCD of 2 numbers

#include <stdio.h>
#include <stdlib.h>

int gcd(int a,int b)


{
if(b!=0)
return gcd(b,a % b);
return a;
}
int main()
{
int a,b;
printf("\n Read two numbers:");
scanf("%d%d",&a,&b);
printf("\n GCD of %d and %d is %d\n",a,b,gcd(a,b));
return 0;
}
ii) To Solve the Tower of Hanoi Problem.

#include <stdio.h>
#include <stdlib.h>

void towerofHanoi(int n,char source,char temp,char destination)


{
if(n==1)
printf("\n Move %d disc from %c to %c",n,source,destination);
else
{
towerofHanoi(n-1,source,destination,temp);
printf("\n Move %d disc from %c to %c",n,source,destination);
towerofHanoi(n-1,temp,source,destination);
}
}

int main()
{
int n;
printf("\n Read number of discs:");
scanf("%d",&n);
towerofHanoi(n,'S','T','D');
return 0;
}

5. Develop a menu driven Program in C for the following operations on QUEUE of


Characters (Array Implementation of QUEUE with maximum size MAX)
a. Enqueue an Element on to Queue
b. Dequeue an Element from Queue
c. Demonstrate Overflow and Underflow situations on Queue
d. Display the status of Queue
e. Exit
Support the program with appropriate functions for each of the above
operations

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
struct queue
{
int front,rear;
char data[SIZE];
};
typedef struct queue QUEUE;

void enqueue(QUEUE *q,char item)


{
if(q->rear==SIZE-1)
printf("\n Queue full");
else
{
q->rear=q->rear+1;
q->data[q->rear]=item;
if(q->front==-1)
q->front=0;
}
}

char dequeue(QUEUE *q)


{
int del;
if(q->front==-1)
{
printf("\n Queue empty");
return -1;
}
else
{
del=q->data[q->front];
if(q->front==q->rear)
{
q->front=-1;
q->rear=-1;
}
else
q->front=q->front+1;
return del;
}
}

void display(QUEUE q)
{
int i;
if(q.front==-1)
printf("\n Queue Empty");
else
{
printf("\n Queue content are\n");
for(i=q.front;i<=q.rear;i++)
printf("%c\t",q.data[i]);
}
}
int main()
{
char item,del;
int ch;
QUEUE q;
q.front=-1;
q.rear=-1;
for(;;)
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4.Exit");
printf("\nRead Choice :");
scanf("%d",&ch);
getchar();
switch(ch)
{
case 1:printf("\n Read element to be inserted :");
scanf("%c",&item);
enqueue(&q,item);
break;
case 2:del=dequeue(&q);
if(del!=-1)
printf("\n Element deleted is %c\n",del);
break;
case 3:display(q);
break;
default:exit(0);
}
}
return 0;

6. Implement a program to multiply two polynomials using singly linked


list.

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int count;
struct node
{
int co,po;
struct node *addr;
};
typedef struct node *NODE;
NODE insertend(NODE start,int co,int po)
{
NODE temp,cur;
temp=(NODE)malloc(sizeof(struct node));
temp->co=co;
temp->po=po;
temp->addr=NULL;
if(start==NULL)
return temp;
cur=start;
while(cur->addr!=NULL)
cur=cur->addr;
cur->addr=temp;
return start;
}

void display(NODE start)


{
NODE temp;
if(start==NULL)
printf("\n Polynomial Empty");
else
{
temp=start;
while(temp->addr!=NULL)
{
printf("%dx^%d+",temp->co,temp->po);
temp=temp->addr;
}
printf("%dx^%d\n",temp->co,temp->po);
}
}

NODE addterm(NODE res,int co,int po)


{
NODE temp,cur;
temp=(NODE)malloc(sizeof(struct node));
temp->co=co;
temp->po=po;
temp->addr=NULL;
if(res==NULL)
return temp;
cur=res;
while(cur!=NULL)
{
if(cur->po==po)
{
cur->co=cur->co+co;
return res;
}
cur=cur->addr;
}
if(cur==NULL)
res=insertend(res,co,po);
return res;
}
NODE multiply(NODE poly1,NODE poly2)
{
NODE p1,p2,res=NULL;
for(p1=poly1;p1!=NULL;p1=p1->addr)
for(p2=poly2;p2!=NULL;p2=p2->addr)
res=addterm(res,p1->co*p2->co,p1->po+p2->po);
return res;
}
int main()
{
NODE poly1=NULL,poly2=NULL,poly;
int co,po;
int i,n,m;
printf("\nRead no of terms of first polynomial:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Read CO and PO of %d term : ",i);
scanf("%d%d",&co,&po);
poly1=insertend(poly1,co,po);
}
printf("\n First polynomial is\n");
display(poly1);
printf("\nRead no of terms of second polynomial:");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
printf("\n Read CO and PO of %d term : ",i);
scanf("%d%d",&co,&po);
poly2=insertend(poly2,co,po);
}
printf("\n Second polynomial is\n");
display(poly2);
poly=multiply(poly1,poly2);
printf("\n Resultant polynomial is\n");
display(poly);
return 0;

7. Design a doubly linked list to represent sparse matrix. Each node in the
list can have the row and column index of the matrix element and the
value
of the element. Print the complete matrix as the output.

#include <stdio.h>
#include <stdlib.h>

struct node
{
int row,col,data;
struct node *next;
struct node *prev;
};
typedef struct node *NODE;

NODE insertend(NODE start,int row,int col,int item)


{
NODE temp,cur;
temp=(NODE)malloc(sizeof(struct node));
temp->row=row;
temp->col=col;
temp->data=item;
temp->next=NULL;
temp->prev=NULL;
if(start == NULL)
return temp;
cur=start;
while(cur->next!=NULL)
cur = cur->next;
cur->next=temp;
temp->prev=cur;
return start;
}

void display(NODE start)


{
NODE temp;
if(start==NULL)
printf("\n list is empty");
else
{
printf("\nROW\tCOL\tDATA\n");
temp=start;
while(temp!=NULL)
{
printf("%d\t%d\t%d\n",temp->row,temp->col,temp->data);
temp=temp->next;
}
}
}

void displaymatrix(NODE start,int m,int n)


{
NODE temp;
int i,j;
temp=start;
printf("\n The Sparse matrix is\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(temp!=NULL && temp->row == i && temp->col == j)
{
printf("%d\t",temp->data);
temp=temp->next;
}
else
printf("0\t");
}
printf("\n");
}

int main()
{
NODE start = NULL;
int i,j,m,n,item;
printf("\n Read the order of the matrix\n");
scanf("%d%d",&m,&n);
printf("\n Read the matrix\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&item);
if(item!=0)
start=insertend(start,i,j,item);
}
}
display(start);
displaymatrix(start,m,n);
return 0;
}

8. Write a C program to create Binary Tree and to traverse the tree using
In-order, Preorder and Post order.

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
typedef struct node *NODE;

NODE create_node(int item)


{
NODE temp;
temp=(NODE)malloc(sizeof(struct node));
temp->data=item;
temp->left=NULL;
temp->right=NULL;
return temp;
}
NODE Insertbst(NODE root,int item)
{
NODE temp;
temp=create_node(item);
if(root==NULL)
return temp;
else
{
if(item < root->data)
root->left=Insertbst(root->left,item);
else
root->right=Insertbst(root->right,item);
}
return root;

void preorder(NODE root)


{
if(root!=NULL)
{
printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
}

void inorder(NODE root)


{
if(root!=NULL)
{
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
}

void postorder(NODE root)


{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->data);
}
}

int main()
{
NODE root = NULL;
int ch,item;
for(;;)
{
printf("\n 1. Insert");
printf("\n 2. Preorder");
printf("\n 3. Inorder");
printf("\n 4. Postorder");
printf("\n 5. Exit");
printf("\n Read ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Read element to be inserted :");
scanf("%d",&item);
root=Insertbst(root,item);
break;
case 2:printf("\n The Preorder traversal is\n");
preorder(root);
break;
case 3:printf("\n The Inorder traversal is\n");
inorder(root);
break;
case 4:printf("\n The Postorder traversal is\n");
postorder(root);
break;
default :exit(0);
}
}
return 0;
}

9. Write a C program to implement priority queue using Heap.

#include <stdio.h>
#include <stdlib.h>

void heapify(int a[10],int n)


{
int i,k,v,j,flag=0;
for(i=n/2;i>=1;i--)
{
k=i;
v=a[k];
while(!flag && 2*k <= n)
{
j=2*k;
if(j<n)
{
if(a[j]<a[j+1])
j=j+1;
}
if(v>=a[j])
flag=1;
else
{
a[k]=a[j];
k=j;
}
}
a[k]=v;
flag=0;
}
}

int main()
{
int n,i,a[10],ch;
for(;;)
{
printf("\n 1. Create Heap");
printf("\n 2. Extractmax");
printf("\n 3. Exit");
printf("\n Read Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Read no of elements :");
scanf("%d",&n);
printf("\n Read Elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapify(a,n);
printf("\n Elements after heap\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
break;
case 2:if(n>=1)
{
printf("\n Element deleted is %d\n",a[1]);
a[1]=a[n];
n=n-1;
heapify(a,n);
if(n!=0)
{
printf("\n Elements after reconstructing heap\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
}
}
else
printf("\n No element to delete");
break;
default:exit(0);

}
}
return 0;
}
10. Write a C program to implement Hashing using Linear probing.
Implement insertion, deletion, search and display.

#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()
{
int key,index,i,flag=0,hkey;
printf("\nenter a value to insert into hash table:");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{
index=(hkey+i)%TABLE_SIZE;
if(h[index] == NULL)
{
h[index]=key;
break;
}
}
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}

void search()
{
int key,index,i,flag=0,hkey;
printf("\nenter search element:");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
{
index=(hkey+i)%TABLE_SIZE;
if(h[index]==key)
{
printf("value is found at index %d",index);
break;
}
}
if(i == TABLE_SIZE)
printf("\n value is not found\n");
}

void display()
{
int i;
printf("\nelements in the hash table are \n");
for(i=0;i< TABLE_SIZE; i++)
printf("\nat index %d \t value = %d",i,h[i]);

}
main()
{
int ch,i;
for(;;)
{
printf("\n1.Insert\n2.Display\n3.Search\n4.Exit\n");
printf("\n Read Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
default:exit(0);
}
}
}

You might also like