How to Make a Stack using Linked List in C++
c++ stack codes
#include <cstdlib>
#include <iostream>
using namespace std;
class Node{
public:
char data;
Node * next;
};
class Stack{
public:
Stack();
void push(char);
char pop();
char top();
bool isempty();
void printStack();
int get_count(){return count;}
private:
Node head;
int count;
};
Stack::Stack(){
count=0;
head.next=NULL;
}
void Stack::push(char c){
Node *newNode = new Node;
newNode->data = c;
if(isempty()){
head.next= newNode;
newNode->next = NULL;
}else{
newNode->next = head.next;
head.next = newNode;
}
count++;
}
char Stack::pop(){
if(get_count()==0){
cout<<"Stack is empty!"<next;
char data= temp->data;
delete temp;
count--;
return data;
}
}
bool Stack::isempty(){
return (get_count()==0)?1:0;
}
char Stack::top(){
return head.next->data;
}
void Stack::printStack(){
Node * temp=head.next;
while(head.next!=NULL){
cout<data<next;
}
head.next=temp;
}
int main(int argc, char *argv[])
{
Stack *s=new Stack();
s.push('a');
s.push('b');
s.push('c');
s.pop();
s.push('d');
cout<<"I am printing mystack for the first time"<<endl;
s.printStack();
system("PAUSE");
return EXIT_SUCCESS;
}
u can use this linked list code and u can share..
stack.cpp
Dear Author http://www.neteyaz.com !
This theme is simply matchless
Comment by Words_day — December 10, 2009 @ 1:45 am