Uploaded by Javaria Tanveer

task1circular

advertisement
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* prev;
Node* next;
Node()
{
prev = next = NULL;
}
};
class DLL
{
Node* head;
Node* tail;
public:
DLL()
{
head = tail = NULL;
}
void insertAtHead(int data)
{
Node* N = new Node;
N->data = data;
Node* temp = head;
if (head == NULL)
{
head = tail = N;
}
else
{
head = N;
head->next = temp;
temp->prev = N;
tail->next = head;
head->prev = tail;
//N->next = head;
//head->prev = N;
//head = N;
}
}
void insertAtTail(int data)
{
Node* N = new Node;
N->data = data;
if (head == NULL)
{
head = tail = N;
}
else
{
tail->next = N;
N->prev = tail;
tail = N;
tail->next = head;
head->prev = tail;
}
}
void display()
{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
Node* temp = head;
while (temp != tail)
{
cout << temp->data << " , ";
temp = temp->next;
}
cout << temp->data << " , ";
}
}
Node* search(int value)
{
if (head == NULL)
{
return NULL;
}
else
{
Node* temp = head;
while (temp != NULL)
{
if (temp->data == value)
return temp;
else
temp = temp->next;
}
}
}
void deleteAnode(int value)
{
if (head == NULL)
{
cout << "list is empty" << endl;
}
else
{
Node* temp = head;
while (temp != tail)
{
if (temp->data == value)
{
if (temp->data == head->data)
{
head = temp->next;
tail->next = head;
head->prev = tail;
delete temp;
break;
}
else
{
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
delete temp;
break;
}
}
temp = temp->next;
}
}
}
void insertAtPosition(int value, int data)
{
Node* N = new Node;
N->data = data;
Node* find = search(value);
if (find == NULL)
{
cout << value << " not found" << endl;
}
else
{
if (find == tail)
{
insertAtTail(data);
}
else
{
find->next->prev = N;
N->next = find->next;
find->next = N;
N->prev = find;
}
}
}
void count()
{
int count1 = 1;
Node* temp = head;
while (temp != tail)
{
count1++;
temp = temp->next;
}
cout << count1 << endl;
}
void acending()
{
if (head == NULL)
{
cout<<"list is empty"<<endl;
}
else
{
Node* temp = head;
while (temp->next != tail)
{
while (temp->next != tail)
{
if (temp->data < temp->next->data)
{
if (temp == head)
{
temp->next = temp->next->next;
temp->next->next->prev = temp;
temp->prev = temp->next;
temp->next = temp;
head = temp->prev;
head->prev = tail;
tail->next = head;
}
else if (temp == tail)
{
temp->next = temp->next->next;
temp->next->next->prev = temp;
temp->prev = temp->next;
temp->next = temp;
tail = temp->prev;
tail->next = head;
head->prev = tail;
}
else
{
temp->next = temp->next->next;
temp->next->next->prev = temp;
temp->prev = temp->next;
temp->next->prev = temp;
}
temp = temp->next;
}
}
}
}
}
~DLL()
{
cout << endl;
cout << "dextructor called" << endl;
delete head;
delete tail;
}
};
int main()
{
DLL list;
list.insertAtHead(10);
list.insertAtHead(20);
list.insertAtHead(100);
list.insertAtTail(15);
list.insertAtTail(150);
list.insertAtTail(101);
list.insertAtPosition(100, 200);
list.deleteAnode(100);
//list.insertAtPosition(20, 155);
//list.insertAtPosition(150, 199);
list.display();
return 0;
}
Download