Ds Lab Final2
Ds Lab Final2
LAB Day- 1
Q-3 Sum of numbers using array:
#include <stdio.h>
int main()
{
int i, n, sum = 0;
int sumEven = 0, sumOdd = 0;
int arr[n];
return 0;
}
LAB Day- 2
Given an element k. Find if the k is present in the array, if YES, find its position,
otherwise -1. Linear Search
#include <stdio.h>
int main(){
int i, n, k;
int Flag = 0;
for (i= 0; i< n; i++){
if (arr[i] == k)
{
Flag= 1;
printf("%d is present in position:%d \n",k,i);
break;
}
}
if(Flag== 0){
printf("%d is not present\n",k);
}
return 0;
}
An array given. Q times Query. every time one integer will be given. If it is present
then answer will be YES. if not then NO.
#include <stdio.h>
int main(){
}
}
Position of minimum element of given array
#include <iostream>
#include <stack>
using namespace std;
int main() {
int position;
int arr[5]= {90,30,22,66,60};
int min_element = arr[0];
LAB Day- 3
Binary Search
#include<stdio.h>
int main()
{
int i,j,n,k, q;
while(start<=end)
{
mid = (start+end)/2;
if(k== arr[mid])
{
printf("%d is present and position is %d\n",k, mid);
break;
}
if(k<arr[mid])
{
end=mid-1;
}
if(k>arr[mid])
{
start= mid+1;
}
}
if (start > end){
printf(" %d isn't present\n", k);
}
}
}
Q: Print the position of the first element of the array which is greater than k.
#include<stdio.h>
int main()
{
int arr[7]={20,30,40,50,60,70,80};
int k,i;
printf("Enter the value of K:");
scanf("%d",&k);
for(i=0;i<7;i++){
if (arr[i]>k){
printf("Position of the element:%d",i);
break;
}
}
}
LAB Day- 4
Selection Sorting:
#include<stdio.h>
int main() {
int i, j, n, step ;
array[i+1] = f_elm;
}
printf("Array A\n");
for(i = 1; i<=m; i++){
for(j=1; j<=n; j++){
printf("%d ", A[i][j]);
}
printf("\n");
}
printf("Array B\n");
for(i = 1; i<=n; i++){
for(j=1; j<=p; j++){
printf("%d ", B[i][j]);
}
printf("\n");
}
int main(){
int n;
double TCGPA=0, ACGPA;
cout<<"Enter the Number of Students: ";
cin>>n;
Student s[n];
int main()
{
int num;
printf("Enter the number:");
scanf("%d", &num);
int main(){
int num;
cout<< "Enter the number:";
cin>> num;
int ans = fib(num);
cout<< num<<"th Fibonacci number is= "<< ans << endl;
}
Sum of N numbers using recursion
#include<iostream>
using namespace std;
int main(){
int num;
cout<< "Enter the number:";
cin>> num;
int ans = sum(num);
cout<< "Sum of first "<<num<<" number is= "<< ans << endl;
}
Take an Integer value x and assign some value to it taking user input. Now use a
level-1 pointer to point the value of x.
POINTER
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter an integer value for x: ";
cin >> x;
cout << "The value of x is: " << *ptr << endl;
cout << "The address of x is: " << ptr << endl;
return 0;
}
#include<stdio.h>
int main()
{
int x = 10;
int *p, **q;
p = &x;
q = &p;
printf("%p\n", &p);
printf("%p\n", p);
printf("%d\n", *p);
*p = 20;
printf("%d\n", x);
printf("%d\n", **q);
return 0;
}
Find the smallest and largest element in the array using pointers.
#include <stdio.h>
return 0;
}
|3. Student |
i) ID
ii) Age
iii) Name
iv) CGPA
take n students Inputs and do
- Print the students by their ID
- Print the students by their Age
- Print the students by their CGPA and Age
struct Student{
int ID;
double Age, CGPA;
string Name;
void input()
{
cout<<"Name, ID, Age, CGPA: ";
cin.ignore();
getline(cin,Name);
cin>>ID>>Age>>CGPA;
}
void print_info()
{
cout<<"Name: "<< Name<<", ID: "<< ID<<", CGPA: "<< CGPA<<", Age: "<< Age<<endl;
}
};
int main(){
int n, i, f_elm;
Student s[n];
cout<<""<< endl;
s[j + 1] = temp;
}
for(int i = 0; i<n; i++){
s[i].print_info();
}
cout<<"\nPrinting the students by their Age using Bubble sort: "<< endl;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (s[j].Age > s[j + 1].Age) {
Student temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
cout<<"\nPrinting the students by their CGPA and Age using Selection sort: "<< endl;
if(minIndex != i){
Student temp = s[i];
s[i] = s[minIndex];
s[minIndex] = temp;
}
}
Linked list
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
node(){
next= NULL;
}
};
int main(){
int n;
cout << "Number of elements" << endl;
cin >> n;
node *head, *previous_node;
head= new node;
cout << "First node element" << endl;
int x;
cin>> x;
head -> data=x;
previous_node= head;
// sum
int sum =0;
for(node* i= head; i != NULL; i = i-> next){
sum = sum + i->data;
}
cout << "Sum of all data in the linked list: " << sum << endl;
// search
int y, flag=0 ;
cout << "Enter search element:" << endl;
cin>> y;
for(node* i= head; i != NULL; i = i-> next){
if(i-> data== y){
cout << "Number is present" << endl;
flag = 1;
}
}
if( flag== 0){
cout << "Number is not present" << endl;
}
// insert at beginning
node *new_element;
new_element = new node;
int key;
cout << "Enter the new element at beginning: " << endl;
cin>> key;
new_element-> data= key;
new_element-> next= head;
head = new_element;
// insert at last
node *new_element2;
new_element2 = new node;
int key2;
cout << "Enter the new element at end: " << endl;
cin>> key2;
new_element2-> data= key2;
node *i;
}
i-> next= new_element2;
// delete
int position;
cout << "Position you want to delete: " << endl;
cin >> position;
if (position == 1){
node *temp;
temp =head;
head= head -> next;
delete temp;
}
else {
node *i,*pre_node, *next_node;
i= head;
for(int k=1; k<=position-1; k++){
pre_node= i;
i= i->next;
}
next_node= i->next;
delete i;
pre_node-> next =next_node;
}
}
Doubly Linked List
#include<iostream>
using namespace std;
int main() {
int n;
cout << "Number of elements: " << endl;
cin >> n;
if(head == NULL) {
head = new_node;
tail = new_node;
} else {
tail->next = new_node;
new_node->prev = tail;
tail = new_node;
}
}
// Displaying the doubly linked list backward using the tail pointer
cout << "Linked List backward:" << endl;
for(node* i = tail; i != NULL; i = i->prev) {
cout << "Data in node: " << i->data << endl;
}
cout << "Sum of all data in the linked list: " << sum << endl;
// Searching
int y, flag=0;
cout << "Enter Search element: "<< endl;
cin>> y;
for(node* i = head; i != NULL; i = i->next) {
if(i-> data==y){
cout<< "Number is present."<< endl;
flag= 1;
}
if(flag==0){
cout<< "Number is not present."<< endl;
}
}
//Insert at beginning
node *new_element;
new_element= new node;
int key;
cout<< "Enter new element at beginning:" << endl;
cin>> key;
new_element->data =key;
head->prev = new_element;
new_element-> next= head;
head= new_element;
// delete
int position;
cout << "Position you want to delete: " << endl;
cin >> position;
if (position == 1){
node *temp;
temp= tail;
tail= tail->prev;
tail->next =NULL;
delete temp;
}
else {
node *i,*pre_node, *next_node;
i= head;
for(int k=1; k<=position-1; k++){
pre_node= i;
i= i->next;
}
next_node= i->next;
pre_node-> next = i->next;
next_node-> prev= i->prev;
delete i;
}
return 0;
}
Circular linked list
#include<iostream>
using namespace std;
struct node {
int data;
node *next;
node() {
next = NULL;
}
};
int main() {
int n;
cout << "Number of elements: ";
cin >> n;
if(head == NULL) {
head = new_node;
} else {
previous_node->next = new_node;
}
previous_node = new_node;
if(i == n) {
new_node->next = head; // connecting the last node to the head
}
}
if (flag == 0) {
cout << "Number is not present" << endl;
}
// Insertion
// Insert a new element at the beginning
int key;
cout << "Enter the new element at beginning: " << endl;
cin >> key;
current = head;
for (int i = 1; i < pos - 1; i++) {
current = current->next;
}
new_element2->next = current->next;
current->next = new_element2;
new_element3->next = head;
previous_node->next = new_element3;
cout << "New Circular Linked List:" << endl;
printCircularList(head);
// Deletion
// Delete a node at a given position
int position;
cout << "Position you want to delete: ";
cin >> position;
if (position == 1) {
// Delete the first node
node *temp = head;
while(temp->next != head) {
temp = temp->next;
}
temp->next = head->next;
delete head;
head = temp->next;
} else {
// Delete any position other than the head
node *current = head;
node *previous = NULL;
for(int i = 1; i < position; i++) {
previous = current;
current = current->next;
}
previous->next = current->next;
delete current;
}
return 0;
}
LAB Day- 7
Calculate power a^b using Recursion
#include <iostream>
using namespace std;
long long int power(int base, int p) {
if (p == 0)
return 1;
return base * power(base, p- 1);
}
int main() {
int base, p;
cout << "Enter base: ";
cin >> base;
cout << "Enter power: ";
cin >> p;
cout << base << " power " << p << ": " << power(base, p) << endl;
return 0;
}
int main(){
int num;
cout<< "Enter the number:";
cin>> num;
if (checkPrime(2,num) == 0){
cout<< "It is a prime number " << endl;
}
else{
cout<< "It is not a prime number " << endl;
}
}
int main() {
int n, result=0,q, rem ;
cout << "Enter a number: ";
cin >> n;
q=n;
while (q> 0) {
rem = q % 10;
result = result * 10 + rem;
q = q/10;
}
if (result == n)
cout << n << " is a palindrome." << endl;
else
cout << n << " is not a palindrome." << endl;
return 0;
}
Reverse a Number
#include <iostream>
using namespace std;
int main() {
int n, result=0,q, rem ;
cout << "Enter a number: ";
cin >> n;
q=n;
while (q> 0) {
rem = q % 10;
result = result * 10 + rem;
q = q/10;
}
cout << result<< " is reversed." << endl;
return 0;
}
LAB Day- 8
Student using linked list to print avg cgpa and add a new student
#include<iostream>
using namespace std;
struct Student {
string Name;
int ID;
double cgpa, age;
Student* next;
void print_info() {
cout << "Name: " << Name << ", ID: " << ID << ", CGPA: " << cgpa << ", Age: " << age << endl;
}
};
int main() {
int n;
cout << "Enter the number of students: ";
cin >> n;
if (head == NULL) {
head = s;
previous_node = s;
} else {
previous_node->next = s;
previous_node = s;
}
}
double total_cgpa = 0;
Student* current = head;
while (current != NULL) {
total_cgpa += current->cgpa;
current = current->next;
}
double average_cgpa = total_cgpa / n;
string newName;
int newID;
double newcgpa, newage;
cout << "\nNew Student Information : " << endl;
cout << "Name: ";
cin.ignore();
getline(cin, newName);
cout << "ID: ";
cin >> newID;
cout << "Age: ";
cin >> newage;
cout << "CGPA: ";
cin >> newcgpa;
Student* newStudent = new Student(newName, newID, newcgpa, newage);
newStudent->next = head;
head = newStudent;
return 0;
}
Student Using Linked list to print the n number of student average cgpa
#include<iostream>
using namespace std;
struct Student {
string Name;
int ID;
double cgpa, age;
Student* next;
};
int main() {
int n;
cout << "Enter the number of students: ";
cin >> n;
if (head == nullptr) {
head = s;
previous_node = s;
} else {
previous_node->next = s;
previous_node = s;
}
}
double total_cgpa = 0;
Student* current = head;
while (current != nullptr) {
total_cgpa += current->cgpa;
current = current->next;
}
double average_cgpa = total_cgpa / n;
return 0;
}
LAB Day- 9
STL Stack
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack <int> Basket;
int n;
cout<< "Enter number of elements:"<< endl;
cin >> n;
if (Basket.size() == n) {
cout << "Stack is full." << endl;
cout<< " " << endl;
}
while(Basket.empty()==0){
int top= Basket.top();
cout << "The popped element is " << top << endl;
Basket.pop();
}
if(Basket.empty()==1){
cout<<"Stack is empty"<<endl;
}
return 0;
}
Stack case
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> Basket;
int n;
int choice;
while (true) {
cout << "\nChoose an operation:\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek (Top element)\n";
cout << "4. Traverse\n";
cout << "5. Check if stack is Empty\n";
cout << "6. Check if stack is Full\n";
cout << "7. Size\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
if (Basket.size() < n) {
int x;
cout << "Enter new element: ";
cin >> x;
Basket.push(x);
cout << x << " pushed into the stack.\n";
} else {
cout << "Stack is full.\n";
}
break;
}
case 2: {
if (!Basket.empty()) {
int top = Basket.top();
Basket.pop();
cout << "The popped element is " << top << ".\n";
} else {
cout << "Stack is empty.\n";
}
break;
}
case 3: {
if (!Basket.empty()) {
cout << "Top element: " << Basket.top() << ".\n";
} else {
cout << "Stack is empty.\n";
}
break;
}
case 4: {
if (!Basket.empty()) {
stack<int> temp = Basket;
cout << "Stack elements: ";
while (!temp.empty()) {
cout << temp.top() << " ";
temp.pop();
}
cout << "\n";
} else {
cout << "Stack is empty.\n";
}
break;
}
case 5: {
if (Basket.empty()) {
cout << "Stack is empty.\n";
} else {
cout << "Stack is not empty.\n";
}
break;
}
case 6: {
if (Basket.size() == n) {
cout << "Stack is full.\n";
} else {
cout << "Stack is not full.\n";
}
break;
}
case 7: {
cout << "Size of the stack: " << Basket.size() << "\n";
break;
}
case 8: {
cout << "Exiting...\n";
return 0; // Exit the program
}
default: {
cout << "Invalid choice. Please try again.\n";
}
}
}
return 0;
}
int basket[100];
int n;
int top = 0;
void push(int x) {
if (top >= n)
cout << "Basket Overflow" << endl;
else {
basket[top] = x;
top++;
}
}
void pop() {
if (top <= 0)
cout << "Basket Underflow" << endl;
else {
top--;
cout << "The popped element is " << basket[top] << endl;
}
}
void traverse() {
if (top > 0) {
cout << "Basket elements are:";
for (int i = top - 1; i >= 0; i--)
cout << basket[i] << " "<< endl;
cout << "Top element is: " << basket[top - 1] << endl;
} else
cout << "Basket is empty" << endl;
}
int main() {
cout << "Enter the size of the basket: ";
cin >> n;
traverse();
if (top == n) {
cout << "Stack is full." << endl;
cout<< " " << endl;
}
int count=0;
while (top != 0) {
pop();
count++;
}
if (top == 0) {
cout << "Stack is empty." << endl;
}
return 0;
}
Using case
#include <iostream>
using namespace std;
int basket[100];
int n;
int top = 0;
void push(int x) {
if (top >= n)
cout << "Basket Overflow" << endl;
else {
basket[top] = x;
top++;
}
}
void pop() {
if (top <= 0)
cout << "Basket Underflow" << endl;
else {
top--;
cout << "The popped element is " << basket[top] << endl;
}
}
void traverse() {
if (top > 0) {
cout << "Basket elements are:";
for (int i = top - 1; i >= 0; i--)
cout << basket[i] << " ";
cout << endl;
cout << "Top element is: " << basket[top - 1] << endl;
} else
cout << "Basket is empty" << endl;
}
bool isEmpty() {
return top == 0;
}
bool isFull() {
return top == n;
}
int size() {
return top;
}
int main() {
cout << "Enter the size of the basket: ";
cin >> n;
int choice;
while (true) {
cout << "\nChoose an operation:\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Traverse\n";
cout << "4. Check if Empty\n";
cout << "5. Check if Full\n";
cout << "6. Get Size\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1: {
int x;
cout << "Enter element to push: ";
cin >> x;
push(x);
break;
}
case 2: {
pop();
break;
}
case 3: {
traverse();
break;
}
case 4: {
if (isEmpty())
cout << "Basket is empty" << endl;
else
cout << "Basket is not empty" << endl;
break;
}
case 5: {
if (isFull())
cout << "Basket is full" << endl;
else
cout << "Basket is not full" << endl;
break;
}
case 6: {
cout << "Size of the basket: " << size() << endl;
break;
}
case 7: {
cout << "Exiting..." << endl;
break;
}
default: {
cout << "Invalid choice. Please try again." << endl;
}
}
}
return 0;
}
LAB Day- 10
Infix to PostFix
#include <stack>
#include <string>
using namespace std;
bool isOperator(char c) {
return c == '^' || c == '*' || c == '/' || c == '+' || c == '-' || c == '=';
}
// Step 1: Push "(" into Basket and add ")" to the end of infix notation
infix = "(" + infix + ")";
else if (c == '(')
basket.push(c);
else if (isOperator(c)) {
while (!basket.empty() && basket.top() != '(' && getPrecedence(basket.top()) >=
getPrecedence(c)) {
postfix += basket.top();
basket.pop();
}
basket.push(c);
} else if (c == ')') {
while (!basket.empty() && basket.top() != '(') {
postfix += basket.top();
basket.pop();
}
basket.pop();
int main() {
string infix;
cout << "Enter the infix expression: ";
getline(cin, infix);
return 0;
}
POSTFIX Evaluation
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
bool isOperator(char c) {
return c == '^' || c == '*' || c == '/' || c == '+' || c == '-' || c == '=';
}
while (!basket.empty()) {
postfix += basket.top();
basket.pop();
}
return postfix;
}
if (isdigit(c)) {
int number = 0;
while (isdigit(c)) {
number = number * 10 + (c - '0');
i++;
if (i < postfix.length()) {
c = postfix[i];
} else {
break;
}
}
basket.push(number);
i--;
} else if (c == ' ') {
continue;
} else if (isOperator(c)) {
int operand2 = basket.top(); basket.pop();
int operand1 = basket.top(); basket.pop();
int result;
switch (c) {
case '^':
result = pow(operand1, operand2);
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '=':
result = operand2;
break;
}
basket.push(result);
}
}
return basket.top();
}
int main() {
string infix;
cout << "Enter the infix expression: ";
getline(cin, infix);
return 0;
}
LAB Day- 11
QUEUE using Array
#include <iostream>
using namespace std;
void enqueue(int x) {
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else if (front == - 1 && rear== - 1){
front++;
rear++;
queue[rear] = x;
}
else{
rear++;
queue[rear] = x;
}
}
void dequeue() {
if (front == - 1 && rear== - 1) {
cout<<"Queue Underflow ";
}
else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void traverse() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" "<<endl;
}
void empty() {
if (front > rear) {
cout<<"Queue is empty.";
}
else{
cout<<"Queue is not empty.";
}
}
int main() {
cout << "Enter the size of the queue: ";
cin >> n;
traverse();
empty();
return 0;
}
void enqueue(int x) {
if (rear == n - 1)
cout << "Queue Overflow" << endl;
else if (front == -1 && rear == -1) {
front++;
rear++;
queue[rear] = x;
} else {
rear++;
queue[rear] = x;
}
}
void dequeue() {
if (front == -1 && rear == -1) {
cout << "Queue Underflow" << endl;
} else {
cout << "Element deleted from queue is: " << queue[front] << endl;
front++;
}
}
void traverse() {
if (front == -1)
cout << "Queue is empty" << endl;
else {
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++)
cout << queue[i] << " ";
cout << endl;
}
}
void empty() {
if (front > rear || (front == -1 && rear == -1)) {
cout << "Queue is empty." << endl;
} else {
cout << "Queue is not empty." << endl;
}
}
void frontElement() {
if (front == -1 || front > rear)
cout << "Queue is empty." << endl;
else
cout << "Front element of the queue: " << queue[front] << endl;
}
void rearElement() {
if (rear == -1 || front > rear)
cout << "Queue is empty." << endl;
else
cout << "Rear element of the queue: " << queue[rear] << endl;
}
bool isFull() {
return rear == n - 1;
}
int size() {
return (rear - front) + 1;
}
int main() {
cout << "Enter the size of the queue: ";
cin >> n;
while (true) {
int choice;
cout << "\nQueue Operation:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Traverse\n";
cout << "4. Check if Queue is Empty\n";
cout << "5. Check if Queue is Full\n";
cout << "6. Front Element\n";
cout << "7. Rear Element\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int x;
cout << "Enter element to enqueue: ";
cin >> x;
enqueue(x);
break;
}
case 2:
dequeue();
break;
case 3:
traverse();
break;
case 4:
empty();
break;
case 5:
if (isFull())
cout << "Queue is full." << endl;
else
cout << "Queue is not full." << endl;
break;
case 6:
frontElement();
break;
case 7:
rearElement();
break;
case 8:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
Queue using STL
#include<iostream>
#include<queue>
int main()
{
queue<int> Queue;
int n;
cout << "Enter the size of Queue:";
cin >> n;
cout << "Enter elements in Queue - " << endl;
CIRCULAR Queue
#include<iostream>
using namespace std;
void enqueue(int x)
{
if ((rear + 1) % n == front)
{
cout << "Overflow" << endl;
return;
}
else if (front == -1 && rear == -1)
{
front= rear=0;
queue[rear] = x;
}
else
{
rear = (rear + 1) % n;
queue[rear] = x;
}
}
void dequeue()
{
if (front == -1 && rear == -1)
{
cout << "Underflow" << endl;
return;
}
else if (front == rear)
{
front = rear = -1;
}
else
{
front = (front + 1) % n;
}
}
void traverse()
{
if (front == -1 && rear == -1)
{
cout << "Queue is empty" << endl;
return;
}
else {
cout<<"Queue elements are : ";
for (int i = front; i!=(rear+1);i=(i+1)%n){
cout<<queue[i]<<" "<<endl;
}
}
}
bool empty()
{
return (front == -1 && rear == -1);
}
int main()
{
cout << "Enter the size of Queue: ";
cin >> n;
int choice;
do {
cout << "Circular Queue Operation: \n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Traverse\n";
cout << "4. Check if Queue is Empty\n";
cout << "5. Check if Queue is Full\n";
cout << "6. Front Element\n";
cout << "7. Rear Element\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int x;
cout << "Enter the element to enqueue: ";
cin >> x;
enqueue(x);
break;
}
case 2:
dequeue();
break;
case 3:
traverse();
break;
case 4:
if (empty())
cout << "Queue is empty.";
else
cout << "Queue is not empty.";
break;
case 5:
if ((rear + 1) % n == front)
cout << "Queue is full.";
else
cout << "Queue is not full.";
break;
case 6:
cout << "Front element: " << queue[front] << endl;
break;
case 7:
cout << "Rear element: " << queue[rear] << endl;
break;
case 8:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice";
}
} while (choice != 8);
return 0;
}
LAB Day- 12
Graph
Unweighted Directed List Representation
#include<iostream>
#include<vector>
#include<cstdlib>
using namespace std;
int main()
{
int M= 10000;
vector< int > G[M+1];
int ne;
cout<< "Number of edge:" ;
cin>> ne;
for (int i=1; i<=ne; i++)
{
int u,v;
cout<< "Enter the value of u, v:";
cin >> u>> v;
G[u].push_back(v);
}
for (int u=0; u<=M; u++)
{
int l =G[u].size();
if(l!=0){
cout << u << " - ";
for (int i=0; i<=l-1; i++)
{
cout<< G[u][i] << " " ;
}
cout << endl;
}
}
Unweighted Undirected List Representation
#include<iostream>
#include<vector>
#include<cstdlib>
using namespace std;
int main()
{
int M= 10000;
vector< int > G[M+1];
int ne;
cout<< "Number of edge:" ;
cin>> ne;
for (int i=1; i<=ne; i++)
{
int u,v;
cout<< "Enter the value of u, v:";
cin >> u>> v;
G[u].push_back(v);
G[v].push_back(u);
}
for (int u=0; u<=M; u++)
{
int l =G[u].size();
if(l!=0){
cout << u << " - ";
for (int i=0; i<=l-1; i++)
{
cout<< G[u][i] << " " ;
}
cout << endl;
}
}
}
if(l!=0){
cout << u << "-";
for (int i=0; i<=l-1; i++){
cout<< " ( " << G[u][i].first<< " , " << G[u][i].second << " )";
}
cout << endl;
}
}
LAB Day- 13
BFS
#include<iostream>
#include<vector>
#include<queue>
#include<fstream>
using namespace std;
int main(){
int M= 1000;
vector<int>G[M+1];
int color[M+1];
int level[M+1];
int ne;
cout<<"Total number of edges:";
cin>> ne;
int src;
cout<<"Enter source :";
cin>>src;
queue<int> Q;
color[src] = 1;
level[src] = 0;
Q.push(src);
while(!Q.empty()){
int u = Q.front();
int l = G[u].size();
for(int i = 0; i<l; i++){
int v = G[u][i];
if(color[v] == 0){
color[v] = 1;
level[v] = level[u] + 1;
Q.push(v);
}
}
cout<< " The order of vertices is =" << u <<endl;
cout<< " The level of " <<u <<" is =" << level[u]<<endl;
Q.pop();
DFS
#include <iostream>
#include <vector>
vector<int> G[1001];
int color[1001];
int entry[1001];
int finish[1001];
int order[1001];
int order_index = 0;
int main() {
int M = 1000;
int ne;
cout << "Total number of edges: ";
cin >> ne;
int src;
cout << "Enter source: ";
cin >> src;
int stop_watch = 0;
DFS(src, stop_watch);
return 0;
}
vector<int> adj_list[20009];
bool color[20009] = {};
bool type[20009] = {};
int main() {
int T;
scanf("%d", &T); // test cases
int n, u, v;
scanf("%d", &n); // Read number of dual fights
while (!q.empty()) {
int u = q.front();
q.pop();