PROGRAM:
//1a.Array implementation of stack
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
{
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
printf("****Stack operations using array****");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
show();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}
void pop ()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
void show()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}
OUTPUT:
RESULT:
PROGRAM:
//1b.Array implementation of Queue
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delet();
void display();
int front = -1, rear = -1;
int queue[maxsize];
int main()
int choice;
while(choice != 4)
printf("\n**********Main Menu**********");
printf("\n1.Insert an element\t2.Delete an element\t3.Display the queue\t4.Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice");
void insert()
int item;
printf("\nEnter the element:");
scanf("%d",&item);
if(rear == maxsize-1)
printf("\nOVERFLOW\n");
return;
if(front == -1 && rear == -1)
front = 0;
rear = 0;
}
else
rear = rear+1;
queue[rear] = item;
printf("\nValue inserted ");
void delet()
int item;
if(front == -1 || front > rear)
printf("\nUNDERFLOW\n");
return;
else
item = queue[front];
if(front == rear)
front = -1;
rear = -1 ;
else
{
front = front + 1;
printf("\nvalue deleted ");
void display()
int i;
if(rear == -1)
printf("\nEmpty queue\n");
else
printf("printing values .....\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
OUTPUT:
RESULT:
PROGRAM:
//1c.Array implementation of Circular Queue
#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:
//2 Implementation of Singly Linked List
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
int choice =0;
while(choice != 9)
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\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");
printf("\nEnter your choice?\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:
printf("Please enter valid choice..");
void beginsert()
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
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()
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
else
{
temp = head;
while (temp -> next != NULL)
temp = temp -> next;
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
void randominsert()
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter element value");
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;
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
void begin_delete()
struct node *ptr;
if(head == NULL)
printf("\nList is empty\n");
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
void last_delete()
struct node *ptr,*ptr1;
if(head == NULL)
printf("\nlist is empty");
else if(head -> next == NULL)
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
else
ptr = head;
while(ptr->next != NULL)
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
void random_delete()
struct node *ptr,*ptr1;
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;
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
if(ptr->data == item)
printf("item found at location %d ",i+1);
flag=0;
else
flag=1;
i++;
ptr = ptr -> next;
if(flag==1)
printf("Item not found\n");
void display()
struct node *ptr;
ptr = head;
if(ptr == NULL)
printf("Nothing to print");
else
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
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:
//4 POLYNOMIAL MANIPULATION
#include<stdio.h>
#include<stdlib.h>
struct node{
float coef;
int expo;
struct node *link;
};
struct node *create(struct node *);
struct node *insert_s(struct node *,float,int);
struct node *insert(struct node *,float,int);
void display(struct node *ptr);
void poly_add(struct node *,struct node *);
void poly_mult(struct node *,struct node *);
main( ){
struct node *start1=NULL,*start2=NULL;
printf("Enter polynomial 1 :\n");
start1=create(start1);
printf("Enter polynomial 2 :\n");
start2=create(start2);
printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);
poly_add(start1, start2);
poly_mult(start1, start2);
struct node *create(struct node *start)
int i,n,ex;
float co;
printf("Enter the number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("Enter coeficient for term %d : ",i);
scanf("%f",&co);
printf("Enter exponent for term %d : ",i);
scanf("%d",&ex);
start=insert_s(start,co,ex);
return start;
struct node *insert_s(struct node *start,float co,int ex)
struct node *ptr,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
/*list empty or exp greater than first one */
if(start==NULL || ex > start->expo)
{
tmp->link=start;
start=tmp;
else
ptr=start;
while(ptr->link!=NULL && ptr->link->expo >= ex)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
return start;
struct node *insert(struct node *start,float co,int ex)
struct node *ptr,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
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;
void display(struct node *ptr)
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)
struct node *start3;
start3=NULL;
while(p1!=NULL && p2!=NULL)
if(p1->expo > p2->expo)
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
else if(p2->expo > p1->expo)
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;
printf("Added polynomial is : ");
display(start3);
}/*End of poly_add() */
void poly_mult(struct node *p1, struct node *p2)
struct node *start3;
struct node *p2_beg = p2;
start3=NULL;
if(p1==NULL || p2==NULL)
printf("Multiplied polynomial is zero polynomial\n");
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;
printf("Multiplied polynomial is : ");
display(start3);
OUTPUT:
RESULT:
PROGRAM:
//5a EVALUATING POSTFIX EXPRESSION
#include<stdio.h>
#include<ctype.h>>
int stack[20];
int top = -1;
void push(int x)
stack[++top] = x;
int pop()
return stack[top--];
int main()
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression: ");
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++;
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
OUTPUT:
RESULT:
PROGRAM:
//5b Infix to Postfix convesion
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
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;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
while((x = pop()) != '(')
printf("%c ", x);
else
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
while(top != -1)
printf("%c ",pop());
}return 0;
OUTPUT:
RESULT:
PROGRAM:
//6 Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
struct Node *left;
struct Node *right;
int height;
};
int max(int a, int b);
int height(struct Node *N) {
if (N == NULL)
return 0;
return N->height;
int max(int a, int b) {
return (a > b) ? a : b;
struct Node *newNode(int key) {
struct Node *node = (struct Node *)
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
struct Node *rightRotate(struct Node *y) {
struct Node *x = y->left;
struct Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
struct Node *leftRotate(struct Node *x) {
struct Node *y = x->right;
struct Node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
int getBalance(struct Node *N) {
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
struct Node *insertNode(struct Node *node, int key) {
if (node == NULL)
return (newNode(key));
if (key < node->key)
node->left = insertNode(node->left, key);
else if (key > node->key)
node->right = insertNode(node->right, key);
else
return node;
node->height = 1 + max(height(node->left),height(node->right));
int balance = getBalance(node);
if (balance > 1 && key < node->left->key)
return rightRotate(node);
if (balance < -1 && key > node->right->key)
return leftRotate(node);
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
struct Node *minValueNode(struct Node *node) {
struct Node *current = node;
while (current->left != NULL)
current = current->left;
return current;
struct Node *deleteNode(struct Node *root, int key) {
if (root == NULL)
return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
if ((root->left == NULL) || (root->right == NULL)) {
struct Node *temp = root->left ? root->left : root->right;
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
struct Node *temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left),
height(root->right));
int balance = getBalance(root);
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root->left) < 0) {
root->left = leftRotate(root->left);
return rightRotate(root);
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root->right) > 0) {
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
void printPreOrder(struct Node *root) {
if (root != NULL) {
printf("%d ", root->key);
printPreOrder(root->left);
printPreOrder(root->right);
}
int main() {
struct Node *root = NULL;
root = insertNode(root, 2);
root = insertNode(root, 1);
root = insertNode(root, 7);
root = insertNode(root, 4);
root = insertNode(root, 5);
root = insertNode(root, 3);
root = insertNode(root, 8);
printPreOrder(root);
root = deleteNode(root, 3);
printf("\nAfter deletion: ");
printPreOrder(root);
return 0;
}
OUTPUT:
RESULT:
PROGRAM
//7 AVL Tree
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
struct Node *left;
struct Node *right;
int height;
};
int max(int a, int b);
int height(struct Node *N) {
if (N == NULL)
return 0;
return N->height;
int max(int a, int b) {
return (a > b) ? a : b;
struct Node *newNode(int key) {
struct Node *node = (struct Node *)
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
struct Node *rightRotate(struct Node *y) {
struct Node *x = y->left;
struct Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
struct Node *leftRotate(struct Node *x) {
struct Node *y = x->right;
struct Node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
int getBalance(struct Node *N) {
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
struct Node *insertNode(struct Node *node, int key) {
if (node == NULL)
return (newNode(key));
if (key < node->key)
node->left = insertNode(node->left, key);
else if (key > node->key)
node->right = insertNode(node->right, key);
else
return node;
node->height = 1 + max(height(node->left),
height(node->right));
int balance = getBalance(node);
if (balance > 1 && key < node->left->key)
return rightRotate(node);
if (balance < -1 && key > node->right->key)
return leftRotate(node);
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
struct Node *minValueNode(struct Node *node) {
struct Node *current = node;
while (current->left != NULL)
current = current->left;
return current;
struct Node *deleteNode(struct Node *root, int key) {
if (root == NULL)
return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
if ((root->left == NULL) || (root->right == NULL)) {
struct Node *temp = root->left ? root->left : root->right;
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
struct Node *temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left),
height(root->right));
int balance = getBalance(root);
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root->left) < 0) {
root->left = leftRotate(root->left);
return rightRotate(root);
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root->right) > 0) {
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
void printPreOrder(struct Node *root) {
if (root != NULL) {
printf("%d ", root->key);
printPreOrder(root->left);
printPreOrder(root->right);
}
}
int main() {
struct Node *root = NULL;
root = insertNode(root, 2);
root = insertNode(root, 1);
root = insertNode(root, 7);
root = insertNode(root, 4);
root = insertNode(root, 5);
root = insertNode(root, 3);
root = insertNode(root, 8);
printPreOrder(root);
root = deleteNode(root, 3);
printf("\nAfter deletion: ");
printPreOrder(root);
return 0;
OUTPUT:
RESULT:
PROGRAM:
//8 Implementation of Heaps using priority queue
#include<stdio.h>
#include<malloc.h>
void insert();
void del();
void display();
struct node
int priority;
int info;
struct node *next;
}*start=NULL,*q,*temp,*New;
typedef struct node N;
int main()
int ch;
do
printf("\n[1] INSERTION\t[2] DELETION\t[3] DISPLAY [4] EXIT\nEnter your choice: ");
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));
printf("ENTER THE ELEMENT TO BE INSERTED :\t");
scanf("%d",&item);
printf("ENTER ITS PRIORITY :\t");
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;
while(q->next != NULL && q->next->priority<=itprio)
{q=q->next;}
New->next=q->next;
q->next=New;
void del()
if(start==NULL)
printf("\nQUEUE UNDERFLOW\n");
else
New=start;
printf("\nDELETED ITEM IS %d\n",New->info);
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)
printf("\n%d priority =%d",temp->info,temp->priority);
}
OUTPUT:
RESULT:
PROGRAM:
//9 Dijkstra’s Algorithm
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
void dijkstra(int G[MAX][MAX],int n,int startnode)
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("\nDistance of node %d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
OUTPUT:
RESULT:
PROGRAM:
//10 Prim’s Algorithm
#include <stdio.h>
#include <limits.h>
#define vertices 5 /Define the number of vertices in the graph/
int minimum_key(int k[], int mst[])
int minimum = INT_MAX, min,i;
for (i = 0; i < vertices; i++)
if (mst[i] == 0 && k[i] < minimum )
minimum = k[i], min = i;
return min;
void prim(int g[vertices][vertices])
int parent[vertices];
int k[vertices];
int mst[vertices];
int i, count,edge,v;
for (i = 0; i < vertices; i++)
k[i] = INT_MAX;
mst[i] = 0;
k[0] = 0; /It select as first vertex/
parent[0] = -1;
for (count = 0; count < vertices-1; count++)
edge = minimum_key(k, mst);
mst[edge] = 1;
for (v = 0; v < vertices; v++) {
if (g[edge][v] && mst[v] == 0 && g[edge][v] < k[v])
parent[v] = edge, k[v] = g[edge][v];
printf("\n Edge \t Weight\n");
for (i = 1; i < vertices; i++)
printf(" %d <-> %d %d \n", parent[i], i, g[i][parent[i]]);
int main()
int g[vertices][vertices] = {{0, 0, 3, 0, 0},
                                 {0, 0, 10, 4, 0},
                                 {3, 10, 0, 2, 6},
                                 {0, 4, 2, 0, 1},
                                 {0, 0, 6, 1, 0}, };
prim(g);
return 0;
}
OUTPUT:
RESULT:
PROGRAM:
//11a Implementation of Linear Search
#include <stdio.h>
int main()
int num;
int i, keynum, found = 0;
printf("Enter the number of elements ");
scanf("%d", &num);
int array[num];
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
scanf("%d", &array[i]);
printf("Enter the element to be searched ");
scanf("%d", &keynum);
for (i = 0; i < num ; i++)
if (keynum == array[i] )
found = 1;
break;
if (found == 1)
printf("Element is present in the array at position %d",i+1);
else
printf("Element is not present in the array\n");
OUTPUT:
RESULT:
PROGRAM:
//11b Implementation of Binary search
#include <stdio.h>
int main()
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
OUTPUT:
RESULT:
PROGRAM:
//12a Insertion sort
#include<stdio.h>
int main(){
int i, j, count, temp, number[25];
printf("Number of elements to be entered: ");
scanf("%d",&count);
printf("Enter %d elements: ", 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;
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
OUTPUT:
RESULT:
PROGRAM:
//12b Selection sort
#include <stdio.h>
void selection(int arr[], int n)
int i, j, small;
for (i = 0; i < n-1; i++)
small = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[small])
small = j;
int temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
void printArr(int a[], int n) /* function to print the array */
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
int main()
int a[] = { 31,77,17,28,32,24 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
selection(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
OUTPUT:
RESULT:
PROGRAM:
//13 Merge sort
#include <stdio.h>
#define max 10
int a[10] = { 23,15,4,19,6,20,72,27,10,35};
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
void sort(int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
int main() {
int i;
printf("List before sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}
OUTPUT:
RESULT:
PROGRAM:
//14 Implementation of Open Addressing
#include <stdio.h>
#include <conio.h>
int tsize;
int hasht(int key)
int i ;
i = key%tsize ;
return i;
//-------LINEAR PROBING-------
int rehashl(int key)
int i ;
i = (key+1)%tsize ;
return i ;
//-------QUADRATIC PROBING-------
int rehashq(int key, int j)
int i ;
i = (key+(j*j))%tsize ;
return i ;
int main()
{
int key,arr[20],hash[20],i,n,s,op,j,k ;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);
printf ("\nEnter the number of elements: ");
scanf ("%d",&n);
for (i=0;i<tsize;i++)
hash[i]=-1 ;
printf ("Enter Elements: ");
for (i=0;i<n;i++)
scanf("%d",&arr[i]);
do
printf("\n\n1.Linear Probing\n2.Quadratic Probing \n3.Exit \nEnter your option: ");
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 ;
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
printf("\n Element at position %d: %d",i,hash[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 ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
printf("\n Element at position %d: %d",i,hash[i]);
break ;
}while(op!=3);
getch() ;
}
OUTPUT:
RESULT:
             INDEX
EX    DATE   TITLE   PAGE   MARKS   SIGN
NO.                  NO.
             INDEX
EX    DATE    TITLE   PAGE   MARKS   SIGN
NO.                   NO.