STACK IMPLEMENTATION USING LINKLIST
// stack_implemention_using_linked_list.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std ;
struct stack {
int data ;
struct stack *next ;
};
struct stack *top = NULL;
struct stack *ptop= NULL;
struct stack *last= NULL;
class stack_List
{
protected :
struct stack * node;
stack_List *stack_obj;
/*stack_List()
{
node = NULL;
}*/
/*~stack_List()
{
delete stack_obj;
}*/
public :
void push(int data);
void pop ();
void printData();
};
void stack_List::push(int data)
{
struct stack *temp = NULL;
temp = new (nothrow) stack ; // notthorw if memroy not availble in heap area then it won't throe badallocation insated of it will return null
if(temp == NULL)
{
cout << "Memory Allocation Fialed";
}
if(top == NULL )
{
temp->data = data;
temp->next = NULL;
top = temp;
ptop = temp;
last = temp;
}
else{
temp->data = data;
top->next = temp ;
temp->next = NULL;
ptop = top;
top = temp;;
}
}
void stack_List::pop(){
struct stack * temp = NULL;
if(top == NULL)
{
cout << "stack underflow" ;
return;
}
temp = last;
while(temp->next != top)
{
temp = temp->next;
}
top= temp;
temp = temp->next;
top->next = NULL;
//ptop = top ;
delete temp;
}
void stack_List::printData(){
struct stack * temp = NULL;
temp = last;
while(temp)
{
cout << temp->data <<endl;
temp = temp->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
stack_List *stack_obj = new stack_List();
cout << " *********************************** " << endl;
cout << " STACK IMPLEMENTATION USING LINKLIST " << endl;
cout << " *********************************** " << endl;
cout <<endl;
cout <<endl;
stack_obj ->push(10);
stack_obj ->push(20);
stack_obj ->push(50);
cout << " printing stack data" << endl;
cout <<endl;
stack_obj ->printData();
stack_obj->pop();
cout <<endl;
cout << " printing stack data after 1st pop element"<<endl;
cout <<endl;
stack_obj ->printData();
delete stack_obj;
cout <<endl;
system("pause");
return 0;
}
//
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std ;
struct stack {
int data ;
struct stack *next ;
};
struct stack *top = NULL;
struct stack *ptop= NULL;
struct stack *last= NULL;
class stack_List
{
protected :
struct stack * node;
stack_List *stack_obj;
/*stack_List()
{
node = NULL;
}*/
/*~stack_List()
{
delete stack_obj;
}*/
public :
void push(int data);
void pop ();
void printData();
};
void stack_List::push(int data)
{
struct stack *temp = NULL;
temp = new (nothrow) stack ; // notthorw if memroy not availble in heap area then it won't throe badallocation insated of it will return null
if(temp == NULL)
{
cout << "Memory Allocation Fialed";
}
if(top == NULL )
{
temp->data = data;
temp->next = NULL;
top = temp;
ptop = temp;
last = temp;
}
else{
temp->data = data;
top->next = temp ;
temp->next = NULL;
ptop = top;
top = temp;;
}
}
void stack_List::pop(){
struct stack * temp = NULL;
if(top == NULL)
{
cout << "stack underflow" ;
return;
}
temp = last;
while(temp->next != top)
{
temp = temp->next;
}
top= temp;
temp = temp->next;
top->next = NULL;
//ptop = top ;
delete temp;
}
void stack_List::printData(){
struct stack * temp = NULL;
temp = last;
while(temp)
{
cout << temp->data <<endl;
temp = temp->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
stack_List *stack_obj = new stack_List();
cout << " *********************************** " << endl;
cout << " STACK IMPLEMENTATION USING LINKLIST " << endl;
cout << " *********************************** " << endl;
cout <<endl;
cout <<endl;
stack_obj ->push(10);
stack_obj ->push(20);
stack_obj ->push(50);
cout << " printing stack data" << endl;
cout <<endl;
stack_obj ->printData();
stack_obj->pop();
cout <<endl;
cout << " printing stack data after 1st pop element"<<endl;
cout <<endl;
stack_obj ->printData();
delete stack_obj;
cout <<endl;
system("pause");
return 0;
}
Comments
Post a Comment