Your own list : your list « List « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » List » your listScreenshots 
Your own list
  
#include <string>
#include <iostream>
#include <cassert>

using namespace std;

class List;
class Iterator;

class Node
{
public:
   Node(string s);
private:
   string data;
   Node* previous;
   Node* next;
friend class List;
friend class Iterator;
};

class List
{
public:
   List();
   void push_back(string s);
   void insert(Iterator iter, string s);
   Iterator erase(Iterator i);
   Iterator begin();
   Iterator end();
private:
   Node* first;
   Node* last;
};

class Iterator
{
public:
   Iterator();
   string get() const;
   void next();
   void previous();
   bool equals(Iterator bconst;
private:
   Node* position;
   Node* last;
friend class List;
};

Node::Node(string s)
{  
   data = s;
   previous = NULL;
   next = NULL;
}

List::List()
{  
   first = NULL;
   last = NULL;
}

void List::push_back(string s)
{  
   Node* newnode = new Node(s);
   if (last == NULL
   {  
      first = newnode;
      last = newnode;
   }
   else
   {  
      newnode->previous = last;
      last->next = newnode;
      last = newnode;
   }
}

void List::insert(Iterator iter, string s)
{  
   if (iter.position == NULL)
   {  
      push_back(s);
      return;
   }

   Node* after = iter.position;
   Node* before = after->previous;
   Node* newnode = new Node(s);
   newnode->previous = before;
   newnode->next = after;
   after->previous = newnode;
   if (before == NULL
      first = newnode;
   else
      before->next = newnode;
}

Iterator List::erase(Iterator i)
{  
   Iterator iter = i;
   assert(iter.position != NULL);
   Node* remove = iter.position;
   Node* before = remove->previous;
   Node* after = remove->next;
   if (remove == first)
      first = after;
   else
      before->next = after;
   if (remove == last)
      last = before;
   else
      after->previous = before;
   iter.position = after;
   delete remove;
   return iter;
}

Iterator List::begin()
{  
   Iterator iter;
   iter.position = first;
   iter.last = last;
   return iter;
}

Iterator List::end()
{  
   Iterator iter;
   iter.position = NULL;
   iter.last = last;
   return iter;
}

Iterator::Iterator()
{  
   position = NULL;
   last = NULL;
}

string Iterator::get() const
{  
   assert(position != NULL);
   return position->data;
}

void Iterator::next()
{  
   assert(position != NULL);
   position = position->next;
}

void Iterator::previous()
{  
   if (position == NULL)
      position = last;
   else 
      position = position->previous;
   assert(position != NULL);
}

bool Iterator::equals(Iterator bconst
{  
   return position == b.position;
}

int main()
{  
   List staff;

   staff.push_back("A");
   staff.push_back("B");
   staff.push_back("C");
   staff.push_back("D");

   Iterator pos;
   pos = staff.begin();
   pos.next();
   pos.next();
   pos.next();

   staff.insert(pos, "Reindeer, Rudolf");

   pos = staff.begin();
   pos.next();

   staff.erase(pos);

   for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())
      cout << pos.get() << "\n";

   return 0;
}
  
    
  
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.