Professional Documents
Culture Documents
Data Structure Lab Manual
Data Structure Lab Manual
Data Structure Lab Manual
RESULT:
PROGRAM:
//1b.Array implementation of Queue
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delet();
void display();
int queue[maxsize];
int main()
int choice;
while(choice != 4)
printf("\n**********Main Menu**********");
scanf("%d",&choice);
switch(choice)
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
void insert()
int item;
scanf("%d",&item);
if(rear == maxsize-1)
printf("\nOVERFLOW\n");
return;
front = 0;
rear = 0;
}
else
rear = rear+1;
queue[rear] = item;
void delet()
int item;
printf("\nUNDERFLOW\n");
return;
else
item = queue[front];
if(front == rear)
front = -1;
rear = -1 ;
else
{
front = front + 1;
void display()
int i;
if(rear == -1)
printf("\nEmpty queue\n");
else
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
OUTPUT:
RESULT:
PROGRAM:
#include <stdio.h>
# define max 6
int queue[max];
int front=-1;
int rear=-1;
void enqueue(int element)
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
int dequeue()
{
if((front==-1) && (rear==-1))
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
void display()
{
int i=front;
if(front==-1 && rear==-1)
printf("\n Queue is empty..");
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x;
while(choice<4 && choice!=0)
{
printf("\nPress 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element which is to be inserted ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}
}
return 0;
}
OUTPUT:
RESULT:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
while(choice != 9)
printf("\n\n*********Main Menu*********\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete
from Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Search for
an element\n8.Show\n9.Exit\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
void beginsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
void lastinsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
head = ptr;
printf("\nNode inserted");
else
{
temp = head;
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
void randominsert()
int i,loc,item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
temp = temp->next;
if(temp == NULL)
printf("\ncan't insert\n");
return;
printf("\nNode inserted");
void begin_delete()
if(head == NULL)
printf("\nList is empty\n");
else
{
ptr = head;
head = ptr->next;
free(ptr);
void last_delete()
if(head == NULL)
printf("\nlist is empty");
head = NULL;
free(head);
else
ptr = head;
while(ptr->next != NULL)
ptr1 = ptr;
ptr1->next = NULL;
free(ptr);
void random_delete()
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
printf("\nCan't delete");
return;
free(ptr);
void search()
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
scanf("%d",&item);
while (ptr!=NULL)
if(ptr->data == item)
flag=0;
else
flag=1;
i++;
ptr = ptr -> next;
if(flag==1)
void display()
ptr = head;
if(ptr == NULL)
printf("Nothing to print");
else
while (ptr!=NULL)
printf("\n%d",ptr->data);
}
OUTPUT:
RESULT:
PROGRAM:
//3a Linked list implementation of stack
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
int main()
{
int choice=0;
printf("\n***Stack operations using linked list*\n");
printf("----------------------------------------------");
while(choice!= 4)
{
printf("\nChose one from the below options...\n");
printf("1.Push\t2.Pop\t3.Show\t4.Exit");
printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node*ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value : ");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
printf("Stack is empty\n");
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
OUTPUT:
RESULT:
PROGRAM:
//3b Linked list implementation of Linear Queue
#include <stdio.h>
#include <stdlib.h>
struct QNode
{
int key;
struct QNode* next;
};
struct Queue
{
struct QNode *front, *rear;
};
struct QNode* newNode(int k)
{
struct QNode* temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue* createQueue()
{
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue* q, int k)
{
struct QNode* temp = newNode(k);
if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
void deQueue(struct Queue* q)
{
if (q->front == NULL)
return;
struct QNode* temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
int main()
{
struct Queue* q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
deQueue(q);
printf("Queue Front : %d \n", q->front->key);
printf("Queue Rear : %d", q->rear->key);
return 0;
}
OUTPUT:
RESULT:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node{
float coef;
int expo;
};
main( ){
start1=create(start1);
start2=create(start2);
printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);
poly_add(start1, start2);
poly_mult(start1, start2);
int i,n,ex;
float co;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%f",&co);
scanf("%d",&ex);
start=insert_s(start,co,ex);
return start;
tmp->coef=co;
tmp->expo=ex;
tmp->link=start;
start=tmp;
else
ptr=start;
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
return start;
tmp->coef=co;
tmp->expo=ex;
if(start==NULL)
tmp->link=start;
start=tmp;
else
{
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
return start;
if(ptr==NULL)
printf("Zero polynomial\n");
return;
while(ptr!=NULL)
printf("(%.1fx^%d)", ptr->coef,ptr->expo);
ptr=ptr->link;
if(ptr!=NULL)
printf(" + ");
else
printf("\n");
}
void poly_add(struct node *p1,struct node *p2)
start3=NULL;
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
else if(p1->expo==p2->expo)
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link;
p2=p2->link;
while(p1!=NULL)
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
while(p2!=NULL)
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
display(start3);
}/*End of poly_add() */
start3=NULL;
if(p1==NULL || p2==NULL)
return;
while(p1!=NULL)
p2=p2_beg;
while(p2!=NULL)
start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo);
p2=p2->link;
p1=p1->link;
display(start3);
OUTPUT:
RESULT:
PROGRAM:
#include<stdio.h>
#include<ctype.h>>
int stack[20];
void push(int x)
stack[++top] = x;
int pop()
return stack[top--];
int main()
char exp[20];
char *e;
int n1,n2,n3,num;
scanf("%s",exp);
e = exp;
while(*e!='\0')
if(isdigit(*e))
{
num = *e - 48;
push(num);
else
n1 = pop();
n2 = pop();
switch(*e)
case '+':
n3 = n1 + n2;
break;
case '-':
n3 = n2 - n1;
break;
case '*':
n3 = n1 * n2;
break;
case '/':
{
n3 = n2 / n1;
break;
push(n3);
e++;
return 0;
OUTPUT:
RESULT:
PROGRAM:
#include<stdio.h>
#include<ctype.h>
char stack[100];
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
return 0;
}
int main()
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
push(*e);
else
printf("%c ",pop());
push(*e);
}
e++;
while(top != -1)
printf("%c ",pop());
}return 0;
OUTPUT:
RESULT:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
int height;
};
if (N == NULL)
return 0;
return N->height;
return (a > b) ? a : b;
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
x->right = y;
y->left = T2;
return x;
y->left = x;
x->right = T2;
return y;
if (N == NULL)
return 0;
return (newNode(key));
else
return node;
node->height = 1 + max(height(node->left),height(node->right));
return rightRotate(node);
return leftRotate(node);
node->left = leftRotate(node->left);
return rightRotate(node);
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
current = current->left;
return current;
if (root == NULL)
return root;
else {
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
root->key = temp->key;
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left),
height(root->right));
return rightRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
return leftRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
if (root != NULL) {
printPreOrder(root->left);
printPreOrder(root->right);
}
int main() {
printPreOrder(root);
printPreOrder(root);
return 0;
}
OUTPUT:
RESULT:
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
int height;
};
if (N == NULL)
return 0;
return N->height;
return (a > b) ? a : b;
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
x->right = y;
y->left = T2;
return x;
y->left = x;
x->right = T2;
return y;
if (N == NULL)
return 0;
return (newNode(key));
else
return node;
node->height = 1 + max(height(node->left),
height(node->right));
return rightRotate(node);
return leftRotate(node);
node->left = leftRotate(node->left);
return rightRotate(node);
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
current = current->left;
return current;
if (root == NULL)
return root;
else {
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
root->key = temp->key;
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left),
height(root->right));
return rightRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
return leftRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
if (root != NULL) {
printPreOrder(root->left);
printPreOrder(root->right);
}
}
int main() {
printPreOrder(root);
printPreOrder(root);
return 0;
OUTPUT:
RESULT:
PROGRAM:
#include<stdio.h>
#include<malloc.h>
void insert();
void del();
void display();
struct node
int priority;
int info;
}*start=NULL,*q,*temp,*New;
int main()
int ch;
do
scanf("%d",&ch);
switch(ch)
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:
break;
while(ch<4);
void insert()
int item,itprio;
New=(N*)malloc(sizeof(N));
scanf("%d",&item);
scanf("%d",&itprio);
New->info=item;
New->priority=itprio;
New->next=NULL;
if(start==NULL )
start=New;
else if(start!=NULL&&itprio<=start->priority)
{ New->next=start;
start=New;
else
q=start;
{q=q->next;}
New->next=q->next;
q->next=New;
void del()
if(start==NULL)
printf("\nQUEUE UNDERFLOW\n");
else
New=start;
start=start->next;
void display()
{
temp=start;
if(start==NULL)
printf("QUEUE IS EMPTY\n");
else
printf("QUEUE IS:\n");
if(temp!=NULL)
for(temp=start;temp!=NULL;temp=temp->next)
}
OUTPUT:
RESULT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
int main()
int G[MAX][MAX],i,j,n,u;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
mindistance=distance[i];
nextnode=i;
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
count++;
for(i=0;i<n;i++)
if(i!=startnode)
printf("\nPath=%d",i);
j=i;
do
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
OUTPUT:
RESULT:
PROGRAM:
#include <stdio.h>
#include <limits.h>
return min;
int parent[vertices];
int k[vertices];
int mst[vertices];
int i, count,edge,v;
k[i] = INT_MAX;
mst[i] = 0;
mst[edge] = 1;
int main()
{0, 4, 2, 0, 1},
{0, 0, 6, 1, 0}, };
prim(g);
return 0;
}
OUTPUT:
RESULT:
PROGRAM:
#include <stdio.h>
int main()
int num;
scanf("%d", &num);
int array[num];
scanf("%d", &array[i]);
scanf("%d", &keynum);
if (keynum == array[i] )
found = 1;
break;
if (found == 1)
printf("Element is present in the array at position %d",i+1);
else
OUTPUT:
RESULT:
PROGRAM:
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
first = middle + 1;
break;
else
last = middle - 1;
return 0;
OUTPUT:
RESULT:
PROGRAM:
#include<stdio.h>
int main(){
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=1;i<count;i++){
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0)){
number[j+1]=number[j];
j=j-1;
number[j+1]=temp;
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
OUTPUT:
RESULT:
PROGRAM:
#include <stdio.h>
int i, j, small;
small = i;
small = j;
arr[small] = arr[i];
arr[i] = temp;
int i;
int main()
printArr(a, n);
selection(a, n);
printArr(a, n);
return 0;
OUTPUT:
RESULT:
PROGRAM:
#include <stdio.h>
#define max 10
int b[10];
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
b[i] = a[l1++];
else
b[i] = a[l2++];
b[i++] = a[l1++];
b[i++] = a[l2++];
a[i] = b[i];
int mid;
sort(low, mid);
sort(mid+1, high);
} else {
return;
int main() {
int i;
sort(0, max);
}
OUTPUT:
RESULT:
PROGRAM:
#include <stdio.h>
#include <conio.h>
int tsize;
int i ;
i = key%tsize ;
return i;
//-------LINEAR PROBING-------
int i ;
i = (key+1)%tsize ;
return i ;
//-------QUADRATIC PROBING-------
int i ;
i = (key+(j*j))%tsize ;
return i ;
int main()
{
int key,arr[20],hash[20],i,n,s,op,j,k ;
scanf ("%d",&tsize);
scanf ("%d",&n);
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for (i=0;i<n;i++)
scanf("%d",&arr[i]);
do
scanf("%d",&op);
switch(op)
case 1:
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
i = rehashl(i);
hash[i]=key ;
for (i=0;i<tsize;i++)
break ;
case 2:
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
j=1;
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
i = rehashq(i,j);
j++ ;
hash[i]=key ;
}
for (i=0;i<tsize;i++)
break ;
}while(op!=3);
getch() ;
}
OUTPUT:
RESULT:
INDEX