0% found this document useful (0 votes)
7 views

Stack in Linklist

It is about stack in link list

Uploaded by

Touqeer Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Stack in Linklist

It is about stack in link list

Uploaded by

Touqeer Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostream>

using namespace std;

class node
{
public:
int data;
node *next;
};
class stack
{
public:
int stksize;
node* head;

stack()
{
head = NULL;
stksize = 0;
}
void push(int val)
{
node *temp = new node();
temp->data = val;
temp->next = head;
head = temp;

cout<<"Element "<<val<<"pushed into the stack!"<<endl;


stksize++;
}
void pop()
{
if(head==NULL)
{
cout<<"Stack is empty!"<<endl;
return;
}
node *temp = head;
head=temp->next;
temp->next=NULL;
delete temp;

cout<<"Element popped!"<<endl;
stksize--;
}
int top()
{
if(head==NULL)
{
cout<<"No top element! stack is empty: "<<endl;
return -1;
}
cout<<"Top element is "<<head->data<<endl;
return head->data;
}
void display()
{
node *temp = head;
while(temp->next!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
int size()
{
cout<<"size of stack is:"<<stksize<<endl;
return stksize;
}
int empty()
{
if(head==NULL)
{
cout<<"stack is empty"<<endl;
return 1;
}
cout<<"Stack is not empty!"<<endl;
return 0;
}
};
int main()
{
stack s1;
int size,i,num;
cout<<"Enter the size of stack: ";
cin>>size;
for(i=1;i<=size;i++)
{
cin>>num;
s1.push(num);
s1.display();
}
//s1.pop();
s1.top();
s1.size();
s1.empty();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy