Dsa File
Dsa File
Dsa File
FILE (INITC201)
SEMESTER -2
ITNS
Group-1
BY -
AVANI AGNIHOTRI
2023UIN2527
PROBLEM-1 :
Write a program to implement two stacks in an array.
CODE –
/ Program to implement two stacks in an
array #include <iostream>
#include <stdlib.h>
class twoStacks {
int* ar;
int size;
public:
twoStacks(int n)
size = n;
ar = new int[n];
top1 = -1;
top2 = size;
void push1(int x)
ar[top1] = x;
}
else {
exit(1);
void push2(int x)
top2--;
ar[top2] = x;
else {
exit(1);
int pop1()
if (top1 >= 0) {
int x = ar[top1];
top1--;
return x;
else {
}
}
int pop2()
int x = ar[top2];
top2++;
return x;
else {
exit(1);
};
int main()
twoStacks ts(5);
ts.push1(5);
ts.push2(10);
ts.push2(16);
ts.push1(13);
ts.push2(6);
< ts.pop1();
ts.push2(12);
return 0;
}
OUTPUT –
PROBLEM-2:
Write a program to implement stack using Linked List.
CODE -
#include <iostream>
#include <stdlib.h>
struct node
int data;
node* next;
};
void push(int x)
node* temp;
temp->data = x;
temp->next = head;
head = temp;
}
bool isEmpty()
if (head == NULL)
return true;
else
return false;
int top_element()
if (head == NULL)
else
return head->data;
void pop()
node* temp;
if (isEmpty())
}
else
{
temp = head;
head = head->next;
delete(temp);
void print_stack()
if (isEmpty())
else
curr = head;
curr = curr->next;
}
int main()
{
push(5);
push(3);
push(6);
print_stack();
isEmpty();
pop();
print_stack();
endl; return 0;
OUTPUT –
PROBLEM-3 :
Write a program to convert an infix expression to post fix and evaluate the post
fix expression using stack.
CODE –
#include <iostream>
#include <stdlib.h>
if (c == '^')
return 3;
else if (c == '/' || c ==
'*') return 2;
else return
-1;
if (c == '^')
return 'R';
return 'L'; // Default to left-associative
}
/ to postfix expression
void infixToPostfix(string s) {
stack<char> st;
string result;
char c = s[i];
st.push('(');
/ If the scanned character is an ‘)’,
else if (c == ')') {
while (st.top() != '(') {
result += st.top();
st.pop();
}
st.pop(); // Pop '('
/ If an operator is
scanned else {
result += st.top();
st.pop();
st.push(c);
}
result += st.top();
st.pop();
/ Driver code
int main() {
string exp = "a+b*(c^d-e)^(f+g*h)-i";
/ Function call
infixToPostfix(exp);
return 0;
}
OUTPUT –
PROBLEM-4:
Write a program to implement queue using array.
CODE –
#include <iostream>
#include <stdlib.h>
struct Queue {
int* queue;
Queue(int c)
front = rear = 0;
capacity = c;
printf("\nQueue is full\
n"); return;
}
queue[rear] = data;
rear++;
return;
/ if queue is empty
if (front == rear) {
printf("\nQueue is empty\n");
return;
else {
/ decrement
rear rear--;
}
return;
}
/ print queue elements
void queueDisplay()
int i;
return;
};
/ Driver code
int main(void)
{
q.queueDequeue();
q.queueDequeue();
q.queueDisplay();
/ print front of the queue
q.queueFront();
return 0;
OUTPUT –
PROBLEM-5:
Write a program to implement priority queue using linked list.
CODE –
#include <iostream>
#include <stdlib.h>
// Node
int data;
/ Lower values indicate
/ higher priority
int priority;
struct node* next;
} Node;
temp->priority = p;
temp->next = NULL;
return temp;
return (*head)->data;
{
Node* temp = *head;
(*head) = (*head)->next;
free(temp);
}
/ Function to push according to priority
void push(Node** head, int d, int p)
if ((*head)->priority > p)
else
start->next->priority < p)
{
start = start->next;
}
/ Either at the ends of the list
/ or at required position
temp->next = start->next;
start->next = temp;
/ Driver code
int main()
/ 7->4->5->6
push(&pq, 5, 2);
push(&pq, 6, 3);
push(&pq, 7, 0);
while (!isEmpty(&pq))
pop(&pq);
}
return 0;
}
OUTPUT –
PROBLEM-6:
Write a program to display a singly linked list in reverse order.
CODE –
#include <iostream>
#include <stdlib.h>
struct Node {
int data;
Node(int data) {
this->data = data;
next = NULL;
};
struct LinkedList {
Node* head;
prev = curr;
head = prev;
curr = temp;
void print() {
temp = temp->next;
temp->next = head;
head = temp;
};
int main() {
LinkedList list;
list.push(20);
list.push(4);
list.push(15);
list.push(85);
list.print();
list.reverse();
}
OUTPUT –
PROBLEM-7:
Write a program to remove duplicates in a singly linked list.
CODE –
#include <bits/stdc++.h>
struct Node {
int data;
};
{
struct Node *ptr1, *ptr2, *dup;
ptr1 = start;
if (ptr1->data == ptr2->next->data) {
dup = ptr2->next;
ptr2->next = ptr2->next->next;
delete (dup);
ptr2 = ptr2->next;
ptr1 = ptr1->next;
node = node->next;
/ Driver code
int main()
printList(start);
removeDuplicates(start);
printList(start);
return 0;
OUTPUT –
Q10
#include <stdio.h>
#include <limits.h>
#include <iostream>
// A utility function to find the vertex with minimum key value, from
// the set of vertices not yet included in MST
int minKey(int key[], bool mstSet[])
{
// Initialize min value
return min_index;
}
// Function to construct and print MST for a graph represented using adjacency
// matrix representation
void primMST(int graph[V][V])
{
int parent[V]; // Array to store constructed MST
int key[V]; // Key values used to pick minimum weight edge in cut
bool mstSet[V]; // To represent set of vertices not yet included in MST
return 0;
}
Q11
// C++ Implementation of the Quick Sort Algorithm.
#include <iostream>
using namespace std;
int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
return pivotIndex;
}
// base case
if (start >= end)
return;
int main()
{
int arr[] = { 9, 3, 4, 2, 1, 8 };
int n = 6;
quickSort(arr, 0, n - 1);
return 0;
}
Q12
// C++ program for implementation of Heap Sort
#include <iostream>
using namespace std;
// Driver program
int main()
{
int arr[] = { 60 ,20 ,40 ,70, 30, 10};
int n = sizeof(arr) / sizeof(arr[0]);
//heapify algorithm
// the loop must go reverse you will get after analyzing manually
// (i=n/2 -1) because other nodes/ ele's are leaf nodes
// (i=n/2 -1) for 0 based indexing
// (i=n/2) for 1 based indexing
for(int i=n/2 -1;i>=0;i--){
heapify(arr,n,i);
}
heapSort(arr, n);
return 0;
}