Problem mit Zeigern



  • Hallo!

    Habe mir eine doppelt verkette Liste gebastelt:

    #include <iostream>
    
    using namespace std;
    
    struct Node
    {
    	int value;
    	Node *pred, *succ;
    };
    
    Node *head=NULL;
    
    void insert(int info)
    {
    	Node *tmp;
    
    	tmp=new Node;
    	tmp->value=info;
    
    	tmp->pred=NULL;
    	tmp->succ=head;
    	head=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 = new Node;
    
    	insert(5);
    	insert(4);
    	insert(3);
    
    	print(head);
    
    	return 0;
    }
    

    Klappt alles so weit auch. Doch wie kann ich mir den Wert des Vorgängers ausgeben lassen?
    Irgendwelche Tipps?

    THX

    Tobias



  • pos->pred->value



  • Dazu sollte er aber pred erstmal irgendwo auf etwas anderes als NULL setzen.



  • Das weiß ich auch. Nur, mein problem liegt in der insert-Funktion:

    tmp->pred=NULL;
    

    Wie Weise ich den Vorgänger zu?



  • if (head)
        head->pred = tmp;
    

    ?



  • in insert fehlt das:

    if (tmp->succ)
      tmp->succ->pred = tmp;
    

    Wenn du einfügen in der Mitte implementierst wird es noch ekliger mit den Spezialfällen.



  • Danke! Dann mach ich mich mal an die anderen Fälle!

    😃



  • 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;
    }
    

Anmelden zum Antworten