0% found this document useful (0 votes)
5 views3 pages

HW1

Uploaded by

iirwed79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

HW1

Uploaded by

iirwed79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

using namespace std;


class Node {
public:
int data;
Node* Link;
Node(int n) {
this->data = n;
this->link = NULL;
}
};
class Stack{
Node* top;
public:
Stack() { top = NULL; }
void Push (int data)
{
Node* temp = new Node(data);
If (!temp)
{
cout<<”Stack is empty.”<<endl;
exit(1);
}
temp->data = data;
temp->link = top;
top = temp;
bool isEmpty() {
return top == NULL;
}
Void Pop()
{
Node* temp;
If (top == NULL) {
cout<<”Stack is empty.”<<endl;
exit(1);
}
else
{
temp = top;
top = top->link;
free(temp);
}
}
void DisplayStack()
{
Node* temp;
If(top == NULL)
{
cout<<”Stack is empty.”<<endl;
exit (1);
}
else
{
cout<<”Top --------- bottom”<<endl;
temp = top;
while (temp != NULL)
{
cout<<temp->data;
temp = temp->data;
if( temp != NULL)
cout<<” <- “;
else
cout<<endl;
}
}
}
};

You might also like