Advertisement
Guest User

trash

a guest
Nov 13th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class IntNode {
  4. private:
  5.     int16_t elem;
  6.     IntNode* last;
  7.     IntNode* next;
  8.     friend class  IntLinkedList;
  9.  
  10. };
  11. class IntLinkedList {
  12. private:
  13.     IntNode* head;
  14.     IntNode* tail;
  15. public:
  16.     IntLinkedList();
  17.     bool empty() const;
  18.     void addFront(int e);
  19.     void removeFront();
  20.     void print() const;
  21. };
  22. IntLinkedList::IntLinkedList() {
  23.     head = new IntNode;
  24.     tail = new IntNode;
  25.     head->next = tail;
  26.     tail->last = head;
  27. }
  28. bool IntLinkedList::empty() const {
  29.     if (head->next == tail)
  30.         return true;
  31.     else
  32.         return false;
  33. }
  34. void IntLinkedList::addFront(int e) {
  35.     IntNode* u=new IntNode;
  36.     IntNode* v = head ->next;
  37.     u->elem = e;
  38.     u->next = v;
  39.     u->last = v->last;
  40.     //v->last = head->last;
  41.     v->last->next = u;
  42.     v->last = u;
  43. }
  44. void IntLinkedList::removeFront() {
  45.     IntNode* v = head->next;
  46.     IntNode* u = v->last;
  47.     IntNode* w = v->next;
  48.     u->next = w;
  49.     w->last = u;
  50.     delete v;
  51. }
  52. void IntLinkedList::print() const {
  53.     IntNode* temp = head->next;
  54.     while (temp != tail) {
  55.         cout << temp->elem << '\n';
  56.         temp = temp->next;
  57.  
  58.     }
  59. }
  60. int main(){
  61.     IntLinkedList myList;
  62.     for (int i = 0;
  63.         i < 10;
  64.         ++i) {
  65.         myList.addFront(i);
  66.  
  67.     }
  68.     for (int i = 0;
  69.         i < 3;
  70.         ++i) {
  71.         myList.removeFront();
  72.  
  73.     }
  74.     myList.print();
  75.     /*for (const auto& s : myList) {
  76.         cout << s;
  77.     }*/
  78.     return 0;
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement