CSF Lab File
CSF Lab File
CSF102-Data Structure
LAB FILE
1
Program in C for the implementation of Array for various operations.
3 Program in C for the creation of Stack for its various operation implementation.
4 Program in C for the creation of Queue for its various operation implementation.
Program in C for the creation of Circular Queue for its various operation
5 implementation.
Program in C for the creation of Link list for its various operation
6 implementation.
Program in C for the creation of Circular Link list for its various operation
7 implementation.
Program in C for the creation of Doubly Link list for its various operation
8 implementation.
Program in C for the creation of Binary Search Tree for its various operation
9 implementation.
EXPERIMENT NO. 1
Index
Program in C for the implementation of Array for various operations.
CODE-
1. Accessing Elements of an Array in C
#include <stdio.h>
int main() {
int a[] = {25, 50, 75, 100};
printf("%d\n", a[0]);
printf("%d\n", a[1]);
printf("%d\n", a[2]);
printf("%d\n", a[3]);
printf("%d\n", a[4]);
return 0;
}
OUTPUT-
#include <stdio.h>
int main()
{
int a[] = {25, 50, 75, 100, 45};
a[3] = 60;
printf("%d\n", a[3]);
return 0;
}
OUTPUT-
3. Traversing an Array in C
#include <stdio.h>
Index
int main(){
int i=0;
int marks[5];
marks[0]=90;
marks[1]=80;
marks[2]=70;
marks[3]=95;
marks[4]=85;
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}
return 0;
}
OUTPUT-
#include <stdio.h>
int main()
{
int a[5];
printf("Enter the values of an integer array:\n ");
for(int i = 0; i < 5; ++i) {
scanf("%d", &a[i]);
}
EXPERIMENT NO. 2
Program in C for Linear Search, Binary Search.
Index
CODE-
1. C program for linear search
#include <stdio.h>
int linearSearch(int* arr, int size, int key)
{
if (size == 0)
return -1;
if (arr[size - 1] == key) {
return size - 1;
}
return linearSearch(arr, size - 1, key);
}
int main()
{
int arr[5] = { 6, 7, 9, 1, 5, 11, 8, 12, 2, 3 };
int size = sizeof(arr) / sizeof(int);
int key = 11;
int index = linearSearch(arr, size, key);
if (index == -1) {
printf("The element is not present in the list.");
}
else {
printf("The element is present at arr[%d].", index);
}
return 0;
}
OUTPUT-
OUTPUT-
EXPERIMENT NO. 3
Index
Program in C for the creation of Stack for its various operation
implementation.
CODE-
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
int isempty(){
if(top == -1)
return 1;
else
return 0;
}
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
}
int peek(){
return stack[top];
}
int pop(){
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
int push(int data){
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
}
}
int main(){
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Element at top of the stack: %d\n" ,peek());
printf("Elements: \n");
Index
while(!isempty()) {
int data = pop();
printf("%d\n",data);
}
printf("Stack full: %s\n" , isfull()?"true":"false");
printf("Stack empty: %s\n" , isempty()?"true":"false");
return 0;
}
OUTPUT-
Index
EXPERIMENT NO. 4
Program in C for the creation of Queue for its various operation
implementation.
CODE-
#include <stdio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int items[SIZE], front = -1, rear = -1;
int main() {
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
enQueue(6);
display();
deQueue();
display();
return 0;
}
Void enQueue(int value) {
if (rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (front == -1)
front = 0;
rear++;
items[rear] = value;
printf("\nInserted -> %d", value);
}
}
void deQueue() {
if (front == -1)
printf("\nQueue is Empty!!");
else {
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
}
void display() {
if (rear == -1)
Index
printf("\nQueue is Empty!!!");
else {
int i;
printf("\nQueue elements are:\n");
for (i = front; i <= rear; i++)
printf("%d ", items[i]);
}
printf("\n");
}
OUTPUT-
Index
EXPERIMENT NO. 5
Program in C for the creation of Circular Queue for its various
operation implementation.
CODE-
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
int isFull() {
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
return 0;
}
int isEmpty() {
if (front == -1) return 1;
return 0;
}
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
}
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
void display() {
Index
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}
int main() {
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
enQueue(8);
return 0;
}
OUTPUT-
Index