当前位置: 首页>>代码示例>>C++>>正文


C++ LinkedList::AddFirst方法代码示例

本文整理汇总了C++中LinkedList::AddFirst方法的典型用法代码示例。如果您正苦于以下问题:C++ LinkedList::AddFirst方法的具体用法?C++ LinkedList::AddFirst怎么用?C++ LinkedList::AddFirst使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在LinkedList的用法示例。


在下文中一共展示了LinkedList::AddFirst方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main() {
   // Demo the use of our LinkedList class.
   LinkedList<int> l; // TData is int....
   l.AddFirst(1);     // ... so all the Add methods take ints.
   l.AddFirst(2);
   l.AddFirst(3);
   l.AddLast(4);
   l.AddLast(5);


   // Use the .Get method to access individual elements.
   // How efficient is this loop? Not as efficient.
	// The loop takes too long to walk through all elements of linked lists
	//	if the size of linked list is large.
   for (int i = 0; i < l.Size(); i++) {
      cout << l.Get(i) << endl;
   }


   cout << "Remove: " << endl;
   // The .RemoveFirst/Last/At methods return the data removed.
   while (l.Size() > 0) {
      cout << l.RemoveFirst() << endl;
   }

   cout << endl;

   // It is easy to get the "border" cases wrong in a linked structure. Let's
   // test to see what happens when adding to a list that had just been emptied.
   l.AddFirst(1);
   l.AddFirst(2);
   l.AddFirst(3);
   l.AddLast(4);
   l.AddLast(5);

   cout << "After adding back to the emptied list:" << endl;
   for (int i = 0; i < l.Size(); i++) {
      cout << l.Get(i) << endl;
   }
   // Does that look correct?
   cout << endl;

   cout << "Contains 14? " << endl;
   // Demo the Contains method. Will this be true or false?
   cout << l.Contains(14) << endl;
   cout << endl;

   // So far we have used the LinkedList directly, so the List base class 
   // doesn't seem necessary. Through polymorphism, we can declare a List
   // pointer to a LinkedList object.
   List<int> *p = new LinkedList<int>();
   // We can now only call List<int> methods on p... which, fortunately, is all
   // of our important functions.
   p->AddLast(4);
   p->AddLast(8);
   p->AddLast(15);
   p->AddLast(16);
   p->AddLast(23);
   p->AddLast(42);
   cout << "Pointer to a List contains 16? " << p->Contains(16) << endl;


   // So far we don't have much of a reason to do this type of polymorphism.
   // But we will...
 


   // Lecture part 2: iterators!
   // Picture the normal iterator loop:
   // for (_____::iterator itr = _____.begin(); itr != _____.end(); itr++) {
   //    do something with *itr or itr->

   /*
   If we want our LinkedList to follow this trend, what do we need to 
   implement?

   1. An inner class named iterator, so our full scoped name will be 
      LinkedList<TData>::iterator
   2. Operators ++, ==, !=, *, and -> in the iterator class.
   3. Member methods .begin() and .end() in LinkedList.
   
   When those are implemented, we can then...
   */
   cout << endl << "ITERATORS!!!" << endl;
   for (LinkedList<int>::iterator itr = l.begin(); itr != l.end(); itr++) {
      cout << *itr << endl;
   }
}
开发者ID:hidtran,项目名称:CECS-Projects,代码行数:88,代码来源:main.cpp


注:本文中的LinkedList::AddFirst方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。