Skip to content

Instantly share code, notes, and snippets.

@dumbbellcode
Created April 25, 2019 05:36
Show Gist options
  • Save dumbbellcode/797830a5458b78b65b183716ed82cf9e to your computer and use it in GitHub Desktop.
Save dumbbellcode/797830a5458b78b65b183716ed82cf9e to your computer and use it in GitHub Desktop.
Linked list implementation of stack
#include <iostream>
using namespace std;
struct node
{ int data;
node* next;
};
node* head;
void push(int x)
{ node* temp=new node();
temp->data=x; // the new element pushed in comes between head and first node
temp->next=head;
head=temp;
}
void pop()
{ node* temp=head;
head=temp->next;
delete temp;
}
void print()
{ node* temp=head;
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
} cout<<"\n";
}
int main()
{ head=NULL;
push(1);
push(2);
push(3);
print();
pop(); //3 pops out
print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment