Liste in C++

From HaskellWiki
Revision as of 20:53, 28 March 2011 by Ha$kell (talk | contribs) (liste in C++ versus liste in Haskell)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Un cod pentru liste in C++, doar ca exemplu minimal, si spre a se face comparatii intre limbaje.

#include <iostream>
#include <stdio.h>

using namespace std;

template <class E>
class Link
{
public: E element;
public: Link <E>  * next;


// Singly Linked Link node
// Value for this node
// Pointer to next node in Link
// Constructors

Link()
  { next = this; }


Link(E it, Link <E> * nextval)
  { element = it; next = nextval; }

Link(Link <E>  * nextval) { next = nextval; }

Link <E> * get_next() { return next; }

Link <E>  * setNext(Link <E> * nextval)
  { return next = nextval; }

E get_element() { return element; }

E setElement(E it) { return element = it; }

}; // class Link

int main()
{   cout << "Test Liste"<<endl;
    Link<int> Vida;
    Link<int> Lista1 (1, &Vida );
    Link<int> Lista2 (2, &Lista1 );
    cout << Lista1.element;
    cout <<endl;
    cout << Lista2.element;
    cout << Lista2.next->element;
    cout <<endl;

    cout << "Test Liste"<<endl;
    Link<char> VidaC;
    Link<char> Lista3 ('a', &VidaC );
    Link<char> Lista4 ('b', &Lista3 );
    cout << Lista3.element;
    cout <<endl;
    cout << Lista4.element;
    cout << Lista4.get_next()->element;
    getchar();
    return 0;


}

... va urma