0% found this document useful (0 votes)
4 views55 pages

Data Structure Practical

The document provides C programming implementations for various sorting algorithms (Bubble Sort, Selection Sort, Insertion Sort) and searching algorithms (Linear Search, Binary Search). It also includes operations for Singly and Doubly Linked Lists, detailing creation, insertion, deletion, traversal, and counting nodes. Each section includes code examples and expected outputs for clarity.

Uploaded by

chauhanramfer90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views55 pages

Data Structure Practical

The document provides C programming implementations for various sorting algorithms (Bubble Sort, Selection Sort, Insertion Sort) and searching algorithms (Linear Search, Binary Search). It also includes operations for Singly and Doubly Linked Lists, detailing creation, insertion, deletion, traversal, and counting nodes. Each section includes code examples and expected outputs for clarity.

Uploaded by

chauhanramfer90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GUJARAT UNIVERSITY

Program Name: Bachelor of Computer Applications

Subject Name : Data Structure

Unit 4: Sorting and Searching


Sorting :
1. Write a C program to implement Bubble Sort.

Bubble Sort
Bubble Sort is a simple sorting technique that repeatedly compares
adjacent elements and swaps them if they are in the wrong order.
#include <stdio.h>
int main() {
int a[10], n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]); }
// Bubble Sort Logic
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;

Output :
Enter number of elements: 5
Enter elements:
64 34 25 12 22
Sorted array:
12 22 25 34 64
2. Write a C program to implement Selection Sort.
Selection Sort :
Selection Sort is a simple sorting technique that repeatedly selects the smallest
element from the unsorted part of the array and places it at the beginning.
#include <stdio.h>

int main() {
int a[10], n, i, j, min, temp;

printf("Enter number of elements: ");


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

// Selection Sort Logic


for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++) {
if (a[j] < a[min]) {
min = j;
}
}
// Swap
temp = a[i];
a[i] = a[min];
a[min] = temp;
}

printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}

Output :
Enter number of elements: 5
Enter elements:
64 25 12 22 11
Sorted array:
11 12 22 25 64

2. Write a C program to implement Insertion Sort.


Insertion Sort:
Insertion Sort is a simple sorting technique in which each element of an
array is taken and inserted into its correct position in the already sorted part of
the array.
#include <stdio.h>

int main() {
int a[50], n, i, j, key;

printf("Enter number of elements: ");


scanf("%d", &n);

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

// Insertion Sort logic


for (i = 1; i < n; i++) {
key = a[i];
j = i - 1;

while (j >= 0 && a[j] > key) {


a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}

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

return 0;
}
Output :
Enter number of elements: 5
Enter elements:
95143
Sorted array:
13459

Searching:
1. Write a C program to implement Linear / Sequential Search.
Linear (Sequential) Search:

Linear Search is a simple technique to find an element in an array by


checking each element one by one until the desired element is found.

#include <stdio.h>

int main() {
int a[50], n, i, key, found = 0;

printf("Enter number of elements: ");


scanf("%d", &n);

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

printf("Enter element to search: ");


scanf("%d", &key);

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


if(a[i] == key) {
found = 1;
break;
}
}

if(found)
printf("%d found at position %d\n", key, i+1);
else
printf("%d not found in the array\n", key);

return 0;
}

Output :
Enter number of elements: 5
Enter elements:
10 20 30 40 50
Enter element to search: 30
30 found at position 3

2. Write a C program to implement Binary Search.


Binary Search :

Binary Search is a searching algorithm that finds an element in a


sorted array by repeatedly dividing the search interval into two halves.

#include <stdio.h>

int main() {
int a[10], n, key;
int low, high, mid, i;
printf("Enter number of elements: ");
scanf("%d", &n);

printf("Enter %d elements (in sorted order):\n", n);


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

printf("Enter element to search: ");


scanf("%d", &key);

low = 0;
high = n - 1;

while(low <= high) {


mid = (low + high) / 2;

if(a[mid] == key) {
printf("Element found at position %d\n", mid + 1);
return 0;
}
else if(a[mid] < key) {
low = mid + 1;
}
else {
high = mid - 1;
}
}

printf("Element not found\n");


return 0;
}

Input:
Enter number of elements: 5
Enter 5 elements (in sorted order):
10 20 30 40 50
Enter element to search: 30

Output:
Element found at position 3

Unit 1 : Linked Lists

Singly Linked List


[Link] a C program to implement following operations using Singly Linked List
a. Creation
b. Insertion at beginning
c. Insertion at end
d. Insertion in between (Before any node)
e. Insertion in between (After any node)
f. Traversal
g. Delete first node
h. Delete last node
i. Delete in between (Before any node)
j. Delete in between (After any node)
k. Delete particular node
l. Count no. of nodes in the list

Definition – Singly Linked List


Singly Linked List is a linear data structure in which each node contains
two parts: data and a pointer to the next node in the list.

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

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

struct node *head = NULL;

/* Create Node */
struct node* createNode(int value) {
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}

/* Insert at Beginning */
void insertBegin(int value) {
struct node *newNode = createNode(value);
newNode->next = head;
head = newNode;
}

/* Insert at End */
void insertEnd(int value) {
struct node *newNode = createNode(value);
struct node *temp = head;
if (head == NULL) {
head = newNode;
return;
}

while (temp->next != NULL)


temp = temp->next;

temp->next = newNode;
}

/* Insert After a Node */


void insertAfter(int key, int value) {
struct node *temp = head;

while (temp != NULL && temp->data != key)


temp = temp->next;

if (temp != NULL) {
struct node *newNode = createNode(value);
newNode->next = temp->next;
temp->next = newNode;
}
}

/* Insert Before a Node */


void insertBefore(int key, int value) {
if (head->data == key) {
insertBegin(value);
return;
}

struct node *temp = head;


while (temp->next != NULL && temp->next->data != key)
temp = temp->next;
if (temp->next != NULL) {
struct node *newNode = createNode(value);
newNode->next = temp->next;
temp->next = newNode;
}
}

/* Delete First Node */


void deleteBegin() {
struct node *temp = head;
head = head->next;
free(temp);
}

/* Delete Last Node */


void deleteEnd() {
struct node *temp = head;
while (temp->next->next != NULL)
temp = temp->next;

free(temp->next);
temp->next = NULL;
}

/* Delete Particular Node */


void deleteParticular(int key) {
struct node *temp = head, *prev;

if (temp->data == key) {
head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}

if (temp != NULL) {
prev->next = temp->next;
free(temp);
}
}

/* Traversal */
void display() {
struct node *temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

/* Count Nodes */
int countNodes() {
int count = 0;
struct node *temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}

/* Main Function */
int main() {
insertEnd(10);
insertEnd(20);
insertEnd(30);

printf("Initial List:\n");
display();

insertBegin(5);
printf("\nAfter Insert at Beginning:\n");
display();

insertAfter(20, 25);
printf("\nAfter Insert After 20:\n");
display();

insertBefore(10, 8);
printf("\nAfter Insert Before 10:\n");
display();

deleteBegin();
printf("\nAfter Delete First Node:\n");
display();

deleteEnd();
printf("\nAfter Delete Last Node:\n");
display();

deleteParticular(20);
printf("\nAfter Delete Node 20:\n");
display();

printf("\nTotal Nodes = %d\n", countNodes());

return 0;
}
Output:

Initial List:
10 -> 20 -> 30 -> NULL

After Insert at Beginning:


5 -> 10 -> 20 -> 30 -> NULL

After Insert After 20:


5 -> 10 -> 20 -> 25 -> 30 -> NULL

After Insert Before 10:


5 -> 8 -> 10 -> 20 -> 25 -> 30 -> NULL

After Delete First Node:


8 -> 10 -> 20 -> 25 -> 30 -> NULL

After Delete Last Node:


8 -> 10 -> 20 -> 25 -> NULL

After Delete Node 20:


8 -> 10 -> 25 -> NULL

Total Nodes = 3
Doubly Linked List:
1. Write a C program to implement following operations using Doubly
Linked List
▪ Creation
▪ Insertion at beginning
▪ Insertion at end
▪ Insertion in between (Before any node)
▪ Insertion in between (After any node)
▪ Traversal
▪ Delete first node
▪ Delete last node
▪ Delete in between (Before any node)
▪ Delete in between (After any node)
▪ Delete particular node
▪ Count no. of nodes in the list

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

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

struct node *head = NULL;

/* Create Node */
struct node* createNode(int val){
struct node *n = malloc(sizeof(struct node));
n->data = val;
n->prev = n->next = NULL;
return n;
}

/* Insert at Beginning */
void insertBegin(int val){
struct node *n = createNode(val);
if(head != NULL){
n->next = head;
head->prev = n;
}
head = n;
}

/* Insert at End */
void insertEnd(int val){
struct node *n = createNode(val);
if(head == NULL){
head = n;
return;
}
struct node *t = head;
while(t->next != NULL)
t = t->next;
t->next = n;
n->prev = t;
}

/* Insert After Node */


void insertAfter(int key,int val){
struct node *t = head;
while(t!=NULL && t->data!=key)
t=t->next;
if(t!=NULL){
struct node *n=createNode(val);
n->next=t->next;
n->prev=t;
if(t->next!=NULL)
t->next->prev=n;
t->next=n;
}
}

/* Insert Before Node */


void insertBefore(int key,int val){
if(head->data==key){
insertBegin(val);
return;
}
struct node *t=head;
while(t!=NULL && t->data!=key)
t=t->next;
if(t!=NULL){
struct node *n=createNode(val);
n->prev=t->prev;
n->next=t;
t->prev->next=n;
t->prev=n;
}
}

/* Delete First */
void deleteBegin(){
struct node *t=head;
head=head->next;
if(head!=NULL) head->prev=NULL;
free(t);
}
/* Delete Last */
void deleteEnd(){
struct node *t=head;
while(t->next!=NULL)
t=t->next;
t->prev->next=NULL;
free(t);
}

/* Delete Particular Node */


void deleteNode(int key){
struct node *t=head;
while(t!=NULL && t->data!=key)
t=t->next;
if(t==head) deleteBegin();
else if(t->next==NULL) deleteEnd();
else{
t->prev->next=t->next;
t->next->prev=t->prev;
free(t);
}
}

/* Traversal */
void display(){
struct node *t=head;
while(t!=NULL){
printf("%d <-> ",t->data);
t=t->next;
}
printf("NULL\n");
}

/* Count Nodes */
int count(){
int c=0;
struct node *t=head;
while(t!=NULL){
c++; t=t->next;
}
return c;
}

int main(){
insertEnd(10);
insertEnd(20);
insertEnd(30);

printf("Initial List:\n");
display();

insertBegin(5);
printf("After Insert at Beginning:\n");
display();

insertAfter(20,25);
printf("After Insert After 20:\n");
display();

insertBefore(10,8);
printf("After Insert Before 10:\n");
display();

deleteBegin();
printf("After Delete First:\n");
display();

deleteEnd();
printf("After Delete Last:\n");
display();

deleteNode(20);
printf("After Delete Node 20:\n");
display();

printf("Total Nodes = %d\n",count());


return 0;
}

Output :

Initial List:
10 <-> 20 <-> 30 <-> NULL
After Insert at Beginning:
5 <-> 10 <-> 20 <-> 30 <-> NULL
After Insert After 20:
5 <-> 10 <-> 20 <-> 25 <-> 30 <-> NULL
After Insert Before 10:
5 <-> 8 <-> 10 <-> 20 <-> 25 <-> 30 <-> NULL
After Delete First Node:
8 <-> 10 <-> 20 <-> 25 <-> 30 <-> NULL
After Delete Last Node:
8 <-> 10 <-> 20 <-> 25 <-> NULL
After Delete Node 20:
8 <-> 10 <-> 25 <-> NULL
Total Nodes = 3
1️⃣ Initial List
insertEnd(10);
insertEnd(20);
insertEnd(30);

List બને છે :
10 <-> 20 <-> 30 <-> NULL

🔹 2️⃣ Insert at Beginning (5)


insertBegin(5);

5 head બની જાય છે .


5 <-> 10 <-> 20 <-> 30 <-> NULL

🔹 3️⃣ Insert After 20 (25)


insertAfter(20, 25);

20 પછી 25 add થાય છે .


5 <-> 10 <-> 20 <-> 25 <-> 30 <-> NULL

🔹 4️⃣ Insert Before 10 (8)


insertBefore(10, 8);

10 પહેલા 8 add થાય છે .


5 <-> 8 <-> 10 <-> 20 <-> 25 <-> 30 <-> NULL
🔹 5️⃣ Delete First Node
deleteBegin();

5 remove થાય છે.


8 <-> 10 <-> 20 <-> 25 <-> 30 <-> NULL

🔹 6️⃣ Delete Last Node


deleteEnd();

30 remove થાય છે.


8 <-> 10 <-> 20 <-> 25 <-> NULL

🔹 7️⃣ Delete Particular Node (20)


deleteParticular(20);

20 remove થાય છે.


8 <-> 10 <-> 25 <-> NULL

🔹 8️⃣ Count Nodes


Remaining nodes:
• 8
• 10
• 25
Total = 3
Unit 2 : Stacks

1. Write a C program to implement following operations in STACK (using


Array)
o PUSH
o POP
o PEEP
o CHANGE
o DISPLAY

• PUSH → Add element at top


• POP → Remove element from top
• PEEP → See element at given position from top
• CHANGE → Modify element at given position
• DISPLAY → Show all stack elements

Short Summary

Operation Work
PUSH Insert element
POP Delete top element
PEEP View element
CHANGE Modify element
DISPLAY Show all elements

C Program (Stack Using Array)


#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;

// PUSH Operation
void push(int value) {
if (top == MAX - 1)
printf("Stack Overflow!\n");
else {
top++;
stack[top] = value;
printf("%d pushed into stack\n", value);
}
}

// POP Operation
void pop() {
if (top == -1)
printf("Stack Underflow!\n");
else {
printf("%d popped from stack\n", stack[top]);
top--;
}
}
// PEEP Operation (View element at position)
void peep(int pos) {
if (top - pos + 1 < 0)
printf("Invalid Position!\n");
else
printf("Element at position %d is %d\n", pos, stack[top - pos + 1]);
}

// CHANGE Operation (Change element at position)


void change(int pos, int value) {
if (top - pos + 1 < 0)
printf("Invalid Position!\n");
else {
stack[top - pos + 1] = value;
printf("Element changed successfully!\n");
}
}

// DISPLAY Operation
void display() {
if (top == -1)
printf("Stack is Empty!\n");
else {
printf("Stack elements are:\n");
for (int i = top; i >= 0; i--)
printf("%d\n", stack[i]);
}
}

// Main Function
int main() {
push(10);
push(20);
push(30);
display();

peep(2);
change(2, 25);
display();

pop();
display();

return 0;
}

Output :
10 pushed into stack
20 pushed into stack
30 pushed into stack
Stack elements are:
30
20
10
Element at position 2 is 20
Element changed successfully!
Stack elements are:
30
25
10
30 popped from stack
Stack elements are:
25
10

2. Write a C program to implement following operations in STACK (using


Linked Lists)
• PUSH

• POP

• PEEP

• CHANGE

• DISPLAY
Easy Explanation

• PUSH → Create new node and insert at beginning

• POP → Delete first node

• PEEP → Move pointer to given position

• CHANGE → Modify node data

• DISPLAY → Traverse and print all nodes

C Program (Stack Using Linked List)


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

// Node structure
struct node {
int data;
struct node *next;
};

struct node *top = NULL;

// PUSH
void push(int value) {
struct node *newnode;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
newnode->next = top;
top = newnode;

printf("%d pushed into stack\n", value);


}

// POP
void pop() {
if (top == NULL)
printf("Stack Underflow!\n");
else {
struct node *temp = top;
printf("%d popped from stack\n", top->data);
top = top->next;
free(temp);
}
}

// PEEP
void peep(int pos) {
struct node *temp = top;
int i;

for (i = 1; i < pos && temp != NULL; i++)


temp = temp->next;

if (temp == NULL)
printf("Invalid Position!\n");
else
printf("Element at position %d is %d\n", pos, temp->data);
}

// CHANGE
void change(int pos, int value) {
struct node *temp = top;
int i;

for (i = 1; i < pos && temp != NULL; i++)


temp = temp->next;

if (temp == NULL)
printf("Invalid Position!\n");
else {
temp->data = value;
printf("Element changed successfully!\n");
}
}

// DISPLAY
void display() {
struct node *temp = top;

if (top == NULL)
printf("Stack is Empty!\n");
else {
printf("Stack elements are:\n");
while (temp != NULL) {
printf("%d\n", temp->data);
temp = temp->next;
}
}
}

// Main
int main() {
push(10);
push(20);
push(30);
display();

peep(2);
change(2, 25);
display();

pop();
display();

return 0;
}

Output :
10 pushed into stack
20 pushed into stack
30 pushed into stack
Stack elements are:
30
20
10
Element at position 2 is 20
Element changed successfully!
Stack elements are:
30
25
10
30 popped from stack
Stack elements are:
25
10
Easy Explanation
• PUSH → Create new node and insert at beginning
• POP → Delete first node
• PEEP → Move pointer to given position
• CHANGE → Modify node data
• DISPLAY → Traverse and print all nodes
Difference from Array Stack

Array Stack Linked List Stack


Fixed size Dynamic size
Overflow possible No overflow (until memory full)
Uses array Uses nodes

3. Write a C program to reverse the string using the STACK.

Reverse String is the process of arranging the characters of a string


in the opposite (reverse) order.

🔹 In simple words:
The first character becomes last and the last character becomes
first.

C Program to Reverse String Using Stack


#include <stdio.h>
#include <string.h>

#define MAX 100

char stack[MAX];
int top = -1;

// PUSH
void push(char ch) {
stack[++top] = ch;
}

// POP
char pop() {
return stack[top--];
}

int main() {
char str[100];
int i;

printf("Enter a string: ");


gets(str); // input string

// Push all characters into stack


for (i = 0; i < strlen(str); i++) {
push(str[i]);
}

// Pop characters back into string (reverse)


for (i = 0; i < strlen(str); i++) {
str[i] = pop();
}

printf("Reversed string: %s", str);

return 0;
}

Output :

Enter a string: HELLO


Reversed string: OLLEH
4. Write a C program to implement recursion
What is Recursion?
Recursion is a process in which a function calls itself.

Important Points
• Must have Base Condition (to stop recursion)
• Function calls itself
• Used for problems like:
o Factorial
o Fibonacci
o Tower of Hanoi

C Program to Implement Recursion (Factorial)

#include <stdio.h>

// Recursive Function
int factorial(int n) {
if (n == 0 || n == 1) // Base condition
return 1;
else
return n * factorial(n - 1); // Recursive call
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

printf("Factorial of %d is %d", num, factorial(num));

return 0;
}

Output

Enter a number: 5
Factorial of 5 is 120

5. Write a C program to implement conversion of infix expression into


postfix expression (parentheses and non-parentheses).
C Program (Infix to Postfix using Stack)

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX 100

char stack[MAX];
int top = -1;

// Push
void push(char x) {
stack[++top] = x;
}

// Pop
char pop() {
return stack[top--];
}

// Priority
int priority(char x) {
if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/')
return 2;
return 0;
}

int main() {
char infix[MAX], postfix[MAX];
int i, j = 0;

printf("Enter Infix Expression: ");


scanf("%s", infix);

for (i = 0; i < strlen(infix); i++) {

// If operand, add to postfix


if (isalnum(infix[i])) {
postfix[j++] = infix[i];
}

// If '(' push to stack


else if (infix[i] == '(') {
push(infix[i]);
}

// If ')', pop until '('


else if (infix[i] == ')') {
while (stack[top] != '(')
postfix[j++] = pop();
pop(); // remove '('
}

// If operator
else {
while (priority(stack[top]) >= priority(infix[i]))
postfix[j++] = pop();
push(infix[i]);
}
}

// Pop remaining operators


while (top != -1)
postfix[j++] = pop();

postfix[j] = '\0';

printf("Postfix Expression: %s", postfix);

return 0;
}

Output

Sample Output 1 (Without Parentheses)


Enter Infix Expression: A+B
Postfix Expression: AB+
✅ Sample Output 2 (With Parentheses)
Enter Infix Expression: (A+B)*C
Postfix Expression: AB+C*

Infix
Operator is between operands
Example:
A+B

Postfix
Operator comes after operands
Example:
AB+
Definition
Infix to Postfix conversion is the process of converting an infix expression
(operator between operands) into postfix expression (operator after
operands).

Unit 3 : Queues and Trees

Queues:
1. Write a C program to implement following operations in SIMPLE
QUEUE (using array)
▪ ENQUEUE (Insertion)
▪ DEQUEUE (Deletion)
▪ DISPLAY

C Program: Simple Queue (Using Array)

#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

void enqueue(int value) {


if (rear == SIZE - 1) {
printf("Queue is Full\n");
} else {
if (front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("%d inserted into queue\n", value);
}
}

void dequeue() {
if (front == -1 || front > rear) {
printf("Queue is Empty\n");
} else {
printf("%d deleted from queue\n", queue[front]);
front++;
}
}

void display() {
if (front == -1 || front > rear) {
printf("Queue is Empty\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}

Output :
10 inserted into queue
20 inserted into queue
30 inserted into queue
Queue elements are: 10 20 30
10 deleted from queue
Queue elements are: 20 30

Queue definition :
Queue is a linear data structure that follows FIFO (First In First Out)
principle where insertion is done at rear and deletion is done from
front.

2. Write a C program to implement following operations in SIMPLE


QUEUE (using Linked List)

• ENQUEUE (Insertion)
• DEQUEUE (Deletion)
• DISPLAY
Simple Queue using Linked List – Definition
Queue using Linked List is a linear data structure that follows FIFO
principle where insertion is done at rear and deletion is done from front using
dynamic memory allocation.

C Program: Simple Queue (Using Linked List)


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

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

struct node *front = NULL, *rear = NULL;


// ENQUEUE
void enqueue(int value) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;

if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("%d inserted\n", value);
}

// DEQUEUE
void dequeue() {
if (front == NULL) {
printf("Queue is Empty\n");
return;
}
struct node* temp = front;
printf("%d deleted\n", front->data);
front = front->next;

if (front == NULL)
rear = NULL;

free(temp);
}

// DISPLAY
void display() {
if (front == NULL) {
printf("Queue is Empty\n");
return;
}
struct node* temp = front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}

Output
10 inserted
20 inserted
30 inserted
Queue elements: 10 20 30
10 deleted
Queue elements: 20 30

Easy Explanation
• ENQUEUE → A new node is added at the rear (end) of the queue.
• DEQUEUE → The node from the front (beginning) of the queue is deleted.
• DISPLAY → All elements are printed from front to rear.

3. Write a C program to implement following operations in


CIRCULAR QUEUE (using array)
• ENQUEUE (Insertion)
• DEQUEUE (Deletion)
• DISPLAY
C Program: Circular Queue (Using Array)
#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

// ENQUEUE
void enqueue(int value) {
if ((rear + 1) % SIZE == front) {
printf("Queue is Full\n");
return;
}

if (front == -1) {
front = rear = 0;
} else {
rear = (rear + 1) % SIZE;
}

queue[rear] = value;
printf("%d inserted\n", value);
}
// DEQUEUE
void dequeue() {
if (front == -1) {
printf("Queue is Empty\n");
return;
}

printf("%d deleted\n", queue[front]);

if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % SIZE;
}
}

// DISPLAY
void display() {
if (front == -1) {
printf("Queue is Empty\n");
return;
}
printf("Queue elements: ");
int i = front;

while (1) {
printf("%d ", queue[i]);
if (i == rear)
break;
i = (i + 1) % SIZE;
}
printf("\n");
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}

Output
10 inserted
20 inserted
30 inserted
Queue elements: 10 20 30
10 deleted
Queue elements: 20 30

Trees:
1. Write a C program to implement following operations on Binary
Search Tree using Linked List.
• CREATION
• INSERTION
• TRAVERSAL (In-Order, Pre-Order, Post-Order)

C Program: Binary Search Tree (Using Linked List)


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

// Node structure
struct node {
int data;
struct node* left;
struct node* right;
};

// Create new node


struct node* createNode(int value) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}

// INSERTION
struct node* insert(struct node* root, int value) {
if (root == NULL)
return createNode(value);

if (value < root->data)


root->left = insert(root->left, value);
else if (value > root->data)
root->right = insert(root->right, value);

return root;
}

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

// Pre-Order Traversal
void preorder(struct node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

// Post-Order Traversal
void postorder(struct node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}

int main() {
struct node* root = NULL;

// CREATION & INSERTION


root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);

printf("In-Order: ");
inorder(root);

printf("\nPre-Order: ");
preorder(root);

printf("\nPost-Order: ");
postorder(root);

return 0;
}

Output
In-Order: 20 30 40 50 60 70 80
Pre-Order: 50 30 20 40 70 60 80
Post-Order: 20 40 30 60 80 70 50

Definition :
Binary Search Tree is a binary tree in which for every node, all elements in
the left subtree are smaller and all elements in the right subtree are greater
than the root node.

You might also like