All pastes #414043 Raw Edit

Untitled

public text v1 · immutable
#414043 ·published 2007-03-28 20:39 UTC
rendered paste body
#include <cstdlib>
#include <iostream>

using namespace std;

struct lista
 {
       double element;
       lista *next;
       };
lista *head=NULL;

void TworzListe();
void insert(double liczba, lista *pos);
void deletel(lista *wsk);
void printlist();
void printlistrev();
inline lista *first();
inline lista *next(lista *wsk);
inline double retrieve(lista *wsk);
lista *prev(lista *wsk);
lista *end();
lista *locate(int liczba);


int main(int argc, char *argv[])
{   short int wybor;
    TworzListe();
while(wybor!=5)
{
    cout << "1 - dodanie elementu do listy\n" ;
    cout << "2 - wyswietlenie zawartosci\n" ;
    cout << "3 - usuniecie danego elementu\n";
    cout << "4 - Wyswietlenie zawartosci od konca\n" ;
    cin >> wybor;
    switch(wybor)
                 {
                              case 1:short int element;
                                   cout << "jaki element dodac?:";
                                   cin >> element;
                                   insert(element,first());
                                   break;
                              case 2:printlist();
                                   break;
                              case 3:cout << "podaj element do usuniecia:";
                                   cin >> element;
                                   deletel(locate(element));
                                   break;
                              case 4:printlistrev();
                                   break;
                                   }
}    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void TworzListe()
{
     head = new lista;
     head->next=NULL;
    
}
inline lista *first()
{
       return head;
}
inline lista *next(lista *wsk)
{
       return wsk->next;
}
lista *prev(lista *wsk)
{
    lista *tmp = first();
    if(tmp!=NULL)
    while(tmp->next)
    {
        if(tmp->next==wsk)
            return tmp;
        tmp = next(tmp);
    }
    return NULL;
}
lista *end()
{
    lista *first = head;
    if(first!=NULL)
    while(first->next!=NULL) first = next(first);
    return first;
} 
void insert(double liczba, lista *pos)
{
    if(pos==NULL) return;
    lista *tmp = new lista;
    tmp->element = liczba;
    tmp->next = pos->next;
    pos->next = tmp;
} 
lista *locate(int liczba)
{
    lista *fst = first();
    if(fst!=NULL)
    while(fst)
    {
        fst = next(fst);
        if(fst->element==liczba) return fst;
    }
    return NULL;
} 
inline double retrieve(lista *wsk)
{
    if(wsk!=NULL) return wsk->element; else return -1;
}
void deletel(lista *wsk)
{   
    lista *tmp = first();
    while(tmp)
    {
        if(tmp->next==wsk)
        {
            tmp->next = wsk->next;
            delete wsk;
            return;
        }
        tmp = next(tmp);
    }
}

void printlist()
{
    lista *fst = first();
    if(fst!=NULL)
    while(fst->next!=NULL)
    {
        fst =next(fst);
        cout << retrieve(fst) << "\t";
    }
    cout << endl;
} 
void printlistrev()
{
    lista *fst = end();
    if(fst!=NULL)
    while(fst!=head)
    {
        cout << retrieve(fst) << endl;
        fst = prev(fst);
    }
}