//=======================================================================
// Basic C++: a simple (and uncomplete) class List of int's
//      Utilization
//-----------------------------------------------------------------------
// Jean-Paul Rigault --- Copyright 2003
// $Id: main_List.cpp,v 1.4 2004/11/20 17:02:51 jpr Exp $
//=======================================================================

#include "list-copy.h"
void List::append(int v)
{
    Cell *p = new Cell(v);
    if (head == 0) head = tail = p;
    else
    {
        tail->next = p;
        tail = p;
    }
}

// Au debut

void List::prepend(int v)
{
    Cell *p = new Cell(v);
    if (head == 0) head = tail = p;
    else
    {
        p->next = head;
        head = p;
    }
}

void List::insert(int e)
{
    Cell *p, *pprev;

    for (p = head, pprev = 0; p != 0 && p->val < e; pprev = p, p = p->next) {}

    if (pprev != 0) // inserting in the middle or at the end
    {
        Cell *ptemp = new Cell(e, pprev->next);
        pprev->next = ptemp;
        if (p == 0) tail = ptemp;
    }
    else prepend(e);   // inserting in front
}

//-----------------------------------------------------------------------
// Removing elements from the list
//-----------------------------------------------------------------------

int List::get_first()
{
    if (head == 0) throw Empty();
    int v = head->val;
    if (tail == head) tail = 0;
    Cell *p = head;
    head = head->next;
    p->next = 0; // needed: otherwise the next line destroy the whole list!
    delete p;
    return v;
}


void List::mirror()
{
	Cell *pp;	// cellule précédente
	Cell *p;	// cellule courante
	Cell *tmp;	// variable temporaire

	for (pp=0, p=head; p!=0; pp=p, p=tmp) // <<p>> parcourt toute la liste
	{
		tmp=p->next;  // sauvegarde de l'ancienne valeur de p->next
		p->next=pp;
	}

	// il ne reste plus qu'à échanger les valeurs de head et tail :
	//
	tmp=head; head=tail; tail=tmp;
}


//-----------------------------------------------------------------------
// Display
//-----------------------------------------------------------------------

ostream& operator<<(ostream& os, const List& l)
{
    List::Cell *p;
    for (p = l.head; p != 0; p = p->next) cout << p->val << ' ';
    return os;

}

int List::get_middle_value() const
{
	Cell *normal;	// (normal) pointer
	Cell *fast;	// (fast) pointer

	for (normal=fast=head; fast!=0 && fast->next !=0; normal=normal->next, fast=fast->next->next) ;  // nothing in the loop
	return normal->val;
}

int List::max() const
{
	int r;
	if ( is_empty() ) throw Empty();

	r=head->val;
	for (Cell *p=head->next; p != 0; p=p->next)
		if (p->val > r) r=p->val;
	return r;
}

int &List::max()
{
	Cell *rp = head; // Result Pointer: more precisely, a Pointer to the Cell containing the result

	if ( is_empty() ) throw Empty();

	for (Cell *p=head->next; p != 0; p=p->next)
		if (p->val > rp->val) rp=p;
	return rp->val;
}
int sum(List l)
{
	int s=0;

	while ( !l.is_empty() ) s += l.get_first();
	return s;
}
List& List::operator=(const List& l)
{
   
    while (!is_empty())
        get_first();

    for (Cell *p = l.head; p != 0; p = p->next)
        append(p->val);

    return *this;
}
List::List(const List& l)
{
    head = 0;
    tail = 0;

    for (Cell* p = l.head; p != 0; p = p->next)
    {
        append(p->val);
    }
}
List::~List()
{
     cout << "Destructeur appele" << endl;
    while (!is_empty())
        get_first();
}
List operator+(const List& l1, const List& l2)
{
   
    List result;

    result.head = l1.head;
    result.tail = l1.tail;
    result.tail->next = l2.head;
    result.tail = l2.tail;

    return result;
}
int main()
{
    int i;

    MSG(List operations);
    
	List la, lb, l1, l2;

    OUT(l1);
    for (i = 0; i < 10; i++)
    {
        l1.append(i);
        l2.prepend(i);
    }
    OUT(l1); OUT(l2);

    l1.prepend(99);
    l2.append(99);
    OUT(l1); OUT(l2);

    OUT(l1.get_first()); OUT(l1);
    OUT(l2.get_first()); OUT(l2);

    while (!l1.is_empty()) OUT(l1.get_first());
    OUT(l1);

    MSG(List copy);

	lb.append(1); lb.append(2); lb.append(3);
	
    la = lb;
	la.max() = 100;
    OUT(la); OUT(lb);
	
	l1.append(1); l1.append(2); l1.append(3);
	
    l1 = l2;
    l2.append(73);
    l1.append(74);
    OUT(l1); OUT(l2);

   
    List l3;
    l3.append(1);
    l3.append(2);
    l3.append(3);
    OUT( sum(l3) );
	
    OUT(l3);
    l3.append(22);
    OUT(l3);
    OUT(l1);
    OUT(l2);
    OUT(l1+l2);
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: