Skip to content

Instantly share code, notes, and snippets.

@lsiddiqsunny
Created September 6, 2017 19:12
Show Gist options
  • Save lsiddiqsunny/669567f85af095de8d181e593fb6ff55 to your computer and use it in GitHub Desktop.
Save lsiddiqsunny/669567f85af095de8d181e593fb6ff55 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
template<typename T>
struct linkedlist
{
T data;
linkedlist *next;
};
template<typename T>
class Queue
{
T data;
linkedlist<T> *root,*tail;
int Size;
public:
Queue ()
{
root=NULL;
tail=NULL;
Size=0;
}
void push(T data)
{
Size++;
if(root==NULL)
{
root=new linkedlist<T>;
root->data=data;
root->next=NULL;
tail=root;
return;
}
if(root->next==NULL){
linkedlist<T> *temp=new linkedlist<T>;
temp->data=data;
temp->next=NULL;
tail=temp;
root->next=tail;
return;
}
linkedlist<T> *temp=new linkedlist<T>;
temp->data=data;
temp->next=NULL;
tail=temp;
}
T pop()
{
if(root==NULL){
return INT_MIN;
}
else{
linkedlist<T> *temp=root->next;
T d=root->data;
root=temp;
return d;
}
}
int size()
{
return Size;
}
void print()
{
linkedlist<T> *c_node=root;
while(c_node!=NULL)
{
cout<<c_node->data<<" ";
c_node=c_node->next;
}
cout<<endl;
}
};
int main()
{
Queue <int> s;
s.push(3);
// s.print();
s.push(43);
//s.print();
s.push(31);
s.print();
s.pop();
s.print();
s.pop();
s.print();
s.pop();
s.print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment