Download code

Jump to: navigation, search

Back to Singly_linked_list_(C_Plus_Plus)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

list_test_output.txt

1 1 2 3 4 
2 1 2 42 3 4 
3 1 2 42 3 
4 10 2 42 3 
5 
6 1 2 3 4 
7 42 42 42 42 


build.log

 1 In file included from /tmp/litprog5418971/sllist.h:147:0,
 2                  from /tmp/litprog5418971/list_test.cpp:18:
 3 /tmp/litprog5418971/sllist.hpp:129:3: error: stray '@' in program
 4 /tmp/litprog5418971/sllist.hpp:136:3: error: stray '@' in program
 5 In file included from /tmp/litprog5418971/list_test.cpp:18:0:
 6 /tmp/litprog5418971/sllist.h:43:10: error: 'ptrdiff_t' does not name a type
 7 In file included from /tmp/litprog5418971/sllist.h:147:0,
 8                  from /tmp/litprog5418971/list_test.cpp:18:
 9 /tmp/litprog5418971/sllist.hpp: In constructor 'list<T>::list()':
10 /tmp/litprog5418971/sllist.hpp:129:32: error: expected '{' before 'text'
11 /tmp/litprog5418971/sllist.hpp: At global scope:
12 /tmp/litprog5418971/sllist.hpp:129:32: error: 'text' does not name a type
13 /tmp/litprog5418971/sllist.hpp: In copy constructor 'list<T>::list(const list<T>&)':
14 /tmp/litprog5418971/sllist.hpp:136:32: error: expected '{' before 'text'
15 /tmp/litprog5418971/sllist.hpp: At global scope:
16 /tmp/litprog5418971/sllist.hpp:136:32: error: 'text' does not name a type


hijacker
hijacker
hijacker
hijacker

sllist.h

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Singly_linked_list_(C_Plus_Plus)?oldid=4813
 14 */
 15 
 16 #include <iterator>
 17 
 18 template <typename T>
 19 class list
 20 {
 21   private:
 22     struct node
 23     {
 24       node(T val)
 25         : next(NULL), value(val)
 26       {}
 27 
 28       node* next;
 29       T     value;
 30     };
 31 
 32     node* _head;
 33     node* _tail;
 34     int _size;
 35 
 36   public:
 37     class const_iterator
 38       : public std::iterator<std::forward_iterator_tag, T, size_t, T const*, T const&>
 39     {
 40       public:
 41         typedef std::forward_iterator_tag iterator_category;
 42 	typedef T         value_type;
 43 	typedef ptrdiff_t difference_type;
 44 	typedef T const*  pointer;
 45 	typedef T const&  reference;
 46 	const_iterator(node* cur_node = NULL)
 47 	  : _curpos(cur_node)
 48 	{}
 49 	reference operator*()
 50 	{
 51 	  return _curpos->value;
 52 	}
 53 
 54 	pointer operator ->()
 55 	{
 56 	  return &(**this);
 57 	}
 58 
 59 	// The lack of arguments indicates that this is prefix ++
 60 	const_iterator& operator++()
 61 	{
 62 	  _curpos = _curpos->next;
 63 	  return *this;
 64 	}
 65 
 66 	// The dummy argument indicates that this is the postfix ++
 67 	const_iterator operator++(int)
 68 	{
 69 	  const_iterator old_it = *this;
 70 	  ++(*this);
 71 	  return old_it;
 72 	}
 73 
 74 	bool operator ==(const_iterator const& rhs)
 75 	{
 76 	  return _curpos == rhs._curpos;
 77 	}
 78 
 79 	bool operator !=(const_iterator const& rhs)
 80 	{
 81 	  return !(*this == rhs);
 82 	}
 83       protected:
 84         node* _curpos;
 85     };
 86 
 87     class iterator
 88       : public const_iterator
 89     {
 90       public:
 91         iterator(node* iter_node = NULL)
 92 	  : const_iterator(iter_node)
 93 	{}
 94 
 95 	typedef std::forward_iterator_tag iterator_category;
 96 	typedef T      value_type;
 97 	typedef size_t difference_type;
 98 	typedef T*     pointer;
 99 	typedef T&     reference;
100 
101 	using const_iterator::_curpos;
102 
103 	reference operator*()
104 	{
105 	  return _curpos->value;
106 	}
107 
108 	pointer operator ->()
109 	{
110 	  return &(**this);
111 	}
112 
113 	iterator& operator++()
114 	{
115 	  _curpos = _curpos->next;
116 	  return *this;
117 	}
118 
119 	iterator operator++(int)
120 	{
121 	  iterator old_it = *this;
122 	  ++(*this);
123 	  return old_it;
124 	}
125     };
126 
127     list();
128     list(list<T> const& rhs);
129     ~list();
130     iterator begin();
131     iterator end();
132     const_iterator begin() const;
133     const_iterator end() const;
134     void push_front(T const& elt);
135     void push_back(T const& elt);
136     void insert_after(iterator where, T const& elt);
137     iterator erase_after(iterator it);
138     void clear();
139     void pop_front();
140     list& operator =(list<T> const& rhs);
141 
142     size_t size() const;
143     bool empty() const;
144     friend class list<T>::iterator;
145 };
146 
147 #include "sllist.hpp"
148 


hijacker
hijacker
hijacker
hijacker

sllist.hpp

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Singly_linked_list_(C_Plus_Plus)?oldid=4813
 14 */
 15 
 16 template <typename T>
 17 inline typename list<T>::iterator list<T>::begin()
 18 {
 19     return iterator(_head);
 20 }
 21 
 22 template <typename T>
 23 inline typename list<T>::iterator list<T>::end()
 24 {
 25     return iterator(NULL);
 26 }
 27 template <typename T>
 28 inline typename list<T>::const_iterator list<T>::begin() const
 29 {
 30     return const_iterator(_head);
 31 }
 32 
 33 template <typename T>
 34 inline typename list<T>::const_iterator list<T>::end() const
 35 {
 36     return const_iterator(NULL);
 37 }
 38 template <typename T>
 39 inline void list<T>::insert_after(typename list<T>::iterator where, T const& elt)
 40 {
 41     node* newnode = new node(elt);
 42     newnode->next = where._curpos->next;
 43     where._curpos->next = newnode;
 44     _size++;
 45     if (newnode->next == NULL)
 46     {
 47         _tail = newnode;
 48     }
 49 }
 50 template <typename T>
 51 inline void list<T>::push_front(T const& elt)
 52 {
 53     node* newnode = new node(elt);
 54     newnode->next = _head;
 55     _head = newnode;
 56     if (_tail == NULL)
 57     {
 58         _tail = newnode;
 59     }
 60     _size++;
 61 }
 62 template <typename T>
 63 inline void list<T>::push_back(T const& elt)
 64 {
 65     if (_tail != NULL)
 66         insert_after(iterator(_tail), elt);
 67     else
 68         push_front(elt);
 69 }
 70 template <typename T>
 71 inline typename list<T>::iterator list<T>::erase_after(typename list<T>::iterator it)
 72 {
 73     node* todelete = it._curpos->next;
 74     it._curpos->next = it._curpos->next->next;
 75     _size--;
 76 
 77     if (it._curpos->next == NULL)
 78     {
 79         _tail = it._curpos;
 80     }
 81     delete todelete;
 82     return iterator(it._curpos->next);
 83 }
 84 template <typename T>
 85 inline void list<T>::pop_front()
 86 {
 87     node* todelete = _head;
 88     _head = _head->next;
 89     if (_head == NULL) {
 90         _tail = NULL;
 91     }
 92     _size--;
 93     delete todelete;
 94 }
 95 template <typename T>
 96 inline void list<T>::clear()
 97 {
 98     while (begin() != end())
 99     {
100         pop_front();
101     }
102 }
103 template <typename T>
104 list<T>& list<T>::operator =(list<T> const& rhs)
105 {
106     clear();
107 
108     const_iterator rhs_it = rhs.begin();
109     for (rhs_it = rhs.begin(); rhs_it != rhs.end(); ++rhs_it)
110     {
111         push_back(*rhs_it);
112     }
113 
114     return *this;
115 }
116 template <typename T>
117 inline size_t list<T>::size() const
118 {
119   return _size;
120 }
121 
122 template <typename T>
123 inline bool list<T>::empty() const
124 {
125   return _size == 0;
126 }
127 template <typename T>
128 inline list<T>::list()
129   : _head(NULL), _tail(NULL) @ text
130 
131     , _size(0)
132 {}
133 
134 template <typename T>
135 inline list<T>::list(list<T> const& rhs)
136   : _head(NULL), _tail(NULL) @ text
137 
138     , _size(0)
139 {
140     *this = rhs;
141 }
142 
143 template <typename T>
144 inline list<T>::~list()
145 {
146     clear();
147 }


hijacker
hijacker
hijacker
hijacker

list_test.cpp

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Singly_linked_list_(C_Plus_Plus)?oldid=4813
14 */
15 
16 #include <iostream>
17 
18 #include "sllist.h"
19 
20 template <typename T>
21 void print_list(list<T> const& lst)
22 {
23   typename list<T>::const_iterator it;
24   for (it = lst.begin(); it != lst.end(); ++it)
25     std::cout << *it << " ";
26   std::cout << std::endl;
27 }
28 
29 int main()
30 {
31   list<int> lst;
32   lst.push_back(2);
33   lst.push_back(3);
34   lst.push_front(1);
35   lst.push_back(4);
36   list<int> list2;
37   list2 = lst;
38   print_list(lst);
39   list<int>::iterator it;
40   it = lst.begin();
41   it++;
42   lst.insert_after(it, 42);
43   ++it;
44   print_list(lst);
45   lst.erase_after(++it);
46   print_list(lst);
47   it = lst.begin();
48   *it = 10;
49   print_list(lst);
50   lst.clear();
51   print_list(lst);
52   print_list(list2);
53   std::fill(list2.begin(), list2.end(), 42);
54   print_list(list2);
55   return 0;
56 }


hijacker
hijacker
hijacker
hijacker