Created
April 25, 2019 05:36
-
-
Save dumbbellcode/797830a5458b78b65b183716ed82cf9e to your computer and use it in GitHub Desktop.
Linked list implementation of stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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