For All Human Beings.......

Tuesday, November 20, 2007

Linked Stack : c++ code

struct NODE
{
int Data;
NODE *link;

};

class LStack
{
NODE *Top;

Public:
LStack(){Top=NULL;}
void Push();
void Pop();
void show_stack();
~LStack();
};

void LStack::Push()
{
NODE * ptr;
ptr=new NODE;
cout<<”Data:”;
cin>>ptr->Data;
ptr->link=Top;
Top=ptr;
}

void LStack::Pop()
{
if (Top!=NULL)
{
NODE *Temp=Top;
Cout<<”deleted../n”;
Top=Top->link;
delete Temp;
}
else
cout<<”Stack Empty..”;

}

void LStack::show_stack()
{
NODE *Temp=Top;
while(Temp!=NULL)
{
cout<
<Temp->Data;
Temp=Temp->link;
}
}

void LStack::~LStack() //Destructer Function
{
while (Top!=NULL)
{
NODE *Temp=Top;
Top=Top->Next;
delete Temp;
}
}

void main()
{
Stack ST; char Ch;
do
{
cout<<”P/O/D/Q”;cin>>Ch;
switch (Ch)
{
case ’P’:ST.Push();break;
case ’O’:ST.Pop();break;
case ’D’:ST.
show_stack();
}
}
while (Ch!=’Q’);

} // Destructor function will be called

// automatically when the scope of the

// object gets over

3 comments:

Unknown said...

struct NODE
{
int Data;
NODE *link;
};

//***********
class LStack
{
NODE *Top;
Public:
LStack(){Top=NULL;}
void Push();
void Pop();
void show_stack();
~LStack();
};

//***********
void LStack::Push()
{
NODE * ptr;
ptr=new NODE;
cout<<”Data:”;
cin>>ptr->Data;
ptr->link=Top;
Top=ptr;
}


//***********
void LStack::Pop()
{
if (Top!=NULL)
{
NODE *Temp=Top;
Cout<<<”deleted../n”;
Extra '<<<' should be '<<'
Top=Top->link;
delete Temp;
}
else
cout<<”Stack Empty..”;
}

//***********
void LStack::show_stack()
{
NODE *Temp=Top;
while(Temp!=NULL)
{


Correct statement(above)

Temp=Temp->link;
}
}

//***********
void LStack::~LStack() //Destructer Function
{
while (Top!=NULL)
{
NODE *Temp=Top;
Top=Top->Next;
delete Temp;
}
}


//***********
void main()
{
clrscr();
Stack ST; char Ch;
do
{
cout<<”P/O/D/Q”;cin>>Ch;
switch (Ch)
{
case ’P’:ST.Push();break;
case ’O’:ST.Pop();break;
case ’D’:ST.Disp(); /*wrong function name is give it should be ST.show_stack*/
}
}
while (Ch!=’Q’);
~~~~~ getch(); ~~~~~
} // Destructor function will be called

// automatically when the scope of the

// object gets over

Nita Arora said...

Thanks prerit I made the corrections.
Nita Arora

Unknown said...

thank u...it helped me for the computer assignments.....