数据结构与算法分析课后习题第三章(5)

来源:互联网 发布:一卡通乘客刷卡数据 编辑:程序博客网 时间:2024/04/28 10:21

3.7 Modify the Vector class to add bounds checks for indexing.

3.8 Add insert and erase to the Vector class.

3.10 Modify the Vector class to provide stringent iterator checking by making iterator class types, rather than pointer variables.

3.11 Assume that a singly linked list is implemented with a header node, but no tail node and that is maintains only a pointer to the header node. Write a class that include methods to

a. return the size of the linked list

b. print the linked list

c. test if value x is contained in the linked list

d. add avalue x if it is not already contained in the linked list

e. remove a value x if it is contained in the linked list

3.12 Repeat Exercise 3.11, maintaining the singly linked list in sorted order.

//vector.hpp Exercise 3.7 - 3.10

#ifndef VECTOR_HPP__
#define VECTOR_HPP__

#include <iostream>

template< typename Object >
class Vector {
public:
 explicit Vector( int initSize = 0 ) :
 theSize(initSize), theCapacity(initSize + SPARE_CAPACITY),
  objects(new Object[theCapacity]){}
 ~Vector() { delete[] objects; }
 Vector(const Vector& rhs) : objects(0)
 { operator=(rhs); }
 Vector& operator=(const Vector& rhs)
 {
  if(this != &rhs)
  {
   delete[] objects;
   theSize = rhs.size();
   theCapacity = rhs.capacity();

   objects = new Object[capacity()];
   for(int i = 0; i < size(); ++i)
    objects[i] = rhs.objects[i];
  }

  return *this;
 }

public:
 class const_iterator {
 public:
  const_iterator() : current(0) {}

  virtual ~const_iterator(){}

  const Object& operator*() const
  { return retrieve(); }

  const_iterator& operator++()
  {
   ++current;
   return *this;
  }

  const_iterator& operator++(int)
  {
   const_iterator old = *this;
   ++(*this);
   return old;
  }

  const_iterator& operator--()
  {
   --current;
   return *this;
  }

  const_iterator& operator--(int)
  {
   const_iterator old = *this;
   --(*this);
   return old;
  }

  bool operator==(const const_iterator& rhs) const
  { return current == rhs.current; }

  bool operator!=(const const_iterator& rhs) const
  { return !(*this = rhs); }

 protected:
  const_iterator(Object* p, Vector* v) : current(p), vec(v)
  {}

  const Object& retrieve() const
  { return *current; }

  Object* current;
  Vector* vec;

  friend class Vector<Object>;
 };

 class iterator : public const_iterator {
 public:
  iterator(){}

  const Object& operator*() { return retrieve(); }
  const Object& operator*() const { return retrieve(); }

  iterator& operator++()
  {
   ++current;
   return *this;
  }

  iterator operator++(int)
  {
   iterator old = *this;
   ++(*this);
   return old;
  }

  iterator& operator--()
  {
   --current;
   return *this;
  }

  iterator operator--(int)
  {
   iterator old = *this;
   --(*this);
   return old;
  }

  bool operator==(const iterator& rhs) const
  { return current == rhs.current; }

  bool operator!=(const iterator& rhs) const
  { return !(*this == rhs); }

 private:
  iterator(Object* p, Vector* v) : const_iterator(p, v) {}

  friend class Vector<Object>;
 };

public:
 const int size() const { return theSize; }
 const int capacity() const { return theCapacity; }
 bool empty() const { return theSize == 0; }

 void resize(int newSize)
 {
  if(newSize > theCapacity)
   reserve(newSize * 2 + 1);
  theSize = newSize;
 }

 void reserve(int newCapacity)
 {
  if(newCapacity < theSize)
   return;

  Object* oldArray = objects;

  objects = new Object[newCapacity];
  for(int i = 0; i < theSize; ++i)
   objects[i] = oldArray[i];

  theCapacity = newCapacity;

  delete[] oldArray;
 }

 void push_back(const Object& x)
 {
  if(theSize == theCapacity)
   reserve(theCapacity * 2 + 1);

  objects[theSize++] = x;
 }

 void pop_back() { --theSize; }

 const Object& back() const
 { return objects[theSize - 1]; }

 iterator begin()
 { return iterator(&objects[0], this); }
 const_iterator begin() const
 { return const_iterator(&objects[0], this); }
 iterator end()
 { return iterator(&objects[theSize], this); }
 const_iterator end() const
 { return const_iteartor(&objects[theSize], this); }

 int insert(int to, const Object& val)
 {
  if(size() == capacity())
   reserve(theCapacity * 2 + 1);
  
  for(int i = size()-1; i >= to; --i)
   objects[i+1] = objects[i];

  objects[to] = val;
  ++theSize;

  return ++to;
 }

 iterator& insert(iterator& to, const Object& val)
 {
  if(to.vec != this)
   cerr << "Iterator not point to the same vector!/n";    //should replaced by throw exceptions when use
  else
  {
   int cnt = 0;
   for(iterator it = begin(); it != to; ++it)
    ++cnt;

   insert(cnt,val);

   return ++to;
  }
 }

 int erase(int pos)
 {
  int i;
  for(i = pos; i < size(); ++i)
   objects[i] = objects[i + 1];
  
  --theSize;
  return pos;
 }

 int erase(int from, int to)
 {
  for(; from < to; ++from)
   objects[from] = objects[from + to];

  theSize = theSize - to;
  return to;
 }

 iterator& erase(iterator& pos)
 {
  if(pos.vec != this)
   std::cerr << "Iterator not point to the same vector!/n";    //should replaced by throw exceptions when use
  int cnt = 0;
  for(iterator i = begin(); i != pos; ++i)
   ++cnt;

  erase(cnt);
  return pos;
 }

 iterator& erase(iterator& from, iterator& to)
 {
  if(from.vec != this || to.vec != this)
   std::cerr << "Iterator not point to the same vector!/n";    //should replaced by throw exceptions when use
  int cnt = 0;
  int f = 0;
  for(iterator i = begin(); i != from; ++i,++f);
  while(from++ != to)
   ++cnt;

  erase(f, cnt);

  return to;
 }
  

 

public:
 Object& operator[](int index)
 {
  if (index > theSize - 1)
   std::cerr << "Out of bound!/n";    //should replaced by throw exceptions when use
    
  else
   return objects[index];
 }

 const Object& operator[](int index) const
 {
  if (index > theSize - 1)
   std::cerr << "Out of bound!/n";    //should replaced by throw exceptions when use
  else
   return objects[index];
 }

private:
 int theSize;
 int theCapacity;
 enum{ SPARE_CAPACITY = 16 };
 Object* objects;
};

#endif

//list.hpp Exercise 3.11 - 3.12

#ifndef LIST_HPP__
#define LIST_HPP__

#include <iostream>

template< typename Object >
class List {
private:
 class Node {
 public:
  Node(const Object& val = Object(), Node* pt = 0) :
    data(val), next(pt){}
  Node(const Node& rhs)
  {
   if(rhs != *this)
   {
    next = rhs.next;
    data = rhs.data;
   }
  }

  ~Node(){}

 public:
  Node* next;
  Object data;
 };

public:
 List() : head(new Node()) {}
 ~List()
 {
  delete head;
 }

 const int size() const
 {
  int theSize = 0;
  Node* p = head;
  while(p->next != 0)
  {
   ++theSize;
   p = p->next;
  }
  p = 0;
  delete p;
  return theSize;
 }

 void print()
 {
  Node* p = head;
  while(p->next != 0)
  {
   std::cout <<"[ " << p->next->data << " ]/n";
   p = p->next;
  }
  p = 0;
  delete p;
 }

 bool contained(const Object& val) const
 {
  Node* p = head;
  while(p->next != 0)
  {
   if(p->next->data == val)
   {
    p = 0;
    delete p;
    return true;
   }
   p = p->next;
  }
  p = 0;
  delete p;
  return false;
 }

 void add(const Object& val) const
 {
  if(contained(val))
   return;

  Node* p = head;
  while(p->next != 0)
   p = p->next;
  
  Node* newnode = new Node(val);
  p->next = newnode;
  
  p = 0;
  delete p;
 }

 void remove(const Object& val)
 {
  Node* p = head;
  while(p->next != 0)
  {
   if(val == p->next->data)
   {
    Node* old = p->next;
    p->next = old->next;
    delete old;
    p = 0;
    delete p;
    return;
   }
   p = p->next;
  }
 }

 void addInOrder(const Object& val)
 {
  Node* p = head;
  if(p->next == 0)
  {
   Node* newnode = new Node(val);
   p->next = newnode;
  }
  else
  {
   while(p->next != 0)
   {
    if(p->next->data > val)
    {
     Node* newnode = new Node(val,p->next);
     p->next = newnode;
     return;
    }
    p = p->next;
   }
   Node* newnode = new Node(val);
   p->next = newnode;
  }
 }

private:
 List(const List&);
 List& operator=(const List&);
 Node* head;
};

#endif

//main.cpp

#include "list.hpp"

using namespace std;

int main()
{
 List<int> lst;
 lst.addInOrder(10);
 lst.addInOrder(2);
 lst.addInOrder(5);
 lst.addInOrder(110);
 lst.addInOrder(84);

 lst.print();
 cout << lst.contained(10);

 lst.remove(0);

 lst.print();

 cout << lst.size();


 return 0;
}

原创粉丝点击