Data Structure Lab File
Data Structure Lab File
6.
Write a program to sort the given set of elements using
Bubble Sort.
7.
Write a program to sort the given set of elements using
Selection Sort.
8.
Write a program to sort the given set of elements using
Quick Sort.
9.
Write a program to sort the given set of elements using
Merge Sort.
10.
Write a program to sort the given set of elements using
Quick Sort.
Experiment 1
AIM: Write a program to Stack.
Code:
#include <stdio.h>
#include <conio.h>
#define MAX 5
void push(){
int element;
if (top == MAX - 1){
printf("Stack is full");
}
else{
printf("Enter element: ");
scanf("%d ", &element);
top = top + 1;
stack[top] = element;
}
}
void pop(){
if (top == -1){
printf("Stack is empty.");
}
else{
top = top - 1;
printf("Element deleted.");
}
}
void display(){
int i;
printf("Stack: ");
void main(){
int cont;
int choice;
do{
Jigyasa A50504923003
1
printf("Enter choice: \nCase 1: push\nCase 2: pop\nCase 3: Display: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
printf("Exit from program");
break;
}
printf("\nWether you want to contine press 1 or to not continue press 0.\n");
scanf("%d", &cont);
} while (cont == 1);
getch();
}
Output:
Enter choice:
Case 1: Push
Case 2: Pop
Case 3: Display: 1
Enter element: 5
Wether you want to contine press 1 or to not continue press 0: 1
Enter choice:
Case 1: Push
Case 2: Pop
Case 3: Display: 1
Enter element: 6
Wether you want to contine press 1 or to not continue press 0: 1
Enter choice:
Case 1: Push
Case 2: Pop
Case 3: Display: 3
Stack: 5 6
Wether you want to contine press 1 or to not continue press 0: 1
Enter choice:
Case 1: Push
Jigyasa A50504923003
2
Case 2: Pop
Case 3: Display: 2
Element deleted.
Wether you want to contine press 1 or to not continue press 0: 1
Enter choice:
Case 1: Push
Case 2: Pop
Case 3: Display: 3
Stack: 5
Wether you want to contine press 1 or to not continue press 0:
Jigyasa A50504923003
3
Experiment 2
AIM: Write a program to Simple queue.
Code:
#include <stdio.h>
#include <conio.h>
#define MAX 5
int rear = -1, front = -1;
int queue[MAX];
void insert_q() {
int element;
if (rear == MAX - 1){
printf("Queue is full.");
}
else if (rear == -1) {
printf("Enter element: ");
scanf("%d", &element);
rear = rear + 1;
front = front + 1;
queue[rear] = element;
}
else {
printf("Enter element: ");
scanf("%d", &element);
rear = rear + 1;
queue[rear] = element;
}
}
void delete_q() {
if (front == -1) {
printf("Queue is empty.");
}
else {
front = front + 1;
printf("Element deleted.");
}
}
void display_q() {
int i;
printf("Queue: ");
for (i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
Jigyasa A50504923003
4
}
void main() {
int cont;
int choice;
do {
printf("Enter choice:\nCase 1: Insertion\nCase 2: Deletion\nCase 3: Display: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert_q();
break;
case 2:
delete_q();
break;
case 3:
display_q();
break;
default:
printf("Exit from program.");
}
printf("\nWhether you want to continue, press 1, or to not continue, press 0: ");
scanf("%d", &cont);
} while (cont == 1);
getch();
}
Output:
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter element: 5
Whether you want to continue, press 1, or to not continue, press 0: 1
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter element: 6
Whether you want to continue, press 1, or to not continue, press 0: 1
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 3
Queue: 5 6
Jigyasa A50504923003
5
Whether you want to continue, press 1, or to not continue, press 0: 1 Enter
choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 2
Element deleted.
Whether you want to continue, press 1, or to not continue, press 0: 1 Enter
choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 3
Queue: 6
Whether you want to continue, press 1, or to not continue, press 0:
Jigyasa A50504923003
6
Experiment 3
AIM: Write a program to input restricted queue.
Code:
#include <stdio.h>
#include <conio.h>
#define MAX 5
void dq_iq_insert() {
int element;
if (rear == MAX - 1) {
printf("Queue is full.");
}
else if (rear == -1)
{
printf("Enter element: ");
scanf("%d", &element);
rear = rear + 1;
front = front + 1;
dequeue[rear] = element;
}
else
{
printf("Enter element: ");
scanf("%d", &element);
rear = rear + 1;
dequeue[rear] = element;
}
}
void dq_iq_delete() {
int end;
if (front == -1) {
printf("Queue is empty.");
}
else
{
printf ("Enter the end for deletion (1 for front end and 2 for rear end): ");
scanf ("%d", &end);
if(end==1){
front = front + 1;
Jigyasa A50504923003
7
printf("Element deleted.");
}
else
{
rear = rear-1;
printf("Element deleted.");
}
}
}
void dq_iq_display() {
int i;
printf("DeQueue: ");
for (i = front; i <= rear; i++)
{
printf("%d ", dequeue[i]);
}
}
void main() {
int cont;
int choice;
do
{
printf("Enter choice:\nCase 1: Insertion\nCase 2: Deletion\nCase 3: Display: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
dq_iq_insert();
break;
case 2:
dq_iq_delete();
break;
case 3:
dq_iq_display();
break;
default:
printf("Exit from program.");
}
printf("\nWhether you want to continue, press 1, or to not continue, press 0: ");
scanf("%d", &cont);
} while (cont == 1);
getch();
Jigyasa A50504923003
8
}
Output:
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter element: 5
Whether you want to continue, press 1, or to not continue, press 0: 1
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter element: 6
Whether you want to continue, press 1, or to not continue, press 0: 1
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 3
DeQueue: 5 6
Whether you want to continue, press 1, or to not continue, press 0: 1 Enter
choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 2
Enter the end for deletion (1 for front end and 2 for rear end): 1 Element deleted.
Whether you want to continue, press 1, or to not continue, press 0: 1 Enter
choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 3
DeQueue: 6
Whether you want to continue, press 1, or to not continue, press 0:
Jigyasa A50504923003
9
Experiment 4
AIM: Write a program to output restricted queue.
Code:
#include <stdio.h>
#include <conio.h>
#define MAX 5
int dequeue[MAX];
int rear = -1, front = -1;
void dq_insert() {
int end;
int element;
if (rear == MAX - 1 && front == 0){
printf("DeQueue is full.");
}
else if (front == -1 && rear == -1){
printf("Enter the Element: ");
scanf ("%d", &element);
front = front + 1;
rear = rear + 1;
dequeue[rear] = element;
}
else {
printf ("Enter the end for insertion (1 for front end and 2 for rear end): ");
scanf ("%d", &end);
if(end==1){
front = front - 1;
dequeue[front] = element;
}
else if(end == 2){
rear = rear+1;
Jigyasa A50504923003
10
dequeue[rear] = element;
}
}
}
void dq_delete() {
if (front == -1){
printf("DeQueue is empty.");
}
else {
if(front == rear){
front = rear -1;
}
else{
front = front + 1;
}
printf("Element deleted.");
}
}
void dq_display() {
int i;
printf("DeQueue: ");
for (i = front; i <= rear; i++)
{
printf("%d ", dequeue[i]);
}
}
void main() {
int cont;
int choice;
do {
printf("Enter choice:\nCase 1: Insertion\nCase 2: Deletion\nCase 3: Display: ");
scanf("%d", &choice);
switch (choice) {
case 1:
dq_insert();
break;
case 2:
dq_delete();
break;
case 3:
dq_display();
Jigyasa A50504923003
11
break;
default:
printf("Exit from program.");
}
printf("\nWhether you want to continue, press 1, or to not continue, press 0: ");
scanf("%d", &cont);
} while (cont == 1);
getch();
}
Output:
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter the Element: 5
Whether you want to continue, press 1, or to not continue, press 0: 1
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter the end for insertion (1 for front end and 2 for rear end): 1
Front is full!
Whether you want to continue, press 1, or to not continue, press 0: 1 Enter
choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter the end for insertion (1 for front end and 2 for rear end): 2 Enter element:
6
Whether you want to continue, press 1, or to not continue, press 0: 1 Enter
choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 3
DeQueue: 5 6
Whether you want to continue, press 1, or to not continue, press 0:■
Jigyasa A50504923003
12
Experiment 5
AIM: Write a program to Circular queue.
Code:
#include <stdio.h>
#include <conio.h>
#define MAX 5
void insert_cq(){
int element;
printf ("Enter element: ");
scanf ("%d", &element);
if (front == 0 && rear == MAX-1) {
printf ("Queue is full.");
}
else {
if (front == -1) {
front = rear = 0;
cQueue[rear] = element;
}
else {
rear = (rear + 1) % MAX;
cQueue[rear] = element;
}
}
}
void delete_cq()
{
if (front == -1 && rear == -1) {
printf ("cQueue is empty.");
}
else if (front == rear) {
front = -1;
rear = -1;
printf ("Element deleted.");
}
else if (rear > front) {
front = (front + 1) % MAX;
printf ("Element deleted.");
}
else if (front == 0 || front <= rear){
front = front + 1;
Jigyasa A50504923003
13
printf ("Element deleted.");
}
}
void display_cq(){
int i;
if (front == -1)
printf ("\nCQueue is empty.\n");
else
{
printf ("\nCQueue: ");
for (i = front; i != rear; i = (i + 1) % MAX) {
printf ("%d ", cQueue[i]);
}
printf ("%d ", cQueue[i]);
}
}
void main() {
int cont;
int choice;
do {
printf("Enter choice: \nCase 1: Insert\nCase 2: Delete\nCase 3: Display: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert_cq();
break;
case 2:
delete_cq();
break;
case 3:
display_cq();
break;
default:
printf ("exit from program");
}
printf ("\nWether you want to contine press 1 or to not continue press 0: ");
scanf ("%d", &cont);
} while (cont == 1);
getch();
}
Jigyasa A50504923003
14
Output:
Enter choice:
Case 1: Insert
Case 2: Delete
Case 3: Display: 1
Enter element: 5
Wether you want to contine press 1 or to not continue press 0: 1
Enter choice:
Case 1: Insert
Case 2: Delete
Case 3: Display: 1
Enter element: 6
Wether you want to contine press 1 or to not continue press 0: 1
Enter choice:
Case 1: Insert
Case 2: Delete
Case 3: Display: 1
Enter element: 7
Wether you want to contine press 1 or to not continue press 0: 1 Enter choice:
Case 1: Insert
Case 2: Delete
Case 3: Display: 1
Enter element: 8
Wether you want to contine press 1 or to not continue press 0: 1 Enter choice:
Case 1: Insert
Case 2: Delete
Case 3: Display: 1
Enter element: 9
Wether you want to contine press 1 or to not continue press 0: 1
Enter choice:
Case 1: Insert
Case 2: Delete
Case 3: Display: 3
CQueue: 5 6 7 8 9
Wether you want to contine press 1 or to not continue press 0: 1 Enter choice:
Case 1: Insert
Case 2: Delete
Case 3: Display: 2
Element deleted.
Wether you want to contine press 1 or to not continue press 0: 1
Enter choice:
Case 1: Insert
Case 2: Delete
Jigyasa A50504923003
15
Case 3: Display: 3
CQueue: 6 7 8 9
Wether you want to contine press 1 or to not continue press 0: 1 Enter choice:
Case 1: Insert Case 2: Delete
Case 3: Display: 2
Element deleted.
Wether you want to contine press 1 or to not continue press 0: 1
Enter choice:
Case 1: Insert
Case 2: Delete
Case 3: Display: 1
Enter element: 10
Wether you want to contine press 1 or to not continue press 0: 1 Enter choice:
Case 1: Insert
Case 2: Delete
Case 3: Display: 3
CQueue: 7 8 9 10
Wether you want to contine press 1 or to not continue press 0:
Jigyasa A50504923003
16
Experiment 6
AIM: Write a program to Linked List.
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void insert_linked(){
int element;
printf("Enter element: ");
scanf("%d", &element);
if (start == NULL){
start = temp;
}
else{
struct node *ptr = start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
void delete_linked() {
if (start == NULL){
printf("\nList is empty\n");
}
else{
struct node *ptr = start;
while (ptr->next->next != NULL) {
Jigyasa A50504923003
17
ptr = ptr->next;
}
ptr->next = NULL;
}
}
void display_linked() {
if (start == NULL) {
printf("\nLinkedlist empty\n");
}
else {
printf("\nElements are: ");
struct node *ptr = start;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
}
void main() {
int cont;
int choice;
do{
printf("Enter choice:\nCase 1: Insertion\nCase 2: Deletion\nCase 3: Display: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert_linked();
break;
case 2:
delete_linked();
break;
case 3:
display_linked();
break;
default:
printf("Exit from program.");
}
printf("\nWhether you want to continue, press 1, or to not continue, press 0: ");
scanf("%d", &cont);
} while (cont == 1);
getch();
Jigyasa A50504923003
18
}
Output:
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter element: 55
Whether you want to continue, press 1, or to not continue, press 0: 1
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter element: 66
Whether you want to continue, press 1, or to not continue, press 0: 1
Enter choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 1
Enter element: 77
Whether you want to continue, press 1, or to not continue, press 0: 1 Enter
choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 3
Elements are: 55 66 77
Whether you want to continue, press 1, or to not continue, press 0: 1 Enter
choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 2
Whether you want to continue, press 1, or to not continue, press 0: 1 Enter
choice:
Case 1: Insertion
Case 2: Deletion
Case 3: Display: 3
Elements are: 55 66
Whether you want to continue, press 1, or to not continue, press 0:
Jigyasa A50504923003
19
Experiment – 7
Linear search program :
#include <stdio.h>
int main() {
int arr[] = {1, 3, 5, 7, 9};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 5;
if (result == -1) {
printf("Element not found\n");
} else {
printf("Element found at index %d\n", result);
}
return 0;
}
Jigyasa A50504923003
20