Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions C++/LinkedListusingArrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

#include <iostream>
# define max 10
using namespace std;
typedef struct Node{
int data;
Node *next;
}Node;
class Stack{

int arr[max];
int top;
public:
Stack(){
top=-1;
}
bool isEmpty();
bool isFull();
void push(int );
int pop();
int size();
void display();
};

bool Stack::isEmpty(){
if (top==-1){
return true;
}
return false;
}
bool Stack::isFull(){
if (top==max-1){
return true;
}
return false;
}
void Stack ::push(int b ){
if(isFull()){
cout<<"Cannot enter as the stack is full";
}
else{
top++;
arr[top]=b;
}}
int Stack ::pop( ){
if(isEmpty()){
return 0;
}else{
int x;
x=arr[top];
top--;
return(x);
}
}
int Stack:: size(){
int i;
for ( i=0;i<=top;i++);
return i;
}

void Stack::display(){
for (int i=0;i<=top;i++){
cout<<arr[i]<<" ";
}

}
int main()
{
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
int v= s.pop();
cout<<"pooped element "<<v<<endl;
cout<<s.size()<<endl;
s.display();
return 0;
}

89 changes: 89 additions & 0 deletions C++/Stacks_Using_LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

#include <iostream>
using namespace std;
typedef struct Node{
int data;
Node *next;
}Node;
class Stack{

Node *top;
public:
Stack(){
top==NULL;
}
bool isEmpty();

void push(int x);
int pop();
int size();
void display();

};

bool Stack::isEmpty(){
if (top==NULL){
return true;
}
return false;
}
void Stack ::push(int b ){
Node *newNode;
newNode = new Node;
newNode->data=b;
newNode->next=top;
top=newNode;

}
int Stack ::pop( ){
if(isEmpty()){
return 0;
}
int y;
Node *temp;
temp=top;
top=top->next;
y=temp->data;

delete temp;
return y;

}
int Stack:: size(){
int count=0;
Node *temp;
temp=top;
while(temp!=NULL){
temp=temp->next;
count++;

}
return count;
}

void Stack::display(){
Node *temp;
temp=top;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s.display();
cout<<endl;
int v;
v= s.pop();
// cout<<"pooped element "<<v<<endl;
cout<<s.size()<<endl;

// return 0;
}