List With Iterator (lab and HW)

本文介绍了一个自定义双向链表的实现细节,包括列表的基本操作如插入、删除等,并提供了伪代码及部分实现代码。该列表使用了迭代器来辅助元素的访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

完成伪标准:用伪迭代器列表完成mylist.h,一起写的声明和定义。

模板如下:

template<class T> 
class list { 
 public: 
  struct listelem;  // forward declarations 
  class iterator;
  list();
  list(size_t n_elements, const T& c);
  list(const list& x);
  list(iterator b, iterator e);
  ~list();
  iterator begin() const;
  iterator end() const;
  size_t size();
  void push_front(const T& c);
  void push_back(const T& c);
  void pop_front();
  void pop_back();
  iterator insert(iterator position, const T& val);
  iterator erase (iterator position);
  bool empty() const;
  T& front();
  T& back();
  void clear();
  friend ostream& operator << (ostream& out, const list& x);
  
  struct listelem {      // list cell 
    T data; 
    listelem *next, *prev; 
    listelem(const T& s, listelem* n, listelem* p);  // suggest u to use the constructor 
  };
  
  class iterator { 
   public: 
    friend class list;  // suggest u to use friend class
    explicit iterator(listelem* p = 0);

    iterator(const iterator& other) ;
    iterator& operator ++ ();
    iterator& operator -- ();
    iterator operator ++ (int);
    iterator operator -- (int);
    listelem* operator -> ();
    T& operator * ();
    
    // u can use iterator as listelem* sometimes
    operator listelem * () {return ptr;}  // conversion;
    
    private: 
    listelem* ptr;  // current listelem or 0; 
  };
  
  private: 
  iterator h, t;  // head and tail 
};

 

Sample input:

4

1 2 3 4

 

Sample output:

test size
0
empty
test push and pop
[ 1 ]
[ ]
[ 1 ]
[ ]
[ 1 ]
[ ]
[ 1 ]

8
[ 4 3 2 1 1 2 3 4 ]
[ 2 1 1 2 3 ]
front : 2
back : 3
not empty
test begin and end
2 1 1 2 3 
test insert and erase and ++ --
100
200
[ 100 2 1 1 2 3 200 ]
1
1
[ 100 1 2 3 200 ]
[ 99 101 100 1 2 3 200 ]
test constructor
[ 9 9 9 9 9 ]
[ 90 101 100 1 2 3 200 ]
[ 90 101 100 1 2 3 200 ]
[ 90 101 100 ]

mylist:

#include <iostream>
#include <cassert>
#include <string>
using namespace std;
 
 
template<class T>
class list {
 public:
  struct listelem;  // forward declarations
  class iterator;
 
  list() {
    h.ptr = t.ptr = 0;
  }  // construct the empty list
  list(size_t n_elements, const T& c) {
    h.ptr = t.ptr = 0;
    for ( size_t i = 0; i < n_elements; ++i )
      push_front(c);
  }
 
  list(const list& x) {
    list::iterator r = x.begin();
    h.ptr = t.ptr = 0;
    while (r != 0) {
      push_back(*r++);
    }
  }
 
  list(iterator b, iterator e)  {
    h.ptr = t.ptr = 0;
    while (b != e) {
      push_back(*b++);
    }
  }
 
  ~list() { clear(); }
 
  iterator begin() const { return h; }
 
  iterator end() const {
    iterator temp = t;
    return (temp.ptr == 0 ? iterator(0) : ++temp);
  }
 
  size_t size() {
    size_t count = 0;
    iterator temp = h;
    while (temp != end()) {
      temp++;
      count++;
    }
    return count;
  }
 
  void push_front(const T& c) {
    listelem* temp = new listelem(c, h, 0);
    if (h.ptr != 0) {  // was a nonempty list
      h->prev = temp;
      h.ptr = temp;
    } else {  // was an empty list
      h.ptr = t.ptr = temp;
    }
  }
 
  void push_back(const T& c) {
    listelem* temp = new listelem(c, 0, t);
    if (t != 0) {  // was a nonempty list
      t->next = temp;
      t.ptr = temp;
    } else {  // was an empty list
      h.ptr = t.ptr = temp;
    }
  }
 
  void pop_front() {
    listelem* temp = h.ptr;
    if (h.ptr != 0) {
      if (h.ptr == t.ptr) t.ptr = 0;
      h.ptr = h->next;
      if (h.ptr != 0) h->prev = 0;
      delete temp;
    }
  }
 
  void pop_back() {
    listelem* temp = t.ptr;
    if (t.ptr != 0) {
      if (h.ptr == t.ptr) h.ptr = 0;
      t.ptr = t->prev;
      if (t.ptr != 0) t->next = 0;
      delete temp;
    }
  }
 
  iterator insert(iterator position, const T& val) {
    listelem* p = position.ptr;
    if (p == 0) {
      // p == end()
      push_back(val);
      return t;
    } else if (p->prev == 0) {
      // p == begin()
      push_front(val);
      return begin();
    } else {
      listelem* temp = new listelem(val, p, p->prev);
      p->prev = temp;
      temp->prev->next = temp;
      return iterator(temp);
    }
  }
 
  iterator erase(iterator position) {
    listelem* temp = position.ptr;
    if (temp == h) {
      // temp == begin();
      pop_front();
      return h;
    }
    temp->prev->next = temp->next;
    temp->next->prev = temp->prev;
    listelem* re = temp->next;
    delete temp;
    return iterator(re);
  }
 
  bool empty() const { return h.ptr == 0; }
 
  T& front() { return *h; }
  T& back() { return *t; }
 
  void clear() {
    while (h.ptr != 0) {
      pop_front();
    }
    h.ptr = t.ptr = 0;
  }
 
  friend ostream& operator << (ostream& out, const list& x)  {
    list<T>::iterator p = x.begin();
    out << "[ ";
    while (p != 0) {
      out << *p << " ";
      ++p;  // advances iterator using next
    }
    out << "]";
    return out;
  }
 
  struct listelem {      // list cell
    T data;
    listelem *next, *prev;
    listelem(const T& s, listelem* n, listelem* p) \
      : data(s), next(n), prev(p) {}
  };
  // scopeed within class list
 
  class iterator {
   public:
    friend class list;
    explicit iterator(listelem* p) : ptr(p) {}
    iterator() : ptr(0) {}
    iterator& operator++() {
      ptr = ptr->next;
      return *this;
    }
 
    iterator& operator--() {
      ptr = ptr->prev;
      return *this;
    }
 
    iterator operator++(int) {
      iterator temp = *this;
      ptr = ptr->next;
      return temp;
    }
 
    iterator operator--(int) {
      iterator temp = *this;
      ptr = ptr->prev;
      return temp;
    }
 
    listelem* operator -> () { return ptr; }
    T& operator * () { return ptr->data; }
    operator listelem * () { return ptr; }  // conversion
 
   private:
    listelem* ptr;  // current listelem or 0;
  };
 
 private:
  iterator h, t;  // head and tail
};


### Java List Iterator Usage and Example In Java, an `Iterator` is used to traverse through the elements of a collection object such as a `List`. The `Iterator` interface provides methods like `hasNext()`, which returns true if there are more elements in the iteration, and `next()`, which retrieves the next element in the sequence. For lists specifically, one can obtain an iterator by calling the `iterator()` method on any class implementing the `List` interface. Here’s how it works: ```java import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main { public static void main(String[] args) { // Create a new ArrayList (which implements List) List<String> myList = new ArrayList<>(); // Add some items into our list myList.add("Apple"); myList.add("Banana"); myList.add("Orange"); // Obtain an Iterator instance from the List Iterator<String> myIterator = myList.iterator(); while(myIterator.hasNext()) { // Check whether there's another item available String fruit = myIterator.next(); // Get the current item System.out.println(fruit); // Print out each item found during traversal } } } ``` The above code snippet demonstrates obtaining an iterator using the `iterator()` method provided by classes that implement the `List` interface[^4]. Each call to `myIterator.next()` advances the cursor position within the underlying data structure until no further entries remain. Additionally, iterators support removal operations via their own `remove()` function without directly modifying the original container outside safe boundaries established when creating them. However, adding or removing elements elsewhere may throw exceptions due to concurrent modification issues unless explicitly supported by specialized implementations.
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值