fork(1) download
  1. #include <iostream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template<typename E> class SLinkedList;
  6.  
  7. template<typename E> class SNode{
  8. private:
  9. E element;
  10. SNode<E> * next;
  11. friend class SLinkedList<E>;
  12.  
  13. public:
  14. E getElement(){
  15. return element;
  16. }
  17. SNode getnext(){
  18. return next;
  19. }
  20. void setElement(E newElement){
  21. this->element=newElement;
  22. }
  23. void setnext(SNode <E> newNext){
  24. this->next=newNext;
  25. }
  26. };
  27.  
  28. template<typename E>
  29. class SLinkedList {
  30. private:
  31.  
  32. SNode<E> *head;
  33.  
  34. public:
  35.  
  36. SLinkedList();
  37. ~SLinkedList();
  38.  
  39. bool empty() const;
  40. const E & front() const;
  41. void addFront(const E& elem);
  42. void removeFront();
  43. void printList();
  44. };
  45.  
  46.  
  47. template<typename E>
  48. SLinkedList<E>::SLinkedList()
  49. :head(NULL){}
  50.  
  51. template <typename E>
  52. SLinkedList<E>::~SLinkedList()
  53. {
  54. while (!empty())
  55. removeFront();
  56. }
  57.  
  58.  
  59. template<typename E>
  60. bool SLinkedList<E>::empty() const{
  61.  
  62. return (head==NULL);
  63.  
  64. }
  65.  
  66. template<typename E>
  67. const E & SLinkedList<E>::front() const{
  68.  
  69. return head->element;
  70.  
  71. }
  72.  
  73. template<typename E>
  74. void SLinkedList<E>::addFront(const E& elem){
  75.  
  76. SNode<E> * n=new SNode<E>;
  77. n->element=elem;
  78. n->next=head;
  79. head=n;
  80.  
  81. }
  82.  
  83. template<typename E>
  84. void SLinkedList<E>::removeFront(){
  85.  
  86. if(this->empty()){
  87. cout<<"Lista jest pusta, nie ma co usunac"<<endl;
  88. }
  89. else{
  90. SNode<E> * tmp=head;
  91. head=head->next;
  92. delete tmp;
  93. }
  94.  
  95. }
  96.  
  97. template<typename E>
  98. void SLinkedList<E>::printList(){
  99.  
  100. SNode<E> *tmp=head;
  101.  
  102. while(tmp !=NULL){
  103. cout<<tmp->element;
  104. tmp=tmp->next;
  105. }
  106.  
  107. }
  108.  
  109.  
  110.  
  111. int main(){
  112.  
  113. SLinkedList<int> lista;
  114.  
  115.  
  116.  
  117.  
  118. return 0;
  119. }
Success #stdin #stdout 0s 3408KB
stdin
Standard input is empty
stdout
Standard output is empty