slist.h
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:5k
开发平台:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  slist.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #ifndef LEDA_SLIST_H
  12. #define LEDA_SLIST_H
  13. /*{Manpage {slist} {E} {Singly Linked Lists}}*/
  14. #include <LEDA/impl/slist.h>
  15. template <class E>
  16. class slist : public SLIST {
  17. /*{Mdefinition
  18. An instance $L$ of the parameterized data type name is a sequence of items
  19. ($slist_item$). Each item in $L$ contains an element of data type $E$, called
  20. the element type of $L$. The number of items in $L$ is called the length of $L$. If $L$ has length zero it is called the empty list. In the sequel $<x>$ is
  21. used to denote a list item containing the element $x$ and $L[i]$ is used to
  22. denote the contents of list item $i$ in $L$.}*/
  23. void print_el(GenPtr& x,ostream& out) const { LEDA_PRINT(E,x,out);  }
  24. void read_el(GenPtr& x,istream& in)   const { E X; Read(X,in); x = Copy(X); }
  25. void clear_el(GenPtr& x)              const { LEDA_CLEAR(E,x); }
  26. void copy_el(GenPtr& x)               const { LEDA_COPY(E,x); }
  27. int  int_type() const { return LEDA_INT_TYPE(E); }
  28. public:
  29. /*{Mcreation L }*/
  30.  
  31. slist() {}
  32. /*{Mcreate creates  an instance var of type name and initializes it to
  33.             the empty list.}*/
  34.  
  35. slist(E x) : SLIST(Convert(x)) { }
  36. /*{Mcreate creates  an instance var of type name and initializes it to
  37.             the one-element list $<x>$. }*/
  38.  slist(const slist<E>& a) : SLIST((SLIST&)a) { }
  39. ~slist() {}
  40.  slist<E>& operator=(const slist<E>& a) { SLIST::operator=(a);  return *this; }
  41.  
  42. /*{Moperations 2 5 }*/
  43. int length() const {return SLIST::length();}
  44. /*{Mop      returns the length of $L$.}*/
  45. int size() const {return SLIST::length();}
  46. /*{Mop      returns $L$.length().}*/
  47. bool empty() const {return SLIST::empty();}
  48. /*{Mop      returns true if $L$ is empty, false otherwise.}*/
  49. slist_item first() const {return SLIST::first();}
  50. /*{Mop      returns the first item of $L$.}*/
  51. slist_item last() const {return SLIST::last();}
  52. /*{Mop      returns the last item of $L$.}*/
  53. slist_item succ(slist_item it) const {return SLIST::succ(it);}
  54. /*{Mop      returns the successor item of item $it$, nil if
  55.      $it=L$.last().\
  56.      precond $it$ is an item in $L$.}*/
  57. slist_item cyclic_succ(slist_item it) const {return SLIST::cyclic_succ(it);}
  58. /*{Mop      returns the cyclic successor of item $it$, i.e.,
  59.      $L$.first() if $it = L$.last(), $L$.succ($it$) otherwise.}*/
  60. E  contents(slist_item it) const { return LEDA_ACCESS(E,SLIST::contents(it)); }
  61. /*{Mop      returns the contents $L[it]$ of item $it$.\
  62.      precond $it$ is an item in $L$.}*/
  63. E  inf(slist_item it) const { return contents(it); }
  64. /*{Mop      returns $L$.contents($it$).\
  65.              precond $it$ is an item in $L$.}*/
  66. E  head() const { return LEDA_ACCESS(E,SLIST::head()); }
  67. /*{Mop      returns the first element of $L$, i.e. the contents
  68.      of $L$.first().\
  69.      precond $L$ is not empty.}*/
  70. E  tail() const { return LEDA_ACCESS(E,SLIST::tail()); }
  71. /*{Mop      returns the last element of $L$, i.e. the contents
  72.      of $L$.last().\
  73.      precond $L$ is not empty.}*/
  74. slist_item push(E x)   { return SLIST::push(Copy(x));}
  75. /*{Mop      adds a new item $<x>$ at the front of $L$ and returns it. }*/
  76.  
  77. slist_item append(E x) { return SLIST::append(Copy(x));}
  78. /*{Mop      appends a new item $<x>$ to $L$ and returns it. }*/
  79. slist_item insert(E x, slist_item it) { return SLIST::insert(Copy(x),it); }
  80. /*{Mopl     inserts a new item $<x>$ after item $it$ into $L$ and
  81.              returns it.\
  82.      precond $it$ is an item in $L$.}*/
  83. E pop() { GenPtr x=SLIST::pop(); 
  84.           E   y=LEDA_ACCESS(E,x); 
  85.           Clear(LEDA_ACCESS(E,x)); 
  86.           return y; }
  87. /*{Mop      deletes the first item from $L$ and returns its
  88.              contents. \
  89.      precond $L$ is not empty.}*/
  90. void conc(slist<E>& L1)      { SLIST::conc((SLIST&)L1); }
  91. /*{Mop      appends list $L_1$ to list $L$ and makes $L_1$ the empty list.\
  92.              precond $L != L_1$.}*/
  93. E   operator[](slist_item it) const { return contents(it); }
  94. E&  operator[](slist_item it) { return LEDA_ACCESS(E,SLIST::entry(it)); }
  95. /*{Marrop   returns a reference to the contents of $it$.}*/
  96. slist_item operator+=(E x)  { return append(x); }
  97. /*{Mbinop      appends a new item $<x>$ to $L$ and returns it. }*/
  98. bool current_element(E& x) const {GenPtr y; bool b=SLIST::current_element(y);
  99.                                      if (b) x = LEDA_ACCESS(E,y); return b; }
  100. bool next_element(E& x) const { GenPtr y; bool b = SLIST::next_element(y);
  101.                                    if (b) x = LEDA_ACCESS(E,y); return b; }
  102. GenPtr forall_loop_test(GenPtr it, E& x) const
  103. { if (it) x = contents(slist_item(it));
  104.   return it;
  105.  }
  106. };
  107. #endif