DSA LAB ASSIGNMENT
DSA LAB ASSIGNMENT
1. Write a menu driven program for Circular Linked list (Singly- CLL) to perform the following
operations:
d. To insert a node in the list ( all 3 cases→ at beginning, at end and at a particular position) and display
the new list.
e. To delete a node in the list ( all 3 cases→ at beginning, at end and at a particular position) and display the
current list.
#include <iostream>
// Node class
class Node {
public:
int data;
Node* next;
};
class CircularLinkedList {
private:
Node* last;
public:
CircularLinkedList() : last(nullptr) {}
void createList() {
insertEnd(i);
}
void display() {
if (!last) {
return;
do {
temp = temp->next;
int countNodes() {
if (!last) return 0;
int count = 0;
do {
count++;
temp = temp->next;
return count;
if (!last) {
last = newNode;
last->next = last;
} else {
newNode->next = last->next;
last->next = newNode;
}
display();
if (!last) {
last = newNode;
last->next = last;
} else {
newNode->next = last->next;
last->next = newNode;
last = newNode;
display();
return;
if (pos == 1) {
insertBegin(val);
insertEnd(val);
} else {
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
display();
}
// e. Delete node from the beginning, end, or a particular position
void deleteBegin() {
if (!last) {
return;
if (last == last->next) {
last = nullptr;
} else {
last->next = temp->next;
delete temp;
display();
void deleteEnd() {
if (!last) {
return;
if (last == last->next) {
delete last;
last = nullptr;
} else {
temp = temp->next;
temp->next = last->next;
last = temp;
delete toDelete;
display();
}
void deleteAtPos(int pos) {
return;
if (pos == 1) {
deleteBegin();
deleteEnd();
} else {
temp = temp->next;
temp->next = toDelete->next;
delete toDelete;
display();
void reverse() {
Node* next;
do {
next = current->next;
current->next = prev;
prev = current;
current = next;
last->next->next = prev;
last->next = prev;
display();
}
};
int main() {
CircularLinkedList cll;
while (true) {
switch (choice) {
case 1:
cll.createList();
break;
case 2:
cll.display();
break;
case 3:
break;
case 4:
cll.insertBegin(value);
break;
case 5:
cll.insertEnd(value);
break;
case 6:
cll.insertAtPos(value, pos);
break;
case 7:
cll.deleteBegin();
break;
case 8:
cll.deleteEnd();
break;
case 9:
cll.deleteAtPos(pos);
break;
case 10:
cll.reverse();
break;
case 11:
exit(0);
default:
return 0;
}
Creating an Stack and its Operation
2. Implement a menu-driven program to perform the Stack operations like
push, pop, display, isempty, and isfull using array.
Note: Initially push five elements into stack and proceed.
#include <iostream>
class Stack {
private:
int arr[5];
int top;
public:
Stack() {
top = -1;
// Push operation
if (isFull()) {
return;
arr[++top] = val;
display();
// Pop operation
void pop() {
if (isEmpty()) {
return;
display();
// Display stack
void display() {
if (isEmpty()) {
return;
bool isEmpty() {
bool isFull() {
return top == 4;
};
int main() {
Stack stack;
stack.push(i);
while (true) {
switch (choice) {
case 1:
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
if (stack.isEmpty()) {
} else {
break;
case 5:
if (stack.isFull()) {
} else {
break;
case 6:
exit(0);
default:
return 0;
Output:-
3. Implement a menu-driven program to perform the Stack operations like push, pop, display,
#include <iostream>
class Node {
public:
int data;
Node* next;
};
private:
Node* top;
int max_size;
int current_size;
public:
// Push operation
if (isFull()) {
return;
newNode->next = top;
top = newNode;
current_size++;
display();
// Pop operation
void pop() {
if (isEmpty()) {
return;
top = top->next;
delete temp;
current_size--;
display();
// Display stack
void display() {
if (isEmpty()) {
return;
while (temp) {
temp = temp->next;
bool isEmpty() {
bool isFull() {
};
int main() {
Stack stack;
stack.push(i);
while (true) {
switch (choice) {
case 1:
stack.push(val);
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
if (stack.isEmpty()) {
} else {
break;
case 5:
if (stack.isFull()) {
} else {
break;
case 6:
exit(0);
default:
}
}
return 0;
OUTPUT :-