#include <iostream.h>

#include <stdlib.h>

#include <stdio.h>

class lista{

private:

char dane[20];

lista *head;

lista *next;

lista *curr;

lista *prev;

public:

lista(void);

void dodaj(char *,int);

void Put(char *a);

int nastepny(void);

char* odczyt(void);

void poczatek(void);

int ilosc_el(void);

int poprzedni(void);

int ustaw(int gdzie);

void usun(int ktory);

};

/***********konstruktor*************************/

lista::lista(void)

{

head=NULL;

next=NULL;

prev=NULL;

curr=NULL;

}

/************ wstawia dane **********************/

void lista::Put(char *a)

{

int i=0;

while((a[i]=='\n')||(i<20))

{

dane[i]=a[i++];

}

dane[i]='\n';

}

/************ przesuwa sie na nastepny **********/

int lista::nastepny(void)

{

if(curr->next)

{

curr=curr->next;

return 1;

}

else {

return 0;

}

}

/*********** przesuwa sie na poprzedni *********/

int lista::poprzedni(void)

{

if(curr->prev)

{

curr=curr->prev;

return 1;

}

else return 0;

}

/*********** odczyt danych ********************/

char* lista::odczyt(void)

{

return curr->dane;

}

void lista::poczatek(void)

{

curr=head;

}

/*********** zwraca ilosc elementow ***********/

int lista::ilosc_el(void)

{

int ilosc=0;

lista *temp;

temp=curr;

curr=head;

do

{

ilosc++;

}

while(nastepny());

curr=temp;

return ilosc;

}

/****************************************************/

void lista::dodaj(char *tekst,int miejsce)

{

lista *a=new lista(); // tworzymy nowy obiekt

a->Put(tekst); // umieszczamy w nim tekst

lista *wsk;

if(miejsce==1 && curr==NULL) //jesli lista byla pusta

{

a->prev=NULL;

a->next=NULL;

head=a;

curr=a;

}

else if(miejsce>this->ilosc_el()) // jesli wstawiamy na koniec listy

{

this->ustaw(miejsce-1);

curr->next=a;

a->prev=curr;

a->next=NULL;

}

else

{

this->ustaw(miejsce);

wsk=curr;

if(this->poprzedni())

{

curr->next=a;

a->prev=curr;

}

else {

head=a;

a->prev=NULL;

}

curr=wsk;

if(wsk)

{

a->next=wsk;

wsk->prev=a;

}

else a->next=NULL;

}

this->ustaw(this->ilosc_el()); // ustawiam sie na ostatnim elemencie

}

/***************************** usuwanie elementu z listy ***********************/

void lista::usun(int ktory)

{

ustaw(ktory);

if(curr->prev)

{

curr->prev->next=curr->next;

}

else head=curr->next;

if(curr->next)

{

curr->next->prev=curr->prev;

curr=curr->next;

}

if(!curr->next && !curr->prev)

{

head=curr=next=prev=NULL;

}

}

/***************************** ustawia sie na okreslonym elemencie *************/

int lista::ustaw(int gdzie)

{

this->poczatek();

int i=1;

do

{

if(i==gdzie) return 1;

i++;

}while(this->nastepny());

return 0;

}

/***********************************************************************************/

void rysuj(void)

{

system("clear");

cout << "************************** LISTA ***********************************\n\n";

cout << "1. kolejen wstawianie \n";

cout << "2. wybor miejsca wstawienia\n";

cout << "3. pokaz liste \n";

cout << "4. usun element\n";

cout << "5. KONIEC \n";

}

/****************** program glowny *********************************************/

int main()

{

lista A;

int z=1;

char t[20];

int odp;

int place;

do

{

rysuj();

cin >> odp;

switch(odp)

{

case 1 :

cout << "Podaj tekst dla "<< z << " pola :" << endl;

//getchar();

cin.getline(t,sizeof(t));

A.dodaj(t,z);

z++;

break;

case 2: cout << " Podaj wartosc : " ;

getchar();

cin.getline(t,sizeof(t));

cout << " Podaj miejsce, w ktore chcesz wstawic obiekt : " ;

cin >> place;

A.dodaj(t,place);

z++;

break;

case 3: { A.poczatek();

cout << " zawartosc listy : " ;

do

{

cout << A.odczyt() << ", ";

}

while(A.nastepny());

cout << endl;

getchar();

break;

}

case 4: { cout << " Ktory element chcesz usunac ? : ";

cin >> place;

A.usun(place);

z--;

break;

}

}

cout << "nacisnij ENTER";

getchar();

rysuj();

}while(odp!=5);

return 0;

}