You are on page 1of 5

Department of Software Engineering

Data Structures & Algorithms Lab


Lab # 9

SUBMITTED TO:
Sir Rehan Ahmed Siddiqui
SUBMITTED BY:
Mahnoor Mustafa
(2020-BSE-051)
CLASS:
BSE III B
Task 1:
Code Task 1:
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
class Circularlist
{
public:
Node* head;
Circularlist()
{
head = NULL;
}
//TRAVERSE
void traverse()
{
Node* temp = head;
if (head != NULL)
{
do
{
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
}
cout << endl;
}
//ADD_BEGINNING
void add_begin(int x)
{
Node* ptr1 = new Node;
Node* temp = head;
ptr1->data = x;
ptr1->next = head;
if (head != NULL)
{
while (temp->next != head)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;
head = ptr1;
}
//ADD_END
void add_end(int x)
{
Node* temp, * ptr;
temp = new Node;
temp->data = x;
if (head == NULL)
{
head = temp;
temp->next = head;
}
else
{
ptr = head;
while (ptr->next != head)
ptr = ptr->next;
temp->next = head;
ptr->next = temp;
}
}
//ADD_SPECIFIIC
void add(int pos, int x)
{
Node* temp, * t;
temp = head;
for (int i = 1; i < pos; i++)
{
temp = temp->next;
if (temp == NULL)
{
cout << "There are less than ";
cout << pos << "Elements. " << endl;
return;
}
}
t = new Node;
t->data = x;
t->next = temp->next;
temp->next = t;
}
void del_beg()
{
Node* temp, * temp1;
temp = head;
if (head == NULL)
{
cout << "\nList has no nodes";
return;
}
if (head->next == head)
{
head = NULL;
delete temp;
return;
}
temp1 = head;
while (temp1->next != head)
temp1 = temp1->next;
head = temp->next;
temp1->next = head;
delete temp;
}
void del_end()
{
Node* temp, * t;
temp = head;
if (temp == NULL)
{
cout << "List does not exist" << endl;
return;
}
if (head->next == head)
{
delete temp;
head = NULL; return;
}
t = head->next;
while (t->next != head)
{
t = t->next;
temp = temp->next;
}
temp->next = head;
delete t;
}
};
int main ()
{
Circularlist c1;
c1.add_begin(30);
c1.add_begin(20);
c1.add_end(10);
c1.add(20, 15);
c1.del_beg();
c1.del_end();
c1.traverse();
system("pause");
return 0;
}

You might also like