LAB: DATA STRUCTURES AND ALGORITHMS
EXPERIMENT – 1
IMPLEMENTATION OF ARRAY OPERATIONS
AIM:
To implement various operations of array using C programming language
ALGORITHM:
Step 1: Start the process
Step 2: Open Turbo C program.
Step 3: Declare an array arr[] of fixed maximum size and other variables.
Step 4: Save the program with the correct file extension (Filename.c)
Step 5: Get the input initial array
Step 6: Perform Array operations based on choice
Step 7: Display the output
Step 8: Stop the Process
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define SIZE 100
void main()
{
int i,arr[SIZE], n, choice, element, pos;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
do
{
clrscr();
printf("\n1. Display Array\n");
printf("2. Insert Element\n");
printf("3. Delete Element\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Array: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
break;
case 2:
if (n == SIZE)
{
printf("Array is full!\n");
break;
}
printf("Enter element to insert: ");
scanf("%d", &element);
printf("Enter position (0 to %d): ", n);
scanf("%d", &pos);
if(pos < 0 || pos > n)
{
printf("Invalid position!\n");
break;
}
for(i = n; i > pos; i--)
arr[i] = arr[i - 1];
arr[pos] = element;
n++;
printf("Element inserted.\n");
break;
case 3:
if(n == 0)
{
printf("Array is empty!\n");
break;
}
printf("Enter position to delete (0 to %d): ", n - 1);
scanf("%d", &pos);
if(pos < 0 || pos >= n)
{
printf("Invalid position!\n");
break;
}
for( i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
n--;
printf("Element deleted.\n");
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
}
} while(choice != 4);
getch();
}
OUTPUT:
EXPERIMENT – 2
IMPLEMENTATION OF STACK USING ARRAYS.
AIM:
To implement stack operations using array.
ALGORITHM :
Step 1: Start the process
Step 2: Open Turbo C program
Step 3: Edit the program
Step 4: Save the program with the correct file extension (Filename.c)
Step 5: Define a array stack of size max = 5 , Initialize top = -1
Step 6: If choice = 2 then, If top < max -1, Increment top, Store element at current position of top
else Print Stack overflow
Step 7: If top < 0 then Print Stack underflow Else Display current top element
Step 8: If choice = 3 then ,Display stack elements starting from top
Step 9: Display the output
Step 10: Stop the Process
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#define max 5
static int stack[max];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return (stack[top--]);
}
void view()
{
int i;
if (top < 0)
printf("\n Stack Empty \n");
else
{
printf("\n Top-->");
for(i=top; i>=0; i--)
printf("%4d", stack[i]);
printf("\n");
}
}
void main()
{
int ch=0, val;
clrscr();
while(ch != 4)
{
clrscr();
printf("\n STACK OPERATIONs \n");
printf("1.PUSH \t ");
printf("2.POP \t");
printf("3.VIEW \t ");
printf("4.QUIT \n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(top < max-1)
{
printf("\nEnter Stack element : ");
scanf("%d", &val);
push(val);
}
else
printf("\n Stack Overflow \n");
break;
case 2:
if(top < 0)
printf("\n Stack Underflow \n");
else
{
val = pop();
printf("\n Popped element is %d\n", val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}
}
getch();
}
OUTPUT :
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 23
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 34
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 45 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 2
Popped element is 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 4
EXPERIMENT – 3
IMPLEMENTATION OF QUEUE USING ARRAYS
AIM:
To implement queue operations using array.
ALGORITHM:
Step 1: Start the process
Step 2: Open Turbo C program
Step 3: Save the program with the correct file extension (Filename.c)
Step 4: Define a array queue of size max = 5
Step 5: Initialize front = rear = –1
Step 6: If choice = 1 then, If rear < max -1, Increment rear
Store element at current position of rear Else Print Queue Full
Step 7: If choice = 2 then, If front = –1 then Print Queue empty
Else Display current front element, Increment front
Step 8: If choice = 3 then, Display queue elements starting from front to rear.
Step 9: Display the output
Step 10: Stop the Process
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#define max 5
static int queue[max];
int front = -1;
int rear = -1;
void insert(int x)
{
queue[++rear] = x;
if (front == -1)
front = 0;
}
int remove()
{
int val;
val = queue[front];
if (front==rear && rear==max-1)
front = rear = -1;
else
front ++;
return (val);
}
void view()
{ int i;
if (front == -1)
printf("\n Queue Empty \n");
else
{
printf("\n Front-->");
for(i=front; i<=rear; i++)
printf("%4d", queue[i]);
printf(" <--Rear\n");
}
}
main()
{
int ch= 0,val;
clrscr();
while(ch != 4)
{
printf("\n QUEUE OPERATION \n");
printf("1.INSERT \t ");
printf("2.DELETE \t ");
printf("3.VIEW \t ");
printf("4.QUIT \t\n");
printf("\n Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(rear< max-1)
{
printf("\n Enter element to be inserted : ");
scanf("%d", &val);
insert(val);
}
else
printf("\n Queue Full \n");
break;
case 2:
if(front == -1)
printf("\n Queue Empty \n");
else
{
val = remove();
printf("\n Element deleted : %d \n", val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}
}
}
OUTPUT :
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 12
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 23
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 34
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 45
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 56
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Queue Full
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 3
Front--> 12 23 34 45 56 <--Rear