Data Structures: Linked List implementation of stacks

Поделиться
HTML-код
  • Опубликовано: 9 окт 2013
  • See complete series on data structures here:
    • Data structures
    In this lesson, we have discussed linked list based implementation of stack data structure.
    For practice problems and more, visit: www.mycodeschool.com
    Like us on Facebook: / mycodeschool
    Follow us on twitter: / mycodeschool

Комментарии • 206

  • @shadowphotography2848
    @shadowphotography2848 3 года назад +143

    7 year's ago he explained ,but still the best explanation

  • @mycodeschool
    @mycodeschool  10 лет назад +45

    Thanks Micheal !! We are working hard to get all the videos for you. :)

  • @ChienChiWang
    @ChienChiWang 9 лет назад +86

    I'm Taiwanese and thank you for making this video!!
    Your videos not only CLEAR but HAVE SUBTITLES!
    It's very helpful for those native language are not English.

    • @zackdon264
      @zackdon264 5 лет назад +1

      taiwan belongs to china so u r chinese

    • @mehularora3234
      @mehularora3234 4 года назад +19

      @@zackdon264 No Taiwan is Independent from China . Do not hurt their soverignity .

    • @zackdon264
      @zackdon264 4 года назад

      @@mehularora3234  u r a idiot

    • @piyushagarwal5830
      @piyushagarwal5830 3 года назад

      @@zackdon264 but he is not xi ping ping

    • @AmanKumar-nj5sz
      @AmanKumar-nj5sz 3 года назад +1

      @@zackdon264 U look more like one

  • @m.mubashar786
    @m.mubashar786 3 года назад +21

    A man telling the definition of linked list and explaining it in detail even he taught in his previous lectures......Superb Bro!!!

    • @maitahom9958
      @maitahom9958 3 года назад +4

      western education is haram....stop coding, spread islam

    • @muhammadfurqanalam
      @muhammadfurqanalam 10 месяцев назад +1

      ​@@maitahom9958show me the proof of your word and tell me does your religion taught you to behave like this?

    • @yousof8546
      @yousof8546 9 месяцев назад

      no hes just ignorant@@muhammadfurqanalam

  • @navneet5084
    @navneet5084 3 года назад +6

    After wasting 2 weeks I finally found a data structures playlist which I needed, LinkedIn Learning, Pluralsight everything failed but RUclips saved me. Thank you!

  • @amangayakwad4936
    @amangayakwad4936 2 года назад +3

    Finally here....After watching countless YT channels and visiting countless websites .... Finally this is the best explanation that I have ever found...❤️

  • @WonderingSoccer
    @WonderingSoccer 9 лет назад +9

    Hi Bro, you make the stack of Linked List very clear and straightforward, like it very much, and I enjoy your accent a lot! Keep up to it!

  • @jcpfmc
    @jcpfmc 5 лет назад +4

    Thank you so much for your tutorials, they are so clear and visual which helps me understand the concepts!

  • @namansingh5933
    @namansingh5933 Год назад +2

    this course is 9 years old still the best explanation topic wise and time saving as well as easy to understand.

  • @droidwala
    @droidwala 5 лет назад +5

    i just saw your stack implementation with Arrays, made my day!

  • @mohamadalidaify2832
    @mohamadalidaify2832 8 лет назад +1

    very clear and straight to the point. Thank you!

  • @akashdubey.consultant
    @akashdubey.consultant 8 лет назад +2

    wonderful explanation, btw on 07:36 you got to correct sizeof to be just sizeof(struct Node)) , please note it may get compiled but you won't be able to loop and print values, i just figured it when i ran into the problem

  • @bryand7958
    @bryand7958 6 лет назад

    Good job explaining the relevance of big O in the stack implementation on the linked list.

  • @EpisodeAnon27
    @EpisodeAnon27 9 лет назад +3

    Such clarity! This is the answer to our midterms in Data Struct last time. How I wished I watched this video earlier. TT ~TT

  • @tomasz-rozanski
    @tomasz-rozanski 7 лет назад +11

    You can also reuse IsEmpty() function inside the Pop() function, so instead of:
    if (top == NULL)
    you could write
    if (IsEmpty())

    • @khawarmehmood1835
      @khawarmehmood1835 6 лет назад

      nice solution

    • @VijayPatil-qj7xd
      @VijayPatil-qj7xd 6 лет назад

      In point of reusability and maintainable its good to have IsEmty call, but will degrade performance of code since it’s additional function call. Correct me if needed.

    • @glenncuevas8698
      @glenncuevas8698 5 лет назад

      Depends on the language and compiler, a function that simple/short is likely to be inline. If not, you're correct, we'll have the cost of the stack frame allocation, etc.

  • @Rajyalakshmi958
    @Rajyalakshmi958 2 месяца назад

    He explained 10years ago still no one can beat him

  • @ofir332
    @ofir332 8 лет назад

    Much more clear than my professor (and eng is not my first language)
    you sir, teach very well and know your job

  • @XieQiu
    @XieQiu 8 лет назад

    thank you for making this. you teach really well.

  • @spikeguy33
    @spikeguy33 9 лет назад +17

    Really helpful and well made video. Also, cool accent, reminds me of my Finnish friend :)

    • @kyssl
      @kyssl 6 лет назад +4

      UrbanTarzan Duh It's an Indian accent btw..

  • @sharvaripatil4298
    @sharvaripatil4298 4 года назад

    Thanks alot! Tried so long to understand this, i finally did today😄

  • @MaNgOsEr85
    @MaNgOsEr85 9 лет назад +18

    Here is my complete code with a switch function to use it properly :)
    Btw, thank you so much mycodeschool !
    #include
    #include
    struct Node {
    int data;
    struct Node* link;
    };
    struct Node* top = NULL;
    void Push(int x) {
    struct Node* temp = (struct Node*)malloc(sizeof( struct Node*));
    temp->data = x;
    temp->link = top;
    top = temp;
    }
    void Pop(){
    struct Node *temp;
    if(top == NULL) return;
    temp = top;
    top = top->link;
    free(temp);
    }
    int isEmpty(){
    if(top == NULL)
    return 1;
    else
    return 0;
    }
    int Top(){
    return top->data;
    }
    int Print(){
    struct Node* temp = top;
    if(isEmpty == 1){
    printf("Stack is Empty");
    return;
    }
    printf("Top| ");
    while(temp != NULL){
    printf("%d > ",temp->data);
    temp = temp->link;
    }
    printf("|End");
    }
    int main()
    {
    int choice,enter;
    printf("--------- Welcome to Stach ---------

    ");
    while(choice =! 0){
    printf("
    Please ENTER your choice
    ");
    printf("1. Push
    ");
    printf("2. Pop
    ");
    printf("3. What is Top ?
    ");
    printf("4. Is Stack Empty ?
    ");
    printf("5. Print Stack
    ");
    printf("Choice: ");
    scanf("%d",&choice);
    printf("
    ");
    switch(choice){
    case 1: printf("Enter the number for Push
    ");
    scanf("%d",&enter);
    Push(enter);
    break;
    case 2: Pop(enter);
    printf("It's popped!
    ");
    break;
    case 3: enter = Top();
    printf("Top is = %d
    ",enter);
    break;
    case 4: enter = isEmpty();
    if(enter == 1)
    printf("Yes, It's empty
    ");
    else
    printf("Nope, It contains elements
    ");
    break;
    case 5: printf("Stack Elements:
    ");
    Print();
    break;
    default : printf("Enter numbers between 1 and 5 !");
    break;
    }
    }
    system("PAUSE");
    return 0;
    }

    • @ebut0oy
      @ebut0oy 8 лет назад

      +Mangoser thanks man!

  • @gauravdotiyal7171
    @gauravdotiyal7171 Год назад

    the best explanation of topics in youtube🙌🙌

  • @SamAshkon1998
    @SamAshkon1998 2 года назад

    great explanation, quick and simple

  • @rahulpamnani6574
    @rahulpamnani6574 10 лет назад +1

    awesome explanation

  • @vatikaprasad
    @vatikaprasad 7 лет назад

    You are truly amazing! Thanks so much :)

  • @nischaysingh
    @nischaysingh 4 года назад

    thankyou so much man for making this video. It cleared all my doubts

  • @12121921
    @12121921 Год назад

    This explanation is very clear .Thank you

  • @mountolympus1098
    @mountolympus1098 7 лет назад

    if we are also storing address of tail as we are doing with head then we can do insertion/deletion at end too in o(1)

  • @soumyaranjanmishra3962
    @soumyaranjanmishra3962 8 лет назад +1

    U saved my sem sir! Thanks :-)

  • @aqsaibrahim5561
    @aqsaibrahim5561 5 лет назад

    Thank you so much sir.........this vedio helps me alot.............

  • @prathyushaappala6528
    @prathyushaappala6528 7 лет назад

    Thanks, sir. Very useful.

  • @faiselll
    @faiselll 10 лет назад +8

    Nice tutorial.
    why do use malloc(sizeof(struct node*))? i thought (struct node*) is a pointer of value 4 byte but our structure (struct node) is ( 4 + 4 ) = 8 bytes . isn't it the allocation is short?
    is it enough memory allocated by doing so? thanks

    • @ononto777
      @ononto777 10 лет назад +7

      It's gotta be malloc(struct Node) without the star. On one of the previous videos while I was testing doubly linked list, my code was failing due to this. I scratched my head for over an hour and found I was malloc'ing size of pointer (i.e. address) rather than the Node itself. This is a classic example where debugging won't give you what you are doing wrong since it's a logical mistake. :)

  • @ShivamRathirockzzz
    @ShivamRathirockzzz 8 лет назад

    You teach well.thumbs up.

  • @vijayendrasdm
    @vijayendrasdm 10 лет назад

    Really nice videos. very helpful

  • @thenmozhikaliappan2546
    @thenmozhikaliappan2546 8 лет назад

    nice video sir thank u so much for the clear explanation

  • @prinxegupta
    @prinxegupta 8 лет назад

    really great work. thanks a lot

  • @thebestfella
    @thebestfella 9 лет назад +1

    very good explanantion.

  • @bhumipatel9617
    @bhumipatel9617 9 лет назад +22

    i hope u will make this type of video again n again.

  • @izmiruruci5092
    @izmiruruci5092 4 года назад

    Very helpful video. Thank you very much

  • @RealMcDudu
    @RealMcDudu 8 лет назад

    If you make a doubly-linked-list, the cost of inserting an element at the end would be also O(1), since you could just call the global tail variable.
    i.e.: head -> n1 n2 n3.... nn

    • @simplymade7802
      @simplymade7802 6 лет назад

      D. Refaeli we can also use singlly link list and the cost of operation will be o(1) only by creating two new pointer variables which will point to last two nodes..
      Although this will require space for two more pointer variables but this will be more efficient then your solution...

  • @patrickren7395
    @patrickren7395 6 лет назад

    to set the first node instead of the last one as top is totally brilliant

  • @adityaojha2701
    @adityaojha2701 4 года назад +1

    Sir, how can you be so good.

  • @SatyendraJaiswalsattu
    @SatyendraJaiswalsattu 9 лет назад

    good explanation..

  • @evelynhernandez370
    @evelynhernandez370 2 года назад

    i love your explanations :')

  • @ericle1236
    @ericle1236 3 года назад

    Great video thank you sir !

  • @andreygoncalves2752
    @andreygoncalves2752 7 лет назад

    Hello, I need help.
    How do I expand the stack capacity?
    Eg every time the stack has overflow expand another 8 capacity positions.
    Excuse me for bad english.

  • @LtMewS
    @LtMewS 3 года назад +4

    From this video I conclude that if I want speed and less space I should better stick with array stacks

  • @abhigupta9234
    @abhigupta9234 5 лет назад

    Indian can tell you are also one of them
    keep the great work up brrro

  • @ayushpareek265
    @ayushpareek265 9 лет назад +36

    Implementation in C++
    #include
    using namespace std;
    class Stack
    {
    private:
    struct Node
    {
    int data;
    Node* link;
    };
    struct Node* top;
    public:
    Stack()
    {
    top=NULL;
    }
    void Push(int x)
    {
    Node* NewNode = new Node();
    NewNode->data = x;
    NewNode ->link = top;
    top = NewNode;
    }
    void Pop()
    {
    Node* temp= top;
    if (top==NULL)
    return;
    top=top->link;
    delete (temp);
    }
    void Print()
    {
    Node* temp = top;
    while(temp!=NULL)
    {
    cout

    • @shreyasdeo3801
      @shreyasdeo3801 6 лет назад

      you prefer writing the class definition inside the class or outside the class.....in our school it is preferred to write the function definition using scope resolution operator

    • @prinzuchoudhury6920
      @prinzuchoudhury6920 4 года назад

      kya baaattt

  • @takanobu3012
    @takanobu3012 7 лет назад

    How would I implement a stack using a LinkedList object? Would I need to create an instance of the LinkedList class in my stack.h and call the constructor for the LinkedList object in the stack's constructor (if there is only a default constructor for LinkedList)?

  • @deepaksinghverma8751
    @deepaksinghverma8751 6 лет назад

    Hello sir why r not uploading the vedios on the channel? u r teaching style is awesome.where r u?

  • @bhumipatel9617
    @bhumipatel9617 9 лет назад +2

    it's really very helpful & i nice try. thank u so much.:-)

  • @mithunsuriyaak6762
    @mithunsuriyaak6762 5 лет назад

    Thank you so much

  • @animeshnayan1
    @animeshnayan1 10 лет назад

    Your code will work, but it will cause memory leak.. you are just moving your top pointer to next node. The previous head node is still occupying space in memory. because it is dynamic memory which is not freed automatically. In pointers series, check the lesson on dynamic memory allocation and memory leak to understand this concept.

  • @shefalimhadadalkar7570
    @shefalimhadadalkar7570 10 лет назад +9

    Exactly! the malloc shud be allocating sizeof(struct node) right? o_O

  • @allThingsYellow
    @allThingsYellow 8 лет назад +6

    Shouldn't the dynamic memory allocation statement be
    struct Node *temp=(struct Node*)malloc(sizeof(struct Node)) ? While using the sizeof operator here our intention is to get the size of "Node" and not the "pointer to Node".

    • @gaurav7894
      @gaurav7894 8 лет назад

      yes

    • @allThingsYellow
      @allThingsYellow 7 лет назад +2

      Naval saini Hey mister,I was right in pointing out the error.You,however,are just an ignorant guy who likes pulling people down.And just so you know,I got job offers from two IT firms a few weeks ago as a full-time programmer.Cheers :D

    • @allThingsYellow
      @allThingsYellow 7 лет назад

      Naval saini Good sense of grammar dude :p Infosys and Cognizant.I am sure they'll be happy to see that I make an effort to learn new things and point out errors in order to help other people :)

    • @allThingsYellow
      @allThingsYellow 7 лет назад

      :)

    • @NimW
      @NimW 7 лет назад +1

      I was about to correct you for not putting the asterisks, but then realized that you used them and youtube used them to make part of the comment bold.

  • @belkhiriasadok7245
    @belkhiriasadok7245 5 лет назад

    why did you declare the variable temp using the size of the pointer instead of the size of struct nodes itself ???

  • @sivasarath1063
    @sivasarath1063 8 лет назад

    at 9:53 when we free temp,we are just freeing only the reference to the node but not node temp is pointed to? and isn't this way memory of machine wasted

  • @nareshkollipora5323
    @nareshkollipora5323 5 лет назад

    good explanation

  • @7huannp
    @7huannp 6 лет назад

    I don't understand about constant time or O(n) , O(1),.... Can you explain for me?

  • @noneofurbusns3139
    @noneofurbusns3139 7 лет назад

    What if we write instead:
    top= temp->link
    temp=top
    free(temp)
    ?

  • @Rudyaification
    @Rudyaification 6 лет назад

    Do we need to set a temp = NULL; after free(temp); ?

  • @TheTahlia1988
    @TheTahlia1988 8 лет назад

    Nicely done. :)

  • @danimanabat5791
    @danimanabat5791 3 года назад

    Thanks a lot!

  • @tranminhhieu3061
    @tranminhhieu3061 5 лет назад +1

    I'm VietNamese,Thank you so much :)

  • @aloksonkershastri681
    @aloksonkershastri681 7 лет назад

    thanks

  • @ajaymaurya5794
    @ajaymaurya5794 5 лет назад

    In pop function, in line 4 shouldnt it be
    Top= temp->link
    rather than
    Top=top->link ??

    • @onelastgiggity
      @onelastgiggity 5 лет назад

      Both are correct...Both top and temp are pointing to 1st node

  • @Sshubham489
    @Sshubham489 7 лет назад

    which compiler are you using?

  • @pujabhattacharya2857
    @pujabhattacharya2857 8 лет назад

    explanation, btw on 09:58 you got to correct top=250 and not 100, otherwise in temp the 100 value will get copied and not 250

    • @gurdyalsingh8228
      @gurdyalsingh8228 7 лет назад

      Puja Bhattacharya top=250,got delete by calling free function.

  • @nathanjames4654
    @nathanjames4654 2 года назад

    how would this look if you used string instead of int

  • @mohitpandey4655
    @mohitpandey4655 Год назад

    We miss you sir

  • @Sujoyr
    @Sujoyr 5 лет назад

    10:56 Thanks for teaching..

  • @somyasharma7693
    @somyasharma7693 6 лет назад

    Does adress 0 in memory point to NULL* memory location??

  • @RishabhSinghal12
    @RishabhSinghal12 3 года назад

    Come back please 🙏

  • @adityasharmacs1-149
    @adityasharmacs1-149 4 года назад

    be hepfull please make some vedios on competitive coding

  • @sai9692
    @sai9692 7 лет назад

    Is there any good reason why we do explicit type casting here.
    [i.e: struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));]
    My code works without the type cast.
    [ i.e: struct Node* temp = malloc(sizeof(struct Node*); ]

    • @kyssl
      @kyssl 6 лет назад

      Sairam D malloc returns void pointers generally

  • @rashmivb2340
    @rashmivb2340 4 года назад

    Cant thank you enough sir :)

  • @siddharthmagadum16
    @siddharthmagadum16 4 года назад

    But what is the use of temp in pop function?

  • @healthcharger971
    @healthcharger971 3 года назад

    *rest in peace sir :(*

  • @prelicantdeezyjunior9
    @prelicantdeezyjunior9 5 лет назад

    how do you implement the print function?

  • @keshavkapoor1515
    @keshavkapoor1515 7 лет назад

    kya hum data structures ke exam mai stack explain krte tym uske saath programs lihk sakte h ya sirf algorithm hi lihkne h.

  • @Geek74
    @Geek74 5 лет назад

    have you code for this concept in c ?

  • @hossainahmed7861
    @hossainahmed7861 7 лет назад

    thx

  • @abdelaziz.ashraf
    @abdelaziz.ashraf 2 года назад

    جامد ⚡

  • @sauravbhondve8307
    @sauravbhondve8307 6 лет назад +2

    Implementation in C++ :
    #include
    using namespace std;

    // Creating a NODE Structure
    struct node
    {
    int data;
    struct node *next;
    };
    // Creating a class STACK
    class stack
    {
    struct node *top;
    public:
    stack() // constructure
    {
    top=NULL;
    }
    void push(); // to insert an element
    void pop(); // to delete an element
    void show(); // to show the stack
    };
    // PUSH Operation
    void stack::push()
    {
    int value;
    struct node *ptr;
    coutnext=NULL;
    if(top!=NULL)
    ptr->next=top;
    top=ptr;
    cout

  • @fernandoyogaa4780
    @fernandoyogaa4780 6 лет назад

    how to use stack using circular linked list ?

  • @vighneshs5069
    @vighneshs5069 7 лет назад +47

    arre mere college mei aaja padane

    • @khawarmehmood1835
      @khawarmehmood1835 6 лет назад +7

      he can't visit your college dear, this person was died in a road accident in 2015 :'(

    • @VS-ey2lf
      @VS-ey2lf 6 лет назад

      Khawar Mehmood Are you serious? oh my god how do you know?

    • @VS-ey2lf
      @VS-ey2lf 6 лет назад +51

      Bro you said he died in 2015 last video he uploaded was oct 24, 2016. I did a little bit research and found out the person who died was humblefool(harsha), india greatest red coder, He was friend of mycodeschool(this guy name is animesh) and also a co-founder of mycodeschool. Bro half knowledge is dangerous!! Please just don't shoot arrows in the dark!!

    • @paritoshsingh588
      @paritoshsingh588 5 лет назад +1

      @@VS-ey2lf yes Harsha was his senior in IIIT Allahabad

    • @shivamb.3973
      @shivamb.3973 4 года назад

      @Atharva Wankhede I think he was just making a joke.

  • @abdullahbabor4876
    @abdullahbabor4876 4 года назад +1

    here is the implementation on c plus plus via OOPs!!
    #include
    using namespace std;
    class node
    {
    public:
    int data;
    node* next;
    };
    node* top = NULL;
    class linkedList
    {
    private:
    public:
    void push(int x)
    {
    node* newNode = new node();
    newNode->data = x;
    newNode->next = top;
    top = newNode;
    }
    void pop()
    {
    if (top == NULL)
    {
    return;
    }
    else
    {
    node* temp;
    temp = top;
    top = temp->next;
    delete temp;
    }
    }
    void print()
    {
    node* temp = top;
    if (top == NULL)
    {
    return;
    }
    else
    {
    while (temp != NULL)
    {
    cout data next;
    }
    }
    }
    int topData()
    {
    if (top == NULL)
    {
    return -1;
    }
    else
    {
    return top->data;
    }
    }
    };
    int main()
    {
    linkedList ll1;
    ll1.push(10);
    ll1.push(10);
    ll1.push(10);
    ll1.print();
    ll1.pop();
    cout

  • @thestarinthesky_
    @thestarinthesky_ 4 года назад +3

    C++ (Object Oriented implementation)
    class Node {
    public:
    int data;
    Node* next;
    };
    class Stack {
    public:
    Stack(int N)
    {
    this->head = NULL;
    size = N;
    }
    Stack()
    {
    this->head = NULL;
    }
    Node* head;
    int size;

    void pop()
    {
    if (!isEmpty())
    {
    Node* temp = head;
    head = head->next;
    delete(temp);
    size++;
    }
    else
    {
    cout data;
    else
    {
    cout data = data;
    temp->next = head;
    head = temp;
    size--;
    cout

  • @ripusudanchourey9063
    @ripusudanchourey9063 2 года назад

    NOICE SIR...

  • @amhanafy
    @amhanafy 6 лет назад

    why at push 09:28 you malloced memory and at pus you just created a new node

  • @Yasjunmine
    @Yasjunmine 8 лет назад

    thank you veeeeeeeeeeeeeeery much

  • @busybanny
    @busybanny 7 лет назад

    so stack in linkedlist doesn't follow LIFO rule.

  • @janhavibhosle683
    @janhavibhosle683 4 года назад

    Is
    struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
    the same as
    struct Node* temp=(struct Node*)malloc(sizeof(struct Node*));
    ... the code works same for both ways

  • @mariosactron7617
    @mariosactron7617 7 лет назад

    When popping from the stack why do you need to declare a temp variable. Can't you just set top = top->link; instead of top = temp->link; ? I know that is what you showed in your video but I feel like it's a mistake because you declared temp, but what's the point of declaring a temp just to free it. I feel like it's redundant.

    • @sarthaksuper284
      @sarthaksuper284 7 лет назад +1

      you can`t mario as if you use top=top->link then the value to which top was pointing earlier will be lost so it can`t be freed after that. so first a temp variable must be declared so that after top=top->link the memory can be freed by using free(temp) as temp will still be pointing at the value to be popped

  • @muhjor
    @muhjor 10 лет назад

    thank you

  • @sejaljain9197
    @sejaljain9197 4 года назад

    Sir why did you stopped making videos????

  • @lifesmysteriesofficial
    @lifesmysteriesofficial 5 лет назад

    07:14

  • @vaishnavibhardwaj8033
    @vaishnavibhardwaj8033 4 года назад

    2 corrections (I guess)-
    1 malloc(sizeof(struct Node)) instead of malloc(sizeof(struct Node*)) PUSH function
    2 top = temp -> link instead of top = top -> link POP function

    • @toka8909
      @toka8909 4 года назад +1

      basically top and temp both are pointing to the same address field i.e. 250 in the video. So, top = top->link is same as top = temp->link in that particular scenario. And for that number 1 point, yes you are correct. It was a typo.

  • @ruzumaki6061
    @ruzumaki6061 5 лет назад

    Waiting for DS and Algo in Java/Python..

    • @prathamanand1037
      @prathamanand1037 4 года назад

      He is no more . He died in an accident. We lost this beautiful soul. U can read about this on Google as well