booooooooooooooooty

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

Write a program to create a dynamic array of user defined size. Array should be character type.

Write a function RemoveVowels() that should remove all the vowels in the array. All array
operations should be done using pointers.

#include <iostream>
using namespace std;

// Function to remove vowels from the array


void RemoveVowels(char* arr, int& size) {
char* newArr = new char[size]; // Temporary array to store non-vowel characters
int j = 0;

for (int i = 0; i < size; i++) {


char ch = *(arr + i);
// Check if the character is not a vowel
if (!(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')) {
*(newArr + j) = ch;
j++;
}
}

size = j; // Update the size of the array


for (int i = 0; i < size; i++) {
*(arr + i) = *(newArr + i); // Copy non-vowel characters back to the original array
}

delete[] newArr; // Free temporary array memory


}
int main() {
int size;

cout << "Enter the size of the character array: ";


cin >> size;

// Dynamically allocate the character array


char* arr = new char[size];

cout << "Enter characters for the array: ";


for (int i = 0; i < size; i++) {
cin >> *(arr + i);
}

// Call the function to remove vowels


RemoveVowels(arr, size);

cout << "Array after removing vowels: ";


for (int i = 0; i < size; i++) {
cout << *(arr + i) << " ";
}
cout << endl;

// Free the dynamically allocated array


delete[] arr;

return 0;
}

You are required to design a program which should allow creation of a dynamic array. User
should provide the size of array as input which should be in range of 1 to 20, when program is
executed. Further user will provide input for elements of array. For each element value to be
given as input should be in range of 0 to 12. Your program should calculate and display average
of all elements of this array. Further program should calculate and display the factorial of each
element of this array.

#include <iostream>
using namespace std;

// Function to calculate factorial of a number


int factorial(int num) {
int result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}

int main() {
int size;

// Input for the size of the array


cout << "Enter the size of the array (1 to 20): ";
cin >> size;

// Validate size input


while (size < 1 || size > 20) {
cout << "Invalid size! Please enter a value between 1 and 20: ";
cin >> size;
}
// Dynamically allocate the array
int* arr = new int[size];

cout << "Enter " << size << " elements (each between 0 and 12):\n";

// Input for array elements


for (int i = 0; i < size; i++) {
cin >> *(arr + i);

// Validate each element input


while (*(arr + i) < 0 || *(arr + i) > 12) {
cout << "Invalid value! Please enter a value between 0 and 12: ";
cin >> *(arr + i);
}
}

// Calculate and display the average


double sum = 0;
for (int i = 0; i < size; i++) {
sum += *(arr + i);
}
double average = sum / size;
cout << "\nAverage of all elements: " << average << endl;

// Calculate and display the factorial of each element


cout << "Factorials of elements:\n";
for (int i = 0; i < size; i++) {
cout << *(arr + i) << "! = " << factorial(*(arr + i)) << endl;
}
// Free the dynamically allocated memory
delete[] arr;

return 0;
}

Write a program which should contain three 2-dimensional dynamic arrays. For two arrays user
will provide the size and values of elements as input. Your program should assume each of
these arrays as matrices and should perform addition between two arrays for which user has
provided input values, your program should store results of addition in third array by using
nested loops. Your program should display the values of elements of third array which should
contain the results of addition
#include <iostream>
using namespace std;

int main() {
int rows, cols;

// Input the size of the matrices


cout << "Enter the number of rows for the matrices: ";
cin >> rows;
cout << "Enter the number of columns for the matrices: ";
cin >> cols;

// Dynamically allocate memory for the first matrix


int** matrix1 = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix1[i] = new int[cols];
}

// Dynamically allocate memory for the second matrix


int** matrix2 = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix2[i] = new int[cols];
}

// Dynamically allocate memory for the result matrix


int** result = new int*[rows];
for (int i = 0; i < rows; i++) {
result[i] = new int[cols];
}

// Input values for the first matrix


cout << "Enter values for the first matrix:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix1[i][j];
}
}

// Input values for the second matrix


cout << "Enter values for the second matrix:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix2[i][j];
}
}

// Perform addition and store the result in the third matrix


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Display the result matrix


cout << "Result of addition:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}

// Free allocated memory


for (int i = 0; i < rows; i++) {
delete[] matrix1[i];
delete[] matrix2[i];
delete[] result[i];
}
delete[] matrix1;
delete[] matrix2;
delete[] result;

return 0;
}

Implement List class with the following functions:


1) Sequential insert in the list
2) Sequential delete in the list
3) Insert a value at a specific index and adjust remaining list afterwards
4) Delete a value at a specific index and adjust remaining list afterwards
5) Show all values present in the list
6) Show the length of the list

#include &lt;iostream&gt;
using namespace std;

class MyList {
private:
int *items;
int size;
int pos;

public:
MyList() {
size = 0;
cout &lt;&lt; &quot;Enter size for array: &quot;;
cin &gt;&gt; size;
items = new int[size];
pos = 0;
}

// Function to add value in sequence


void add(int val) {
if (pos &lt; size) {
items[pos] = val;
pos++;
} else {
cout &lt;&lt; &quot;List is full. Cannot add more items.&quot; &lt;&lt; endl;

}
}

// Function to retrieve value at a specific location


int retrieve(int loc) {
if (loc &gt;= 0 &amp;&amp; loc &lt; pos) {
return items[loc];
} else {
cout &lt;&lt; &quot;Location out of bounds.&quot; &lt;&lt; endl;
return -1; // or some error value
}
}

// Insert value at a specific location


void InsertAnyWhere(int val, int loc) {
if (loc &gt;= 0 &amp;&amp; loc &lt;= pos &amp;&amp; pos &lt; size) {
for (int i = pos; i &gt; loc; i--) {
items[i] = items[i - 1];
}
items[loc] = val;
pos++;
} else {
cout &lt;&lt; &quot;Location not correct or list is full.&quot; &lt;&lt; endl;
}
}

// Delete value at a specific location


void deleteAnyWhere(int loc) {
if (loc &gt;= 0 &amp;&amp; loc &lt; pos) {
for (int i = loc; i &lt; pos - 1; i++) {
items[i] = items[i + 1];
}
pos--;
} else {
cout &lt;&lt; &quot;Location out of bounds.&quot; &lt;&lt; endl;
}
}

// Display all values in the list


void display() {
if (pos == 0) {
cout &lt;&lt; &quot;List is empty.&quot; &lt;&lt; endl;
} else {
cout &lt;&lt; &quot;List items: &quot;;
for (int i = 0; i &lt; pos; i++) {
cout &lt;&lt; items[i] &lt;&lt; &quot; &quot;;
}
cout &lt;&lt; endl;
}
}

// Delete the last value in the list


void deleteVal() {
if (pos &gt; 0) {
pos--;
} else {
cout &lt;&lt; &quot;List is empty. Nothing to delete.&quot; &lt;&lt; endl;
}
}
~MyList() {
delete[] items; // Clean up dynamically allocated memory

}
};

int main() {
MyList list;
int val = 0, loc = 0;
int ch = 0;

do {
cout &lt;&lt; &quot;Press 1 for Add value in sequence&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Press 2 for Retrieve value&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Press 3 for Add value anywhere&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Press 4 for Display&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Press 5 for Delete in sequence&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Press 6 for Delete anywhere&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Press 7 for Quit&quot; &lt;&lt; endl;
cin &gt;&gt; ch;

switch (ch) {
case 1:
cout &lt;&lt; &quot;Enter value for add: &quot;;
cin &gt;&gt; val;
list.add(val);
break;

case 2:
cout &lt;&lt; &quot;Enter location to retrieve value: &quot;;
cin &gt;&gt; loc;
val = list.retrieve(loc);
if (val != -1) {
cout &lt;&lt; &quot;Value at location &quot; &lt;&lt; loc &lt;&lt; &quot;: &quot; &lt;&lt; val &lt;&lt;
endl;

}
break;

case 3:
cout &lt;&lt; &quot;Enter value to add: &quot;;
cin &gt;&gt; val;
cout &lt;&lt; &quot;Enter location to insert value: &quot;;
cin &gt;&gt; loc;
list.InsertAnyWhere(val, loc);
break;

case 4:
list.display();
break;

case 5:
list.deleteVal();
break;

case 6:
cout &lt;&lt; &quot;Enter location to delete value: &quot;;
cin &gt;&gt; loc;
list.deleteAnyWhere(loc);
break;
case 7:
exit(0);
break;

default:
cout &lt;&lt; &quot;Invalid choice&quot; &lt;&lt; endl;

}
} while (1);

return 0;
}

Create a list of atleast 10 students. Program should save the following information for each
student: Name, Reg. No., CGPA and Semester. Now perform the following tasks:
a) Display only those students whose CGPA is greater than 3.0
b) Display the students of 3 rd and 4 th semester
c) Display the students alphabetically

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

using namespace std;

struct Student {
string name;
string regNo;
float cgpa;
int semester;

Student(string n, string r, float c, int s) : name(n), regNo(r), cgpa(c), semester(s) {}


};

void displayHighCGPA(const vector&lt;Student&gt;&amp; students) {


cout &lt;&lt; &quot;Students with CGPA greater than 3.0:&quot; &lt;&lt; endl;
for (const auto&amp; student : students) {
if (student.cgpa &gt; 3.0) {
cout &lt;&lt; student.name &lt;&lt; &quot; (&quot; &lt;&lt; student.regNo &lt;&lt; &quot;) - CGPA:
&quot; &lt;&lt; student.cgpa &lt;&lt; &quot;, Semester:
&quot; &lt;&lt; student.semester &lt;&lt; endl;
}

}
}

void displaySemesters(const vector&lt;Student&gt;&amp; students) {


cout &lt;&lt; &quot;Students in 3rd and 4th semester:&quot; &lt;&lt; endl;
for (const auto&amp; student : students) {
if (student.semester == 3 || student.semester == 4) {
cout &lt;&lt; student.name &lt;&lt; &quot; (&quot; &lt;&lt; student.regNo &lt;&lt; &quot;) - CGPA:
&quot; &lt;&lt; student.cgpa &lt;&lt; endl;
}
}
}

void displayAlphabetically(vector&lt;Student&gt; students) {


sort(students.begin(), students.end(), [](const Student&amp; a, const Student&amp; b) {
return a.name &lt; b.name;
});
cout &lt;&lt; &quot;Students sorted alphabetically:&quot; &lt;&lt; endl;
for (const auto&amp; student : students) {
cout &lt;&lt; student.name &lt;&lt; &quot; (&quot; &lt;&lt; student.regNo &lt;&lt; &quot;) - CGPA:
&quot; &lt;&lt; student.cgpa &lt;&lt; &quot;, Semester: &quot;
&lt;&lt; student.semester &lt;&lt; endl;
}
}

int main() {
vector&lt;Student&gt; students = {
{&quot;Alice Smith&quot;, &quot;CS101&quot;, 3.5, 2},
{&quot;Bob Johnson&quot;, &quot;CS102&quot;, 2.9, 3},
{&quot;Charlie Brown&quot;, &quot;CS103&quot;, 3.8, 4},
{&quot;David Wilson&quot;, &quot;CS104&quot;, 3.2, 3},

{&quot;Eva Green&quot;, &quot;CS105&quot;, 3.7, 1},


{&quot;Frank White&quot;, &quot;CS106&quot;, 3.0, 4},
{&quot;Grace Hall&quot;, &quot;CS107&quot;, 3.1, 2},
{&quot;Hannah Adams&quot;, &quot;CS108&quot;, 2.5, 3},
{&quot;Ian Roberts&quot;, &quot;CS109&quot;, 3.9, 4},
{&quot;Julia Miller&quot;, &quot;CS110&quot;, 3.3, 2}
};

displayHighCGPA(students);
cout &lt;&lt; endl;

displaySemesters(students);
cout &lt;&lt; endl;

displayAlphabetically(students);
return 0;
}

Write a program containing two stacks of static arrays of size 5 each, elements of the array
should be of integer (int) type. The user will push values in the first stack, when a value is
popped from the first stack it should be pushed to the second stack. For each stack, you have to
check stack overflow and underflow conditions. User may remove an element from the second
stack as well.
#include <iostream>
using namespace std;

#define STACK_SIZE 5

class Stack {
private:
int arr[STACK_SIZE];
int top;

public:
Stack() : top(-1) {} // Initialize the stack with top as -1

// Push an element onto the stack


void push(int value) {
if (top >= STACK_SIZE - 1) {
cout << "Stack Overflow! Cannot push " << value << endl;
} else {
arr[++top] = value;
cout << value << " pushed onto the stack." << endl;
}
}
// Pop an element from the stack
int pop() {
if (top < 0) {
cout << "Stack Underflow! Cannot pop." << endl;
return -1;
} else {
int value = arr[top--];
cout << value << " popped from the stack." << endl;
return value;
}
}

// Check if the stack is empty


bool isEmpty() {
return top == -1;
}

// Display stack elements


void display() {
if (top < 0) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};

int main() {
Stack stack1, stack2; // Two stacks of size 5 each
int choice, value;

while (true) {
cout << "\nMenu:\n"
<< "1. Push to Stack 1\n"
<< "2. Pop from Stack 1 and Push to Stack 2\n"
<< "3. Pop from Stack 2\n"
<< "4. Display Stack 1\n"
<< "5. Display Stack 2\n"
<< "6. Exit\n"
<< "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to push onto Stack 1: ";
cin >> value;
stack1.push(value);
break;
case 2:
value = stack1.pop();
if (value != -1) { // Only push to Stack 2 if pop was successful
stack2.push(value);
}
break;
case 3:
stack2.pop();
break;
case 4:
cout << "Stack 1: ";
stack1.display();
break;
case 5:
cout << "Stack 2: ";
stack2.display();
break;
case 6:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}

Write a program which should implement a stack using a dynamic array, whose size will
provided by user at program execution time. Elements of array used for stack are objects of
“student” class. “student” class contains attributes (privately defined) reg_no(int), st_name
(string) and cgpa (float). “student” class should also contain member functions (publicly
defined); constructor, input and output functions. User will push objects of class “student”,
values of attributes of objects will be provided by user. When an object will be popped from
stack, value of its attributes should be displayed on screen.
#include <iostream>
#include <string>
using namespace std;
// Define the 'student' class
class Student {
private:
int reg_no;
string st_name;
float cgpa;

public:
// Constructor to initialize attributes
Student() : reg_no(0), st_name(""), cgpa(0.0) {}

// Function to input student details


void input() {
cout << "Enter Registration Number: ";
cin >> reg_no;
cout << "Enter Name: ";
cin.ignore();
getline(cin, st_name);
cout << "Enter CGPA: ";
cin >> cgpa;
}

// Function to display student details


void output() const {
cout << "Registration Number: " << reg_no << endl;
cout << "Name: " << st_name << endl;
cout << "CGPA: " << cgpa << endl;
}
};
// Define the 'Stack' class for 'Student' objects
class Stack {
private:
Student* arr; // Dynamic array of 'Student' objects
int top; // Index of the top element
int maxSize; // Maximum size of the stack

public:
// Constructor to initialize stack with given size
Stack(int size) : maxSize(size), top(-1) {
arr = new Student[maxSize];
}

// Destructor to free allocated memory


~Stack() {
delete[] arr;
}

// Function to push an object onto the stack


void push(const Student& student) {
if (top >= maxSize - 1) {
cout << "Stack Overflow! Cannot push more students.\n";
} else {
arr[++top] = student;
cout << "Student pushed onto the stack.\n";
}
}

// Function to pop an object from the stack


void pop() {
if (top < 0) {
cout << "Stack Underflow! No student to pop.\n";
} else {
cout << "Popped student details:\n";
arr[top--].output();
}
}

// Function to check if the stack is empty


bool isEmpty() const {
return top == -1;
}

// Function to display all elements in the stack


void display() const {
if (isEmpty()) {
cout << "Stack is empty.\n";
} else {
cout << "Students in stack:\n";
for (int i = 0; i <= top; i++) {
cout << "Student " << i + 1 << ":\n";
arr[i].output();
}
}
}
};

// Main function to interact with the stack


int main() {
int size;
cout << "Enter the size of the stack: ";
cin >> size;

// Create a stack with the user-defined size


Stack studentStack(size);

int choice;
while (true) {
cout << "\nMenu:\n"
<< "1. Push Student\n"
<< "2. Pop Student\n"
<< "3. Display Stack\n"
<< "4. Exit\n"
<< "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
Student student;
cout << "Enter details for the student:\n";
student.input();
studentStack.push(student);
break;
}
case 2:
studentStack.pop();
break;
case 3:
studentStack.display();
break;
case 4:
cout << "Exiting program.\n";
return 0;
default:
cout << "Invalid choice. Please try again.\n";
}
}
}

Write a program to implement an airport runway simulation. The airport has only one runway
so scheduling has to be done for the arrival and departure of planes on the runway. At one
time, only one plane can arrive or leave the airport. Scheduling has to be done on the basis of
first come, first serve. Whichever flight comes first at the airport will be given the space on the
runway. Similarly, whichever flight requests for departure first, will be given the space on the
runway.
[Hint: Arrival of plane=enqueue, Departure of plane=dequeue]
#include &lt;iostream&gt;
using namespace std;

class SimpleQueue {
int front, rear, capacity;
int* queue;

public:
SimpleQueue(int size) {
capacity = size;
queue = new int[capacity];
front = rear = -1;
}

void enqueue(int flightNumber) {


if (rear == capacity - 1) {
cout &lt;&lt; &quot;Queue is Full\n&quot;;
} else {
if (front == -1) front = 0;
rear++;
queue[rear] = flightNumber;

cout &lt;&lt; &quot;Flight &quot; &lt;&lt; flightNumber &lt;&lt; &quot; added to the queue\n&quot;;
}
}

int dequeue() {
if (front == -1 || front &gt; rear) {
cout &lt;&lt; &quot;Queue is Empty\n&quot;;
return -1;
} else {
int flightNumber = queue[front];
front++;
return flightNumber;
}
}

bool isEmpty() {
return (front == -1 || front &gt; rear);
}
};

int main() {
int runwayCapacity = 5;
SimpleQueue arrivalQueue(runwayCapacity);
SimpleQueue departureQueue(runwayCapacity);

int choice, flightNumber;

while (true) {

cout &lt;&lt; &quot;\n1. Add a flight to Arrival queue\n&quot;;


cout &lt;&lt; &quot;2. Add a flight to Departure queue\n&quot;;
cout &lt;&lt; &quot;3. Use the runway (process a flight)\n&quot;;
cout &lt;&lt; &quot;4. Exit\n&quot;;
cout &lt;&lt; &quot;Enter your choice: &quot;;
cin &gt;&gt; choice;

switch (choice) {
case 1:
cout &lt;&lt; &quot;Enter flight number to add to arrival queue: &quot;;
cin &gt;&gt; flightNumber;
arrivalQueue.enqueue(flightNumber);
break;

case 2:
cout &lt;&lt; &quot;Enter flight number to add to departure queue: &quot;;
cin &gt;&gt; flightNumber;
departureQueue.enqueue(flightNumber);
break;

case 3:
if (!arrivalQueue.isEmpty()) {
flightNumber = arrivalQueue.dequeue();
if (flightNumber != -1) {
cout &lt;&lt; &quot;Flight &quot; &lt;&lt; flightNumber &lt;&lt; &quot; is arriving on the runway.\
n&quot;;
}
} else if (!departureQueue.isEmpty()) {
flightNumber = departureQueue.dequeue();
if (flightNumber != -1) {
cout &lt;&lt; &quot;Flight &quot; &lt;&lt; flightNumber &lt;&lt; &quot; is departing from the runway.\
n&quot;;
}
} else {

cout &lt;&lt; &quot;No flights waiting for arrival or departure.\n&quot;;


}
break;

case 4:
cout &lt;&lt; &quot;Exiting...\n&quot;;
return 0;

default:
cout &lt;&lt; &quot;Invalid choice. Please try again.\n&quot;;
}
}

return 0;
}

Write a program which should implement a circular queue using static array of size 10 (10
elements array), elements of array should be of integer (int) type. User will input values to be
inserted at rear of circular queue (enqueue) and also number of elements to be removed from
front of circular queue (dequeue). Your program should display the value of elements which are
being removed from circular queue. Program should also calculate and display the average of
elements which have been removed from circular queue.
#include &lt;iostream&gt;
using namespace std;

class CircularQueue {
int front, rear, size, count;
int queue[10];
int removedSum;
int removedCount;

public:
CircularQueue() {
front = -1;
rear = -1;
size = 10;
count = 0;
removedSum = 0;
removedCount = 0;
}

void enqueue(int value) {


if ((rear + 1) % size == front) {
cout &lt;&lt; &quot;Queue is Full. Cannot enqueue &quot; &lt;&lt; value &lt;&lt; endl;
} else {
if (front == -1) {
front = 0;
}
rear = (rear + 1) % size;
queue[rear] = value;
count++;
cout &lt;&lt; &quot;Enqueued: &quot; &lt;&lt; value &lt;&lt; endl;
}
}

void dequeue(int numElements) {


if (front == -1) {
cout &lt;&lt; &quot;Queue is Empty. No elements to dequeue.&quot; &lt;&lt; endl;
return;
}

for (int i = 0; i &lt; numElements &amp;&amp; count &gt; 0; i++) {


int removedValue = queue[front];
cout &lt;&lt; &quot;Dequeued: &quot; &lt;&lt; removedValue &lt;&lt; endl;
removedSum += removedValue;
removedCount++;

if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % size;
}
count--;
}

if (removedCount &gt; 0) {
double average = static_cast&lt;double&gt;(removedSum) / removedCount;
cout &lt;&lt; &quot;Average of removed elements: &quot; &lt;&lt; average &lt;&lt; endl;
}
}
};

int main() {
CircularQueue cq;
int choice, value, numDequeue;

while (true) {
cout &lt;&lt; &quot;\n1. Enqueue value\n&quot;;
cout &lt;&lt; &quot;2. Dequeue values\n&quot;;
cout &lt;&lt; &quot;3. Exit\n&quot;;
cout &lt;&lt; &quot;Enter your choice: &quot;;
cin &gt;&gt; choice;

switch (choice) {
case 1:
cout &lt;&lt; &quot;Enter value to enqueue: &quot;;
cin &gt;&gt; value;
cq.enqueue(value);
break;

case 2:
cout &lt;&lt; &quot;Enter number of elements to dequeue: &quot;;
cin &gt;&gt; numDequeue;
cq.dequeue(numDequeue);
break;

case 3:
cout &lt;&lt; &quot;Exiting...\n&quot;;
return 0;

default:
cout &lt;&lt; &quot;Invalid choice. Try again.\n&quot;;
}
}

return 0;
}

Implement the following functions for singly linkedlist:


1) Insert front
2) Insert End
3) Insert after number
4) Delete front
5) Delete End
6) Delete any number from list
7) Display all element
8) Limit user so that he/she can only add 10 numbers in the list. After that give error that list
is full.

Insert front
void insertFront(Node*&amp; head, int newData) {
Node* newNode = new Node();
newNode-&gt;data = newData;
newNode-&gt;next = head;
head = newNode;
}
Insert End

void insertEnd(Node*&amp; head, int newData) {


Node* newNode = new Node();
newNode-&gt;data = newData;
newNode-&gt;next = nullptr;
if (head == nullptr) {
head = newNode;
return;
}

Node* temp = head;


while (temp-&gt;next != nullptr) {
temp = temp-&gt;next;
}

temp-&gt;next = newNode;

Insert after number

void insertAfterNumber(Node* head, int target, int newData) {


Node* temp = head;

while (temp != nullptr &amp;&amp; temp-&gt;data != target) {


temp = temp-&gt;next;
}

if (temp != nullptr) {
Node* newNode = new Node();
newNode-&gt;data = newData;
newNode-&gt;next = temp-&gt;next;
temp-&gt;next = newNode;
} else {
cout &lt;&lt; &quot;Node with value &quot; &lt;&lt; target &lt;&lt; &quot; not found.&quot; &lt;&lt;
endl;
}
}

Delete front
void deleteFront(Node*&amp; head) {
if (head == nullptr) {
cout &lt;&lt; &quot;List is already empty.&quot; &lt;&lt; endl;
return;
}

Node* temp = head;


head = head-&gt;next;
delete temp;

Delete End

void deleteEnd(Node*&amp; head) {


if (head == nullptr) {
cout &lt;&lt; &quot;List is already empty.&quot; &lt;&lt; endl;
return;
}

if (head-&gt;next == nullptr) {
delete head;
head = nullptr;
return;
}

Node* temp = head;

while (temp-&gt;next-&gt;next != nullptr) {


temp = temp-&gt;next;
}

delete temp-&gt;next;
temp-&gt;next = nullptr;
}

Delete any number from list

void deleteNode(Node*&amp; head, int target) {


if (head == nullptr) {
cout &lt;&lt; &quot;List is empty.&quot; &lt;&lt; endl;
return;
}

if (head-&gt;data == target) {
Node* temp = head;
head = head-&gt;next;
delete temp;
return;
}
Node* temp = head;
while (temp-&gt;next != nullptr &amp;&amp; temp-&gt;next-&gt;data != target) {
temp = temp-&gt;next;
}

if (temp-&gt;next != nullptr) {
Node* nodeToDelete = temp-&gt;next;
temp-&gt;next = temp-&gt;next-&gt;next;
delete nodeToDelete; /
} else {
cout &lt;&lt; &quot;Node with value &quot; &lt;&lt; target &lt;&lt; &quot; not found.&quot; &lt;&lt;
endl;
}
}

Display all element


void displayList(Node* head) {
Node* temp = head;

if (temp == nullptr) {
cout &lt;&lt; &quot;List is empty.&quot; &lt;&lt; endl;
return;
}

while (temp != nullptr) {


cout &lt;&lt; temp-&gt;data &lt;&lt; &quot; -&gt; &quot;;
temp = temp-&gt;next;
}
cout &lt;&lt; &quot;NULL&quot; &lt;&lt; endl; Indicate the end of the
}list

Limit user so that he/she can only add 10 numbers in the list. After that give error that list
is full.

void insertEnd(Node*&amp; head, int newData) {


if (nodeCount &gt;= MAX_SIZE) {
cout &lt;&lt; &quot;Error: List is full. Cannot add more elements.&quot; &lt;&lt; endl;
return;
}

Node* newNode = new Node();


newNode-&gt;data = newData;
newNode-&gt;next = nullptr;

if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp-&gt;next != nullptr) {
temp = temp-&gt;next;

}
temp-&gt;next = newNode;
}
nodeCount++;
}

Stack code
#include<iostream>
using namespace std;
class MyStack
{
private:
int *stack;
int size;
int top;
public:
MyStack()
{
cout<<"Enter size of Stck"<<endl;
cin>>size;
stack=new int[size];
top=0;
}
bool IsFull()
{
if(top==size)
return true;
else
return false;
}
bool IsEmpty()
{
if(top==0)
return true;
else
return false;

}
void push(int val)
{
if(IsFull()==0)
{
stack[top]=val;
top++;
}
else
cout<<"Stack OverFlow"<<endl;
}
void POP()
{
if(IsEmpty()==0)
{
cout<<stack[top-1]<<" Is POPED"<<endl;
top--;
}
else
{
cout<<"Stack UnderFlow"<<endl;
}
}
};
void main()
{
int rerun;
int choice;
int val;
MyStack S1;
do{
cout<<"Press 1 to PUSH"<<endl;
cout<<"Press 2 to POP"<<endl;
cin>>choice;
if(choice==1)
{
cout<<"Enter value to push"<<endl;
cin>>val;
S1.push(val);
}
else if(choice==2)
{
S1.POP();
}
else
{
cout<<"Invalid choice"<<endl;
}
cout<<"Press 1 to rerun"<<endl;
cin>>rerun;
}while(rerun==1);
}

Ass1
#include <iostream>

#include <string>

using namespace std;

class Student {

private:

int id;

string name;

string phoneNo;
public:

void setId(int studentId) {

id = studentId;

void setName(string studentName) {

name = studentName;

void setPhoneNo(string studentPhoneNo) {

phoneNo = studentPhoneNo;

int getId() {

return id;

string getName() {

return name;

string getPhoneNo() {

return phoneNo;

void inputData() {
cout << "Enter student ID: ";

cin >> id;

cin.ignore();

cout << "Enter student name: ";

getline(cin, name);

cout << "Enter student phone number: ";

getline(cin, phoneNo);

void outputData() {

cout << "Student ID: " << id << endl;

cout << "Student Name: " << name << endl;

cout << "Student Phone Number: " << phoneNo << endl;

};

int main() {

Student student1;

student1.inputData();

cout << "\nStudent Details:\n";

student1.outputData();

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy