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

Data Structure 190170107114

Practical: 1
(1) Implement a program for stack that performs following operations
using array. (a)PUSH,(b)POP,(c)PEEP,(d)DISPLAY

Program:

#include<stdio.h>

main()
{
int TOP=-1,MAX,choice,i;

printf("Enter Size of Stack:");


scanf("%d",&MAX);

int stack[MAX];

printf("\n Menu \n"); printf("\nPress\


n1.PUSH\n2.POP\n3.PEEK\n4.Display\n5.Exit\n");

while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1: if(TOP==MAX-1)
{
printf("Stack is Full!\n");
break;
}

else
{
printf("Enter the value:");
scanf("%d",&stack[++TOP]);
break;
}

case 2:
if(TOP==-1)
{

Sem Page|1
Data Structure 190170107114

printf("Stack is Empty!\n");
break;
}
else
{
printf("Value %d has been removed from Stack.\n",stack[TOP]);
stack[TOP--]=0;
break;
}

case 3:
if(TOP==-1)
{
printf("Stack is Empty!\n");
break;
}
else
{
printf("Peek value is %d\n",stack[TOP]);
break;
}
case 4:
if(TOP==-1)
{
printf("Stack is Empty!\n");
break;
}
else
{
printf("Your Stack :- ");
for(i=TOP;i>=0;i--)
{
printf("%d\n",stack[i]);
}
break;
}
case 5:
exit(0);

default:printf("Invalid Input!\n");
}
}
getch();
}

Sem Page|2
Data Structure 190170107114

Output:

Enter Size of Stack:3

Menu - Press
1. PUSH
2. POP
3. PEEK
4. Displ
ay
5.Exit
Enter your choice :
1 Enter the
value:68 Enter
your choice : 1
Enter the value:56
Enter your choice :
2
56 has been removed from
Stack. Enter your choice : 3
Peek value is 68.
Enter your choice :
4 Your Stack :-
68

Sem Page|3
Data Structure 190170107114

(2) Implement a program to conert infix notation to postfix notation


using stack.

Program:

#include<stdio.h>

//Global declaration

int top=-1;
char stack[100];
char pop(void);
void push(char);
int precedence(char);

//Main Function

main()
{
char input[100];
char output[100];
int outvar=0,i;

printf("Enter Infix Expression: ");


gets(input);

for(i=0; input[i]!='\0'; i++)


{
if((input[i]>='a' && input[i]<='z') || (input[i]>='A' && input[i]<='Z'))
{
output[outvar++]=input[i];
}
else
{
if(top==-1)
push(input[i]);

else if(input[i]=='(')
push(input[i]);

else if(input[i]==')')
{
while(stack[top]!='(')
{
output[outvar++]=pop();

Sem Page|4
Data Structure 190170107114

}
top--;
}

else
{
while((precedence(input[i])<=precedence(stack[top])) && (top!=-1))
output[outvar++]=pop();

push(input[i]);
}
}

}
while(1)
{
if(top>=0) output[outvar+
+]=pop();
else
break;
}

output[outvar]='\0';
printf("Post Expression: %s",output);
}

// Extra functions

char pop(void)
{
char val;
val=stack[top--];
return(val);
}
void push(char val)
{
stack[++top]=val;

int precedence(char val)


{
if(val=='+' || val=='-')
return(1);

else if(val=='*' || val=='/' || val=='%')


return(2);

Sem Page|5
Data Structure 190170107114

else if(val=='^' || val=='$')


return(3);

else if(val=='(')
return(0);
}

Output:

Enter Infix Expression: a-b+(c*d/e)*f-g

Post Expression: ab-cd*e/f*+g-

Sem Page|6
Data Structure 190170107114

(3) Write a program to evaluate postfix expression using stack.

Program:

#include<stdio.h>
int top=-1;
float stack[100];

main()
{
int i;
float ans,n1,n2;
char input[100];

printf("Enter Postfix Expression: ");


gets(input);

for(i=0;input[i]!='\0';i++)
{
if(input[i]>='0' && input[i]<='9')
stack[++top]= input[i]-'0';

else
{
n1=stack[top--];
n2=stack[top--];
switch(input[i])
{
case '+':
ans=n2+n1;
break;
case '-':
ans=n2-n1;
break;
case '*':
ans=n2*n1;
break;
case '/':
ans=n2/n1;
break;
case '%': ans=(int)n2%
(int)n1; break;
default:
printf("\nInvalid Operator!");
}

Sem Page|7
Data Structure 190170107114

stack[++top]=ans;
}
}
printf("Your answer is %f",stack[top]);
getch();
}

Output:

Enter Postfix Expression: 934*8+4/-

Your answer is 4.000000

Sem Page|8
Data Structure 190170107114

(4) Write a program to implement tower of Hanoi problem.

Program:

#include<stdio.h>

void TH(int,char,char,char);
main()
{
int n;
printf("Enter number of Disks:");
scanf("%d",&n);
printf("\nFollow these steps to solve Tower of Hanoi\n");
TH(n,'A','B','C');
getch();
}

void TH(int n,char source,char extra,char destination)


{
if(n==1)
{
printf("\n%c- - - ->%c",source,destination);
}
else
{
TH(n-1,source,destination,extra);
TH(1,source,extra,destination);
TH(n-1,extra,source,destination);
}
}

Output:

Enter number of Disks:3

Follow these steps to solve Tower of


Hanoi A >C
A-- - ->B
C-- - ->B
A-- - ->C
B-- - ->A
B-- - ->C
A-- - ->C

Sem Page|9
Data Structure 190170107114

Practical: 2
(1) Write a program to implement QUEUE using arrays that
performs following operations (a)INSERT (b)DELETE (c)DISPLAY

Program:

#include<stdio.h>

main()
{
int front=-1,rear=-1;
int n,choice,val,i;

printf("Enter size of Queue : ");


scanf("%d",&n);

int queue[n];

printf("\n Menu \n");


printf("1. Insert an Element\n");
printf("2. Remove an Element\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");

while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(rear==n-1)
printf("Queue is Full!\n");
else
{
if(front==-1 && rear==-1)
front=rear=0;
else
rear++;
printf("Enter value : ");
scanf("%d",&val);
queue[rear]=val;

Sem Page|
Data Structure 190170107114

}
break;

case 2:
if(front==-1 || front>rear)
printf("Queue is Empty!\n");
else
printf("%d has been deleted from Queue.\n",queue[front++]);
break;

case 3:
if(rear==-1 || front>rear)
printf("Queue is Empty!\n");
else
printf("Peek value is %d\n",queue[front]);
break;

case 4:
if(rear==-1 || front>rear)
printf("Queue is Empty!\n");
else
{
printf("Your Queue :-\n");
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
break;

case 5:
exit(0);

default:
printf("Invalid Input!");
}
}
}

Sem Page|
Data Structure 190170107114

Output:

Enter size of Queue : 3

Menu
1. Insert an Element
2. Remove an Element
3. Peek
4. Display
5. Exit
Enter your choice :
1 Enter value : 68
Enter your choice :
1 Enter value : 64
Enter your choice :
2
68 has been deleted from
Queue. Enter your choice : 3
Peek value is 64
Enter your choice :
4 Your Queue :-
64

Sem Page|
Data Structure 190170107114

(2) Write a program to implement CIRCULAR QUEUE using arrays that


performs following operations (a)INSERT (b)DELETE (c)DISPLAY.

Program:

#include<stdio.h>

main()
{
int front=-1,rear=-1;
int n,choice,val,i;

printf("Enter size of Queue : ");


scanf("%d",&n);

int queue[n];

printf("\n Menu \n");


printf("1. Insert an Element\n");
printf("2. Remove an Element\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");

while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
if((front==0 && rear==n-1) || front==rear+1)
printf("Queue is Full!\n");
else
{
if(front==-1 && rear==-1)
front=rear=0;
else if(front!=0 && rear==n-1)
rear=0;
else
rear++;
printf("Enter value : ");
scanf("%d",&val);
queue[rear]=val;
}

Sem Page|
Data Structure 190170107114

break;

case 2:
if(front==-1)
printf("Queue is Empty!\n");
else if(front==n-1)
{
printf("%d has been removed from Queue.\n",queue[front]);
front=0;
}
else if(front==rear)
{
printf("%d has been removed from Queue.\n",queue[front]);
front=rear=-1;
}
else
printf("%d has been removed from Queue.\n",queue[front++]);
break;

case 3:
if(front==-1)
printf("Queue is Empty!\n");
else
printf("Peek value is %d.\n",queue[front]);\

break;

case 4:
if(front==-1)
printf("Queue is Empty!\n");
else
{
printf("Your Queue :- \n");
if(front<rear)
{
for(i=front; i<=rear; i++)
printf("%d\n",queue[i]);
}
else
{
for(i=front;i<n;i++) printf("%d\
n",queue[i]);
for(i=0;i<=rear;i++) printf("%d\
n",queue[i]);
}
}
break;

Sem Page|
Data Structure 190170107114

case 5:
exit(0);

default :
printf("Invalid Input!\n");
}
}
}

Output:

Enter size of Queue : 3

Menu
1. Insert an Element
2. Remove an Element
3. Peek
4. Display
5. Exit
Enter your choice :
1 Enter value : 45
Enter your choice :
1 Enter value : 67
Enter your choice :
1 Enter value : 78
Enter your choice :
1 Queue is Full!
Enter your choice : 2
45 has been removed from
Queue. Enter your choice : 1
Enter value : 89
Enter your choice :
3 Peek value is 67.
Enter your choice :
4 Your Queue :-
67
78
89
Enter your choice :
1 Queue is Full!
Enter your choice : 5

Sem Page|
Data Structure 190170107114

Practical: 3
(1) Write a menu driven program to implement following operations on the
singly linked list. (a) Insert a node at the front of the linked list. (b) Insert a
node at the end of the linked list. (c) Insert a node such that linked list is in
ascending order. (according to info. Field)(d) Delete a first node of the linked
list. (e) Delete a node before specified position. (f)Delete a node after
specified position.

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

struct linked_list
{
int data;
struct linked_list *next;
};

typedef struct linked_list node;

void create_linked_list(node *);


void display_linked_list(node *);
int count_elements(node *);
node * insert_at_beginning(node *);
node * insert_at_end(node *);
node * insert_after_key(node *);
node * insert_before_key(node *);
node * insert_ascending(node *);
node * delete_at_beginning(node *);
node * delete_at_end(node *);
node * delete_after_key(node *);
node * delete_before_key(node *);

void main()
{
int choice;
node *head,*start;
node *new_node;

head=(node*)malloc(sizeof(node));

start=head;

printf("\n Menu \n");


printf("1. Create Linked List\n");

Sem Page|
Data Structure 190170107114

printf("2. Display Linked List\n");


printf("3. Count Total Elements\n");
printf("4. Insert At Beginning\n");
printf("5. Insert At End\n");
printf("6. Insert After The Key Value\n");
printf("7. Insert Before The Key Value\n");
printf("8. Insert Value at Ascending Order\n");
printf("9. Delete At Beginning\n");
printf("10. Delete At End\n");
printf("11. Delete After The Key Value\n");
printf("12. Delete Before The Key Value\n");
printf("13. Exit\n");

while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("(Enter -1 to stop Insertion)\n");
create_linked_list(start);
break;

case 2:
display_linked_list(start);
printf("\n");
break;

case 3:
printf("Total Elements : %d\n",count_elements(start));
break;

case 4:
start=insert_at_beginning(start);
break;

case 5:
head=insert_at_end(start);
break;

case 6:
head=insert_after_key(start);
break;

case 7:
if(insert_before_key(start)==NULL)
{
start=insert_at_beginning(start);
}

Sem Page|
Data Structure 190170107114

break;

case 8:
start=insert_ascending(start);
break;

case 9:
start=delete_at_beginning(start);
break;

case 10:
head=delete_at_end(start);
break;

case 11:
head=delete_after_key(start);
break;

case 12:
if(delete_before_key(start)==NULL)
{
start=delete_at_beginning(start);
}
break;

case 13:
exit(0);

default:
printf("Invalid Input!\n");
}
}
}

void create_linked_list(node *list)


{
printf("Enter value : ");
scanf("%d",&list->data);

if(list->data==-1)
{
list->next=NULL;
}
else
{
list->next=(node*)malloc(sizeof(node));
create_linked_list(list->next);
}
}

void display_linked_list(node *list)

Sem Page|
Data Structure 190170107114

{
if(list->next!=NULL && list->next->next!=NULL)
{
printf("%d-->",list->data);
display_linked_list(list->next);
}
else if(list->next!=NULL && list->next->next==NULL)
{
printf("%d",list->data);
}
return;
}

int count_elements(node *list)


{
if(list->next==NULL)
return(0);
else
return(1+count_elements(list->next));
}

node * insert_at_beginning(node *list)


{
node *new_node;
new_node=(node *)malloc(sizeof(node));

printf("Enter value : ");


scanf("%d",&new_node->data);

new_node->next=list;
list=new_node;
return list;
}

node * insert_at_end(node *list)


{
node *new_node;
new_node=(node*)malloc(sizeof(node));

printf("Enter value : ");


scanf("%d",&new_node->data);

while(list->next!=NULL)
{
if(list->next->next==NULL)
{
new_node->next=list->next;
list->next=new_node;
break;
}
list=list->next;

Sem Page|
Data Structure 190170107114

}
return list;
}

node * insert_after_key(node *list)


{
int key;
node *new_node;
new_node=(node*)malloc(sizeof(node));

printf("Enter Key Value : ");


scanf("%d",&key);

printf("Enter value : ");


scanf("%d",&new_node->data);

if(list->data==key)
{
new_node->next=list->next;
list->next=new_node;
}
else
{
while(list->next!=NULL)
{
if(list->next->data==key)
{
new_node->next=list->next->next;
list->next->next=new_node;
break;
}
list=list->next;
}
}
return list;
}

node * insert_before_key(node *list)


{
int key;
node *new_node;

printf("Enter Key Value : ");


scanf("%d",&key);

if(list->data==key)
{
return NULL;
}
else
{

Sem Page|
Data Structure 190170107114

new_node=(node*)malloc(sizeof(node));
printf("Enter value : ");
scanf("%d",&new_node->data);

while(list->next!=NULL)
{
if(list->next->data==key)
{
new_node->next=list->next;
list->next=new_node;
break;
}
list=list->next;
}
return list;
}
}

node * delete_at_beginning(node *list)


{
if(list->next==NULL)
{
printf("Linked List is Empty!\n");
return;
}
node *head;
head=list->next;
free(list);
return head;
}

node * delete_at_end(node *list)


{
if(list->next==NULL)
{
printf("Linked List is Empty!\n");
return;
}
else
{
while(list->next!=NULL)
{
if(list->next->next==NULL)
{
list->next=NULL;
return (list->next);
}
list=list->next;
}
}
}

Sem Page|
Data Structure 190170107114

node * delete_after_key(node *list)


{
int key;

printf("Enter Key Value : ");


scanf("%d",&key);

while(list->next!=NULL)
{
if(list->data==key)
{
list->next=list->next->next;
return(list);
}
list=list->next;
}
}

node * delete_before_key(node *list)


{
int key;

printf("Enter Key Value : ");


scanf("%d",&key);

if(list->data==key)
{
printf("There is no node before the Key Value's Node!\n");
return list;
}
else if(list->next->data==key)
{
return NULL;
}
else
{
while(list->next!=NULL)
{
if(list->next->next->data==key)
{
list->next=list->next->next;
return(list->next);
}
list=list->next;
}
}
}

node * insert_ascending(node *list)


{

Sem Page|
Data Structure 190170107114

node *new_node;
new_node=(node*)malloc(sizeof(node));

printf("Enter value : ");


scanf("%d",&new_node->data);

if(list->data>=new_node->data)
{
new_node->next=list;
list=new_node;
return list;
}

else if(list->next->data>=new_node->data)
{
new_node->next=list->next;
list->next=new_node;
return (list);
}

else
{
while(list->next!=NULL)
{
if(list->next->next->data>=new_node->data)
{
new_node->next=list->next->next;
list->next->next=new_node;
return(list->next->next);
}
list=list->next;
}
}

Sem Page|
Data Structure 190170107114

Output:

Menu
1. Create Linked List
2. Display Linked List
3. Count Total Elements
4. Insert At Beginning
5. Insert At End
6. Insert After The Key Value
7. Insert Before The Key Value
8. Insert Value at Ascending Order
9. Delete At Beginning
10. Delete At End
11. Delete After The Key Value
12. Delete Before The Key Value
13. Exit
Enter your choice : 1
(Enter -1 to stop Insertion)
Enter value : 10
Enter value : 11
Enter value : 12
Enter value : 13
Enter value : 14
Enter value : 15
Enter value : -1
Enter your choice : 2
10-->11-->12-->13-->14-->15
Enter your choice : 3
Total Elements : 6
Enter your choice : 4
Enter value : 9
Enter your choice : 2
9-->10-->11-->12-->13-->14-->15
Enter your choice : 5
Enter value : 16
Enter your choice : 2
9-->10-->11-->12-->13-->14-->15-->16
Enter your choice : 6
Enter Key Value : 12
Enter value : 17
Enter your choice : 2
9-->10-->11-->12-->17-->13-->14-->15-->16
Enter your choice : 7
Enter Key Value : 12
Enter value : 18
Enter your choice : 2
9-->10-->11-->18-->12-->17-->13-->14-->15-->16

Sem Page|
Data Structure 190170107114

Enter your choice :


8 Enter value : 5
Enter your choice : 2
5-->9-->10-->11-->18-->12-->17-->13-->14-->15-->16
Enter your choice :
9 Enter your
choice : 2
9-->10-->11-->18-->12-->17-->13-->14-->15-->16
Enter your choice :
10 Enter your
choice : 2
9-->10-->11-->18-->12-->17-->13-->14-->15
Enter your choice :
11 Enter Key Value :
12 Enter your
choice : 2
9-->10-->11-->18-->12-->13-->14-->15
Enter your choice :
12 Enter Key Value :

Sem Page|
Data Structure 190170107114

(2) Write a program to implement Stack using linked list.

Program:
#include<conio.h>
#include<stdio.h>

struct linked_list
{
int data;
struct linked_list *next;
};

typedef struct linked_list node;

node *start=NULL;

void push(void);
void pop(void);
void display(void);

void main()
{
int choice;

printf("\n Menu \n");


printf("1. PUSH \n");
printf("2. POP \n");
printf("3. Display \n");
printf("4. Exit \n");

while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
push();
break;

case 2:
pop();
break;

case 3:
display();
printf("\n");
break;

Sem Page|
Data Structure 190170107114

case 4:
exit(0);

default :
printf("Invalid Input!\n");
}
}
}

void push(void)
{
node *new_node;

if(start == NULL)
{
new_node=(node*)malloc(sizeof(node));

printf("Enter value : ");


scanf("%d",&new_node->data);

new_node->next = NULL;
start = new_node;
}
else
{
new_node=(node*)malloc(sizeof(node));

printf("Enter value : ");


scanf("%d",&new_node->data);

new_node->next = start;
start=new_node;
}
printf("%d has been stored into Stack.\n",new_node->data);
}

void pop(void)
{
node *ptr;
ptr=start;

if(ptr==NULL)
printf("Stack is Empty!\n");
else
{
start = ptr ->next;
printf("%d has been removed from Stack.\n",ptr->data);
free(ptr);
}
}

Sem Page|
Data Structure 190170107114

void display()
{
node *ptr;
ptr=start;

if(ptr==NULL)
printf("Stack is Empty!");
else
{
while(ptr!=NULL)
{
if(ptr->next==NULL)
printf("%d",ptr-
>data);
else
{
printf("%d-->",ptr->data);

if(ptr->next->next==NULL)
{
printf("%d",ptr->next->data);
break;
}
}

ptr=ptr->next;
}

Sem Page|
Data Structure 190170107114

Output:

Menu
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice :
1 Enter value : 12
12 has been stored into
Stack. Enter your choice : 1
Enter value : 13
13 has been stored into
Stack. Enter your choice : 1
Enter value : 14
14 has been stored into
Stack. Enter your choice : 3
14-->13-->12
Enter your choice : 2
14 has been removed from
Stack. Enter your choice : 3
13-->12
Enter your choice : 2
13 has been removed from
Stack. Enter your choice : 2
12 has been removed from
Stack. Enter your choice : 3
Stack is Empty!
Enter your
choice : 2 Stack is
Empty!

Sem Page|
Data Structure 190170107114

(3)Write a program to implement Queue using linked list.

Program:
#include<conio.h>
#include<stdio.h>

struct linked_list
{
int data;
struct linked_list *next;
};

typedef struct linked_list node;

node *front=NULL;
node *rear=NULL;

void insert_element(void);
void delete_element(void);
void display(void);

void main()
{
int choice;

printf("\n Menu \n");


printf("1. Insert an Element\n");
printf("2. Delete an Element\n");
printf("3. Display\n");
printf("4. Exit\n");

while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
insert();
break;

case 2:
delete_element();
break;

case 3:
display();
printf("\n");

Sem Page|
Data Structure 190170107114

break;

case 4:
exit(0);

default :
printf("Invalid Input\n");
}
}
}

void insert(void)
{
node *new_node;

new_node=(node *)malloc(sizeof(node));
if(front==NULL && rear==NULL)
{
printf("Enter value : ");
scanf("%d",&new_node->data);

new_node->next = NULL;

front = new_node;
rear = new_node;
}
else
{
printf("Enter value : ");
scanf("%d",&new_node->data);

new_node->next = NULL;

rear->next = new_node;
rear=new_node;
}
}

void delete_element(void)
{
if(front==NULL && rear==NULL)
{
printf("Queue is Empty!\n");
}
else
{
node *ptr;
if(front==rear)
{
ptr=front;
front=front->next;

Sem Page|
Data Structure 190170107114

ptr->next=NULL;
printf("%d has been deleted from Queue.\n",ptr->data);
free(ptr);
front = NULL;
rear = NULL;
}
else
{
ptr=front;
front = front->next;
ptr->next=NULL;
printf("%d has been deleted from Queue.\n",ptr->data);
free(ptr);
}
}
}

void display(void)
{
node *ptr;
ptr=front;
if(front== NULL && rear == NULL)
{
printf("Queue is Empty!");
}
else
{
while(ptr!=NULL)
{
if(ptr->next==NULL)
printf("%d",ptr-
>data);
else
{
printf("%d-->",ptr->data);

if(ptr->next->next==NULL)
{
printf("%d",ptr->next->data);
break;
}
}
ptr=ptr->next;
}
}
}

Sem Page|
Data Structure 190170107114

Output:

Menu
1. Insert an Element
2. Delete an Element
3. Display
4. Exit
Enter your choice :
1 Enter value : 1
Enter your choice :
1 Enter value : 2
Enter your choice :
1 Enter value : 3
Enter your
choice : 3 1-->2--
>3
Enter your choice : 2
1 has been deleted from
Queue. Enter your choice : 3
2-->3
Enter your choice : 2
2 has been deleted from
Queue. Enter your choice : 2
3 has been deleted from
Queue. Enter your choice : 2
Queue is Empty!
Enter your choice :
3 Queue is Empty!

Sem Page|
Data Structure 190170107114

Practical: 4
(1) Write a program to implement following operations on the doubly linked list.
(a) Insert a node at the front of the linked list. (b) Insert a node at the end of
the linked list. (c) Delete a last node of the linked list. (d) Delete a node before
specified position.

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

struct linked_list
{
struct linked_list *prev;
int data;
struct linked_list *next;
};

typedef struct linked_list node;

node *start = NULL, *head = NULL;

void create_linked_list(void);
void display_linked_list(void);
void count_elements(void);
void insert_at_beginning(void);
void insert_at_end(void);
void insert_after_key(void);
void insert_before_key(void);
void
delete_at_beginning(void);
void delete_at_end(void);
void delete_after_key(void);
void delete_before_key(void);

void main()
{
int choice;

printf("----------Menu---------\n");
printf("1. Input Data in Linked List\n");
printf("2. Display List\n");
printf("3. Count Elements of Linked List\n");
printf("4. Insert Element at Beginning\n");
printf("5. Insert Element at End\n");
printf("6. Insert Element After The Key Value\n");

Sem Page|
Data Structure 190170107114

printf("7. Insert Element Before The Key Value\


n"); printf("8. Delete Element at Beginning\n");
printf("9. Delete Element at End\n");
printf("10. Delete Element After The Key Value\n");
printf("11. Delete Element Before The Key Value\n");
printf("12. Exit\n");
while (1)
{
printf("Enter your choice : ");
scanf("%d", &choice);

switch (choice)
{
case 1:
create_linked_list();
break;

case 2:
display_linked_list();
break;

case 3:
count_elements();
break;

case 4:
insert_at_beginning();
break;

case 5:
insert_at_end();
break;

case 6:
insert_after_key();
break;

case 7:
insert_before_key();
break;

case 8:
delete_at_beginning();
break;

case 9:
delete_at_end();
break;

case 10:
delete_after_key();

Sem Page|
Data Structure 190170107114

break;

case 11:
delete_before_key();
break;

case 12:
printf("See you soon!\n");
exit(0);

default:
printf("Invalid Input!\n");
}
}
}

void create_linked_list(void)
{
int n;
printf("Enter value : ");
scanf("%d", &n);

if (n == -1)
return;
else
{
node *new_node;
new_node = (node *)malloc(sizeof(node));

if (start == NULL)
{
start = new_node;
head = new_node;
new_node->prev = start;
new_node->data = n;
new_node->next = NULL;
}
else
{
new_node->prev = head->next;
head->next = new_node;
new_node->next = NULL;
new_node->data = n;
head = new_node;
}
create_linked_list();
}
}

void display_linked_list(void)
{

Sem Page|
Data Structure 190170107114

node *temp = start;

if (start == NULL)
{
printf("List is Empty!\n");
}
else if (start->next == NULL)
{
printf("|%d|\n", start->data);
}
else
{
while (temp != NULL)
{
printf("|%d|<-->", temp->data);
if (temp->next->next == NULL)
{
printf("|%d|", temp->next->data);
break;
}
temp = temp->next;
}
printf("\n");
}
}

void count_elements(void)
{
node *temp = start;
int count = 0;

if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
while (temp != NULL)
{
count++;
temp = temp->next;
}
printf("Total number of Elements in List : %d\n", count);
}
}

void insert_at_beginning(void)
{
node *new_node;
new_node = (node *)malloc(sizeof(node));

Sem Page|
Data Structure 190170107114

printf("Enter value : ");


scanf("%d", &new_node->data);

new_node->next = start;
start = new_node;
new_node->prev = start;
}

void insert_at_end(void)
{
node *new_node;
new_node = (node *)malloc(sizeof(node));

printf("Enter value : ");


scanf("%d", &new_node->data);

head->next = new_node;
new_node->prev = head;
new_node->next = NULL;
head = new_node;
}

void insert_after_key(void)
{
node *new_node, *temp = start;
new_node = (node *)malloc(sizeof(node));

if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
int key;
printf("Enter Key Value : ");
scanf("%d", &key);

while (temp != NULL)


{
if (temp->data == key && temp->next != NULL)
{
printf("Enter value : ");
scanf("%d", &new_node->data);

new_node->next = temp->next;
temp->next = new_node;
new_node->prev = temp;
return;
}
else if (temp->data == key && temp->next == NULL)
{

Sem Page|
Data Structure 190170107114

printf("Enter value : ");


scanf("%d", &new_node->data);

new_node->next = NULL;
new_node->prev = head;
head->next = new_node;
head = new_node;
return;
}
temp = temp->next;
}
printf("Invalid key!\n");
}
free(new_node);
}

void insert_before_key(void)
{
node *new_node, *temp = start;
new_node = (node *)malloc(sizeof(node));

if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
int key;
printf("Enter Key value : ");
scanf("%d", &key);

while (temp != NULL)


{
if (start->data == key)
{
printf("Enter value : ");
scanf("%d", &new_node->data);

new_node->next = start;
start = new_node;
new_node->prev = start;
return;
}
else if (temp->next->data == key)
{
printf("Enter value : ");
scanf("%d", &new_node->data);

new_node->next = temp->next;
new_node->prev = temp;
(temp->next)->prev = new_node;

Sem Page|
Data Structure 190170107114

temp->next = new_node;
return;
}
else if (temp->next->data != key && temp->next->next ==
NULL) break;
temp = temp->next;
}
printf("Invalid Key!\n");
}
free(new_node);
}

void delete_at_beginning(void)
{
node *store;

if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
if (start->next == NULL)
{
store = start;
start = NULL;
head = NULL;
}
else
{
store = start;
start = start->next;
start->prev = start;
}
printf("%d has Deleted from List!\n", store->data);
free(store);
}
}

void delete_at_end(void)
{
node *store, *temp = start;

if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
if (start->next == NULL)
{

Sem Page|
Data Structure 190170107114

store = start;
start = NULL;
head = NULL;
}
else
{
while (temp != NULL)
{
if (temp->next->next == NULL)
{
store = temp->next;
temp->next = NULL;
head = temp;
break;
}
temp = temp->next;
}
}
printf("%d has Deleted from List!\n", store->data);
free(store);
}
}

void delete_after_key(void)
{
node *temp = start, *store;

if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
int key;
printf("Enter Key Value : ");
scanf("%d", &key);

while (temp != NULL)


{
if (temp->data == key && temp->next != NULL && temp->next->next == NULL)
{
store = head;
temp->next = NULL;
head = temp;
break;
}
else if (temp->data == key && temp->next != NULL)
{
store = temp->next;
temp->next = temp->next-
>next; (temp->next)->prev =
temp;

Sem Page|
Data Structure 190170107114

break;
}
else if (temp->data == key && temp->next == NULL)
{
printf("Node doesn't exist after key value!\n");
return;
}
else if (temp->data != key && temp->next == NULL)
{
printf("Invalid Key!\n");
return;
}
temp = temp->next;
}
printf("%d has Deleted from List!\n", store->data);
free(store);
}
}

void delete_before_key(void)
{
node *temp = start, *store;

if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
int key;
printf("Enter Key Value : ");
scanf("%d", &key);

while (temp != NULL)


{
if (start->data == key)
{
printf("Node doesn't exist before Key Value!\n");
return;
}

else if (start->next == NULL && start->data != key)


{
printf("Invalid Key!\n");
return;
}

else if (start->next->data == key)


{
store = start;
start = start->next;

Sem Page|
Data Structure 190170107114

start->prev = start;
break;
}

else if (start->next->data != key && start->next->next == NULL)


{
printf("Invalid Key!\n");
return;
}

else if (temp->next->next->data == key)


{
store = temp->next;
temp->next = temp->next-
>next; temp->next->prev =
temp; break;
}
else if (temp->next->next->data != key && temp->next->next->next == NULL)
{
printf("Invalid Key!\n");
return;
}
temp = temp->next;
}
printf("%d has Deleted from List!\n", store->data);
free(store);
}
}

Sem Page|
Data Structure 190170107114

Output:

Menu
1. Input Data in Linked List
2. Display List
3. Count Elements of Linked List
4. Insert Element at Beginning
5. Insert Element at End
6. Insert Element After The Key Value
7. Insert Element Before The Key Value
8. Delete Element at Beginning
9. Delete Element at End
10. Delete Element After The Key Value
11. Delete Element Before The Key Value
12. Exit
Enter your choice :
1 Enter value : 1
Enter value :
2 Enter value
: 3 Enter
value : 4
Enter value :
5 Enter value
: -1
Enter your choice : 2
|1|<-->|2|<-->|3|<-->|4|<-->|
5| Enter your choice : 4
Enter value : 0
Enter your choice : 2
|0|<-->|1|<-->|2|<-->|3|<-->|4|<-->|5|
Enter your choice : 5
Enter value : 6
Enter your choice : 2
|0|<-->|1|<-->|2|<-->|3|<-->|4|<-->|5|<-->|6|
Enter your choice : 9
6 has Deleted from
List! Enter your
choice : 2
|0|<-->|1|<-->|2|<-->|3|<-->|4|<-->|
5| Enter your choice : 11
Enter Key Value : 1
0 has Deleted from
List! Enter your

Sem Page|
Data Structure 190170107114

Practical: 5
(1) Write a program to implement following operations on the circular linked list.
(a) Insert a node at the end of the linked list. (b) Insert a node before
specified position. (c) Delete a first node of the linked list. (d) Delete a
node after specified position.

Program:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

struct linked_list
{
int data;
struct linked_list *next;
};

typedef struct linked_list node;

node *start=NULL;

void insert_at_front(int);
void insert_before_key(int,int);
void delete_after_key(int);
void delete_at_front(void);
void display(void);

void main()
{
int choice;
int value,prev_value,next_value;

printf("\n------------Menu-----------\n");
printf("1. Insert Element at Front\n");
printf("2. Insert Element Before Key Value\
n"); printf("3. Delete Element at Front\n");
printf("4. Delete Element After Key Value\n");
printf("5. Display\n");
printf("6. Exit\n");

while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);

Sem Page|
Data Structure 190170107114

switch(choice)
{
case 1:
printf("Enter value : ");
scanf("%d",&value);
insert_at_front(value);
break;

case 2:
printf("Enter Key Value : ");
scanf("%d",&next_value);
printf("Enter value : ");
scanf("%d",&value);
insert_before_key(next_value,value);
break;

case 3:
delete_at_front();
break;

case 4:
printf("Enter Key Value : ");
scanf("%d",&value);
delete_after_key(value);
break;

case 5:
display();
printf("\n");
break;

case 6:
exit(0);

default :
printf("Invalid Input!\n");
}
}
}

void insert_at_front(int data)


{
node *new_node,*ptr;
if(start==NULL)
{
new_node=(node*)malloc(sizeof(node));
new_node->data = data;
start = new_node;
new_node->next = start;
}

Sem Page|
Data Structure 190170107114

else
{
new_node=(node*)malloc(sizeof(node));
new_node->data = data;
ptr=start;

while(ptr->next!=start)
{
ptr=ptr->next;
}
ptr->next=new_node;
new_node->next=start;
start=new_node;
}
}

/*void display()
{
node *ptr;
if(start == NULL)
{
printf("Queue is Empty!\n");
}
else
{
ptr=start;
while(ptr->next!= start )
{
printf("\t %d",ptr->data);
ptr = ptr->next;
}
printf("\t %d \n",ptr->data);
}
}*/
void display(void)
{
node *ptr;
ptr=start;

if(start==NULL)
{
printf("Queue is Empty!");
}
else
{
do
{
if(ptr->next==start)
printf("%d",ptr-
>data);
else

Sem Page|
Data Structure 190170107114

{
printf("%d-->",ptr->data);

if(ptr->next->next==NULL)
{
printf("%d",ptr->next->data);
break;
}
}
ptr=ptr->next;
}while(ptr!=start);
}
}

void insert_before_key(int next_data,int data)


{
node *temp,*new_node;

new_node=(node*)malloc(sizeof(node));

new_node->data = data;

temp = start ;
while(temp->next->data!=next_data)
{
temp = temp->next;
}
if(temp->next==start)
{
insert_at_front(data);
}
else
{
new_node->next = temp->next;
temp->next = new_node;
}

void delete_at_front()
{
node *ptr;
if(start==NULL)
{
printf("Queue is Empty!\n");
}
else if(start->next == start)
{
start = NULL;
free(start);

Sem Page|
Data Structure 190170107114

}
else
{
ptr=start;
while(ptr->next != start)
{
ptr=ptr->next;
}
ptr->next = start->next;
free(start);
start=ptr->next;
}
}

void delete_after_key(int key)


{
node *ptr,*after,*new_start;
int count=0;
if(start==NULL)
{
printf("Queue is Empty!\n");
}

ptr=start;
while(ptr->next!=start)
{
if(ptr->data!=key)
ptr=ptr->next;
else
break;
count++;
}
if(ptr->next==NULL && count!=0 && ptr->data!=key || ptr->next==NULL && count==0 && ptr-
>data!=key)
{
printf("Key doesn't exist!\n");
return;
}
else if(ptr->next==start)
{
if(ptr==start)
{
printf("No node after Key!\n");
return;
}
after = ptr->next;
new_start = ptr->next->next;
ptr->next = after->next;
after->next = NULL;
start = new_start;
free(after);

Sem Page|
Data Structure 190170107114

}
else
{
after = ptr->next;
ptr->next = after-
>next; after->next =
NULL; free(after);
}
}

Output:

Menu
1. Insert Element at Front
2. Insert Element Before Key Value
3. Delete Element at Front
4. Delete Element After Key Value
5. Display
6. Exit
Enter your choice :
1 Enter value : 1
Enter your choice :
1 Enter value : 2
Enter your choice :
1 Enter value : 3
Enter your
choice : 5 3-->2--
>1
Enter your choice :
2 Enter Key Value :
1 Enter value : 4
Enter your
choice : 5 3-->2--
>4-->1
Enter your
choice : 3 Enter
your choice : 5 2--
>4-->1
Enter your
choice : 4 Enter

Sem Page|
Data Structure 190170107114

Practical: 6
(1) Write a program which create binary search tree.

Program:

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

struct bst
{
int data;
struct bst *left;
struct bst *right;
};

typedef struct bst node;


node *root = NULL, *temp = NULL, *t1, *t2;
int flag = 1;

void getnode(void);
void insert(node *);
void create(void);
void inorder(node *);
void delete (void);
void search1(node *, int);
void delete1(node *);
int smallest(node *);
int largest(node *);

void main()
{
int choice;
printf(" Menu \n");
printf("1. Insert an element into tree\n");
printf("2. Delete an element from the tree\n");
printf("3. Inorder Traversal\n");
printf("4. Exit\n");

while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &choice);

Sem Page|
Data Structure 190170107114

switch (choice)
{
case 1:
create();
break;

case 2:
delete ();
break;

case 3:
inorder(root);
break;

case 4:
printf("See you soon!\n");
exit(0);

default:
printf("Invalid Input!\n");
}
}
}

void getnode(void)
{
int data;

printf("Enter Value : ");


scanf("%d", &data);
temp = (node *)malloc(sizeof(node));
temp->data = data;
temp->left = temp->right = NULL;
}

void insert(node *root)


{

if (temp->data < root->data)


{
if (root->left == NULL)
{
root->left = temp;
}
else
{
insert(root->left);

Sem Page|
Data Structure 190170107114

}
}
if (temp->data > root->data)
{
if (root->right == NULL)
{
root->right = temp;
}
else
{
insert(root->right);
}
}
}

void create(void)
{
getnode();
if (root == NULL)
{
root = temp;
}
else
{
insert(root);
}
}

void inorder(node *temp)


{
if (root == NULL)
{
printf("Tree is Empty!");
return;
}
if (temp->left != NULL)
inorder(temp->left);

printf("%d -> ", temp->data);

if (temp->right != NULL)
inorder(temp->right);
}

void delete (void)


{
int data;

Sem Page|
Data Structure 190170107114

if (root == NULL)
{
printf("Tree is Empty!\n");
return;
}
printf("Enter Value : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}

void search1(node *temp, int value)


{
if (value > temp->data)
{
t1 = temp;
search1(temp->right, value);
}
else if (value < temp->data)
{
t1 = temp;
search1(temp->left, value);
}
else if (value == temp->data)
{
delete1(temp);
}
}

/* To delete a node */
void delete1(node *temp)
{
int k;

/* To delete leaf node */


if ((temp->left == NULL) && (temp->right == NULL))
{
if (t1->left == temp)
{
t1->left = NULL;
}
else
{
t1->right = NULL;
}
temp = NULL;
free(temp);

Sem Page|
Data Structure 190170107114

return;
}

/* To delete node having one left hand child */


else if ((temp->right == NULL))
{
if (t1 == temp)
{
root = temp->left;
t1 = root;
}
else if (t1->left == temp)
{
t1->left = temp->left;
}
else
{
t1->right = temp->left;
}
temp = NULL;
free(temp);
return;
}
/* To delete node having right hand child */
else if (temp->left == NULL)
{
if (t1 == temp)
{
root = temp->right;
t1 = root;
}
else if (t1->right == temp)
t1->right = temp->right;
else
t1->left = temp->right;
temp == NULL;
free(temp);
return;
}

//To delete node having two child


else if ((temp->left != NULL) && (temp->right != NULL))
{
t2 = root;
if (temp->right != NULL)
{
k = smallest(temp->right);
flag = 1;

Sem Page|
Data Structure 190170107114

}
else
{
k = largest(temp->left);
flag = 2;
}
search1(root, k);
temp->data = k;
}
}

//To find the smallest element in the right sub tree


int smallest(node *temp)
{
t2 = temp;
if (temp->left != NULL)
{
t2 = temp;
return (smallest(temp->left));
}
else
return (temp->data);
}

//To find the largest element in the left sub tree


int largest(node *temp)
{
if (temp->right != NULL)
{
t2 = temp;
return (largest(temp->right));
}
else
return (temp->data);
}

Sem Page|
Data Structure 190170107114

Output:

Menu
1. Insert an element into tree
2. Delete an element from the tree
3. Inorder Traversal
4. Exit

Enter your choice :


1 Enter Value : 1

Enter your choice :


1 Enter Value : 2

Enter your choice :


1 Enter Value : 3

Enter your choice :


1 Enter Value : 4

Enter your choice :


1 Enter Value : 5

Enter your choice :


3 1 -> 2 -> 3 -> 4 ->
5
Enter your choice :
2 Enter Value : 2

Enter your choice :


3 1 -> 3 -> 4 -> 5
Enter your choice :

Sem Page|
Data Structure 190170107114

(2) Implement recursive and non-recursive tree traversing


methods in-order, pre-order and post-order traversal.

Program:

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

struct tree
{
int data;
struct tree *left;
struct tree *right;
};

typedef struct tree node;


node *root, *temp;

void inorder(node *);


void preorder(node *);
void postorder(node *);
void insert(node *root, node *new);
node *getnode(void);

int main()
{
int choice;
char ans = 'n';
node *newnode;
root = NULL;

printf("\n------------Menu-----------\n");
printf("1. Create\n");
printf("2. Inorder\n");
printf("3. Preorder\n");
printf("4. Postorder\n");
printf("5. Exit\n");

while(1)
{
printf("\nEnter Your Choice : ");
scanf("%d", &choice);

switch (choice)
{
case 1:
root = NULL;

Sem Page|
Data Structure 190170107114

do
{
newnode = getnode(); printf("\
nEnter Value : "); scanf("%d",
&newnode->data);

if (root == NULL)
root = newnode;
else
insert(root, newnode);

printf("Want to add more elements?(y/n) ");


ans = getche();
printf("\n");
} while (ans != 'n');
break;

case 2:
inorder(root);
break;

case 3:
preorder(root);
break;

case 4:
postorder(root);
break;

case 5:
printf(“See you Soon!\n”);
exit(0);

default:
printf("Invalid Input!\n");
}
}
getch();
}

// Inorder Traversal
void inorder(node *root)
{
if (root == NULL)
return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}

// preorderTraversal traversal

Sem Page|
Data Structure 190170107114

void preorder(node *root)


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

// postorderTraversal traversal
void postorder(node *root)
{
if (root == NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}

node *getnode(void)
{
temp = (node *)malloc(sizeof(node));
temp->left = NULL;
temp->right = NULL;
return temp;
}

void insert(node *root,node *new)


{
char ch;
printf("Insert in left/right of %d ?(l/r) ",root->data);
ch = getche();
printf("\n");
if (ch == 'r')
{
if (root->right == NULL)
{
root->right = new;
}
else
{
insert(root->right, new);
}
}
else if (ch == 'l')
{
if (root->left == NULL)
{
root->left = new;
}
else

Sem Page|
Data Structure 190170107114

{
insert(root->left, new);
}
}
}

Output:

Menu
1. Create
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice :

1 Enter Value : 1
Want to add more elements?(y/n) y

Enter Value : 2
Insert in left/right of 1 ?(l/r) l
Want to add more elements?(y/n) y

Enter Value : 3
Insert in left/right of 1 ?(l/r) r
Want to add more elements?(y/n) n

Enter Your Choice :


2 2 ->1 ->3
Enter Your Choice :
3 1 ->2 ->3
Enter Your Choice :
4 2 ->3 ->1
Enter Your Choice :
5 See you soon!

Sem Page|
Data Structure 190170107114

Practical: 8
(1) Write a program to implement Linear Search.

Program:

#include<stdio.h>

void main()
{
int n,search,position=0;

printf("Enter Size of Array : ");


scanf("%d",&n);

int a[n];

for(int i=0;i<n;i++)
{
printf("Enter Value : ");
scanf("%d",&a[i]);
}

printf("Search Value : ");


scanf("%d",&search);

printf("\n-----------Search Result----------\n");
for(int i=0;i<n;i++)
{
if(a[i]==search)
{
position++;
printf("Position : %d\n",i+1);
}
}

if(position==0)
{
printf("Value not Found!\n");
}
else
{
printf("Total Result : %d\n",position);
}
}

Sem Page|
Data Structure 190170107114

Output:

Enter Size of
Array : 5 Enter
Value : 12 Enter
Value : 20 Enter
Value : 18 Enter
Value : 56 Enter
Value : 29 Search
Value : 12

Search Result Position : 1


Total Result : 1

Sem Page|
Data Structure 190170107114

(2) Write a program to implement Binary Search.

Program:

#include <stdio.h>

void main()
{
int n, temp, position = 0;
printf("Enter Size of Array : ");
scanf("%d", &n);

int a[n];

// Insertion
for (int i = 0; i < n; i++)
{
printf("Enter value : ");
scanf("%d", &a[i]);
}

// Sorting
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] >= a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

printf("\n---------------Sorted Array--------------\n");
printf("Position\tValue\n");
for (int i = 0; i < n; i++)
{
printf("%d\t\t%d\n", i + 1, a[i]);
}

//Searching
int search;
printf("Search Value : ");
scanf("%d", &search);

Sem Page|
Data Structure 190170107114

printf("\n---------------Search Result--------------\n");

int start = 0, end = n - 1, center;

while (start <= end)


{
center = (start + end) / 2;
if (a[center] == search)
{
position = 1;
printf("position : %d\n", center + 1);
break;
}
else if (a[center] > search)
{
end = center - 1;
}
else if (a[center] < search)
{
start = center + 1;
}
}

if (start > end && position == 0)


{
printf("Value Not Found!\n");
}
}

Sem Page|
Data Structure 190170107114

Output:

Enter Size of Array : 5


Enter value : 12 Enter value : 10 Enter value : 28 Enter value : 36 Enter value : 26

Sorted Array

Position Value
1 10
2 12
3 26
4 28
5 36
Search Value : 26

Search Result position : 3

Sem Page|
Data Structure 190170107114

Practical: 9
(1) Write a program to implement Quick Sort.

Program:

#include <stdio.h>
#include <conio.h>
#define size 100

int process(int [],int,int);


void quick_sort(int [],int,int);

void main()
{
int a[size], i, n;
printf("Enter size of Array : ");
scanf("%d", &n);

for (i = 0; i < n; i++)


{
printf("Enter Value : ");
scanf("%d", &a[i]);
}

quick_sort(a, 0, n - 1);

printf("------------Sorted Array-----------\n");

for (i = 0; i < n; i++)


printf(" %d\n",a[i]);

int process(int array[], int start, int end)


{
int left, right, temp, loc, flag;
loc = left = start;
right = end;
flag = 0;
while (flag != 1)
{
while ((array[loc] <= array[right]) && (loc != right))
right--;

Sem Page|
Data Structure 190170107114

if (loc == right)
flag = 1;

else if (array[loc] > array[right])


{
temp = array[loc];
array[loc] = array[right];
array[right] = temp;
loc = right;
}

if (flag != 1)
{
while ((array[loc] >= array[left]) && (loc != left))
left++;

if (loc == left)
flag = 1;

else if (array[loc] < array[left])


{
temp = array[loc];
array[loc] = array[left];
array[left] = temp;
loc = left;
}
}
}
return loc;
}
void quick_sort(int array[], int start, int end)
{
int loc;
if (start < end)
{
loc = process(array, start, end);
quick_sort(array, start, loc - 1);
quick_sort(array, loc + 1, end);
}
}

Sem Page|
Data Structure 190170107114

Output:

Enter size of
Array : 5 Enter
Value : 5
Enter
Value : 4
Enter
Value : 3
Enter
Value : 2
Enter
Value : 1
Sorted Array 1

Sem Page|
Data Structure 190170107114

(2) Write a program to implement Merge Sort.

Program:
#include <stdio.h>

void merge(int[], int, int, int);


void merge_sort(int[], int, int);

void main()
{
int n, i;
printf("Enter size of Array : ");
scanf("%d", &n);

int a[n];

for (i = 0; i < n; i++)


{
printf("Enter value : ");
scanf("%d", &a[i]);
}

//sorting
merge_sort(a, 0, n - 1);

printf("\n--------------Sorted Array-------------\n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
}

void merge(int array[], int start, int middle, int end)


{
int lb1 = start, lb2 = middle + 1, i = start, temp[100], j;
while ((lb1 <= middle) && (lb2 <= end))
{
if (array[lb1] < array[lb2])
{
temp[i] = array[lb1];
lb1++;
}
else
{
temp[i] = array[lb2];
lb2++;
}
i++;
}

Sem Page|
Data Structure 190170107114

if (lb1 > middle)


{
while (lb2 <= end)
{
temp[i] = array[lb2];
lb2++;
i++;
}
}
else
{
while (lb1 <= middle)
{
temp[i] = array[lb1];
lb1++;
i++;
}
}

for (j = start; j < i; j++)


array[j] = temp[j];
}

void merge_sort(int array[], int start, int end)


{
int middle;
if (start < end)
{
middle = (start + end) / 2;
merge_sort(array, start, middle);
merge_sort(array, middle + 1, end);
merge(array, start, middle, end);
}
}

Sem Page|
Data Structure 190170107114

Output:

Enter size of
Array : 5 Enter
value : 5
Enter
value : 4
Enter
value : 3
Enter
value : 2
Enter
value : 1

Sorted Array 1

Sem Page|
Data Structure 190170107114

(3) Write a program to implement Bubble Sort.

Program:

#include <stdio.h>

void main()
{
int n, flag = 0;
printf("Enter size of Array : ");
scanf("%d", &n);

int a[n];
for (int i = 0; i < n; i++)
{
printf("Enter value : ");
scanf("%d", &a[i]);
}
for (int i = n; i >= 0; i--)
{
for (int j = 0; j < i; j++)
{
if (a[j] > a[j + 1])
{
flag = 1;
int temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
if (flag == 0)
break;
}
if (flag == 0)
{
printf("Array is already Sorted!\n");
}
else
{
printf("Sorted Array : \n");
for (int i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}
}
}

Sem Page|
Data Structure 190170107114

Output:

Enter size of
Array : 5 Enter
value : 132 Enter
value : 34 Enter
value : 34 Enter
value : 23 Enter
value : 1

Sorted Array :
1
23
34
34
132

Second Output

Enter size of Array : 5


Enter value :
1 Enter
value : 2
Enter value :
3 Enter
value : 43

Sem Page|

You might also like