0% found this document useful (0 votes)
5 views9 pages

DS Practical Programs

The document contains 10 practical programs in C language that demonstrate various data structures and their operations. Programs include insertion and deletion in arrays, recursion for factorial and Fibonacci, stack and queue implementations using arrays and linked lists, linked list operations, and binary search tree functionalities. Each program is accompanied by code snippets and explanations of their functionalities.

Uploaded by

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

DS Practical Programs

The document contains 10 practical programs in C language that demonstrate various data structures and their operations. Programs include insertion and deletion in arrays, recursion for factorial and Fibonacci, stack and queue implementations using arrays and linked lists, linked list operations, and binary search tree functionalities. Each program is accompanied by code snippets and explanations of their functionalities.

Uploaded by

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

Data Structure Practical Programs

All 10 Programs in C Language

Program 1: Insertion and Deletion at Specific Position in Array


#include <stdio.h>
#define MAX 100

void insert(int arr[], int *n, int pos, int val) {


if (*n >= MAX) { printf("Array is full\n"); return; }
for (int i = *n; i > pos; i--)
arr[i] = arr[i-1];
arr[pos] = val;
(*n)++;
}

void delete(int arr[], int *n, int pos) {


if (*n == 0) { printf("Array is empty\n"); return; }
for (int i = pos; i < *n - 1; i++)
arr[i] = arr[i+1];
(*n)--;
}

void display(int arr[], int n) {


printf("Array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[MAX], n = 5;
int init[] = {10, 20, 30, 40, 50};
for (int i = 0; i < n; i++) arr[i] = init[i];
display(arr, n);
insert(arr, &n, 2, 99);
printf("After inserting 99 at position 2:\n");
display(arr, n);
delete(arr, &n, 2);
printf("After deleting element at position 2:\n");
display(arr, n);
return 0;
}

Program 2: Demonstration of Recursion (Factorial & Fibonacci)


#include <stdio.h>

long factorial(int n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1);
}

int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %ld\n", num, factorial(num));
printf("Fibonacci series up to %d terms:\n", num);
for (int i = 0; i < num; i++)
printf("%d ", fibonacci(i));
printf("\n");
return 0;
}

Program 3: Stack Using Array


#include <stdio.h>
#define MAX 100

int stack[MAX], top = -1;

void push(int val) {


if (top == MAX - 1) { printf("Stack Overflow\n"); return; }
stack[++top] = val;
printf("%d pushed\n", val);
}

void pop() {
if (top == -1) { printf("Stack Underflow\n"); return; }
printf("%d popped\n", stack[top--]);
}

void peek() {
if (top == -1) printf("Stack is empty\n");
else printf("Top element: %d\n", stack[top]);
}

void display() {
if (top == -1) { printf("Stack is empty\n"); return; }
printf("Stack (top to bottom): ");
for (int i = top; i >= 0; i--) printf("%d ", stack[i]);
printf("\n");
}

int main() {
push(10); push(20); push(30);
display();
peek();
pop();
display();
return 0;
}

Program 4: Linear Queue Using Array


#include <stdio.h>
#define MAX 100

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

void enqueue(int val) {


if (rear == MAX - 1) { printf("Queue is Full\n"); return; }
if (front == -1) front = 0;
queue[++rear] = val;
printf("%d enqueued\n", val);
}

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

void display() {
if (front == -1) { printf("Queue is Empty\n"); return; }
printf("Queue: ");
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;
}

Program 5: Circular Queue Using Array


#include <stdio.h>
#define MAX 5

int cq[MAX], front = -1, rear = -1;

int isFull() { return (front == 0 && rear == MAX-1) || (rear == (front-1)%(MAX-1)); }


int isEmpty() { return front == -1; }

void enqueue(int val) {


if (isFull()) { printf("Circular Queue is Full\n"); return; }
if (front == -1) { front = rear = 0; }
else rear = (rear + 1) % MAX;
cq[rear] = val;
printf("%d enqueued\n", val);
}

void dequeue() {
if (isEmpty()) { printf("Circular Queue is Empty\n"); return; }
printf("%d dequeued\n", cq[front]);
if (front == rear) front = rear = -1;
else front = (front + 1) % MAX;
}

void display() {
if (isEmpty()) { printf("Circular Queue is Empty\n"); return; }
printf("Circular Queue: ");
int i = front;
while (1) {
printf("%d ", cq[i]);
if (i == rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}

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

Program 6: Singly Linked List – Basic Operations


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

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


struct Node *head = NULL;

void insertFront(int val) {


struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->data = val; n->next = head; head = n;
}

void insertEnd(int val) {


struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->data = val; n->next = NULL;
if (!head) { head = n; return; }
struct Node *t = head;
while (t->next) t = t->next;
t->next = n;
}

void deleteNode(int val) {


struct Node *t = head, *prev = NULL;
while (t && t->data != val) { prev = t; t = t->next; }
if (!t) { printf("Not found\n"); return; }
if (!prev) head = t->next;
else prev->next = t->next;
free(t); printf("%d deleted\n", val);
}

void display() {
struct Node *t = head;
printf("List: ");
while (t) { printf("%d -> ", t->data); t = t->next; }
printf("NULL\n");
}

int main() {
insertEnd(10); insertEnd(20); insertEnd(30);
insertFront(5);
display();
deleteNode(20);
display();
return 0;
}

Program 7: Doubly Linked List – Basic Operations


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

struct Node { int data; struct Node *prev, *next; };


struct Node *head = NULL;

void insertEnd(int val) {


struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->data = val; n->next = NULL; n->prev = NULL;
if (!head) { head = n; return; }
struct Node *t = head;
while (t->next) t = t->next;
t->next = n; n->prev = t;
}

void deleteNode(int val) {


struct Node *t = head;
while (t && t->data != val) t = t->next;
if (!t) { printf("Not found\n"); return; }
if (t->prev) t->prev->next = t->next;
else head = t->next;
if (t->next) t->next->prev = t->prev;
free(t); printf("%d deleted\n", val);
}
void displayForward() {
struct Node *t = head;
printf("Forward: ");
while (t) { printf("%d <-> ", t->data); t = t->next; }
printf("NULL\n");
}

int main() {
insertEnd(10); insertEnd(20); insertEnd(30);
displayForward();
deleteNode(20);
displayForward();
return 0;
}

Program 8: Double-Ended Queue (Deque) Using Linked List


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

struct Node { int data; struct Node *prev, *next; };


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

void insertFront(int val) {


struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->data = val; n->prev = NULL; n->next = front;
if (!front) rear = n;
else front->prev = n;
front = n; printf("%d inserted at front\n", val);
}

void insertRear(int val) {


struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->data = val; n->next = NULL; n->prev = rear;
if (!rear) front = n;
else rear->next = n;
rear = n; printf("%d inserted at rear\n", val);
}

void deleteFront() {
if (!front) { printf("Deque empty\n"); return; }
printf("%d deleted from front\n", front->data);
struct Node *t = front;
front = front->next;
if (front) front->prev = NULL; else rear = NULL;
free(t);
}

void deleteRear() {
if (!rear) { printf("Deque empty\n"); return; }
printf("%d deleted from rear\n", rear->data);
struct Node *t = rear;
rear = rear->prev;
if (rear) rear->next = NULL; else front = NULL;
free(t);
}

void display() {
struct Node *t = front;
printf("Deque: ");
while (t) { printf("%d ", t->data); t = t->next; }
printf("\n");
}

int main() {
insertRear(10); insertRear(20); insertFront(5); insertRear(30);
display();
deleteFront(); deleteRear();
display();
return 0;
}

Program 9: Stack Using Linked List


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

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


struct Node *top = NULL;

void push(int val) {


struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->data = val; n->next = top; top = n;
printf("%d pushed\n", val);
}

void pop() {
if (!top) { printf("Stack Underflow\n"); return; }
printf("%d popped\n", top->data);
struct Node *t = top;
top = top->next;
free(t);
}

void peek() {
if (!top) printf("Stack is empty\n");
else printf("Top: %d\n", top->data);
}

void display() {
struct Node *t = top;
printf("Stack: ");
while (t) { printf("%d -> ", t->data); t = t->next; }
printf("NULL\n");
}

int main() {
push(10); push(20); push(30);
display();
peek();
pop();
display();
return 0;
}

Program 10: Binary Search Tree – Insert, Search & Traversals


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

struct Node { int data; struct Node *left, *right; };

struct Node* newNode(int val) {


struct Node *n = (struct Node*)malloc(sizeof(struct Node));
n->data = val; n->left = n->right = NULL;
return n;
}

struct Node* insert(struct Node *root, int val) {


if (!root) return newNode(val);
if (val < root->data) root->left = insert(root->left, val);
else if (val > root->data) root->right = insert(root->right, val);
return root;
}

struct Node* search(struct Node *root, int val) {


if (!root || root->data == val) return root;
if (val < root->data) return search(root->left, val);
return search(root->right, val);
}

void inorder(struct Node *root) { if(root){inorder(root->left);printf("%d ",root-


>data);inorder(root->right);} }
void preorder(struct Node *root) { if(root){printf("%d ",root->data);preorder(root-
>left);preorder(root->right);} }
void postorder(struct Node *root) { if(root){postorder(root->left);postorder(root-
>right);printf("%d ",root->data);} }

int main() {
struct Node *root = NULL;
int vals[] = {50, 30, 70, 20, 40, 60, 80};
for (int i = 0; i < 7; i++) root = insert(root, vals[i]);

printf("Inorder: "); inorder(root); printf("\n");


printf("Preorder: "); preorder(root); printf("\n");
printf("Postorder: "); postorder(root); printf("\n");

int key = 40;


struct Node *res = search(root, key);
printf("Search %d: %s\n", key, res ? "Found" : "Not Found");
return 0;
}

You might also like