Sei sulla pagina 1di 15


A sLack ls a fundamenLal compuLer sclence daLa sLrucLure and can be deflned ln an absLracL lmplemenLaLlonfree
manner or lL can be generally deflned as SLack ls a llnear llsL of lLems ln whlch all addlLlons and deleLlon ls
resLrlcLed Lo one end LhaL ls 1op A sLack ls a resLrlcLed daLa sLrucLure because only a small number of operaLlons
are performed on lL 1he naLure of Lhe pop and push operaLlons also mean LhaL sLack elemenLs have a naLural
order LlemenLs are removed from Lhe sLack ln Lhe reverse order Lo Lhe order of Lhelr addlLlon Lherefore Lhe
lower elemenLs are Lhose LhaL have been on Lhe sLack Lhe longes
A slmple sLack example push empLy pop and Lop

#lnclude losLream
#lnclude sLack
uslng namespace sLd
lnL maln()

sLackchar sLackCb[ecL
couL opplng
couL sLackCb[ecLLop() endl

reLurn 0

A queue (pronounced /rk[ur/ kew) ls a parLlcular klnd of collecLlon ln whlch Lhe enLlLles ln Lhe collecLlon are kepL
ln order and Lhe prlnclpal (or only) operaLlons on Lhe collecLlon are Lhe addlLlon of enLlLles Lo Lhe rear Lermlnal
poslLlon and removal of enLlLles from Lhe fronL Lermlnal poslLlon 1hls makes Lhe queue a llrsLlnllrsLCuL (lllC)
daLa sLrucLure ln a lllC daLa sLrucLure Lhe flrsL elemenL added Lo Lhe queue wlll be Lhe flrsL one Lo be removed
1hls ls equlvalenL Lo Lhe requlremenL LhaL once an elemenL ls added all elemenLs LhaL were added before have Lo
be removed before Lhe new elemenL can be lnvoked A queue ls an example of a llnear daLa sLrucLure
Cueues are common ln compuLer programs where Lhey are lmplemenLed as daLa sLrucLures coupled wlLh access
rouLlnes as an absLracL daLa sLrucLure or ln ob[ecLorlenLed languages as classes Common lmplemenLaLlons are
clrcular buffers and llnked llsLs
Common operaLlons from Lhe C++ SLandard 1emplaLe Llbrary lnclude Lhe followlng
bool empLy()
8eLurns 1rue lf Lhe queue ls empLy and lalse oLherwlse
1 fronL()
8eLurns a reference Lo Lhe value aL Lhe fronL of a nonempLy queue 1here ls also a consLanL verslon of Lhls
funcLlon consL 1 fronL()
vold pop()
8emoves Lhe lLem aL Lhe fronL of a nonempLy queue
vold push(consL 1 foo)
lnserLs Lhe argumenL foo aL Lhe back of Lhe queue
slze_Lype slze()
8eLurns Lhe LoLal number of elemenLs ln Lhe queue
ueue |mp|ementat|on
1heoreLlcally one characLerlsLlc of a queue ls LhaL lL does noL have a speclflc capaclLy 8egardless of how many
elemenLs are already conLalned a new elemenL can always be added lL can also be empLy aL whlch polnL
removlng an elemenL wlll be lmposslble unLll a new elemenL has been added agaln
llxed lengLh arrays are llmlLed ln capaclLy and lnefflclenL because lLems need Lo be copled Lowards Lhe head of Lhe
queue Powever concepLually Lhey are slmple and work wlLh early languages such as lC818An and 8ASlC whlch
dld noL have polnLers or ob[ecLs MosL modern languages wlLh ob[ecLs or polnLers can lmplemenL or come wlLh
llbrarles for dynamlc llsLs Such daLa sLrucLures may have noL speclfled flxed capaclLy llmlL besldes memory
consLralnLs Cueue overflow resulLs from Lrylng Lo add an elemenL onLo a full queue and queue underflow happens
when Lrylng Lo remove an elemenL from an empLy queue
Lxample C++ language code uslng array

1hls C++ language example lmplemenLs a clrcular queue wlLh a flxed lengLh array and global varlables for
concepLual slmpllclLy lL does noL requlre allocaLlng dynamlc sLorage whlch can affecL performance 8y changlng
Lhe locaLlon of Lhe head and Lall Lhere ls no need for daLa Lo change places as ls Lhe case of a physlcal llne where
people acLually walk from Lhe end of Lhe llne Lo Lhe head
// global daLa sLrucLure for slmpllclLy
// CpplnLervlewcpp ueflnes Lhe enLry polnL for Lhe console appllcaLlon
#lnclude sLdafxh
#lnclude losLream
#lnclude maLhh
#lnclude Llmeh
uslng namespace sLd
#deflne CMAx 10
sLrucL node

lnL value
node* nexL

node* CCMAx
lnL head0
lnL Lall0

// reLurn 1 lf successful oLherwlse 0
lnL enqueue(node* p)


Lall (Lall+1)CMAx
reLurn 1


reLurn 0

// reLurn 1 lf successful oLherwlse 0
lnL dequeue(node** p)

lf(Lall head)

reLurn 0

*p Chead
Chead nuLL
head CMAx
reLurn 1

vold prlnLqueue()

lnL lndex head
lnL queuelengLh (Lall+CMAxhead) CMAx
for(lnL lndexhead lndex head+ queuelengLh lndex ++)

couL C(lndex+CMAx) CMAxvalue

couL endl

vold rlnLArray()

for(lnL l0 l 10 l++)


couL Clvalue


couL 00

couL endl

// generaLe random number from 1100
lnL generaLe8andnum()

///* lnlLlallze random seed */
//srand ( Llme(nuLL) )

/* generaLe number of nodes */
reLurn rand() 100 + 1

lnL maln(lnL argc char* argv)

lnL l1

lnL rand generaLe8andnum()

node* n (node*)malloc(slzeof(node))


node* p



reLurn 0
|nked ||st
Basically, a linked list is a collection of nodes. Each node in the list has two components - a
component to store information about that node (i.e. data) and a component to store the
next "linked node. The last node in a linked list indicates that it does not have a "linked
node - usually by a null reference. The address of the first node is stored in a separate
location, called the head or first.

The code snippet below shows a basic structure of a node declaration in c++.
struct nodeType
int data;
nodeType link;
The variable declaration is as follows.
nodeType head;

Basic Operations of a Linked List
There are a few basic operations that are typically needed with a linked list
O $earch a list to determine whether a particular item is in the list
O nsert an item in the list
O elete an item from the list
%raversing/Searching a List
To traverse a linked list you need some way to move around the list. The *head is a pointer
in the list, but you cannot use it to traverse the list because then you would loose a
reference to the beginning of the list.
Typically to traverse the list, you will need to declare another pointer that follows the same
declaration as head, but that is not critical if you change where its pointing to.
current = head;
while (current != NULL)
cout << current-data << " ";
current = current-link;

tem nsertion and Deletion
To insert a new node into a linked list, you need to do the following:
O reate a new node
O etermine the position in the list that you want to insert it and assign it to current
O ake the new node is point to the same link that current is pointing to
O ake current now point to the new node
n code, assuming current was at the correct position, then it would look something like
nodeType newNode;

newNode = new nodeType;
newNode-data = 123;
newNode-link = current-link;
current-link = newNode;

To delete a node from a linked list, you need to do the following:
O etermine the node that you want to remove and point current to the node prior to
that node
O reate a pointer to point to the node you want to delete (i.e. *p)
O ake currents link now point to ps link
O delete p from memory
n code, assuming current was at the correct position, then it would look something like
deleteNode = new nodeType;
deleteNode = current-link;
current-link = deleteNode-link;
delete deleteNode;
Building a Linked List
There are two main ways to build a linked list, forward and backward.
find the best way to explain this approach is code.. below if the forward approach,
meaning the node is inserted forwards - i.e. at the back of the list.
int main()
// Init the list
nodeType first;
nodeType last;
nodeType newNode;
int num;

first = NULL;
last = NULL;

// Build the list the forward approach
for (int i=0; i<3; i++)
cout << "Enter number :";
cin num;
newNode = new nodeType; // Create the new node
newNode-data = num; // and assign its data value
newNode-link = NULL; // make its link point to nothing
if (first == NULL) // If there is nothing in the list,
then make the
, // newNode the first item and the
last item
first = newNode;
last = newNode;
else // Else if first already has a
, // make the last item link to the
last-link = newNode; // and make newNode the last item
last = newNode;
// Display the list
f you look at the forward approach, you need two pointers to maintain the list - first &
last. This is because you dont want to traverse the list each time you want to add
something. The backward way of building a list in my opinion is easier to read and code
since you only need to worry about the head of the list. The code implementation is listed
// Init the list
nodeType head;
nodeType newNode;
int num;
head = NULL;

// Build the list using the backward approach
for (int i=0; i<3; i++)
cout << "Enter number :";
cin num;
newNode = new nodeType; // Create the new node
newNode-data = num; // and assign its data value
newNode-link = head; // make its link point to the front of
the list
head = newNode; // make the head now be the newNode

1ree class represenLs a node ln Lhe Lree so lL has parenL and lL has chlldren ln Lhls way l use a slngle class for
represenLlng a node and a Lree so Lhe Lhe Lree ls a node wlLhouL parenL and leaf ls a node wlLhouL chlldren lL also
has reference (8ef) Lo Lhe daLa lLs lmplemenLaLlon ls dlvlded beLween Lwo classes nodeuaLa and 1ree nodeuaLa
ob[ecLs holds Lhe acLual daLa for Lhe parenL and Lhe chlldren buL 1ree ob[ecL manlpulaLes Lhls daLa and glves
access Lo Lhem AcLually youll always use only 1ree class dlrecLly
Very s|mp|e usage examp|e
#nclude sLdloh

#lnclude Lreeh

#lnclude sLrlng

uslng namespace sLd

Lypedef 1reesLrlng SLrlng1ree
SLrlng1reenode SLrlngnode //

recurslve funcLlon for prlonlLng nodes vold
rlnLnode(SLrlngnode nodelnL nLevel) //

prlnL new llne fpuLs(nsLdouL)

prlnL spaces for lndenL (2 for every level) sLrlng
s sreslze(nLevel*2
) fpuLs(sc_sLr()sLdouL)

prlnL Lhe value fpuLs(nodegeL_uaLa()c_sLr()sLdouL)

lLeraLe Lhrough chlldren nLevel++
n 0nnodeCounLn++)

lnL maln(lnL argc char* argv)

// deflne Lhe Lree

SLrlng1ree Lree

//add few nodes

SLrlngnode nodeLree

SLrlngnode node2nodeAddnode(2)

// prlnL Lhe Lree
// walL for enLer
char buf3
fpuLs(nress Ln1L8 sLdouL)
reLurn 0

ash @ab|es
@he very s|mp|e hash tab|e examp|e
ln Lhe currenL arLlcle we show Lhe very slmple hash Lable example lL uses slmple hash funcLlon colllslons are
resolved uslng llnear problng (open addresslng sLraLegy) and hash Lable has consLanL slze 1hls example clearly
shows Lhe baslcs of hashlng Lechnlque

Pash Lable
underlylng array has consLanL slze Lo sLore 128 elemenLs and each sloL conLalns keyvalue palr key ls sLored Lo
dlsLlngulsh beLween keyvalue palrs whlch have Lhe same hash

Pash funcLlon
1able allows only lnLegers as values Pash funcLlon Lo be used ls Lhe remalnder of dlvlslon by 128 ln Lhe vlew of
lmplemenLaLlon Lhls hash funcLlon can be encoded uslng remalnder operaLor or uslng blLwlse Anu wlLh 127
Colllslon resoluLlon sLraLegy

Llnear problng ls applled Lo resolve colllslons ln case Lhe sloL lndlcaLed by hash funcLlon has already been
occupled algorlLhm Lrles Lo flnd an empLy one by problng consequenL sloLs ln Lhe array
C++ lmplemenLaLlon

class PashLnLry
lnL key
lnL value
PashLnLry(lnL key lnL value)
Lhlskey key
Lhlsvalue value

lnL geLkey()
reLurn key

lnL geLvalue()
reLurn value

consL lnL 1A8LL_SlZL 128

class PashMap
PashLnLry **Lable
Lable new PashLnLry*1A8LL_SlZL
for (lnL l 0 l 1A8LL_SlZL l++)
Lablel nuLL

lnL geL(lnL key)
lnL hash (key 1A8LL_SlZL)
whlle (Lablehash ! nuLL LablehashgeLkey() ! key)
hash (hash + 1) 1A8LL_SlZL
lf (Lablehash nuLL)
reLurn 1
reLurn LablehashgeLvalue()

vold puL(lnL key lnL value)
lnL hash (key 1A8LL_SlZL)
whlle (Lablehash ! nuLL LablehashgeLkey() ! key)
hash (hash + 1) 1A8LL_SlZL
lf (Lablehash ! nuLL)
deleLe Lablehash
Lablehash new PashLnLry(key value)

for (lnL l 0 l 1A8LL_SlZL l++)
lf (Lablel ! nuLL)
deleLe Lablel
deleLe Lable