You are on page 1of 31

LAB FILE

Subject-Data Structures & Algorithms


Course Code-BCSE2361

Submitted By :- Vikram Pratap


Admission Number :-22SCSE1010985

Submitted To :- Mrs. Deepti Singh


Index
Exp.No. Particular Page No.
1 WAP to implement linear search.
2 WAP to implement binary search.
3 WAP to implement linked list.
4 WAP to implement STACK using arrays.
5 WAP to implement STACK using linked list.
6 WAP that uses STACK operations to convert
given infix expression to its postfix expression.
7 WAP that uses STACK operations to evaluate
given postfix operations.
8 WAP to implement QUEUE using arrays.
9 WAP to implement QUEUE using linked list.
10 WAP to implement bubble-sort.
11 WAP to find factorial of a number using
recursive function.
12 WAP to find sum of numbers range starting from
1 to n using recursive function.

Experiment No – 1
Object :- WAP to implement linear search.
Code :- #include <stdio.h>
int linearSearch(int a[], int n, int val) {
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
int main() {
int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; //
given array
int val = 41; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = linearSearch(a, n, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of
array", res);
return 0;
}
Output :-

Experiment No – 2
Object :- WAP to implement binary search.
Code :- #include <stdio.h>
int binarySearch(int a[], int beg, int end, int val)
{
int mid;
if(end >= beg)
{ mid = (beg + end)/2;
/* if the item to be searched is present at middle */
if(a[mid] == val)
{
return mid+1;
}
/* if the item to be searched is smaller than
middle, then it can only be in left subarray */
else if(a[mid] < val)
{
return binarySearch(a, mid+1, end, val);
}
/* if the item to be searched is greater than
middle, then it can only be in right subarray */
else
{
return binarySearch(a, beg, mid-1, val);
}
}
return -1;
}
int main() {
int a[] = {11, 14, 25, 30, 40, 41, 52, 57, 70}; //
given array
int val = 40; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = binarySearch(a, 0, n-1, val); // Store
result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of
array", res);
return 0;
}
Experiment No – 3
Object :- WAP to implement linked list.
Code :-
#include <stdio.h>
#include <stdlib.h>

// Creating a node
struct node {
int value;
struct node *next;
};

// print the linked list value


void printLinkedlist(struct node *p) {
while (p != NULL) {
printf("%d ", p->value);
p = p->next;
}
}

int main() {
// Initialize nodes
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;

// Allocate memory
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));

// Assign value values


one->value = 1;
two->value = 2;
three->value = 3;

// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;

// printing node-value
head = one;
printLinkedlist(head);
}
Output:-
Experiment No – 4
Object :- WAP to implement STACK using
arrays.
Code :- #include<stdio.h>
#include<stdlib.h>

int n, top = -1, *stack;

void push(int x){


if(top==n) return;
stack[++top]=x;
}

int pop(){
if(top==-1) return -1;
return stack[top--];
}

int peek(){
if(top==-1) return -1;
return stack[top];
}

void display(){
for(int i=top ; i>-1 ; i--) printf("%d ",stack[i]);
printf("\n\n");
}

int main(){

n = 10;

printf("Initializing the stack with size 10\n\n");

stack = (int*)malloc(n*sizeof(int));

printf("Pushing elements into the


stack\n1\n2\n3\n\n");

push(1);
push(2);
push(3);

printf("Displaying elements of the stack -\n");

display();

printf("The top of the stack = %d\n\n",peek());

printf("Pop the top of the stack =


%d\n\n",pop());

printf("Pop the top of the stack =


%d\n\n",pop());

printf("Displaying elements of the stack -\n");

display();

return 0;
}

Experiment No – 5
Object:- WAP to implement STACK using linked
list.
Code :- #include <stdio.h>
#include <stdlib.h>

// Structure to create a node with data and the next


pointer
struct Node {
int data;
struct Node *next;
};
Node* top = NULL;

// Push() operation on a stack


void push(int value) {
struct Node *newNode;
newNode = (struct Node *)malloc(sizeof(struct
Node));
newNode->data = value; // assign value to the
node
if (top == NULL) {
newNode->next = NULL;
} else {
newNode->next = top; // Make the node as
top
}
top = newNode; // top always points to the
newly created node
printf("Node is Inserted\n\n");
}

int pop() {
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
struct Node *temp = top;
int temp_data = top->data;
top = top->next;
free(temp);
return temp_data;
}
}

void display() {
// Display the elements of the stack
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
printf("The stack is \n");
struct Node *temp = top;
while (temp->next != NULL) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL\n\n", temp->data);
}
}

int main() {
int choice, value;
printf("\nImplementation of Stack using Linked
List\n");
while (1) {
printf("1. Push\n2. Pop\n3. Display\n4.
Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
printf("Popped element is :%d\n", pop());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}
Experiment No – 6
Object :- WAP that uses STACK operations to
convert given infix expression to its postfix
expression.
Code :- #include<stdio.h>
#include<ctype.h>

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

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

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

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

int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}
Output :-
Experiment No – 7
Object :- WAP that uses STACK operations to
evaluate given postfix operations.
Code :- #include<stdio.h>
int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s =
%d\n\n",exp,pop());
return 0;
}

Experiment No – 8
Object :- WAP to implement QUEUE using
arrays.
Code :- #include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display
\n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is
%d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the
options");
}
}
}
return 0;
}
Output :-

You might also like