0% found this document useful (0 votes)
12 views

Lab Task 6

Uploaded by

CHOCOLATE YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lab Task 6

Uploaded by

CHOCOLATE YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

23108189

Syed Ali Tarikh


Lab Task 6

Code:
#include <iostream>
#include <string>
using namespace std;

struct Student {
int regNo;
string name;
string address;
string degree;
int semester;
Student* next;

Student(int r, string n, string a, string d, int s)


: regNo(r), name(n), address(a), degree(d), semester(s), next(NULL) {}
};

class Queue {
private:
Student* front;
Student* back;

public:
Queue() : front(NULL), back(NULL) {}
bool isempty() {
return front == NULL;
}

void enqueue(int regNo, string name, string address, string degree, int semester) {
Student* newStudent = new Student(regNo, name, address, degree, semester);
if (isempty()) {
front = back = newStudent;
return;
} else {
back->next = newStudent;
back = newStudent;
}
}

void dequeue() {
if (isempty()) {
cout << "Queue is empty, cannot dequeue." << endl;
return;
}
Student* temp = front;
front = front->next;
delete temp;
}

void deleteAtPosition(int pos) {


if (isempty() || pos < 1) return;
if (pos == 1) {
dequeue();
return;
}
Student* current = front;
for (int i = 1; current != NULL && i < pos - 1; i++) {
current = current->next;
}
if (current == NULL || current->next == NULL) return;
Student* temp = current->next;
current->next = temp->next;
delete temp;
}

void displayTop() {
if (!isempty()) {
cout << "Top Student: " << front->name << ", Reg#: " << front->regNo
<< ", Address: " << front->address << ", Degree: " << front->degree
<< ", Semester: " << front->semester << endl;
}
}

void displayAll() {
if (isempty()) {
cout << "Queue is empty." << endl;
return;
}
Student* current = front;
while (current != NULL) {
cout << "Name: " << current->name << ", Reg#: " << current->regNo
<< ", Address: " << current->address << ", Degree: " << current->degree
<< ", Semester: " << current->semester << endl;
current = current->next;
}
}
};

int main() {
Queue qu;
qu.enqueue(101, "Ali", "123 St", "CS", 3);
qu.enqueue(102, "Babar", "456 St", "IT", 2);
qu.enqueue(103, "Chaudary", "789 St", "SE", 1);
qu.enqueue(104, "Dave", "101 St", "EE", 4);
qu.enqueue(105, "Even", "202 St", "ME", 2);
qu.enqueue(106, "Frady", "303 St", "CE", 3);
qu.enqueue(107, "Grade", "404 St", "BA", 1);
qu.enqueue(108, "Hansk", "505 St", "CS", 2);
qu.enqueue(109, "Ivsy", "606 St", "IT", 4);
qu.enqueue(110, "Jakae", "707 St", "SE", 3);

cout << "All Students:" << endl;


qu.displayAll();

cout << "\nTop Student:" << endl;


qu.displayTop();
cout << "\nDeleting student at position 6..." << endl;
qu.deleteAtPosition(6);

cout << "\nAll Students after deletion:" << endl;


qu.displayAll();

return 0;
}
OUTPUT

You might also like