Mid Exame

You might also like

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

#include <iostream>

using namespace std;


struct Node
{
string item;
string data;
Node* next, * prev;
}; Node* first = NULL, * last = NULL;
Node* current;
class DLL{
string item;
int count=0;
public:
bool isempty() {
return (first == NULL);
}
void insertfirst(string item)
{
Node* n = new Node;
n->data = item;
if (isempty())
{
first = last = n;
n->next = n->prev = NULL;
}
else
{
n->next = first;
first->prev = n;
n->prev = NULL;
first = n;
}
count++;

}
void insertlast(string item)
{
Node* n = new Node;
n->data = item;
if (isempty())
{
first = last = n;
n->next = n->prev = NULL;
}
else
{
last->next = n;
n->prev = last;
n->next = NULL;
last = n;
}
count++;
}
void insertmid(int post, string item)
{
if (post == 0 || post < 0)
cout << "i cant insert from the linklist" << endl;
Node* n = new Node;
n->data = item;
if (post == 0)
insertfirst(item);
else if (post == count)
insertlast(item);
else
{
Node* current = first;
for (int i = 1; i < post; i++)
current = current->next;
n->next = current->next;
n->prev = current;
current->next->prev = n;
current->next = n;
count++;
}
}
void print() {
Node* n = new Node;
n = first;
while (n != NULL) {
cout << n->data << endl;
n = n->next;
}

}
void insertnewlast(string item) {
Node* n = new Node;
n->data = item;
if (isempty())
{
first = last = n;
n->next = n->prev = NULL;
}
else
{
last->next = n;
n->prev = last;
n->next = NULL;
last = n;
}
count++;
}
};

int main()
{
DLL F;
F.insertfirst(A);
F.insertlast(D);
F.insertmid(1, B);
F.insertmid(2, C);
F.print();
F.insertnewlast(E);
return 0;
}

You might also like