0% found this document useful (0 votes)
17 views94 pages

Algo & Os

The document describes an algorithm to implement dynamic list operations like insertion and deletion of elements in a linked list. It defines functions to create a header node, check if list is empty, find a node, insert a node, delete a node and delete the entire list. The program implements these functions and takes user input to perform list operations and displays the output.

Uploaded by

kope krissnan
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)
17 views94 pages

Algo & Os

The document describes an algorithm to implement dynamic list operations like insertion and deletion of elements in a linked list. It defines functions to create a header node, check if list is empty, find a node, insert a node, delete a node and delete the entire list. The program implements these functions and takes user input to perform list operations and displays the output.

Uploaded by

kope krissnan
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
You are on page 1/ 94

EX NO:1)

PERFORM EMPIRICAL ANALYSIS ON ALGORITHM


DATE:

AIM:

To calculate the clock speed of a program for different sizes of inputs .

ALGORITHM:

Step 1: Start
Step 2: Initialize variables start_t and end_t using clock_t of time.h header file
Step 3: Initialize variables n, i, j, swap using integer data type
Step 4: Get the start time using clock() function and display it in the screen
Step 5: Get number of elements of the array and declare an array
Step 6: Get the elements from the user using for loop
Step 7: Using rand() function generate random elements and store it in the array using for loop
Step 8: Print the time taken for getting inputs using clock() function
Step 9: Using two for loops compare each and every element of the array
Step 10: If the element is greater than the other, swap their positions using 3rd variable swap
Step 11: If the element is not greater, move to the next element and continue Step 10
Step 12: Now, Print the sorted elements
Step 13: Print the time taken for sorting and displaying result using clock() function
Step 14: Convert the time taken to execute the program into seconds using (double)(clock() -
start_t)/CLOCKS_PER_SEC;
Step 15: Print the time taken to compile program in seconds(clock speed)
Step 16: Stop

PROGRAM:

#include <stdio.h>
#include <time.h>
int main()
{
clock_t start_t, end_t;
int n,j,swap,i;

K.S.AJAY VARSHAAN (71812253006) 1


start_t=clock();
printf("Starting time before getting input...,start_t=%ld\n",start_t);

printf("Enter number of elements to sort...");


scanf("%d",&n);
int num[n];
printf("\nEnter the elements...\n");
for(int i=0;i<n;i++)
{
num[i]=rand()%20000;
printf("%d\n",num[i]);
}
start_t=clock();
printf("Time after getting input...,start_t=%ld\n",start_t);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
swap=num[i];
num[i]=num[j];
num[j]=swap;

}
}
}
printf("\nSorted elements...\n");
for(i=0;i<n;i++)
{
printf("%d\n",num[i]);
}
end_t=clock();
printf("Time after displaying output needed...,end_t=%ld\n",end_t);

K.S.AJAY VARSHAAN (71812253006) 2


double total_t = ((double)(clock() - start_t))/CLOCKS_PER_SEC;
printf("Total time taken by CPU: %f\n",total_t);
printf("Exiting of the program...\n");
return 0;
}

OUTPUT:

RESULT:

Thus, program to find to calculate the clock speed of a program for different sizes of inputs was
executed and output was verified successfully.

K.S.AJAY VARSHAAN (71812253006) 3


K.S.AJAY VARSHAAN (71812253006) 4
K.S.AJAY VARSHAAN
(71812253006)
5
EX NO: 2)
STATIC IMPLEMENTATION OF LIST ADT
DATE:

AIM:
To write a C program to perform array operations like insertion, deletion, display, sorting and searching
using switch cases and do while loops.

ALGORITHM:

Step 1: Start
Step 2: Get the number of elements and array elements from the user
Step 3: Print the array before operation and show the list of operations to the user with
respective numbers
Step 4: Get the users choice if it is 1 go to Step 5, If 2 go to Step 8, If 3 go to Step 11, If 4 go to
step 12, If 5 go to Step 14, If 6 go to Step 17
Step 5: Get the element to be inserted from user and the location where the element to be inserted
Step 6: From last index move the element to (i+1)th index until the specified location is reached
Step 7: Now insert the element in that location and print the array
Step 8: Get the element to be deleted from user
Step 9: Compare the element with the elements in the array and move the element to be deleted
out of the array
Step 10: Print the modified array
Step 11: Do step 10
Step 12: Compare the array elements if it is greater than the other element swap the elements using for
loop
Step 13: Do it till the end of the array and go to Step 10
Step 14: Get the search element from the user
Step 15: Compare the elements in the array with the search element, if same print the respective
location of the element
Step 16: Else continue Step 15
Step 17: Get the location from the user
Step 18: Print the element in that location
Step 19: Ask the user whether he wants to continue or stop the operation
Step 20: Continue from Step 4 if user wants to continue or stop the loop

K.S.AJAY VARSHAAN (71812253006) 4


Step 21: Stop

PROGRAM:

#include <stdio.h>
int main() {
int n, i, choice, go_on = 0, swap, count, in, loc, del, j, find;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter array elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("\nArray before operation: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

do {
printf("\nEnter the choice\n");
printf("1. Insert an element\n");
printf("2. Delete an element\n");
printf("3. Display array\n");
printf("4. Sort array\n");
printf("5. Search by element\n");
printf("6. Search by location\n\n");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter an element to insert: ");
scanf("%d", &in);
printf("Enter location to insert: ");
scanf("%d", &loc);
loc--;
for (i = n - 1; i >= loc; i--) {
arr[i + 1] = arr[i];
}
arr[loc] = in;
n++;
break;

case 2:
printf("Enter an element to delete: ");

K.S.AJAY VARSHAAN (71812253006) 5


scanf("%d", &del);
for (i = 0; i < n; i++) {
if (arr[i] == del) {
for (j = i; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
n--;
break;
}
}
break;

case 3:
printf("\nDisplaying array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;

case 4:
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
}
}
printf("\nSorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;

case 5:
printf("Enter an element to search: ");
scanf("%d", &find);
count = -1;
for (i = 0; i < n; i++) {
if (arr[i] == find) {
count = i;
break;
}
}
if (count != -1) {

K.S.AJAY VARSHAAN (71812253006) 6


printf("The element %d is at position %d", find, count + 1);
} else {
printf("Element not found in the array.");
}
break;

case 6:
printf("Enter a location: ");
scanf("%d", &loc);
if (loc >= 1 && loc <= n) {
printf("The element at location %d is: %d", loc, arr[loc - 1]);
} else {
printf("Invalid location.");
}
break;

default:
printf("Sorry! Please read the operations in the above list and give a valid choice.");
break;
}

printf("\nDo you want to continue doing array operations (YES(1)/NO(0)): ");


scanf("%d", &go_on);
} while (go_on == 1);

printf("\nThank you!\n");

return 0;
}

OUTPUT:

K.S.AJAY VARSHAAN (71812253006) 7


RESULT:

Thus, program to find to perform array operations like insertion, deletion, display, sorting and
searching using switch cases and do while loops was executed and output was verified successfully.

K.S.AJAY VARSHAAN (71812253006) 8


K.S.AJAY VARSHAAN (71812253006) 9
K.S.AJAY VARSHAAN
(71812253006)
10
EX NO: 3)
DYNAMIC IMPLEMENTATION OF LIST ADT
DATE:

AIM:

To save and delete an element and check whether the list is empty or not using dynamic implementation.

ALGORITHM:

Step 1: Start
Step 2: Create structure node with element and next pointer as its members
Step 3: Get the choice to perform a specific operation from user
Step 4: If it is 1 do Step 5,6, If 2 do Step 7,8, If 3 do Step 9,10,11, If 4 do Step 12,13, If
5 do step 14,15, If 6 do step 16
Step 5: Create header for the single linked list by allocating memory to the node
Step 6: Assume the element in the header node to be zero, As it is the first node created the next pointer
is NULL
Step 7: Create a node and allocate memory to it
Step 8: Save the element entered by the user in the new node created and save the address of this node in
the next pointer of head C.
Step 9: Get the element to be deleted from the user and Check whether the list is empty or not
Step 10: If not empty, create a variable and save the address of the element to be deleted in it.
Step 11: Stop

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
typedef struct node *ptrtonode;
typedef ptrtonode List;
typedef ptrtonode Position;
struct node
{
int element;
Position next;
};

K.S.AJAY VARSHAAN (71812253006) 9


List CreateHeader();
int IsEmpty(List L);
int IsLast(Position P, List L);
Position Find(int data, List L);
Position FindPrevious(int data, List L);
void Insert(int data, List L, Position P);
void Delete(int data, List L);
void DeleteList(List L);
void DisplayList(List L);

List L;
int main(void)
{
int choice, InsAfterWhich, data;
int x = 1;
Position P;

do
{
printf("1. CREATE HEADER\n2. INSERT NODE\n3. DELETE NODE\n4. DELETE LIST\n5.
DISPLAY LIST\n6. EXIT\n");
printf("Enter your option: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
L = CreateHeader();
break;
case 2:
if (L != NULL)
{
printf("Enter position after which the new node to be inserted: ");
scanf("%d", &InsAfterWhich);
P = Find(InsAfterWhich, L);

K.S.AJAY VARSHAAN (71812253006) 10


printf("Enter the element to be inserted: ");
scanf("%d", &data);
Insert(data, L, P);
}
else
{
printf("Create Header node...\n");
}
break;
case 3:
if (L != NULL)
{
printf("Enter element to be deleted: ");
scanf("%d", &data);
Delete(data, L);
}
else
{
printf("Create Header node...\n");
}
break;
case 4:
if (!IsEmpty(L))
{
DeleteList(L);
}
else
{
printf("No list...\n");
}
break;
case 5:
if (IsEmpty(L))
{
printf("Empty List...\n");

K.S.AJAY VARSHAAN (71812253006) 11


}

DisplayList(L);
break;
case 6:
printf("Do you want to exit (yes(0)/no(1)): ");
scanf("%d", &x);
break;
}
} while (x == 1);

return 0;
}

List CreateHeader()
{
List L;
L = (List)malloc(sizeof(struct node));
if (L == NULL)
{
printf("Memory is not allocated...\n");
return NULL;
}
L->element = 0;
L->next = NULL;
printf("Header created...\n");
return L;
}

void Insert(int data, List L, Position P)


{
List temp = (List)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("Memory is not allocated...\n");
return;
K.S.AJAY VARSHAAN (71812253006) 12
}
temp->element = data;

if (P == NULL) // If position is not found, insert at the end


{
temp->next = NULL;
Position last = L;
while (last->next != NULL)
{
last = last->next;
}
last->next = temp;
}
else
{
temp->next = P->next;
P->next = temp;
}

printf("Element inserted...\n");
}

Position Find(int data, List L)


{
Position P = L;
while (P != NULL && P->element != data)
{
P = P->next;
}
return P;
}

void Delete(int data, List L)


{
if (IsEmpty(L))

K.S.AJAY VARSHAAN (71812253006) 13


{
printf("Empty List...\n");
return;

Position P = FindPrevious(data, L);


if (P != NULL && P->next != NULL)
{
Position temp = P->next;
P->next = temp->next;
free(temp);
printf("Element deleted...\n");
}
else
{
printf("Element not found...\n");
}
}

Position FindPrevious(int data, List L)


{
Position P = L;
while (P != NULL && P->next != NULL && P->next->element != data)
{
P = P->next;
}
return P;
}
int IsEmpty(List L)
{
return L->next == NULL;
}
int IsLast(Position P, List L)
{
return P->next == NULL;
K.S.AJAY VARSHAAN (71812253006) 14
}

void DeleteList(List L)
{

Position P, temp;
P = L->next;
L->next = NULL;
while (P != NULL)
{
temp = P->next;
free(P);
P = temp;
}
printf("List deleted...\n");
}
void DisplayList(List L)
{
Position P = L->next;
printf("\n...LINKED LIST...\n");
while (P != NULL)
{
printf("%d->", P->element);
P = P->next;
}
printf("NULL\n");
}

OUTPUT:

K.S.AJAY VARSHAAN (71812253006) 15


RESULT:

Thus, program to find to save and delete an element and check whether the list is empty or not
using dynamic implementation was executed and output was verified successfully.

K.S.AJAY VARSHAAN (71812253006) 16


K.S.AJAY VARSHAAN (71812253006) 17
K.S.AJAY VARSHAAN
(71812253006)
18
EX NO: 4A)
IMPLEMENTATION OF DOUBLY LINKED LIST
DATE:

AIM:

To write a C program to demonstrate the implementation of Doubly Linked List.

ALGORITHM:

Step 1: Start
Step 2: Create structure node with element and next, prev pointer as its members
Step 3: Get the choice to perform a specific operation from user.
Step 4: If it is 1 do Step 5,6,If 2 do Step 7,8,If 3 do Step 9,10,11,If 4 do Step 12,13,If 5 do Step 14.
Step 5: Create header for the double linked list by allocating memory to the node
Step 6: Assume the element in the header node to be zero, As it is the first node created the next pointers
value is NULL and previous pointer also points NULL
Step 7: Create a node and allocate memory to it
Step 8: Save the element entered by the user in the new node created and save the address of this node in
the next pointer of head and same address in previous pointer of new node
Step 9: Get the element to be deleted from the user and Check whether the list is empty or not
Step 10: If not empty, Create a variable and save the address of the element to be deleted in it
Step 11: Now take the address of the next element and save it in next pointer of the previous element of
the deleted element
Step 12: Display all the elements in the Linked List until the next pointer points NULL which means the
element to be printed is the last element
Step 13: And print the last element separately, Specify the first previous pointer and last next pointer as
NULL
Step 14: Ask the user whether to continue or stop, Take users choice and continue the program if 1 else
stop the program
Step 15: Stop

PROGRAM:

# include <stdio.h>
# include <malloc.h>
# include <conio.h>
typedef struct node *ptrtonode;
K.S.AJAY VARSHAAN (71812253006) 17
typedef ptrtonode List;
typedef ptrtonode Position;
List CreateHeader();
int IsEmpty(List L);
int IsLast(Position P,List L);
Position Find(int data,List L);
Position FindPrevious(int data,List L);
void Insert(int data,List L,Position P);
void Delete(int data,List L);
void DisplayList(List L);
struct node
{
int element;
Position next;
Position prev;
};
List L;
List head=NULL;
void main()
{
int choice,InsAfterWhich,data; int x=1;
Position P;
// clrscr();
do
{
printf(" 1. CREATE HEADER \n 2. INSERT NODE \n 3. DELETE NODE \n 4. DISPLAY LIST \n 5.
EXIT \n");
printf("Enter your option : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
L=CreateHeader();
break;
case 2:
if(L!=NULL)
{
printf("Enter position after which the new node to be inserted : ");
scanf("%d",&InsAfterWhich);
P=Find(InsAfterWhich,L);
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
Insert(data,L,P); break;
}
else
{

K.S.AJAY VARSHAAN (71812253006) 18


printf("Create Header node...\n");
break;
}
case 3:
if(L!=NULL)
{
printf("Enter element to be deleted : ");
scanf("%d",&data);
Delete(data,L);
break;
}
else
{
printf("Create Header node...\n");
break;
}
case 4:
if(IsEmpty(L))
{
printf("Empty List...\n");
break;
}
DisplayList(L);
break;
case 5:
printf("Do you want to exit(yes(0)/no(1)) : \n");
scanf("%d",&x);
break;
}
}while(x==1);
}
List CreateHeader()
{
List L;
L=malloc(sizeof(struct node));
if(L==0)
{
printf("Memory is not allocated...");
return 0;
}
L->element=0;
L->next=NULL;
L->prev=NULL;
printf("Header created...\n");
return L;
}

K.S.AJAY VARSHAAN (71812253006) 19


void Insert(int data,List L,Position P)
{
List temp; temp=(Position)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Memory is not allocated...");
return;
}
temp->element=data;
temp->next=P->next;
P->next=temp;
temp->prev=P;
if(temp->next!=NULL)
{
temp->next->prev=temp;
}
printf("Element inserted...\n");
return;
}
Position Find(int data,List L)
{
Position P;
P=L;
while((P->next!=NULL)&&(P->element!=data))
{
P=P->next;
}
return P;
}
void Delete(int data,List L)
{
Position temp,P;
if(IsEmpty(L))
{
printf("Empty List...");
return;
}
P=Find(data,L);
if(!IsLast(P,L))
{
P->prev->next=P->prev;
P->prev->next=P->next;
free(P);
printf("\nElement deleted...\n");
}
else

K.S.AJAY VARSHAAN (71812253006) 20


{
P->prev->next=NULL;
free(P);
printf("\nElement deleted...\n");
}
}
Position FindPrevious(int data,List L)
{
Position P;
P=L;
while((P->next!=NULL) && (P->next->element!=data))
{
P=P->next;
}
return P;
}
int IsEmpty(List L)
{
if(L->next==NULL)
{
return 1;
}
else
{
return 0;
}
}
int IsLast(Position P,List L)
{
return P->next==NULL;
}
void DisplayList(List L)
{
Position P=L;
printf("\n...LINKED LIST...\n");
printf("NULL<->");
while(P->next!=NULL)
{
printf("%d<->",P->element);
P=P->next;
}
printf("%d<->NULL\n",P->element);
}

K.S.AJAY VARSHAAN (71812253006) 21


OUTPUT:

K.S.AJAY VARSHAAN (71812253006) 22


RESULT:
Thus, the program to demonstrate the implementation of Doubly Linked List was executed and
output was verified successfully.

K.S.AJAY VARSHAAN (71812253006) 23


K.S.AJAY VARSHAAN (71812253006) 24
K.S.AJAY VARSHAAN
(71812253006)
25
EX NO: 5A)
ARRAY IMPLEMENTATION OF STACK ADT
DATE:

AIM:

To write a C program to demonstrate the array implementation of stack ADT.

ALGORITHM:

Step 1: Start
Step 2: Create a stack array and declare top as -1
Step 3: Get the choice to perform a specific operation from user
Step 4: If it is 1 do Step 5,6,7,If 2 do Step 8,9,10,If 3 do Step 11,If 4 do Step 12
Step 5: To push an element check whether a stack is empty
Step 6: If empty push the element entered by the user and increment the top value
Step 7: If not print Overflow
Step 8: Check whether the stack array is empty, If so print Underflow
Step 9: If not do step 10
Step 10: To pop element reduce the value of top
Step 11: Display the element from top to bottom
Step 12: Do the cases until the user wants to stop
Step 13: Stop

PROGRAM:

#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1,temp=0;
void push();
void pop();
void show();
void main ()
{
printf("Enter the number of elements in the stack : ");
scanf("%d",&n);
do
{
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\nEnter your choice: \n");
scanf("%d",&choice);

K.S.AJAY VARSHAAN (71812253006) 32


switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
printf("Do you want to exit yes(1)/no(0)...");
scanf("%d",&temp);
break;
default:
printf("Please Enter valid choice...");
}
}while(temp==0);
}
void push ()
{
int val; if(top==n)
{
printf("\n Overflow...");
}
else
{
printf("Enter the value to insert: ");
scanf("%d",&val);
top=top+1;
stack[top]=val;
printf("\nValue inserted...");
}
}
void pop ()
{
if(top==-1)
{
printf("Underflow...");
}
else
{
top=top-1;
printf("\nElement popped...");
}

K.S.AJAY VARSHAAN (71812253006) 33


}
void show()
{
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty...");
}
}

OUTPUT:

K.S.AJAY VARSHAAN (71812253006) 34


K.S.AJAY VARSHAAN (71812253006) 35
RESULT:

Thus, the program to demonstrate the array implementation of stack ADT was done successfully and the
output is verified.

K.S.AJAY VARSHAAN (71812253006) 36


K.S.AJAY VARSHAAN (71812253006) 37
K.S.AJAY VARSHAAN
(71812253006)
38
EX NO: 5B)

LINKED LIST IMPLEMENTATION OF STACK ADT


DATE:

AIM:
To write a C program to demonstrate the Linked List implementation of stack ADT.

ALGORITHM:

Step 1: Start
Step 2: Create a structure with val next and head pointer as its member
Step 3: Get the choice to perform a specific operation from user
Step 4: If it is 1 do Step 5,6,7,If 2 do Step 8,9,10,If 3 do Step 11,If 4 do Step 12
Step 5: To push an element check whether a stack is empty
Step 6: If empty push the element entered by the user and make it as head
Step 7: If not print Overflow
Step 8: Check whether the stack array is empty,If so print Underflow
Step 9: If not do step 10
Step 10: Make the previous of top element as head
Step 11: Display the element from top to bottom
Step 12: Do the cases until the user wants to stop
Step 13: Stop

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void show();
struct node
{
int val;
struct node *next;
};
struct node *head;
int n,choice,temp=0;
void main ()
K.S.AJAY VARSHAAN (71812253006)
37
{

printf("Enter the number of elements in the stack : ");


scanf("%d",&n);
do
{
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\nEnter your choice: \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
printf("Do you want to exit yes(1)/no(0)...");
scanf("%d",&temp);
break;
default:
printf("Please Enter valid choice...");
}
}while(temp==0);
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("Memory not allocated...");
}
else
{
printf("Enter the value: ");
scanf("%d",&val);
if(head==NULL)
{
ptr->val=val;
ptr->next=NULL;
head=ptr;

K.S.AJAY VARSHAAN (71812253006)


38
}
else

{
ptr->val=val;
ptr->next=head;
head=ptr;
}
printf("Item pushed...");
}
}
void pop()
{
int item;
struct node *ptr;
if (head==NULL)
{
printf("Underflow...");
}
else
{
item=head->val;
ptr=head;
head=head->next;
free(ptr);
printf("Item popped...");
}
}
void show()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty...\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}

K.S.AJAY VARSHAAN (71812253006)


39
}

OUTPUT:

K.S.AJAY VARSHAAN (71812253006)


40
K.S.AJAY VARSHAAN (71812253006)
41
RESULT:
Thus, the program to demonstrate the Linked List implementation of stack ADT was done
successfully and the output is verified.

K.S.AJAY VARSHAAN (71812253006)


42
K.S.AJAY VARSHAAN (71812253006)
43
EX NO:6A)

ARRAY IMPLEMENTATION OF QUEUE ADT


DATE:

AIM:

To write a C program to demonstrate the array implementation of queue ADT

ALGORITHM:

Step 1: Start
Step 2: Create a queue and declare front and rear as -1
Step 3: Get the choice to perform a specific operation from user
Step 4: If it is 1 do Step 5,6,7,If 2 do Step 8,9,10,If 3 do Step 11,If 4 do Step 12
Step 5: To push an element check whether a queue is empty
Step 6: If empty push the element entered by the user at the end that is rear
Step 7: If not print Overflow
Step 8: Check whether the queue array is empty,If so print Underflow
Step 9: If not do step 10
Step 10: Remove element from front
Step 11: Display the element from top to bottom
Step 12: Do the cases until the user wants to stop
Step 13: Stop

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void enqueue();
void dequeue();
void display();
int front=-1,rear=-1,temp=1;
int queue[maxsize];
void main ()
{

K.S.AJAY VARSHAAN (71812253006) 42


int choice;
do
{
printf("\n1.Insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Do you want to exit yes(0)/no(1): ");
scanf("%d",&temp);
break;
default:
printf("\nEnter valid choice...\n");
}
}while(temp==1);
}
void enqueue()
{
int item;
printf("Enter the element: \n");
scanf("%d",&item);
if(rear==maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front==-1 && rear==-1)
K.S.AJAY VARSHAAN (71812253006) 43
{
front=0;
rear=0;
}
else
{
rear=rear+1;
}
queue[rear]=item;
printf("\nValue inserted... ");
}
void dequeue()
{
int item;
if(front==-1||front>rear)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
item=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=front+1;
}
printf("\nValue deleted. ");
}
}
void display()
{
int i;
K.S.AJAY VARSHAAN (71812253006) 44
if(rear==-1)
{
printf("\nEmpty queue\n");
}
else
{
printf("\nPrinting values \n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}

OUTPUT:

K.S.AJAY VARSHAAN (71812253006) 45


K.S.AJAY VARSHAAN (71812253006) 46
RESULT:
Thus, the program to demonstrate the Array implementation of queue ADT was done successfully
and the output is verified.

K.S.AJAY VARSHAAN (71812253006) 47


K.S.AJAY VARSHAAN (71812253006) 48
K.S.AJAY VARSHAAN
(71812253006)
49
EX NO: 6B)

LINKED LIST IMPLEMENTATION OF QUEUE ADT


DATE:

AIM:
To write a C program to demonstrate the Linked List implementation of Queue ADT.

ALGORITHM:

Step 1: Create a class QNode with data members integer data and QNode* next
Step 2: A parameterized constructor that takes an integer x value as a parameter and sets data
equal to x and next as NULL
Step 3: Create a class Queue with data members QNode front and rear
Step 4: Enqueue Operation with parameter x:
Step 5: Initialize QNode* temp with data = x
Step 6: If the rear is set to NULL then set the front and rear to temp and return(Base Case)
Step 7: Else set rear next to temp and then move rear to temp
Step 8: Dequeue Operation:
Step 9: If the front is set to NULL return(Base Case)
Step 10: Initialize QNode temp with front and set front to its next
Step 11: If the front is equal to NULL then set the rear to NULL
Step 12: Delete temp from the memory

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
K.S.AJAY VARSHAAN (71812253006) 48
{

printf("\n**********Main Menu**********\n");

printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");


printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;

K.S.AJAY VARSHAAN (71812253006) 49


}
}

}
void delete ()

struct node *ptr;


if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

OUTPUT:

K.S.AJAY VARSHAAN (71812253006) 50


K.S.AJAY VARSHAAN (71812253006) 51
RESULT:

Thus, the program to demonstrate the Linked List implementation of queue ADT was done
successfully and the output is verified.

K.S.AJAY VARSHAAN (71812253006) 52


K.S.AJAY VARSHAAN (71812253006) 53
K.S.AJAY VARSHAAN
(71812253006)
54
EX NO:7)
HASHING
DATE:

AIM:

To write a C program to implement hash using array.

ALGORITHM:

Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Initialize all the elements of the array as -1 to depict the space as empty
Step 4: Get the choice to perform a specific operation from user
Step 5: If it is 1 do Step 6,7,8,9, If 2 do Step 10,11,If 3 do Step 12,13,14,If 4 do Step 15,If 5 do Step 16
Step 6: Key is the remainder of value and size of array
Step 7: To insert an element,check if the key position is -1
Step 8: If -1 insert the element entered by the user
Step 9: Else if collision, Check for the next position and go to step 7
Step 10: To delete do step 6 and check if the value entered and value stored are equal
Step 11: If yes, change the value to -1. Now take the address of the next element and save it in next
pointer of the previous element of the deleted element
Step 12: To search do step 6 and check if the value entered and value stored are equal
Step 13: If yes, Print the key of the element
Step 14: Else, print not found
Step 15: Display all the elements of the array using for loop
Step 16: Exit the do while loop
Step 17: Stop

PROGRAM:

#include<stdio.h>
void init(int size,int arr[])
{
int i; for(i=0;i<size;i++)
{
arr[i]=-1;
}
}
K.S.AJAY VARSHAAN (71812253006) 53
void insert(int value,int size,int arr[])
{
int key=value%size; if(arr[key]==-1)
{
arr[key]=value;
printf("%d inserted at arr[%d]\n", value,key);
}
else
{
printf("Collision : arr[%d] has element %d already!\n",key,arr[key]); printf("Unable to insert
%d\n",value);
printf("Finding next free position...\n"); for(int i=key;i<size;i++)
{
if(arr[i]==-1)
{
arr[i]=value;
printf("%d inserted at arr[%d]\n", value,i); break;
}
}

}
}
void delete(int value,int size,int arr[])
{
int key=value%size; if(arr[key]==value)
{
printf("%d element deleted from position %d\n",value,key); arr[key]=-1;
}
else
{
printf("%d not present in the hash table\n",value);
}
}
void search(int value,int size,int arr[])
{

int key=value%size; if(arr[key]==value)


{
printf("Search Found...\nElement %d found at position %d...\n",value,key);
}
else
{
printf("Search Not Found...\nElement not found in hash table...\n");
}
}
void print(int size,int arr[])
{
K.S.AJAY VARSHAAN (71812253006) 54
int i; for(i=0;i<size;i++)
{
printf("arr[%d] = %d\n",i,arr[i]);
}
}
int main()
{
int temp=0,choice,element,size; printf("Enter the size of array: "); scanf("%d",&size);
int arr[size]; init(size,arr); do
{
printf("\n...OPERATIONS ON HASH TABLE...\n");
printf("1.INSERT ELEMENT\n");
printf("2.DELETE ELEMENT\n");
printf("3.SEARCH ELEMENT\n");
printf("4.PRINT HASH TABLE\n");
printf("5.EXIT \n");
printf("Enter your choice: ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("Enter an element to insert: "); scanf("%d",&element); insert(element,size,arr);
break; case 2:
printf("Enter an element to delete: "); scanf("%d",&element); delete(element,size,arr);
break; case 3:
printf("Enter an element to search: "); scanf("%d",&element); search(element,size,arr);
break; case 4:
printf("\n...HASH TABLE...\n"); print(size,arr);
printf("\n"); break;
case 5:
printf("\nDo you want to exit (yes-1)/(no-0): "); scanf("%d",&temp);
break;
}
}while(temp==0); return 0;
}

OUTPUT:

K.S.AJAY VARSHAAN (71812253006) 55


K.S.AJAY VARSHAAN (71812253006) 56
RESULT:
Thus, the program to demonstrate the implementation of hash using array was done successfully
and the output is verified.

K.S.AJAY VARSHAAN (71812253006) 57


K.S.AJAY VARSHAAN (71812253006) 58
K.S.AJAY VARSHAAN
(71812253006)
59
EX NO:8)
IMPLEMENTATION OF LINEAR AND BINARY SEARCH
ARRAY ADT
DATE:

AIM:

To write a C program to implement linear and binary search using array ADT.

ALGORITHM:

Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Get elements of the array from user
Step 4: Get the choice to perform a specific operation from user
Step 5: If it is 1 do Step 6,7,8,9,If 2 do Step 10,11,12,13,14,15If 3 do Step 16,If 4 do Step 17
Step 6: Call LinearSearch() function
Step 7: Start from the first element and compare every element with search element
Step 8: If values match, Print element found
Step 9: Else, Print element not found and return to Step 5
Step 10: Call BinarySearch() function and sort elements
Step 11: Find the middle element, Compare the middle element and search element
Step 12: If same, Print element found and return to Step 5
Step 13: If greater, Find the middle element to the left of middle element (0-mid) and do
step 11
Step 14: If lesser, Find the middle element to the right of middle element(mid-n) and do Step 11
Step 15: Else, print element not found and return to Step 5.
Step 16: Display elements on the array
Step 17: Exit the loop
Step 18: Stop

PROGRAM:

#include<stdio.h>
void LinearSearch(int arr[],int,int); int BinarySearch(int arr[],int,int,int); int main()

{
int n,choice,search,search1,x=1,temp; printf("Enter number of elements in an array: "); scanf("%d",&n);
int arr[n];
printf("Enter %d elements of the array: \n",n); for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
do
{
printf("...ARRAY SEARCHING TECHNIQUES...\n");
printf("1.LINEAR SEARCH\n2.BINARY SEARCH\n3.DISPLAY\n4.EXIT\n");
K.S.AJAY VARSHAAN (71812253006) 58
printf("Your choice: "); scanf("%d",&choice); switch(choice)
{
case 1:
printf("\nEnter an element to search: "); scanf("%d",&search); LinearSearch(arr,n,search);
break; case 2:
printf("\nEnter an element to search: "); scanf("%d",&search1);
printf("\nArray Sorting..."); for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i]; arr[i]=arr[j]; arr[j]=temp;
}
}
}
for(int i=0;i<n;i++)

{
printf("%d ",arr[i]);
}
int r=BinarySearch(arr,0,n-1,search1); if(r==-1)
{
printf("\n%d element not found...\n\n",search1);
}
break; case 3:
printf("\n...ARRAY...\n"); for(int i=0;i<n;i++)
{
printf("ARRAY[%d] = %d\n",i,arr[i]);
}
break; case 4:
printf("Do you want to exit(yes 0/no 1): "); scanf("%d",&x);
break;
}
}while(x==1);
}
void LinearSearch(int arr[],int n,int search)
{
int flag;
for(int i=0;i<n;i++)
{
if(arr[i]==search)
{
printf("\n%d element found at index %d\n\n",search,i); flag=1;
break;
}
}
if(flag!=1)
{
printf("%d element not found...\n\n",search);
}
}

int BinarySearch(int arr[],int l,int n,int search)


{
K.S.AJAY VARSHAAN (71812253006) 59
int mid; if(n>=l)
{
int mid=l+(n-l)/2; if(arr[mid]==search)
{
printf("\n%d element found at index %d\n\n",search,mid); return mid;
}
if(arr[mid]>search)
{
return BinarySearch(arr,l,mid-1,search);
}
if(arr[mid]<search)
{
return BinarySearch(arr,mid+1,n,search);
}
}
return -1;
}

OUTPUT:

K.S.AJAY VARSHAAN (71812253006) 60


RESULT:
Thus, the program to demonstrate the implementation of linear search and binary search using
Array ADT was executed successfully and the output is verified.

K.S.AJAY VARSHAAN (71812253006) 61


K.S.AJAY VARSHAAN (71812253006) 62
K.S.AJAY VARSHAAN
(71812253006)
63
EX NO: 9)
IMPLEMENTATION OF INSERTION SORT USING ARRAY
DATE: ADT

AIM:
To write a C program to implement Insertion sort using array ADT.

ALGORITHM:
Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Get elements of the array from user
Step 4: Iterate from arr[i] to arr[n] over the array
Step 5: Assume the first element to be sorted, Store next element as key
Step 6: Now compare the key with all the elements in the array
Step 7: If the element is smaller than the current element move to the next element
Step 8: Else, shift greater elements in the array towards right
Step 9: Insert the element
Step 10: Continue from Step 5, Until the array is sorted
Step 11: Stop

PROGRAM:
# #include&lt;stdio.h&gt;
void display(int,int arr[]);
void main()
{
int n,key,j;
printf(&quot;Enter number of elements: &quot;);
scanf(&quot;%d&quot;,&amp;n);
int arr[n];
printf(&quot;Enter array elements: \n&quot;);
for(int i=0;i&lt;n;i++)
{
scanf(&quot;%d&quot;,&amp;arr[i]);
}
printf(&quot;\n...INSERTION SORT...\n&quot;);
for(int i=1;i&lt;n;i++)
{

K.S.AJAY VARSHAAN (71812253006)


62
key=arr[i];
j=i-1;
while(j&gt;=0 &amp;&amp; arr[j]&gt;key)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
display(n,arr);
}
printf(&quot;\n...SORTED ELEMENTS...\n&quot;);
display(n,arr);
}
void display(int n,int arr[])
{
for(int i=0;i&lt;n;i++)
{
printf(&quot;%d &quot;,arr[i]);
}
printf(&quot;\n&quot;);
}

OUTPUT:

K.S.AJAY VARSHAAN (71812253006)


63
RESULT:
Thus, the program to demonstrate the implementation of insertion sort using Array ADT was
executed successfully and the output is verified.
K.S.AJAY VARSHAAN (71812253006)
64
K.S.AJAY VARSHAAN (71812253006)
65
K.S.AJAY VARSHAAN (71812253006)
66
EX NO:10

IMPLEMENTATION OF BUBBLE SORT USING ARRAY


DATE: ADT

AIM:

To write a C program to implement bubble sort using array ADT.

ALGORITHM:

Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Get elements of the array from user
Step 4: Iterate from arr[i] to arr[n] over the array
Step 5: Compare adjacent elements of the array
Step 6: If current element is greater than the next element, swap its position and continue
Step 7: Else, move to next element
Step 8: Continue from Step 4, Until the array gets sorted
Step 9:Stop

PROGRAM:

#include <stdio.h>
void printArray(int arr[],int); void swap(int *x, int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
void bubbleSort(int arr[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{

K.S.AJAY VARSHAAN (71812253006) 65


if(arr[j]>arr[j+1])
{
swap(&arr[j],&arr[j+1]); printArray(arr,n);
}
}
}
}
void printArray(int arr[],int size)
{
int i;
for (i=0;i<size;i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int n;
printf("Enter number of elements: "); scanf("%d",&n);
int arr[n];
printf("Enter array elements: \n"); for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\n...BUBBLE SORT...\n"); bubbleSort(arr,n);
printf("\nSorted array \n"); printArray(arr,n);
return 0;
}

K.S.AJAY VARSHAAN (71812253006) 66


OUTPUT:

RESULT:

Thus, the program to demonstrate the implementation of bubble sort using Array ADT was executed
successfully and the output is verified.

K.S.AJAY VARSHAAN (71812253006) 67


K.S.AJAY VARSHAAN (71812253006) 68
K.S.AJAY VARSHAAN
(71812253006)
69
EX NO: 11)
IMPLEMENTATION OF QUICK SORT USING ARRAY ADT
DATE:

AIM:
To write a C program to implement quick sort using array ADT.

ALGORITHM:
Step 1: Start
Step 2: Choose the highest index value has pivot
Step 3: Take two variables to point left and right of the list excluding pivot Step 4: Move left points to
the low index
Step 5: Move right points to the high index
Step 6: While value at left is less than pivot move right Step 7: While value at right is greater than pivot
move left
Step 8: If both step 5 and step 6 does not match swap left and right
Step 9: If left ≥ right, the point where they met is new pivot
Step 10: Stop

PROGRAM:
#include <stdio.h>
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int array[], int low, int high)

{
int pivot=array[high]; int i=(low-1);
for(int j=low;j<high;j++)
{
if(array[j]<=pivot)
{
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i+1], &array[high]); return(i+1);
}
void quickSort(int array[], int low, int high)
{
if(low<high)
{
int pi=partition(array, low, high); quickSort(array, low, pi - 1); quickSort(array, pi + 1, high);
}
}
K.S.AJAY VARSHAAN (71812253006) 68
void printArray(int array[], int size)
{
for(int i=0;i<size;i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int n;
printf("Enter size of the array: "); scanf("%d",&n);
int data[n];
printf("Enter the elements of the array:\n"); for(int i=0;i<n;i++)
{

scanf("%d",&data[i]);
}
printf("Unsorted Array\n"); printArray(data, n); quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n"); printArray(data, n);
}

OUTPUT:

RESULT:

Thus, the program to demonstrate the implementation of Quick sort using Array ADT was executed
successfully and the output was verified.

K.S.AJAY VARSHAAN (71812253006) 69


K.S.AJAY VARSHAAN
(71812253006)
70
EX NO: 11)
IMPLEMENTATION OF QUICK SORT USING ARRAY ADT
DATE:

AIM:
To write a C program to implement quick sort using array ADT.

ALGORITHM:
Step 1: Start
Step 2: Choose the highest index value has pivot
Step 3: Take two variables to point left and right of the list excluding pivot Step 4: Move left points to
the low index
Step 5: Move right points to the high index
Step 6: While value at left is less than pivot move right Step 7: While value at right is greater than pivot
move left
Step 8: If both step 5 and step 6 does not match swap left and right
Step 9: If left ≥ right, the point where they met is new pivot
Step 10: Stop

PROGRAM:
#include <stdio.h>
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int array[], int low, int high)

{
int pivot=array[high]; int i=(low-1);
for(int j=low;j<high;j++)
{
if(array[j]<=pivot)
{
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i+1], &array[high]); return(i+1);
}
void quickSort(int array[], int low, int high)
{
if(low<high)
{
int pi=partition(array, low, high); quickSort(array, low, pi - 1); quickSort(array, pi + 1, high);
}
}
K.S.AJAY VARSHAAN (71812253006) 68
void printArray(int array[], int size)
{
for(int i=0;i<size;i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int n;
printf("Enter size of the array: "); scanf("%d",&n);
int data[n];
printf("Enter the elements of the array:\n"); for(int i=0;i<n;i++)
{

scanf("%d",&data[i]);
}
printf("Unsorted Array\n"); printArray(data, n); quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n"); printArray(data, n);
}

OUTPUT:

RESULT:

Thus, the program to demonstrate the implementation of Quick sort using Array ADT was executed
successfully and the output was verified.

K.S.AJAY VARSHAAN (71812253006) 69


K.S.AJAY VARSHAAN
(71812253006)
70
EX NO: 12)

IMPLEMENTATION OF MERGE SORT USING ARRAY


DATE: ADT

AIM:

To write a C program to implement merge sort using array ADT.

ALGORITHM:

Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Get elements of the array from user
Step 4: If it is only one element it is already sorted, return
Step 5: Divide the array iteratively in equal halves until atomic values are achieved
Step 6: Merge the small lists exactly in the same way they were broken down
Step 7: Compare the elements in each list and combine them in a sorted manner into a new list
Step 8: Print the merged list after each recursion
Step 9: Print the final sorted array
Step 10: Stop

PROGRAM:

#include <stdio.h>
void printArray(int arr[],int);
void merge(int arr[], int start, int mid, int end)
{
int n1=mid-start+1; int n2=end-mid;
int first_half[n1],second_half[n2]; for(int i=0;i<n1;i++)
{
first_half[i]=arr[start+i];
}
for(int j=0;j<n2;j++)
second_half[j]=arr[mid+1+j];
}
int i, j, k; i=0;
j=0;
k=start;
while (i<n1&&j<n2)
{
if (first_half[i]<=second_half[j])
{
arr[k]=first_half[i]; i++;

K.S.AJAY VARSHAAN (71812253006) 70


}
else
{
arr[k]=second_half[j]; j++;
}
k++;
}
while(i<n1)
{
arr[k]=first_half[i]; i++;
k++;
}
while(j<n2)
{
arr[k]=second_half[j]; j++;
k++;
}
printf("MERGE SORT\n"); printArray(arr,k);
}
void mergeSort(int arr[],int beg,int end)
{
if (beg<end)
{
int mid=beg+(end-beg)/2;

mergeSort(arr, beg, mid); mergeSort(arr, mid + 1, end); merge(arr, beg, mid, end);
}
}
void printArray(int arr[], int size)
{
for (int i=0;i<size;i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int size;
printf("Enter the size of the array: "); scanf("%d",&size);
int arr[size];
printf("Enter array elements: \n"); for(int i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
mergeSort(arr, 0, size-1); printf("Sorted array: \n"); printArray(arr, size);
}

K.S.AJAY VARSHAAN (71812253006) 71


OUTPUT:

RESULT:
Thus, the program to demonstrate the implementation of merge sort using Array ADT was
executed successfully and the output was verified.

K.S.AJAY VARSHAAN (71812253006) 72


K.S.AJAY VARSHAAN (71812253006) 73
K.S.AJAY VARSHAAN
(71812253006)
74

You might also like