S
Hier mal die komplette Implementierung, für die die's interessiert:
#include <iostream>
using namespace std;
struct Node
{
int value;
Node *pred, *succ;
};
Node *head=NULL;
void add_front(int info)
{
Node *tmp, *n;
tmp=new Node;
tmp->value=info;
tmp->pred=NULL;
tmp->succ=head;
if (head) head->pred = tmp;
head=tmp;
//if (tmp->succ) tmp->succ->pred = tmp;
}
void delete_front(void)
{
Node *tmp;
if(head==NULL)
return;
else
{
tmp = head;
head = head->succ;
head->pred=NULL;
delete tmp;
}
}
void add_rear(int info)
{
Node *tmp, *pos;
tmp = new Node;
tmp->value=info;
pos=head;
if(head==NULL)
add_front(info);
else
{
while(pos->succ)
{
pos=pos->succ;
}
pos->succ=tmp;
tmp->pred=pos;
tmp->succ=NULL;
}
}
void delete_rear()
{
Node *tmp, *pos;
pos=head;
if(head==NULL)
return;
else
{
while(pos->succ)
{
pos=pos->succ;
}
tmp=pos;
pos->pred->succ=NULL;
delete tmp;
}
}
void add_after(Node *p, int info)
{
Node *neu;
neu = new Node;
neu->value=info;
if(!p->succ)
add_rear(info);
else
{
neu->succ=p->succ;
p->succ->pred=neu;
p->succ=neu;
neu->pred=p;
}
}
void delete_after(Node *p)
{
Node *tmp;
tmp=p->succ;
p->succ=tmp->succ;
tmp->succ->pred=p;
delete tmp;
}
void print(Node *first)
{
Node *pos;
pos=first;
int i=1;
cout << "HEAD" << endl;
cout << "-------------------------------" << endl;
while(pos)
{
cout << i << ". Wert: " << pos->value << endl;
if(pos->succ)
cout << "Wert Nachfolger: " << (pos->succ)->value << endl;
if(pos->pred)
cout << "Wert Vorgaenger: " << (pos->pred)->value << endl;
cout << "-------------------------------" << endl;
pos=pos->succ;
i++;
}
}
int main(void)
{
Node *n, *p;
n = new Node;
add_front(5);
add_front(4);
add_front(3);
add_front(2);
add_front(1);
print(head);
cout << "*********************************************" << endl;
delete_front();
print(head);
cout << "*********************************************" << endl;
add_rear(10);
print(head);
cout << "*********************************************" << endl;
delete_rear();
print(head);
cout << "*********************************************" << endl;
p=head->succ;
add_after(p, 22);
print(head);
cout << "*********************************************" << endl;
delete_after(p);
print(head);
return 0;
}