Practical File On Data Structures Using C++: Submitted To: Submitted by
Practical File On Data Structures Using C++: Submitted To: Submitted by
FILE ON
DATA STRUCTURES USING C++
12. Program to implement different operations of circular queue using linked list.
16. Program adding nodes to the list , and then printing from the beginning until the
end.
17. Program to insert the element at the end of the linked list.
18. Program to insert the element at the top of the linked list.
19. Program to insert the element at the middle of the linked list.
20. Program to delete a node from a specific value from the list.
int main() {
while (true) {
// Display menu
switch (choice) {
case 1: // Insert
} else {
}
break;
case 2: // Delete
if (arr[i] == value) {
found = true;
break;
if (!found) {
break;
case 3: // Traverse
if (size == 0) {
} else {
}
break;
case 4: // Exit
default:
return 0;
}
// 2. To implement stack operations using array.
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val)
{
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else
{
top++;
stack[top]=val;
}
}
void pop()
{
if(top<=-1)
{
cout<<"Stack Underflow"<<endl;
}
else
{
cout<<"The popped element is "<< stack[top]
<<endl; top--;
}
}
void display()
{
if(top>=0)
{
cout<<"Stack elements are:"<<endl;
for(int i=top; i>=0; i--) cout<<stack[i]<<"\
n";
cout<<endl;
}
else
cout<<"Stack is empty"<<endl;
}
int main()
{
int ch, val;
cout<<"Roll No. 23/MCA/92."<<endl;
cout<<"1. Push in stack"<<endl;
cout<<"2. Pop from stack"<<endl;
cout<<"3. Display stack"<<endl;
cout<<"4. Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch)
{
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
//3. To implement Queue using Array.
#include<iostream>
using namespace std;
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void pop() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow "<<endl;
return ;
}
else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl; front+
+;;
}
}
void display() {
if (front == -1 || front>rear)
{
cout<<"Queue is empty"<<endl;
}
else
{
cout<<"Queue elements are : "<<"\n";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<"\n";
cout<<endl;
}
}
int main() {
int ch;
cout<<"Roll No. 23/MCA/92."<<endl;
cout<<"1. Insert element to queue"<<endl;
cout<<"2. Delete element from queue"<<endl;
cout<<"3. Display all the elements of queue"<<endl;
cout<<"4. Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: push();
break;
case 2:
pop(); break;
case 3: display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
//4. To implement operations in circular queue using array in C++.
#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1, n=5;
}
cqueue[rear] = val ;
}
void deleteCQ() {
if (front == -1) {
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;
if (front == rear) {
front = -1;
rear = -1;
}
else
{
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<"\n";
f++;
}
}
else
{
while (f <= n - 1) {
cout<<cqueue[f]<<"\n"; f+
+;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<"\n";
f++;
}
}
cout<<endl;
}
int main() {
int ch, val;
cout<<"Roll No. 23/MCA/92"<<endl;
cout<<"1. Insert\n";
cout<<"2. Delete\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";
do {
cout<<"Enter choice : "<<endl;
cin>>ch;
switch(ch) {
case 1:
cout<<"Input for insertion: "<<endl;
cin>>val;
insertCQ(val);
break;
case 2:
deleteCQ();
break;
case 3:
displayCQ();
break;
case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch !=
4); return 0;
}
// 5. To implement Linear Search.
#include<iostream>
using namespace std;
int main()
{
int n, arr[n], i, num, index;
cout<<"Roll No. 23/MCA/92"<<endl;
cout<<"Enter total no. of elements: ";
cin>>n;
cout<<"Enter elements:"<<endl;
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"\nEnter a Number to Search:
"; cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
index =
i; break;
}
}
cout<<"\nFound at Index No."<<index;
cout<<endl;
return 0;
}
//6. Program to insert binary search
#include <iostream>
using namespace std;
int main() {
int n; // Number of elements
cout << "Enter total number of elements: ";
cin >> n;
// Binary search
while (left <= right) {
int mid = left + (right - left) / 2; // Calculate mid index
if (arr[mid] == target) {
cout << "Found at Index: " << mid << endl;
found = true;
break;
} else if (arr[mid] < target) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}
if (!found) {
cout << "Number not found!" << endl; // Number not found
}
return 0;
}
//7.To implement selection sort.
#include<iostream>
int main()
int n;
cin>>n;
cout<<"Enter elements:"<<endl;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(arr[j]<arr[i])
int temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
for(int i=0;i<n;i++)
cout<<" "<<arr[i];
}
return 0;
}
//8.To implement bubble sort.
#include<iostream>
int main()
int n;
cin>>n;
cout<<"Enter elements:"<<endl;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int counter=0;
while(counter<n-1)
for(int i=0;i<n-counter-1;i++)
if(arr[i]>arr[i+1]){
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
counter++;
for(int i=0;i<n;i++)
cout<<" "<<arr[i];
}
return 0;
}
//9.To implement insertion sort.
#include<iostream>
int main()
int n;
cin>>n;
cout<<"Enter elements:"<<endl;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=1;i<n;i++)
int current=arr[i];
int j=i-1;
arr[j+1]=arr[j];
j--;
arr[j+1]= current;
for(int i=0;i<n;i++)
{
cout<<" "<<arr[i];
return 0;
public:
Stack() : top(nullptr) {} // Constructor to initialize stack
// Push operation
void push(int value) {
Node* newNode = new Node(); // Create a new node
newNode->data = value; // Set the node's data
newNode->next = top; // Link to the previous top
top = newNode; // Update top
}
// Pop operation
void pop() {
if (top == nullptr) {
cout << "Stack is empty!" << endl;
return;
}
Node* temp = top; // Store the top node
top = top->next; // Move top to the next node
delete temp; // Free memory
cout << "Popped from stack." << endl;
}
int main() {
Stack stack; // Create a stack
int choice, value;
while (true) {
cout << "\n1. Push\n2. Pop\n3. Display\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
return 0; // Exit
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
public:
Queue() : front(nullptr), rear(nullptr) {} // Constructor
// Enqueue operation
void enqueue(int value) {
Node* newNode = new Node(); // Create a new node
newNode->data = value; // Set data
newNode->next = nullptr; // New node will be at the end
if (rear) {
rear->next = newNode; // Link old rear to new node
} else {
front = newNode; // If empty, set front to new node
}
rear = newNode; // Update rear to new node
cout << value << " enqueued." << endl;
}
// Dequeue operation
void dequeue() {
if (front == nullptr) {
cout << "Queue is empty!" << endl;
return;
}
Node* temp = front; // Store front node
front = front->next; // Move front to the next node
cout << temp->data << " dequeued." << endl;
delete temp; // Free memory
if (front == nullptr) {
rear = nullptr; // If queue is empty, reset rear
}
}
int main() {
Queue queue; // Create a queue
int choice, value;
while (true) {
cout << "\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
queue.enqueue(value);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
return 0; // Exit
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
//12. Program to implement different operations of circular queue using linked list.
#include <iostream>
using namespace std;
public:
CircularQueue() : front(nullptr), rear(nullptr) {} // Constructor
// Enqueue operation
void enqueue(int value) {
Node* newNode = new Node(); // Create a new node
newNode->data = value; // Set data
if (front == nullptr) { // If the queue is empty
front = newNode; // Set front to new node
rear = newNode; // Set rear to new node
rear->next = front; // Point rear to front (circular)
} else {
rear->next = newNode; // Link the old rear to the new node
rear = newNode; // Update rear to the new node
rear->next = front; // Point rear to front (circular)
}
cout << value << " enqueued." << endl;
}
// Dequeue operation
void dequeue() {
if (front == nullptr) {
cout << "Queue is empty!" << endl;
return;
}
Node* temp = front; // Store front node
if (front == rear) {
// If only one node is in the queue
front = nullptr; // Reset front
rear = nullptr; // Reset rear
} else {
front = front->next; // Move front to the next node
}
cout << temp->data << " dequeued." << endl;
delete temp; // Free memory
}
int main() {
CircularQueue queue; // Create a circular queue
int choice, value;
while (true) {
cout << "\nMenu:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
queue.enqueue(value);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
return 0; // Exit the program
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
public:
DoublyLinkedList() : head(nullptr) {} // Constructor
int main() {
DoublyLinkedList list; // Create a doubly linked list
int choice, value;
while (true) {
cout << "\nMenu:\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Delete Node\n";
cout << "4. Display List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to insert at beginning: ";
cin >> value;
list.insertAtBeginning(value);
break;
case 2:
cout << "Enter value to insert at end: ";
cin >> value;
list.insertAtEnd(value);
break;
case 3:
cout << "Enter value to delete: ";
cin >> value;
list.deleteNode(value);
break;
case 4:
list.display();
break;
case 5:
return 0; // Exit the program
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
#include<iostream>
struct Node
int data;
Node *link;
};
int main()
{
Node *Start=NULL;
Node *t;
Node *n;
n=new Node;
n->data=10;
n->link=NULL;
Start=n;
t=n;
n=new Node;
n->data=20;
n->link=NULL;
cout<<Start->data<<endl;
cout<<Start<<endl;
cout<<t->data<<endl;
cout<<t<<endl;
cout<<n->data<<endl;
cout<<n<<endl;
Start->link=n;
cout<<n<<endl;
t=t->link;
cout<<t<<endl;
}
//15. Adding or Appending a Node to the list.
#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void appendNode(int value)
{
Node *n, *curr;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;
else // Otherwise, insert newNode at end
{
// Initialize curr to head of list
curr = head;
// Find the last node in the list
while (curr->next!= NULL)
{
curr = curr->next;
}
// Insert newNode as the last node
curr->next = n;
}
}
int main()
{
appendNode(2);
appendNode(14);
appendNode(26);
return 0;
}
//16. Adding Nodes to the list and then and then printing them from the beginning until the end.
#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void appendNode(int value)
{
Node *n, *curr;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;