Ds Lab Manual
Ds Lab Manual
MANUAL
Course Objectives:
1. To develop skills to design and analyze simple linear and non-linear data
structures
2. To strengthen the ability to identify and apply the suitable data structures for
the given real-
world problem
3. To gain knowledge in practical applications of data structures.
List of Experiments:
1. Write a C program that uses functions to perform the following:
a) Create a singly linked list of integers.
b) Delete a given integer from the above linked list.
c) Display the contents of the above list after deletion.
2. Write a C program that uses functions to perform the following:
a) Create a doubly linked list of integers.
b) Delete a given integer from the above doubly linked list.
c) Display the contents of the above list after deletion.
3. Write a C program implement the Stack ADT using Arrays and Linked List.
4. Write a C program that uses stack operations to convert a given infix
expression into its postfix equivalent.
5. Write a C program that evaluates a postfix expression.
6. Write C program to implement queue ADT using array and doubly linked list.
7. a) Write C program to implement priority queue ADT using array.
b) Write C program to implement circular queue ADT using array.
8. Write C program for implementing the following sorting methods:
a) Insertion sort
b) Merge sort
9. Write C program for implementing the following sorting methods:
a) Quick sort
b) Selection sort
10. Write a C program for implementing Heap sort algorithm.
11. Write a C program that uses functions to perform the following:
a) Create a Binary Search Tree (BST).
b) Insert data in BST
c) Traverse the above BST recursively in Postorder.
12. Write a C program that uses functions to perform the following:
a) Deletion an element BST
b) Traverse the above BST non recursively in Inorder.
13. Write a C program to implement all the functions of a dictionary (ADT)
using hashing.
14. Write C program for implementing Depth first traversal and Breadth first
traversal.
Course Outcomes:
At the end of this lab session, the student will
CO 1 Be able to design and analyze the time and space efficiency of the
data structure
CO 2 Be capable to identity the appropriate data structure for given
problem
CO 3 Have practical knowledge on the application of data structures
Assessment Method:
Assessment Tool Experiments Report/Viva-Voce/ Total
Quiz/MCQ/Lab
project
Weightage (%) 25% 15% 40%
PROGRAM:
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *prev;
int data;
struct node *next;
};
struct node *head=NULL,*newnode,*temp,*temp1;
struct node *create(struct node *head){
int option;
while(option){
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter the value in data field : ");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL){
head=temp=newnode;
printf("\nOne node created!\n");
}
else{
temp->next=newnode;
newnode->prev=temp;
temp=newnode;
}
printf("Do you want to continue creating nodes to be in the doubly linked list ?
\nPress any number to continue creating nodes or 0 to terminate creating
nodes : ");
scanf("%d",&option);
}
printf("\n");
printf("Doubly Linked List is created...!\n");
return head;
}
struct node *display(struct node *head){
if(head==NULL) printf("List is empty..\nNothing to display..\n");
else{
temp=head;
printf("The nodes present in the Doubly Linked list : \n");
while(temp->next!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
return head;
}
struct node *insbeg(struct node *head){
int num;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
newnode->prev=NULL;
if(head==NULL){
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
newnode->next=head;
head->prev=newnode;
head=newnode;
printf("Newnode having data %d inserted at the beginning of the existing
linked list.\n",num);
}
return head;
}
struct node *insend(struct node *head){
int num;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
newnode->next=NULL;
if(head==NULL){
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(temp->next!=NULL) temp=temp->next;
temp->next=newnode;
newnode->prev=temp;
newnode->next=NULL;
printf("Newnode having data %d inserted at the ending of the existing linked
list.\n",num);
}
return head;
}
struct node *insbeforenode(struct node *head){
int num,value;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the value present in the list for which the newnode has to be
inserted before to that given node : ");
scanf("%d",&value);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(temp->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=newnode;
newnode->prev=temp1;
newnode->next=temp;
temp->prev=newnode;
printf("Newnode having data %d inserted before to the given node having
data %d in the existing linked list.\n",num,value);
}
return head;
}
struct node *insafternode(struct node *head){
int num,value;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the value present in the list for which the newnode has to be
inserted after to that given node : ");
scanf("%d",&value);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
newnode->prev=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head,temp1=head;
while(temp1->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=newnode;
newnode->prev=temp1;
newnode->next=temp;
temp->prev=newnode;
printf("Newnode having data %d inserted after to the given node having
data %d in the existing linked list.\n",num,value);
}
return head;
}
struct node *inspos(struct node *head){
int num,pos,i=1;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the position in which the newnode has to be inserted : ");
scanf("%d",&pos);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(i<pos-1){
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
newnode->next->prev=newnode;
printf("Newnode having data %d inserted at the given position %d in the
existing linked list.\n",num,pos);
}
return head;
}
struct node *insposafter(struct node *head){
int num,pos,i=1;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the position in which the newnode has to be inserted after that
position : ");
scanf("%d",&pos);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(i<pos){
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
newnode->next->prev=newnode;
printf("Newnode having data %d inserted after to the given position %d in
the existing linked list.\n",num,pos);
}
return head;
}
struct node *delbeg(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
temp=head;
head=head->next;
head->prev=NULL;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("One node is deleted from the beginning of the list\n");
}
return head;
}
struct node *delend(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
temp=head,temp1=head;
while(temp->next!=NULL){
temp1=temp;
temp=temp->next;
}
temp->prev=temp1;
temp1->next=NULL;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("One node is deleted from the ending of the list\n");
}
return head;
}
struct node *delnode(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
int value;
temp=head,temp1=head;
printf("Enter the data of the node to be deleted in the given list : ");
scanf("%d",&value);
while(temp->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
temp->next->prev=temp->prev;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("Given node is deleted from the list\n");
}
return head;
}
struct node *delafternode(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
int value;
temp=head,temp1=head;
printf("Enter the data of the node to be deleted after to the given node in the
given list : ");
scanf("%d",&value);
while(temp1->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
temp->next->prev=temp->prev;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("One node is deleted after to the given node from the list\n");
}
return head;
}
void main()
{
int option;
while(option){
printf("\n1. Creating a Doubly Linked List\n2. Displaying the list\n3. Insertion of
newnode at beginning of the list\n4. Insertion of newnode at ending of the list\
n5. Insertion of newnode before to the given node in the list\n6. Insertion of
newnode after to the given node in the list\n7. Insertion of newnode at given
position\n8. Insertion of newnode after the given position\n9. Deletion of node
from beginning of the list\n10. Deletion of node from the end of the list\n11.
Deletion of given node from the list\n12. Deletion of node after to the given
node from the list\n");
printf("\nEnter an option of your choice : ");
scanf("%d",&option);
switch(option){
case 1: printf("\n***** Creating a Doubly Linked List *****\n");
head = create(head);
break;
case 2: printf("\n***** Displaying the list *****\n");
head = display(head);
break;
case 3: printf("\n***** Insertion of newnode at beginning of the list *****\n");
head = insbeg(head);
break;
case 4: printf("\n***** Insertion of newnode at ending of the list *****\n");
head = insend(head);
break;
case 5: printf("\n***** Insertion of newnode before to the given node in the list
*****\n");
head = insbeforenode(head);
break;
case 6: printf("\n***** Insertion of newnode after to the given node in the list
*****\n");
head = insafternode(head);
break;
case 7: printf("\n***** Insertion of newnode at given position *****\n");
head = inspos(head);
break;
case 8: printf("\n***** Insertion of newnode after the given position *****\n");
head = insposafter(head);
break;
case 9: printf("\n***** Deletion of node from beginning of the list *****\n");
head = delbeg(head);
break;
case 10: printf("\n***** Deletion of node from the end of the list *****\n");
head = delend(head);
break;
case 11: printf("\n***** Deletion of given node from the list *****\n");
head = delnode(head);
break;
case 12: printf("\n***** Deletion of node after to the given node from the list
*****\n");
head = delafternode(head);
break;
default: printf("Please enter a valid option.....\n");
break;
}
}
}
OUTPUT:
PROGRAM:
int push()
{
int val;
printf("Enter the val to push in to the stack..:");
scanf("%d",&val);
if(top>=n-1) printf("\nOverFlow.The stack is full.");
else
{
top++;
stack[top]=val;
}
}
int pop()
{
int temp;
if(top<=-1) printf("\nUnderFlow.The stack is Empty.");
else
{
temp=stack[top];
top--;
printf("\nThe poped item is..:%d",temp);
}
}
int peek()
{
if(top<=-1) printf("\nUnderFlow.The stack is Empty.");
else
{
printf("\nThe peek item is..:%d",stack[top]);
}
}
int display()
{
int i;
for(i=top;i>=0;i--)
{
printf("\t%d",stack[i]);
}
}
OUTPUT:
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:2
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:4
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:6
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:8
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:10
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:12
PROGRAM:
//STACK IMPLEMENTATION USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
int data;
struct node *next;
};
typedef struct node node;
node *top=NULL;
int push();
int pop();
int peek();
int display();
int main()
{
int option;
do
{
printf("\n***********************MENU**********************");
printf("\n1.Push\n2.Pop\n3.peek\n4.Display");
printf("\n****************************************************");
printf("\nEnter one of the above options to perform.....:");
scanf("%d",&option);
switch(option)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peek();
break;
case 4:display();
break;
default: printf("\nInvalid Option please enter correct position");
}
}while(option);
}
int push()
{
int x;
node *new_node;
printf("\nEnter new_node data...:");
scanf("%d",&x);
new_node=(node *) malloc(sizeof(node));
new_node->data=x;
new_node->next=top;
top=new_node;
}
int pop()
{
node *temp;
temp=top;
if(top==NULL) printf("\nStack is Empty");
else
{
printf("The popped item is...:%d",top->data);
top=top->next;
free(temp);
}
}
int peek()
{
if(top==NULL) printf("\nStack is Empty");
else
{
printf("The Peek item is...:%d",top->data);
}
}
int display()
{
node *temp;
temp=top;
if(top==NULL) printf("\nStack is Empty");
else
{
printf("\nThe stack Elements are...:");
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->next;
}
}
}
OUTPUT:
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:4
PROGRAM:
#include<stdio.h>
char stack[100];
int top=-1;
void push(char x){
stack[++top]=x;
}
char pop(){
if(top==-1) return -1;
return stack[top--];
}
int priority(char x){
if(x==’c’) return 0;
if(x==’+’ || x==’-’) return 1;
if(x==’*’ || x==’/’) return 2;
}
int main(){
char exp[100],*e,x;
printf(“Enter an expression: ”);
scanf(“%s”,exp);
printf(“\n”);
printf(“Postfix Expression: \n”);
e=exp;
while(*e!=’\0’){
if(isalnum(*e)) printf(“%c”,x);
else if(*e==’c’) push(*e);
else if(*e==’)’){
while(x==pop()!=’c’) printf(“%c”,x);
}
else{
while(priority(stack[top])>=priority(*e)) printf(“%c”,pop());
push(*e);
}
e++;
}
while(top!=-1) printf(“%c”,pop());
}
OUTPUT:
PROGRAM:
#include<stdio.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:
void Dequeue()
{
if(front==-1 || front>rear) printf("\nList is Empty..,UnderFlow");
else if(front==rear)
{
front=rear=0;
}
else
{
printf("\nDeleted data in Queue is...:%d",Queue[front]);
front=front+1;
}
}
void display()
{
int i;
if(front==-1 || rear==-1) printf("\nQueue is Empty");
else
{
printf("\nThe Queue Elements are...:");
for(i=front;i<=rear;i++) printf("\t%d",Queue[i]);
}
}
void peek()
{
if(front==-1 || rear==-1) printf("\nQueue is Empty");
else
{
printf("The front element in the Queue is..:%d",Queue[front]);
}
}
int main()
{
int option;
do
{
printf("\n\n*********************QUEUE OPERATIONS*********************");
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.peek");
printf("\n****************************************************************");
printf("\nEnter one of the above options to perform..:");
scanf("%d",&option);
switch(option)
{
case 1:enqueue();
break;
case 2:Dequeue();
break;
case 3:display();
break;
case 4:peek();
break;
default: printf("Please Enter correct option");
}
}while(option);
}
OUTPUT:
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
Enter data to be Enqueue in to Queue...:4
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
Queue is Full,OverFlow
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:2
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:4
The front element in the Queue is..:4
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:0
Please Enter correct option
PROGRAM:
//QUEUE IMPLEMENTATION USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *front=0,*rear=0;
void enqueue()
{
struct node *newnode;
int x;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data in the queue...:");
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(front==NULL && rear==NULL)
{
front=rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
void Dequeue()
{
struct node *temp;
if(front==NULL || front>rear) printf("\nQueue is Empty");
else
{
temp=front;
printf("\nThe deleted Element is..:%d",temp->data);
front=front->next;
free(temp);
}
}
void display()
{
struct node *temp;
if(front==NULL || front>rear) printf("\nQueue is Empty");
else
{
temp=front;
printf("\nThe Queue Elements are..:");
while(temp!=0)
{
printf("\t%d",temp->data);
temp=temp->next;
}
}
}
void peek()
{
struct node *temp;
if(front==NULL || front>rear) printf("\nQueue is Empty");
else
{
printf("\nThe peek Element is..:%d",front->data);
}
}
int main()
{
int option;
do
{
printf("\n\n*********************QUEUE OPERATIONS*********************");
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.peek");
printf("\n****************************************************************");
printf("\nEnter one of the above options to perform..:");
scanf("%d",&option);
switch(option)
{
case 1:enqueue();
break;
case 2:Dequeue();
break;
case 3:display();
break;
case 4:peek();
break;
default: printf("Please Enter correct option");
}
}while(option);
}
OUTPUT:
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:2
The deleted Element is..:2
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:2
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:4
a)PROGRAM:
#include <stdio.h>
int size = 0;
void swap(int *a, int *b) {
int temp = *b;
*b = *a;
*a = temp;
}
insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);
deleteRoot(array, 4);
printArray(array, size);
}
OUTPUT:
Max-Heap array: 9 5 4 3 2
After deleting an element: 9 5 2 3
b)PROGRAM:
#include<stdio.h>
#define max 5
int Queue[max];
int front=-1,rear=-1;
int enqueue()
{
int x;
if((rear==max-1 && front==0)|| (rear+1==front)) printf("\nOverFlow
Condition,Queue is Full");
else
{
printf("\nEnter data to be Enqueue in to Queue...:");
scanf("%d",&x);
if(front==-1 && rear==-1)
{
front=rear=0;
Queue[rear]=x;
}
else if(rear==max-1 && front!=0) rear=0;
else rear++;
Queue[rear]=x;
}
}
void Dequeue()
{
if(front==-1 && rear==-1) printf("\nUnderFlow Condition,Queue is Empty");
else if(front==rear)
{
printf("\nThe Dequeued Element is..:%d",Queue[front]);
front=rear=-1;
}
else if(front==max-1)
{
printf("\nThe Dequeued Element is..:%d",Queue[front]);
front=0;
}
else
{
printf("\nThe Dequeued Element is..:%d",Queue[front]);
front++;
}
}
void display()
{
int i,j;
if(front==-1 && rear==-1) printf("\nUnderFlow Condition,Queue is Empty");
else if(front>rear)
{
for(i=front;i<=max-1;i++) printf("\t%d",Queue[i]);
for(j=0;j<=rear;j++) printf("\t%d",Queue[j]);
}
else
{
for(i=front;i<=rear;i++) printf("\t%d",Queue[i]);
}
}
void peek()
{
if(front==-1 && rear==-1) printf("\nUnderFlow Condition,Queue is Empty");
else
{
printf("The peek of the Element in the queue is..:%d",Queue[front]);
}
}
int main()
{
int option;
do
{
printf("\n\n*********************QUEUE OPERATIONS*********************");
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.peek");
printf("\n****************************************************************");
printf("\nEnter one of the above options to perform..:");
scanf("%d",&option);
switch(option)
{
case 1:enqueue();
break;
case 2:Dequeue();
break;
case 3:display();
break;
case 4:peek();
break;
default: printf("Please Enter correct option");
}
}while(option);
}
OUTPUT:
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3
2 4 6 8 10
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:2
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3
4 6 8 10
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:4
The peek of the Element in the queue is..:4
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:0
Please Enter correct option
a)PROGRAM:
#include<stdio.h>
void main()
{
int a[10],n,i,j,temp;
printf("\nEnter the number of elements in the array...:");
scanf("%d",&n);
printf("\nEnter the array Elements..:");
for(i=0;i<n;i++)
{
printf("\nEnter a[%d] element...:",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("\nThe sorted array elements using insertion sort..:");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT:
Enter the number of elements in the array...:5
b)PROGRAM:
#include<stdio.h>
int Merge(int a[],int lb,int mid,int ub)
{
int b[10];
int i=lb;
int j=mid+1;
int k=lb;
while(i<=mid && j<=ub)
{
if(a[i]<=a[j])
{
b[k]=a[i];
i++;
}
else
{
b[k]=a[j];
j++;
}
k++;
}
if(i>mid)
{
while(j<=ub)
{
b[k]=a[j];
j++;
k++;
}
}
else
{
while(i<=mid)
{
b[k]=a[i];
i++;
k++;
}
}
for(k=lb;k<=ub;k++)
{
a[k]=b[k];
}
//return b[10];
}
OUTPUT:
Enter the number of elements in the array...:9
a)PROGRAM:
#include<stdio.h>
int swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int Paritition(int a[10],int lb,int ub)
{
int start,end;
int pivot=a[lb];
start=lb;
end=ub;
//int i=(start-1);
while(start<end)
{
while(a[start]<=pivot)
{
start++;
}
while(a[end]>pivot) end--;
if(start<end)
{
swap(&a[start],&a[end]);
}
}
swap(&a[lb],&a[end]);
return end;
/*
for(int j=start;j<=end-1;j++)
{
if(a[j]<=pivot)
{
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[end]);
return (i+1);
*/
}
OUTPUT:
Enter the number of elements in the array...:6
b)PROGRAM:
#include<stdio.h>
void main()
{
int a[10],n,i,j,temp,min;
printf("\nEnter the number of elements in the array...:");
scanf("%d",&n);
printf("\nEnter the array Elements..:");
for(i=0;i<n;i++)
{
printf("\nEnter a[%d] element...:",i);
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
if(min!=i)
{
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
printf("\nThe sorted array elements using Selection sort..:");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT:
Enter the number of elements in the array...:7
PROGRAM:
#include <stdio.h>
int main()
{
int arr[] = { 20, 13, 34, 56, 12, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
heap_sort(arr, n);
return 0;
}
OUTPUT:
Array:
20 13 34 56 12 10
After performing Heap Sort:
10 12 13 20 34 56
And
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);
return current;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
OUTPUT:
Inorder traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 10 -> 14 ->
After deleting 10
Inorder traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 14 ->
PROGRAM:
#include <stdio.h>
#include <conio.h>
int ht[10], i, found = 0, key;
void insert_val();
void search_val();
void delete_val();
void display();
int main()
{
int option;
clrscr();
for ( i = 0;i < 10;i++ ) //to initialize every element as ‘–1’
ht[i] = –1;
do
{
printf( "\n MENU \n1.Insert \n2.Search \n3.Delete \n4.Display \n5.Exit");
case 2:
search_val();
break;
case 3:
delete_val();
break;
case 4:
display();
break;
default:
}while (option!=5);
getch();
return 0;
}
void insert_val()
{
int val, f = 0;
key = ( val % 10 ) – 1;
if ( ht[key] == –1 )
{
ht[key] = val;
}
else
{
if ( key < 9 )
{
for ( i = key + 1;i < 10;i++ )
if ( ht[i] == –1 )
{
ht[i] = val;
break;
}
}
}
{
ht[i] = val;
break;
}
}
}
}
void display()
{
for (i = 0;i < 10;i++)
printf( "\t%d", ht[ i ] );
}
void search_val()
{
int val, flag = 0;
printf( "\nEnter the element to be searched :: " );
scanf( "%d", &val );
key = ( val % 10 ) – 1;
if ( ht[ key ] == val )
flag = 1;
else
{
if(ht[i] == val)
{
flag = 1;
key = i;
break;
}
}
}
if (flag == 0)
{
key = i;
break;
}
}
}
if (flag == 1)
{
found=1;
key = –1;
printf( "\nThe item searched was not found in the hash table" );
}
}
void delete_val()
{
search_val();
if (found==1)
{
if ( key != –1 )
{
OUTPUT:
MENU
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your option: 1
Enter the element to be inserted :1
Enter your option: 4
1 –1 –1 –1 –1 –1 –1 –1 –1 –1
Enter your option: 5
PROGRAM:
//BREADTH FIRST SEARCH
#include <stdio.h>
#define MAX 10
void breadth_first_search(int adj[][MAX],int visited[],int start)
{
int queue[MAX],rear = –1,front =– 1, i;
queue[++rear] = start;
visited[start] = 1;
while(rear != front)
{
start = queue[++front];
if(start == 4)
printf("5\t");
else
OUTPUT:
PROGRAM
//DEPTH FIRST SEARCH
#include <stdio.h>
#define MAX 5
void depth_first_search(int adj[][MAX],int visited[],int start)
{
int stack[MAX];
int top = –1, i;
printf("%c–",start + 65);
visited[start] = 1;
stack[++top] = start;
while(top ! = –1)
{
start = stack[top];
for(i = 0; i < MAX; i++)
{
OUTPUT:
Enter the adjacency matrix:
01010
10110
01001
11001
00110
DFS Traversal: A –> C –> E –>
**********************************