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

Mid Exame

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

Mid Exame

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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