U18csi6211-Dsa Lab - Manual
U18csi6211-Dsa Lab - Manual
Certificate
___________________________________________________laboratory.
Faculty In charge
________________
Examiner I Examiner II
PROGRAM EDUCATIONAL OBJECTIVES (PEOs):
PSO1: Apply the knowledge acquired in Electrical and Electronics Engineering to technological
advancements.
PSO2: Identify suitable solutions for design and control of electrical and electronic systems.
2
INDEX
1
Array implementation of stack
2
Linked implementation stack
3
Implementation of singly linked List
4
Implementation of doubly linked List
5
Implementation of circular Queue
9
Implement Heap Sort
10
Implement Quick Sort
3
Experiment No: Date:
AIM:
To implement stack using array and to perform the basic operations PUSH () and POP ().
ALGORITHM
PUSH OPERATION
Step 1: Start
Step 2: Declare Stack [MAX]; //Maximum size of Stack
Step 3: Check if the stack is full or not by comparing top with (MAX-1).If the stack is full, Then
print "Stack Overflow" i.e, stack is full and cannot be pushed with another element
Step 4: Else, the stack is not full. Increment top by 1 and Set, a[top] = x which pushes the element x
into the address pointed by top // The element x is stored in a[top]
Step 5: Stop
POP OPERATION
Step 1: Start
Step 2: Declare Stack[MAX]
Step 3: Push the elements into the stack
Step 4: Check if the stack is empty or not by comparing top with base of array i.e 0. If top is less
than 0, then stack is empty, print "Stack Underflow”.
Step 5: Else, If top is greater than zero the stack is not empty, then store the value pointed by top in
a variable x=a[top] and decrement top by 1. The popped element is x
DISPLAY OPERATION
Step 1: Start
Step 2: Declare Stack [MAX]
Step 3: Push the elements into the stack.
Step 4: Print the value stored in the stack pointed by top.
Step 6: Stop
PROGRAM:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
4
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
5
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
INFERENCE:
RESULT:
Thus, the array implementation of stack is performed and the output is verified
6
Experiment No: Date:
AIM:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
7
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
8
}
}
}
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
10
INFERENCE:
Using an array will put a restriction on the maximum capacity of the array which can lead to stack
overflow. Using a linked list over arrays is that it is possible to implement a stack that can shrink or
grow as much as needed.
RESULT:
Thus, the implementation of stack using Linked List is performed and the output is verified
11
Experiment No: Date:
AIM:
PROGRAM:
#include<stdio.h>
#include<malloc.h>
int main()
{
printf("\n------------------------------\n");
switch(user_choice)
{
13
case 1:
printf("\nInserting a node at beginning");
data = getData();
insert_at_beginning(data);
break;
case 2:
printf("\nInserting a node at end");
data = getData();
insert_at_end(data);
break;
case 3:
printf("\nInserting a node at the given position");
data = getData();
position = getPosition();
insert_at_position(data, position);
break;
case 4:
printf("\nDeleting a node from beginning\n");
delete_at_beginning();
break;
case 5:
printf("\nDeleting a node from end\n");
delete_at_end();
break;
case 6:
printf("\nDelete a node from given position\n");
position = getPosition();
delete_at_position(position);
break;
case 7:
printf("\nPrinting the list from beginning\n\n");
print_from_beginning();
break;
case 8:
printf("\nPrinting the list from end\n\n");
print_from_end(head);
break;
case 9:
printf("\nSearching the node data");
data = getData();
search_data(data);
break;
case 10:
printf("\nUpdating the node data");
data = getData();
position = getPosition();
14
update_node_data(data, position);
break;
case 11:
printf("\nProgram was terminated\n\n");
return 0;
default:
printf("\n\tInvalid Choice\n");
}
printf("\n...............................\n");
printf("\nDo you want to continue? (Y/N) : ");
fflush(stdin);
scanf(" %c", &user_active);
}
return 0;
}
/*
* Function will show an empty list message
*/
void empty_message()
{
printf("\n\tList is Empty!\n");
}
/*
* Function is used to show the memory allocation failure
*/
void memory_message()
{
printf("\nMemory can't be allocated\n");
}
/*
* Creates a new node and returns the address of that node
*/
struct node* create_node(int data)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
if(new_node == NULL)
{
memory_message();
return NULL;
}
new_node->data = data;
new_node->next = NULL;
return new_node;
}
/*
15
* Insert the new node at the beginning of the list
*/
void insert_at_beginning(int data)
{
struct node* new_node = NULL;
new_node = create_node(data);
if(new_node != NULL)
{
new_node->next = head;
head = new_node;
printf("\n* Node with data %d was Inserted\n", data);
}
}
/*
* Insert the new node at the end of the list
*/
void insert_at_end(int data)
{
struct node* new_node = NULL;
new_node = create_node(data);
if(new_node != NULL)
{
//if list is empty
if(head == NULL)
{
head = new_node;
}
else
{
struct node* last = head;
/*
* Insert the new node at the given position
*/
void insert_at_position(int data, int pos)
{
//calculate the size of the list
int list_size = 0;
16
list_size = size_of_list();
//if the list is empty and the position is greater than the 1
if(head == NULL && (pos <= 0 || pos > 1))
{
printf("\nInvalid position to insert a node\n");
return;
}
if(new_node != NULL)
{
struct node* temp = head;
/*
* Delete the node from the beginning of the list
*/
void delete_at_beginning()
{
if(head == NULL)
{
empty_message();
17
return;
}
/*
* Delete the node from the ending of the list
*/
void delete_at_end()
{
if(head == NULL)
{
empty_message();
return;
}
data = temp->data;
else
{
free(temp);
prev->next = NULL;
}
printf("\n* Node with data %d was Deleted\n", data);
}
/*
18
* Deleting the node from the given position
*/
void delete_at_position(int pos)
{
//calculate the size of the list
int list_size = 0;
list_size = size_of_list();
if(temp == head)
{
head = head->next;
free(temp);
}
else
{
prev->next = temp->next;
free(temp);
}
printf("\n* Node with data %d was Deleted\n", data);
/*
* Search the node with given data in the list
*/
void search_data(int data)
{
int position = 0;
int flag = 0;
while(temp != NULL)
{
19
position += 1;
if(temp->data == data)
{
flag = 1;
break;
}
temp = temp->next;
}
if(flag == 0)
{
printf("\nNode with data %d was not found!\n", data);
}
else
{
printf("\nFound data at %d position\n", position);
}
}
/*
* Update the node with the given new data
*/
void update_node_data(int new_data, int pos)
{
//calculate the size of the list
int list_size = 0;
list_size = size_of_list();
temp->data = new_data;
printf("\nUpdated node data is %d\n", new_data);
}
/*
* Prints the data from the start of the list
*/
void print_from_beginning()
{
if(head == NULL)
{
20
empty_message();
return;
}
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
/*
* Prints the list from the end of the list
*/
void print_from_end(struct node* head)
{
if(head == NULL)
{
return;
}
print_from_end( head->next );
printf("%d ", head->data);
}
/*
* Returns the size of the list
*/
int size_of_list()
{
struct node* temp = head;
int count = 0;
while(temp != NULL)
{
count += 1;
temp = temp->next;
}
return count;
}
/*
* Getting node data from the user
*/
int getData()
{
int data;
printf("\n\nEnter Data: ");
scanf("%d", &data);
return data;
}
/*
21
* Getting the position of the node from the user
*/
int getPosition()
{
int pos;
return pos;
}
INFERENCE:
Singly linked lists are fundamental data structures that strike a balance between efficiency and
flexibility. While they excel in dynamic size management and efficient insertions/deletions, they do
come with limitations, such as a lack of backward traversal and relatively inefficient access times.
RESULT:
Thus, the implementation of singly linked list using C is performed and the output is verified.
22
Experiment No: Date:
AIM:
PROGRAM 1:
/* C Program to Implement Doubly Linked List using Singly Linked List */
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
23
void display(struct node *);
int main()
{
struct node *p = NULL, *q = NULL;
int result, count;
return 0;
}
p = q = head;
printf("\nPointer at %d\n", head->num);
do
{
printf("Select option:\n1. Move front\n2. Move back\n3. Exit\nYour choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: if(q->next != NULL)
{
q = q->next;
printf("\nPointer at %d\n", q->num);
}
else
{
printf("\nPointer at last node %d. Cannot move ahead.\n", q->num);
}
break;
case 2: while (p->next != q)
{
p = p->next;
}
if (p == q)
{
printf("\nPointer at first node %d. Cannot move behind.\n", q->num);
}
else
{
q = p;
p = head;
printf("\nPointer at %d\n", q->num);
}
break;
24
case 3: return;
default: printf("\nInvalid choice entered. Try again\n");
}
} while (1);
}
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}
PROGRAM 2:
25
/ * C program to Implement the Doubly Linked List and its Operations */
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node* prev;
int data;
struct node* next;
};
/* functions prototyping */
void insert_at_beginning(int);
void insert_at_end(int);
void insert_at_position(int, int);
void delete_from_beginning();
void delete_from_position(int);
void delete_from_end();
void print_from_beginning();
void print_from_end(struct node*);
void search_data(int);
void update_node_data(int, int);
void list_sort();
/* helper functions */
struct node* create_node(int);
int size_of_list();
int getData();
int getPosition();
void empty_list_message();
void memory_error_message();
void invalid_position_message();
int main()
{
char user_active = 'Y';
int user_choice;
int data, position;
printf("\n------------------------------\n");
switch(user_choice)
{
case 1:
printf("\nInserting a node at beginning");
data = getData();
insert_at_beginning(data);
break;
case 2:
printf("\nInserting a node at end");
data = getData();
insert_at_end(data);
break;
case 3:
printf("\nInserting a node at the given position");
data = getData();
position = getPosition();
insert_at_position(data, position);
break;
case 4:
printf("\nDeleting a node from beginning\n");
delete_from_beginning();
break;
case 5:
printf("\nDeleting a node from end\n");
delete_from_end();
break;
case 6:
printf("\nDelete a node from given position\n");
position = getPosition();
delete_from_position(position);
break;
case 7:
printf("\nPrinting the list from beginning\n\n");
print_from_beginning();
break;
case 8:
printf("\nPrinting the list from end\n\n");
print_from_end(head);
27
printf("NULL\n");
break;
case 9:
printf("\nSearching the node data");
data = getData();
search_data(data);
break;
case 10:
printf("\nUpdating the node data");
data = getData();
position = getPosition();
update_node_data(data, position);
break;
case 11:
printf("\nSorting the list\n");
list_sort();
break;
case 12:
printf("\nProgram was terminated\n\n");
return 0;
default:
printf("\n\tInvalid Choice\n");
}
printf("\n...............................\n");
printf("\nDo you want to continue? (Y/N) : ");
fflush(stdin);
scanf(" %c", &user_active);
}
return 0;
}
/* prints the message when the position is not valid for operation*/
void invalid_position_message()
{
printf("\nInvalid position!\n");
}
if (new_node == NULL)
{
return NULL;
}
else
{
new_node->prev = NULL;
new_node->data = data;
new_node->next = NULL;
}
}
if (new_node == NULL)
{
memory_error_message();
return;
}
else if(head == NULL)
{
head = new_node;
}
else
{
head->prev = new_node;
new_node->next = head;
head = new_node;
}
printf("\n* Node with data %d was inserted \n", data);
}
if (new_node == NULL)
{
memory_error_message();
return;
}
else if (head == NULL)
{
head = new_node;
}
29
else
{
struct node* temp = head;
//traverse to the last node
while (temp->next != NULL)
{
temp = temp = temp->next;
}
temp->next = new_node;
new_node->prev = temp;
}
printf("\n* Node with data %d was inserted \n", data);
}
if (new_node == NULL)
{
memory_error_message();
return;
}
else if (head != NULL && (pos < 1 || pos > size))
{
invalid_position_message();
return;
}
else if (head == NULL && pos == 1)
{
head = new_node;
}
else if (head != NULL && pos == 1)
{
new_node->next = head;
head->prev = new_node;
head = new_node;
}
else
{
temp->next->prev = new_node;
new_node->next = temp->next;
30
temp->next = new_node;
new_node->prev = temp;
}
printf("\n* Node with data %d was inserted \n", data);
}
if (head == NULL)
{
empty_list_message();
return;
}
struct node* temp = head;
int data = 0;
if (temp->prev == NULL)
{
head = NULL;
}
else
{
temp->prev->next = temp->next;
}
data = temp->data;
free(temp);
printf("\n* Node with data %d was deleted \n", data);
}
31
/* deletes a node from the given position */
void delete_from_position(int pos)
{
if (head == NULL)
{
empty_list_message();
return;
}
int size = size_of_list();
struct node* temp = head;
int data = 0;
// if deleting the last node then just update the previous node
if (pos != size)
{
//update next node
temp->next->prev = temp->prev;
}
data = temp->data;
//free memory
free(temp);
print_from_end(head->next);
printf("%d ", head->data);
}
if (flag == 0)
{
printf("\nNode with data %d was not found\n", data);
}
else
{
printf("\nNode found at %d position\n", position);
}
}
temp->data = data;
return data;
}
return position;
}
return count;
}
INFERENCE:
A Doubly linked list is a bidirectional linked list. Traversal the list from head to tail node or tail to head
node. Unlike singly linked lists, its node has an extra pointer that points at the last node.
RESULT:
Thus, the implementation of singly linked list using C is performed and the output is verified.
35
Experiment No: Date:
AIM:
To implement circular queue using C and to perform enqueue and dequeue operations.
ALGORITHM
ENQUEUE OPERATION
Step 1 - Check if the queue is full
Step 2 - Set value of FRONT to 0, for the first element
Step 3 - Circularly increase the REAR index by 1 (i.e. if the rear reaches the end, next it would be
at the start of the queue)
Step 4 - Add the new element in the position pointed to by REAR
DEQUEUE OPERATION
Step 1 - Check if the queue is empty
Step 2 - Return the value pointed by FRONT
Step 3 - Circularly increase the FRONT index by 1
Step 4 - For the last element, reset the values of FRONT and REAR to -1
PROGRAM:
#include <stdio.h>
# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}
switch(choice)
{
case 1:
}}
return 0;
}
INFERENCE:
The circular queue is a linear data structure whose end is connected to the start and is used in the traffic
system, memory management, and CPU scheduling.
RESULT:
Thus, the circular queue is implemented using C and the output is verified.
38
Experiment No: Date:
AIM:
ALGORITHM
PROGRAM:
INFERENCE:
RESULT:
39
Experiment No: Date:
AIM:
ALGORITHM:
PROGRAM:
INFERENCE:
RESULT:
40
Experiment No: Date:
AIM:
ALGORITHM:
PROGRAM:
INFERENCE:
RESULT:
41
Experiment No: Date:
AIM:
ALGORITHM:
PROGRAM:
INFERENCE:
RESULT:
42
Experiment No: Date:
AIM:
ALGORITHM:
PROGRAM:
;
INFERENCE:
RESULT:
43