Experiment No 3
Experiment No 3
operations */
#include <bits/stdc++.h>
class Stack {
int top;
public:
int pop();
int peek();
bool isEmpty();
};
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
return false;
else {
a[++top] = x;
return true;
int Stack::pop()
if (top < 0) {
return 0;
else {
int x = a[top--];
return x;
int Stack::peek()
if (top < 0) {
cout << "Stack is Empty";
return 0;
else {
int x = a[top];
return x;
bool Stack::isEmpty()
int main()
class Stack s;
s.push(10);
s.push(20);
s.push(30);
while(!s.isEmpty())
s.pop();
return 0;
OUTPUT :-
Top element is : 20
#include<stdio.h>
#include<stdlib.h>
struct Node
int data;
};
newNode->data = val;
newNode->next = head;
//so that head will always point the last inserted data
head = newNode;
}
void pop()
if(head == NULL)
printf("Stack is Empty\n");
else
temp = head;
head = head->next;
free(temp);
while(temp != NULL)
printf("%d->", temp->data);
temp = temp->next;
printf("NULL\n");
int main()
push(10);
push(20);
push(30);
printf("Linked List\n");
display();
pop();
display();
pop();
return 0;
OUTPUT:-
Linked List
30->20->10->NULL
Poped element = 30
20->10->NULL
Poped element = 20
10->NULL