Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
amandewatnitrr authored Jun 25, 2021
1 parent 0242728 commit d958d99
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <bits/stdc++.h>
using namespace std;

class node
{
public:
int data;
node* next;
node(int x)
{
data = x;
next = NULL;
}
};

class que
{
node* front;
node* back;

public:

que()
{
front = NULL;
back = NULL;
}

void push(int x);
void pop();
int peek();
bool isempty();
};

void que::push(int x)
{
node* n = new node(x);

if(isempty())
{
front = n;
back = n;
}

back->next = n;
back = n;

}

void que::pop()
{
if(isempty())
{
cout<<"Queue is empty";
return ;
}
node* todelete = front;
front = front->next;
delete todelete;
}

int que::peek()
{
if(isempty())
{
cout<<"Queue is empty";
exit(1);
}
return front->data;
}

bool que::isempty()
{
if(front == NULL)
{
return true;
}
else{return false;}
}

int main()
{
que q;
int n,m,x;

cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
q.push(x);
}

while(!q.isempty())
{
cout<<q.peek()<<" ";
q.pop();
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <bits/stdc++.h>
using namespace std;

class que
{
stack<int>s1;

public:

void push(int x)
{
s1.push(x);
}

int pop()
{
if(s1.empty())
{
cout<<"Empty Queue";
return -1;
}

int x = s1.top();
s1.pop();

if(s1.empty())
{
return x;
}
int item = pop();
s1.push(x);
return item;

}

bool isempty()
{
if(s1.empty()){ return true;}
return false;
}

};

int main()
{
que q;
int n,x;
cin>>n;

for(int i=0;i<n;i++)
{
cin>>x;
q.push(x);
}

while(!q.isempty())
{
cout<<q.pop()<<" ";
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <bits/stdc++.h>
using namespace std;

class que
{
stack<int>s1;
stack<int>s2;

public:

void push(int x)
{
s1.push(x);
}

int pop()
{
if(s1.empty() and s2.empty())
{
cout<<"Empty Queue";
return -1;
}

if(s2.empty())
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
int topval = s2.top();
s2.pop();
return topval;
}

bool isempty()
{
if(s1.empty() and s2.empty()){ return true;}
return false;
}

};

int main()
{
que q;
int n,x;
cin>>n;

for(int i=0;i<n;i++)
{
cin>>x;
q.push(x);
}

while(!q.isempty())
{
cout<<q.pop()<<" ";
}

return 0;
}
Binary file not shown.

0 comments on commit d958d99

Please sign in to comment.