Quick actions

cmd+k|ctrl+k

Navigation

Languages

STACKS LINKEDLIST

Snippet info

Language

Cpp

Visibility

public

Author

jameeulla3

Created

2024-08-05T23:32:22.288529Z

Updated

2024-08-05T23:32:22.288529Z

#include <iostream>
using namespace std;
struct node{
    int value; 
    struct node* next; 
};
class Stack
{
    struct node *top;
    public:
    Stack()
    {
        top = NULL;
    }
    void push(int value)
    {
        struct node *temp = new struct node;
        if(top==NULL)
        {
            temp->value = value; 
            temp->next = NULL;
            top = temp;
        }
        else
        {
             temp->value = value;
             temp->next = top; 
             top = temp;
        }
    }
    int pop()
    {
        int value = top->value;
        struct node *temp = top;
        top = top->next;
        free(temp);
        return value;
        
    }
    int peek()
    {
        return top->value;
    }
};
int main() {
    Stack s1; 
    s1.push(5);
    s1.push(6);
    s1.push(7);
    cout<<s1.pop()<<"\n";
    cout<<s1.peek()<<"\n";
    return 0;
}
INFO