Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node
- {
- int info;
- Node* next;
- };
- class Lista
- {
- public:
- Node* head;
- int counter = 0;
- Lista()
- {
- head = NULL;
- }
- void printList();
- void insert(int val, int pos);
- void deleteByPos(int pos);
- void reverse();
- void deleteList();
- int getElement(int pos);
- };
- void Lista::printList()
- {
- for (Node* p = head; p != NULL; p = p->next)
- {
- cout << p->info;
- if (p->next == NULL)
- cout<<". ";
- else
- cout<<", ";
- }
- cout<<"\n";
- }
- int Lista::getElement(int pos)
- {
- Node *temp = head;
- int i = 0;
- while (temp->next != NULL && i != pos - 1)
- {
- temp = temp->next;
- i++;
- }
- if (i == pos - 1)
- return temp->info;
- else
- return -1; // pozitia nu exista in lista
- }
- void Lista::insert(int val, int pos)
- {
- Node *p = new Node;
- p->info = val;
- p->next = NULL;
- counter++;
- if (head == NULL) // inserare lista goala
- {
- head = p;
- return;
- }
- if (pos == 0) //inserare la inceput
- {
- p -> next = head;
- head = p;
- return;
- }
- int i = 0; //inserare dupa o anumita pozitie
- Node* temp;
- temp = head;
- while(temp->next != NULL && i != pos - 1) //parcurgem lista pana pe pozitia pos - 1
- {
- temp = temp -> next;
- i++;
- }
- p->next = temp->next;
- temp->next = p;
- }
- void Lista::deleteByPos(int pos)
- {
- counter--;
- if (head == NULL) //daca lista e goala
- return;
- if (pos == 0) //daca stergem primul element
- {
- Node* p = head;
- delete p;
- head = head -> next;
- return;
- }
- int i = 0; //stergere la o anumita pozitie
- Node* temp;
- temp = head;
- while(temp->next != NULL && i != pos - 1) //parcurgem lista pana pe pozitia pos - 1
- {
- temp = temp -> next;
- i++;
- }
- Node* p;
- p = temp->next;
- delete p;
- if(temp->next->next != NULL)
- temp->next = temp->next->next;
- else
- temp->next = NULL;
- }
- void Lista::reverse()
- {
- Node *current = head, *prev = NULL, *urm = NULL;
- while (current != NULL)
- {
- urm = current->next;
- current->next = prev;
- prev = current;
- current = urm;
- }
- head = prev;
- }
- void Lista::deleteList()
- {
- Node *p;
- p = head;
- while (head != NULL)
- {
- head = head->next;
- delete p;
- p = head;
- counter--;
- }
- }
- class Coada
- {
- Lista l;
- public:
- void push(int x);
- void pop();
- int size();
- int top();
- void printCoada();
- void deleteCoada();
- };
- void Coada::printCoada()
- {
- l.printList();
- }
- void Coada::push(int x)
- {
- l.insert(x,l.counter);
- }
- void Coada::pop()
- {
- l.deleteByPos(0);
- }
- int Coada::size()
- {
- return l.counter;
- }
- int Coada::top()
- {
- return l.getElement(l.counter);
- }
- void Coada::deleteCoada()
- {
- l.deleteList();
- return;
- }
- int main()
- {
- Coada c;
- c.push(1);
- c.push(5);
- c.push(10);
- c.push(15);
- c.pop();
- c.printCoada();
- cout<<c.top();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement