Tech Unleashed

Wohoo!! We're on YouTube

Friday, 27 October 2023

C++ : Lecture 22: Stack - Linked List implementation : Mastering Stack Made Simple !

 Mastering Stack Made Simple: Your Easy Guide to Data Structures !


Linked List Implementation of Stack code [C++] :


// Shyam Sunder Kanth
// Insta : still_23.6_8
// Stack Linked List implementation

#include<iostream>

using namespace std;

class Stack{
    public:
    int data;
    Stack *next;

    Stack(int data){
        this->data = data;
        this->next = NULL;
    }
};

// Function
void push(Stack* &head, Stack* &tail, int data){
    // Step 1: Creation of new Node
    Stack* newNode = new Stack(data);

    // Step 2: Handle empty list case
    if(head == NULL){
        head = newNode;
        tail = newNode;
        return;
    }

    // Step 3:Handle non empty list case
    tail->next = newNode;
    tail = newNode;
}

void pop(Stack* &head, Stack* &tail){
    // Step 1: Handle empty list case
    if(head == NULL && tail == NULL){
        cout<<"Stack underflow"<<endl;
        return;
    }

    // // Step 2 : handle single node case
    if(head == tail){
        head = NULL;
        tail = NULL;
        return;
    }

    // Step 3: Handle more than one node case
    Stack* temp = head;
    while(temp->next != tail){
        temp = temp->next;
    }
    tail = temp;
    tail->next = NULL;
}

int size(Stack* head){
    if(head == NULL){
        return 0;
    }
    Stack* temp = head;
    int cnt = 0;
    while(temp != NULL){
        temp = temp->next;
        cnt++;
    }
    return cnt;
}

int peek(Stack* head, Stack* tail){
    if(head == NULL){
        cout<<"Stack is empty";
        return -1;
    }
    return tail->data;
}

bool empty(Stack* head){
    if(head == NULL){
        return true;
    }else{
        return false;
    }
}

int main()
{
    Stack* head = NULL;
    Stack* tail = NULL;

    push(head,tail,4);
    push(head,tail,5);
    push(head,tail,6);

    pop(head,tail);
    pop(head,tail);
    pop(head,tail);

    cout<<"Top : "<<peek(head,tail)<<endl;
    cout<<"Size: "<<size(head)<<endl;

    cout<<"isEmpty : "<<empty(head)<<endl;

    return 0;
}

Hey, thanks for watching our video about Stack Linked List implementation ! In this video we’ll walk you through:

- Concept

- Properties

- Operations

- Implementation

- Code


All codes are available at my GitHub account, check them out here:

https://github.com/shyamkanth/Placements


Check out other videos from DS Revealed playlist: https://www.youtube.com/playlist?list=PLNXqJgOsTCZOB60T9HDhMf_o8RXbNWgqS


Check out our channel here: https://www.youtube.com/@DevelopersByte


Find us at: https://shyamkanth.github.io/


Timestamps:

0:00 Intro

0:30 Concept

1:00 Properties

2:35 Functions

4:40 Creation of linked list

8:47 Push Operation

12:48 Pop Operation

15:01 Top Operation

17:10 Empty Operation

18:30 Size Operation

21:55 Code

43:25 Outro


About our channel:

Our channel is about Revealing the secrets of Data Structure. We cover lots of cool stuff such as Codes, Concepts and Implementations.

Check out our channel here: https://www.youtube.com/@DevelopersByte

Don’t forget to subscribe!


Follow me on social media:

Get updates or reach out to Get updates on our Social Media Profiles!

GitHub: https://github.com/shyamkanth/

Instagram: https://instagram.com/still_23.6_8

Instagram personal: https://instagram.com/itz_sammmii

No comments:

Post a Comment