Data Structures Lab Programs
Data Structures Lab Programs
Data Structures Lab Programs
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
struct stack
{
int top;
int data[SIZE];
};
typedef struct stack STACK;
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;
}
}
}
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;
}
#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;
}
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;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("\n Read number of discs:");
scanf("%d",&n);
towerofHanoi(n,'S','T','D');
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
struct queue
{
int front,rear;
char data[SIZE];
};
typedef struct queue QUEUE;
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;
#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;
}
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;
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;
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;
}
#include <stdio.h>
#include <stdlib.h>
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);
}
}
}