Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
cpp virtual tutorials       Share
2008-09-28 |  RatheeshTR  | Viewed: 127  |    0

#include <iostream.h>

enum { kSmaller, kLarger, kSame};


class Data

{

public:

Data(
int val):dataValue(val){}
virtual ~Data(){}

virtual 
int Compare(const Data &);
virtual 
void Show() { cout << dataValue << endl; }
private:

int dataValue;
};




int Data::Compare(const Data & theOtherData)
{

if (dataValue < theOtherData.dataValue)
return kSmaller;

if (dataValue > theOtherData.dataValue)
return kLarger;

else

return kSame;

}


class Node  
//  abstract  data  type
{

public:

Node(){}

virtual ~Node(){}

virtual Node * Insert(Data * theData) = 0;

virtual 
void Show() = 0;
private:

};


class InternalNode: public Node

{

public:

InternalNode(Data * theData, Node * next);

~InternalNode(){ delete nextNode; delete myData; }

virtual Node * Insert(Data * theData);

virtual 
void Show() { myData->Show(); nextNode->Show(); } 

private:

Data * myData; 

Node * nextNode;

};



InternalNode::InternalNode(Data * theData, Node * next):

myData(theData),nextNode(next)

{

}


Node * InternalNode::Insert(Data * theData)

{

int result = myData->Compare(*theData);

switch(result)

{

case kSame: 
//  fall  through
case kLarger: 
//  new  data  comes  before  me
{

InternalNode * dataNode = new InternalNode(theData, this);

return dataNode;

}


case kSmaller:

nextNode = nextNode->Insert(theData);

return this;

}

return this; 

}


class TailNode : public Node

{

public:

TailNode(){}

~TailNode(){}

virtual Node * Insert(Data * theData);

virtual 
void Show() { }

private:


};


Node * TailNode::Insert(Data * theData)

{

InternalNode * dataNode = new InternalNode(theData, this);

return dataNode;

}


class LinkedList : public Node

{

public:

LinkedList();

~LinkedList() { delete nextNode; }

virtual Node * Insert(Data * theData);

virtual 
void Show() { nextNode->Show(); }
private:

Node * nextNode;

};


LinkedList::LinkedList()

{

nextNode = new TailNode;

}


Node * LinkedList::Insert(Data * theData)

{

nextNode = nextNode->Insert(theData);

return this;

}


int main()
{

Data * pData;

int val;
LinkedList ll;


for (;;)
{

cout << "What value do you want to add to the list? (0 when done): ";

cin >> val;

if (!val)
break;

pData = new Data(val);

ll.Insert(pData);

}


cout << "\n\n";

ll.Show();

cout << "\n\n";

return 0; 

}




Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
Cpp - Access a Class Member Function Without Creating a Class Object
Cpp - Writing Macro Definition
Cpp - using namespace std
Cpp - Using enum
Cpp - Pointers in C++
Cpp - How to use DLL file in the C++ Program
Cpp - creating and Linking DLL Files to C++ program
Cpp - 1) vector method and iterator (simple Example)
Cpp - 2) Sort and Binary search
Cpp - 3) Vector and Copy vector
Cpp - 4) for_each ( Vector )
Cpp - 5) List and Iterator
Cpp - 6) list and Vector example.2
Cpp - 7) example"fill, copy, list" statement
Cpp - 8) list and set

Related References
cpp - operator overloading
cpp - default arguments
cpp - c++ interview tips
cpp - virtual destructors
cpp - c++ destructors
cpp - csc interview questions
object oriented programming paradigm
elements of object oriented programming
basic concepts of oops and structure of c++ program
exception (runtime error) handling - cpp
cpp function pointers
cpp - access specifiers between base and derived classes
cpp - inautix interview questions
comparing popular programming languages

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us