You are on page 1of 14

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Data Structures
and Algorithm
Assignment 01

Submitted To: Sir Affan Ahmad Toor

Student Name: HAROON

Reg. Number: 2080173

DSA (Lab) BSSE-3B SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

1. Title
Implement a doubly circular linked list of students in SZABIST using
classes. The ‘Student’class should have following data members:
1. Student Name
2. Registration Number
3. CGPA
4. Phone Number
The linked list must have following functions:
1. Insert node in ascending order according to the registration
2. Delete node at beginning of the list
3. Delete node at the end of the list
4. Delete node at random location in the list
5. Search a node(s) by:
a. Student name
b. Registration number
c. Phone number
6. Reverse the linked list in descending order of the registration no
7. Make a copy of a selected node in linked list
8. Traverse all nodes and display the student information in TB.
Code:

#include <iostream>
#include <string>

using namespace std;

class student
{
public:
string name;
int reg_no;
float gpa;
int phone;
student* next;
student* prev;
student()
{
name = "";
reg_no = 0;
gpa = 0;
phone = 0;
next = NULL;
prev = NULL;
}
student(string name, int reg_no, float gpa, int phone)
{
this->name = name;
this->reg_no = reg_no;
this->gpa = gpa;
this->phone = phone;
next = NULL;
prev = NULL;
}
void display()
{
cout << "Name: " << name << endl;
cout << "Registration Number: " << reg_no << endl;
cout << "gpa: " << gpa << endl;
cout << "Phone: " << phone << endl;
}
};

class LinkedList
{
public:
student* head;
student* tail;
LinkedList()
{
head = NULL;
tail = NULL;
}
void insert(student* p)
{
if (head == NULL)
{
head = p;
tail = p;
p->next = head;
p->prev = tail;
}
else
{
if (p->reg_no < head->reg_no)
{
p->next = head;
p->prev = tail;
head->prev = p;
tail->next = p;
head = p;
}
else if (p->reg_no > tail->reg_no)
{
p->next = head;
p->prev = tail;
tail->next = p;
head->prev = p;
tail = p;
}
else
{
student* temp = head;
while (temp->reg_no < p->reg_no)
{
temp = temp->next;
}
p->next = temp;
p->prev = temp->prev;
temp->prev->next = p;
temp->prev = p;
}
}
}
void delete_begin()
{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
student* temp = head;
head = head->next;
head->prev = tail;
tail->next = head;
delete temp;
}

}
void delete_end()
{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
student* temp = tail;
tail = tail->prev;
tail->next = head;
head->prev = tail;
delete temp;
}
}

void delete_random(int n)
{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
student* temp = head;
for (int i = 0; i < n; i++)
{
temp = temp->next;
}
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
}
}

void search_name(string name)


{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
student* temp = head;
while (temp->name != name)
{
temp = temp->next;
}
temp->display();
}
}

void search_reg_no(int reg_no)


{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
student* temp = head;
while (temp->reg_no != reg_no)
{
temp = temp->next;
}
temp->display();
}
}

void search_phone(int phone)


{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
student* temp = head;
while (temp->phone != phone)
{
temp = temp->next;
}
temp->display();
}
}

void insert_end(student* p)
{
if (head == NULL)
{
head = p;
tail = p;
p->next = head;
p->prev = tail;
}
else
{
p->next = head;
p->prev = tail;
head->prev = p;
tail->next = p;
tail = p;
}
}

void insert_random(student* p, int n)


{
if (head == NULL)
{
head = p;
tail = p;
p->next = head;
p->prev = tail;
}
else
{
student* temp = head;
for (int i = 0; i < n; i++)
{
temp = temp->next;
}
p->next = temp;
p->prev = temp->prev;
temp->prev->next = p;
temp->prev = p;
}
}

void reverse()
{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
student* temp = head;
while (temp->next != head)
{
temp = temp->next;
}
while (temp != head)
{
student* temp2 = temp->prev;
temp->prev = temp->next;
temp->next = temp2;
temp = temp->prev;
}
}
}
void display()
{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
student* temp = head;
while (temp->next != head)
{
temp->display();
temp = temp->next;
}
temp->display();
}
}
};

int main()
{
LinkedList list;
int choice;
int reg_no;
string name;
float gpa;
int phone;
student* p;
while (1)
{
cout << "1. Insert at beginning" << endl;
cout << "2. Insert at end" << endl;
cout << "3. Insert at random location" << endl;
cout << "4. Delete at beginning" << endl;
cout << "5. Delete at end" << endl;
cout << "6. Delete at random location" << endl;
cout << "7. Search by name" << endl;
cout << "8. Search by registration number" << endl;
cout << "9. Search by phone number" << endl;
cout << "10. Reverse" << endl;
cout << "11. Display" << endl;
cout << "12. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter name : ";
cin >> name;
cout << "Enter registration number: ";
cin >> reg_no;
cout << "Enter gpa : ";
cin >> gpa;
cout << "Enter phone number: ";
cin >> phone;
p = new student(name, reg_no, gpa, phone);
list.insert(p);
break;
case 2:
cout << "Enter name: ";
cin >> name;
cout << "Enter registration number: ";
cin >> reg_no;
cout << "Enter gpa: ";
cin >> gpa;
cout << "Enter phone number: ";
cin >> phone;
p = new student(name, reg_no, gpa, phone);
list.insert_end(p);
break;
case 3:
cout << "Enter name: ";
cin >> name;
cout << "Enter registration number: ";
cin >> reg_no;
cout << "Enter gpa: ";
cin >> gpa;
cout << "Enter phone number: ";
cin >> phone;
p = new student(name, reg_no, gpa, phone);
cout << "Enter the location: ";
cin >> choice;
list.insert_random(p, choice);
break;
case 4:

list.delete_begin();
break;

case 5:
list.delete_end();
break;

case 6:
cout << "Enter the location: ";
cin >> choice;
list.delete_random(choice);
break;

case 7:
cout << "Enter name: ";
cin >> name;
list.search_name(name);
break;

case 8:
cout << "Enter registration number: ";
cin >> reg_no;
list.search_reg_no(reg_no);
break;

case 9:
cout << "Enter phone number: ";
cin >> phone;
list.search_phone(phone);
break;

case 10:
list.reverse();
break;
case 11:

list.display();
break;

case 12:
exit(0);
break;
default:
cout << "Invalid choice" << endl;
break;
}
}
return 0;
}

OUTPUT

You might also like