Demonstrate virtual functons: list interface : List « Data Structure « 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++ » Data Structure » ListScreenshots 
Demonstrate virtual functons: list interface
Demonstrate virtual functons: list interface

#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

class list {
public:
  list *head;             // pointer to start of list
  list *tail;             // pointer to end of list 
  list *next;             // pointer to next item
  int num;                // value to be stored

  list() { 
     head = tail = next = NULL; 
  }
  virtual void store(int i0;
  virtual int retrieve() 0;
};

class queue : public list {
public:
  void store(int i);
  int retrieve();
};

void queue::store(int i)
{
  list *item;

  item = new queue;
  if(!item) {
    cout << "Allocation error.\n";
    exit(1);
  }
  item->num = i;

  if(tail
     tail->next = item;
  tail = item;
  item->next = NULL;
  if(!head
     head = tail;
}

int queue::retrieve()
{
  int i;
  list *p;

  if(!head) {
    cout << "List empty.\n";
    return 0;
  }

  // remove from start of list
  i = head->num;
  p = head;
  head = head->next;
  delete p;

  return i;
}

class stack : public list {
public:
  void store(int i);
  int retrieve();
};

void stack::store(int i)
{
  list *item;

  item = new stack;
  if(!item) {
    cout << "Allocation error.\n";
    exit(1);
  }
  item->num = i;

  if(head
     item->next = head;
  head = item;
  if(!tail
     tail = head;
}

int stack::retrieve()
{
  int i;
  list *p;

  if(!head) {
    cout << "List empty.\n";
    return 0;
  }

  // remove from start of list
  i = head->num;
  p = head;
  head = head->next;
  delete p;

  return i;
}

int main()
{
  list *p;

  // demonstrate queue
  queue queueObject;
  p = &queueObject; // point to queue

  p->store(1);
  p->store(2);
  p->store(3);

  cout << "Queue: ";
  cout << p->retrieve();
  cout << p->retrieve();
  cout << p->retrieve();

  cout << '\n';

  // demonstrate stack
  stack stackObject;
  p = &stackObject; // point to stack

  p->store(1);
  p->store(2);
  p->store(3);

  cout << "Stack: ";
  cout << p->retrieve();
  cout << p->retrieve();
  cout << p->retrieve();

  cout << '\n';

  return 0;
}



           
       
Related examples in the same category
1.List basics: push, begin, end popList basics: push, begin, end pop
2.Elements can be put on the front or end of a list.Elements can be put on the front or end of a list.
3.Define class and store in a listDefine class and store in a list
4.An example of the transform algorithm.An example of the transform algorithm.
5.Demonstrate merge() in listDemonstrate merge() in list
6.Merging won't work if the lists aren't ordered.Merging won't work if the lists aren't ordered.
7.Merge into descending order.Merge into descending order.
8.A list splicing example.A list splicing example.
9.Demonstrate remove() in listDemonstrate remove() in list
10.Demonstrate unique() in listDemonstrate unique() in list
11.Using reverse() to create a palindrome tester.Using reverse() to create a palindrome tester.
12.Using a list to store mailing addresses.Using a list to store mailing addresses.
13.Use ostream_iterator for stringUse ostream_iterator for string
14.Demonstrate advance() and distance() in listDemonstrate advance() and distance() in list
15.Using an arrays as a containerUsing an arrays as a container
16.Using a list: push_back, begin, end, sizeUsing a list: push_back, begin, end, size
17.Merge and splice lists.Merge and splice lists.
18.Copy a list to a vector.Copy a list to a vector.
19.Push value in the listPush value in the list
20.Use a binary function object in list 'transform'Use a binary function object in list 'transform'
21.end() in list.end() in list.
22.The difference between push_back() and push_front().The difference between push_back() and push_front().
23.Sort a list.
24.Merge two lists.Merge two lists.
25.Store class objects in a list.Store class objects in a list.
26.Transform algorithm based on list.Transform algorithm based on list.
27.Demonstrate bind2nd().Demonstrate bind2nd().
28.Create a reciprocal function object.Create a reciprocal function object.
29.Understanding end() in ListUnderstanding end() in List
30.Difference between push_back() and push_front()Difference between push_back() and push_front()
31.Merging One List with AnotherMerging One List with Another
32.Storing Class Objects in a ListStoring Class Objects in a List
33.Traverse a List Using an IteratorTraverse a List Using an Iterator
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.