Você está na página 1de 4

//

// main.cpp
// sorting linked list
//
// Created by skyphyo on 20/04/14.
// Copyright (c) 2014 skyphyo. All rights reserved.
//
#inclde !iostream"
enm # k$s%maller& k$s'arger& k$s%ame()
// *ata class to pt into the linked list
// Any class in this linked list mst spport t+o methods,
// %ho+ (displays the vale) and Compare
// (retrns relative position)
class *ata
#
pblic,
*ata(int val),my-ale(val)#(
.*ata()#(
int Compare(const *ata /))
void %ho+() # std,,cot !! my-ale !! std,,endl) (
private,
int my-ale)
()
// Compare is sed to decide +here in the list
// a particlar ob0ect belongs.
int *ata,,Compare(const *ata / the1ther*ata)
#
i2 (my-ale ! the1ther*ata.my-ale)
retrn k$s%maller)
i2 (my-ale " the1ther*ata.my-ale)
retrn k$s'arger)
else
retrn k$s%ame)
(
// 2or+ard declarations
class 3ode)
class 4ead3ode)
class 5ail3ode)
class $nternal3ode)
// A*5 representing the node ob0ect in the list
// 6very derived class mst override $nsert and %ho+
class 3ode
#
pblic,
3ode()#(
virtal .3ode()#(
virtal 3ode 7 $nsert(*ata 7 the*ata)80)
virtal void %ho+() 8 0)
private,
()
// 5his is the node +hich holds the actal ob0ect
// $n this case the ob0ect is o2 type *ata
// 9e:ll see ho+ to make this more general +hen
// +e cover templates
class $nternal3ode, pblic 3ode
#
pblic,
$nternal3ode(*ata 7 the*ata& 3ode 7 ne;t))
virtal .$nternal3ode()# delete my3e;t) delete my*ata) (
virtal 3ode 7 $nsert(*ata 7 the*ata))
virtal void %ho+()
# my*ata<"%ho+()) my3e;t<"%ho+()) ( // delegate=

private,
*ata 7 my*ata) // the data itsel2
3ode 7 my3e;t) // points to ne;t node in the linked list
()
// All the constrctor does is to initiali>e
$nternal3ode,,$nternal3ode(*ata 7 the*ata& 3ode 7 ne;t),
my*ata(the*ata)&my3e;t(ne;t)
#
(
// the meat o2 the list
// 9hen yo pt a ne+ ob0ect into the list
// it is passed ot the node +hich 2igres ot
// +here it goes and inserts it into the list
3ode 7 $nternal3ode,,$nsert(*ata 7 the*ata)
#

// is the ne+ gy bigger or smaller than me?
int reslt 8 my*ata<"Compare(7the*ata))


s+itch(reslt)
#
// by convention i2 it is the same as me it comes 2irst
case k$s%ame, // 2all throgh
case k$s'arger, // ne+ data comes be2ore me
#
$nternal3ode 7 data3ode 8
ne+ $nternal3ode(the*ata& this))
retrn data3ode)
(

// it is bigger than $ am so pass it on to the ne;t
// node and let 4$@ handle it.
case k$s%maller,
my3e;t 8 my3e;t<"$nsert(the*ata))
retrn this)
(
retrn this) // appease @%C
(
// 5ail node is 0st a sentinel
class 5ail3ode , pblic 3ode
#
pblic,
5ail3ode()#(
virtal .5ail3ode()#(
virtal 3ode 7 $nsert(*ata 7 the*ata))
virtal void %ho+() # (
private,
()
// $2 data comes to me& it mst be inserted be2ore me
// as $ am the tail and 3154$3A comes a2ter me
3ode 7 5ail3ode,,$nsert(*ata 7 the*ata)
#
$nternal3ode 7 data3ode 8 ne+ $nternal3ode(the*ata& this))
retrn data3ode)
(
// 4ead node has no data& it 0st points
// to the very beginning o2 the list
class 4ead3ode , pblic 3ode
#
pblic,
4ead3ode())
virtal .4ead3ode() # delete my3e;t) (
virtal 3ode 7 $nsert(*ata 7 the*ata))
virtal void %ho+() # my3e;t<"%ho+()) (
private,
3ode 7 my3e;t)
()
// As soon as the head is created
// it creates the tail
4ead3ode,,4ead3ode()
#
my3e;t 8 ne+ 5ail3ode)
(
// 3othing comes be2ore the head so 0st
// pass the data on to the ne;t node
3ode 7 4ead3ode,,$nsert(*ata 7 the*ata)
#
my3e;t 8 my3e;t<"$nsert(the*ata))
retrn this)
(
// $ get all the credit and do none o2 the +ork
class 'inked'ist
#
pblic,
'inked'ist())
.'inked'ist() # delete my4ead) (
void $nsert(*ata 7 the*ata))
void %ho+All() # my4ead<"%ho+()) (
private,
4ead3ode 7 my4ead)
()
// At birth& i create the head node
// $t creates the tail node
// %o an empty list points to the head +hich
// points to the tail and has nothing bet+een
'inked'ist,,'inked'ist()
#
my4ead 8 ne+ 4ead3ode)
(
// *elegate& delegate& delegate
void 'inked'ist,,$nsert(*ata 7 p*ata)
#
my4ead<"$nsert(p*ata))
(
// test driver program
int main()
#
*ata 7 p*ata)
int val)
'inked'ist ll)

// ask the ser to prodce some vales
// pt them in the list
2or ()))
#
std,,cot !! B9hat vale? (0 to stop), B)
std,,cin "" val)
i2 (=val)
break)
p*ata 8 ne+ *ata(val))
ll.$nsert(p*ata))
(

// no+ +alk the list and sho+ the data
ll.%ho+All())
retrn 0) // ll 2alls ot o2 scope and is destroyed=
(

Você também pode gostar