Reverse a string or linked list using stack.

Поделиться
HTML-код
  • Опубликовано: 14 окт 2013
  • See complete series on data structures here:
    • Data structures
    In this lesson, we have described how we can reverse a string or linked list using stack. Reversal is a simple application of stack.
    To know about implicit stack, see this video:
    • Pointers and dynamic m...
    Reversal of linked list using recursion:
    • Reverse a linked list ...
    For practice problems and more, visit: www.mycodeschool.com
    Like us on Facebook: / mycodeschool
    Follow us on twitter: / mycodeschool

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

  • @yokedici90
    @yokedici90 9 лет назад +389

    "...a warrior should not just possess a weapon, but he must also know when and how to use it." well said bro

    • @SonuSonu-tk5pk
      @SonuSonu-tk5pk 7 лет назад +15

      bhai bhai bhai bhai bhai bhai

    • @IndianDesiTennis
      @IndianDesiTennis 6 лет назад +5

      did you create this meme "bhai bhai bhai bhai" :o

    • @vikrant4666
      @vikrant4666 6 лет назад +5

      sonu sonu you are the creater of arey bhai bhai meme , salute respeccc

    • @rishabhvarshney7522
      @rishabhvarshney7522 3 года назад +1

      Vinod

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

      @@rishabhvarshney7522 O MY Binod!!!! Binod is also here............im fainting

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

    Hi Shadman
    Data structures is our priority. We publish 2 to 3 videos in a week. So the complete series will take some time.

  • @suyash.01
    @suyash.01 4 года назад +15

    so grateful that this person made such a nice library of SUPER HELPFUL content which continues to help so many even after he's no longer here.

  • @vigneshwarm
    @vigneshwarm 5 лет назад +58

    Here is the complete code for 2nd program:
    #include
    #include
    using namespace std;
    struct Node
    {
    int value;
    Node *next;
    };
    void reverse(Node** head){
    Node* temp = *head;
    if(*head == NULL) return;
    stack s;
    // push to stack
    while (temp!=NULL)
    {
    s.push(temp);
    temp = temp->next;
    }
    temp = s.top();
    *head = temp;
    s.pop();
    //reverse
    while (!s.empty())
    {
    temp->next = s.top();
    s.pop();
    temp = temp->next;
    }
    temp->next = NULL;
    }
    void push(Node **head, int value)
    {
    Node *temphead = *head;
    Node *temp = new Node();
    temp->value = value;
    temp->next = NULL;
    if (*head == NULL)
    {
    *head = temp;
    return;
    }
    while (temphead->next != NULL)
    {
    temphead = temphead->next;
    }
    temphead->next = temp;
    }
    // To print the linked list
    void print(Node *head)
    {
    Node *temp = head;
    while (temp != NULL)
    {
    cout value next;
    }
    }
    int main(){
    Node *head = NULL;
    push(&head, 1);
    push(&head, 2);
    push(&head, 3);
    print(head);
    reverse(&head);
    cout

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

      In print function can't we declare a stack and pick all the elements form linked list into stack & print them

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

      @@ravenx You could dereference the address of each pointer in the stack and print the data field of it

  • @nirajabcd
    @nirajabcd 10 лет назад +32

    I really learned a lot from your videos. Thank you so much for your time and effort.

  • @kalyanroy6687
    @kalyanroy6687 8 лет назад +3

    The power of simplicity, which each word of your's enforces is just divine. The way you carry ADT , Implementation & Analysis of Running Time on a nutshell,is simply the best thing that could ever be done in any Planet,on and beyond eternity.

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

    Thanks for all your efforts. So grateful that these tutorial videos are still here while you are no longer with us. You will always be remembered Harsha Suryanarayana!

  • @2222stunner
    @2222stunner 6 лет назад

    Thanks for all the effort you people have put in to make these videos. These are the best. ☺

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

    Awesome video,you make it very easy to understand, need more,linked lists,with previous and next, thank you very much.

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

    awesome teaching...cleared my concepts

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

    Love you man! Next level teaching.

  • @emrekorkmaz3359
    @emrekorkmaz3359 5 лет назад +3

    I wish you could be my data structure teacher :) thanks for the videos , it helped me a lot :)

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

    thank you so much sir... thx for motivating us in DSA !! wish u good luck :)

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

    very precise and helpful video. Thank you very much

  • @danhle7999
    @danhle7999 3 года назад +8

    just in case you have any error for implementing the 1st example, check out my code in c++ :
    #include
    #include
    #include
    using namespace std;
    void Reverse(char *C, int n)
    {
    stack S;
    for (int i = 0; i < n; i++)
    {
    S.push(C[i]);
    }
    for (int i = 0; i < n; i++)
    {
    C[i] = S.top();
    S.pop();
    }
    }
    int main()
    {
    char C[51];
    cout > C;
    Reverse(C, strlen(C));
    cout

  • @PavanKumar-mr9td
    @PavanKumar-mr9td 3 года назад

    what an awesome explanation bro....

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

    good job ....
    you can not only print the reversed array one after the other , you can actually store in in an all new char array by changing function void to char pop() and return elements in it.
    and while adding elements to array , an extra space for null char mus b left

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

    Urs tutorial is too good nd pfcorse ...of ur voice. .it's easy to understand everything..post more videos..on c

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

    when will you upload nore videos on data structures.best lessons ever guys.thank you so much.

  • @ayusharyan161
    @ayusharyan161 6 лет назад +1

    sir there is wrong link you provided in description for extra resources..!!! please provide me link for extra resources, i am very thankfull to u..

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

    best explanation ever

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

    Hi Shadman,
    Data structures is our priority. We publish 2-3 videos in a week. So, the complete series will take some time.

    • @HARIHaran-ks7wp
      @HARIHaran-ks7wp 3 года назад

      its been a while...

    • @santhanaselvi6
      @santhanaselvi6 2 года назад +1

      @@HARIHaran-ks7wp Complete series is finished go and check their playlist

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

    Precious Gift to the world , thanks buddy , CS DOJO approved , BULLDOG MINDSET approved etc etc .

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

    In the loop for pop function you could have done it in one line like
    c[i]=S.pop();
    right??

  • @PratikShende91
    @PratikShende91 6 лет назад +5

    the teacher is not between us any more ...but his teaching would always be here for years....thanks for making all those videos ..may he is happy wherever he is...

    • @vailaruthvik
      @vailaruthvik 5 лет назад +2

      blog.mycodeschool.com/ he's not dead his teammate died in a hit and run case in bangalore in 2014!!

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

    I am really confused how does stack can actually store the addresses ? Can vectors be used in the same way to store the addresses i.e can we use vector of pointers? if yes can vector be used the same way as stack to reverse the linked list? I am just not able to see the importance of use of stacks, Please clarify!!

  • @cybrarybr1558
    @cybrarybr1558 5 лет назад +2

    As always , like before watching :)

  • @redlamaravind104
    @redlamaravind104 3 года назад +1

    could you share the code ones I can't find anywhere..

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

    as said at last where is the code link in the description : (

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

    Ongoing charity love this playlist

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

    now i want a double like button on yt bc this video explains shit great

  • @Stacy8209
    @Stacy8209 7 лет назад +11

    8:33 closed caption says "space complexity O(n)" while you're correctly saying O(1),
    just letting you know

    • @Stacy8209
      @Stacy8209 7 лет назад +3

      same thing at 11:05

  • @user-qy2yu4ky6c
    @user-qy2yu4ky6c 4 года назад

    How can I copy the stack’s elements to another new stack?

  • @aswinvishnua8330
    @aswinvishnua8330 3 года назад +1

    for using strlen(c) ,include headerfile:
    #include

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

    You are just amazing! You are truly, honestly, clearly deserved to work at Google!

  • @karthickraja6419
    @karthickraja6419 6 лет назад +1

    it throws me a error of "Fatal error directive " how it can be cleared

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

    the link to the practice problems is not working says "502 Bad Gateway"

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

    is there a code to reverse link list using stack in C? I tried but it doesn't work

  • @jyotichhabra25
    @jyotichhabra25 6 лет назад +1

    Can u please upload reverse function code using C

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

    You should verify the input first. Before dereferencing C, you should check if C is not
    a null pointer. cool video outside of that.

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

    does anyone have the code for Java, please i am very stuck on this idk what to do anymore its so frustrating.

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

    nice .. thanks

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

    Where do I can search source code in tutorial? Please I'm implementing stack use linked list that i implement on your previous lesson.

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

      yes as mentioned it is not available in description

  • @burakbosluk1297
    @burakbosluk1297 7 лет назад +4

    code in c :using linked list
    #include
    #include
    struct n{
    char word;
    struct n *next;
    };
    typedef struct n node; //
    void reverse_string(char *word);
    void print();
    void push(char);
    node* root;
    int main()
    {
    root=NULL; //
    char *word="bugun seminer vardi!";
    reverse_string(word);
    print();
    }
    void reverse_string(char *word)
    {
    int i=0;
    for(i=0;(word[i])!='\0';i++)
    {
    printf("%c",word[i]);
    push(word[i]);
    }
    printf("
    ");
    }
    void push(char letter)
    {
    node *temp=(node*)malloc(sizeof(node));
    temp->word=letter;
    if(root==NULL)
    {
    root=temp;
    root->next=NULL;
    return;
    }
    temp->next=root;
    root=temp;
    }
    void print()
    {
    node *iter=root;
    int i=0;
    while(iter!=NULL)
    {
    printf("%c",iter->word);
    iter=iter->next;
    }
    printf("
    ");
    }

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

      thanks man but why we have to use malloc every time we want to push (node temp=(node)malloc(sizeof(node));)??? isnot it waste of memory?

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

      @amateur_beginner, malloc function allocates memory during runtime and thus saves the wastage of memory. Push means Insert and malloc will allocate (sizeof(node)) only for each value you want to push.

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

    Can anyone give a link for STL function Signature and how to use them

  • @GauravKumar-dw2ml
    @GauravKumar-dw2ml 4 года назад

    In linked list pop operation why you call pop outside while...?

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

    I know how 60 dislike happened....after I watch all this episodes I just feel that’s awesome!but I click dislike by mistake and I didn’t notice it!Right now I correct that mistake and I want to say thank you for offer such amazing video!!!

  • @kartikey_Kq
    @kartikey_Kq 5 лет назад +8

    c variant of this code
    #include
    #include
    #include
    #define max 20
    char a[max];
    int top = -1;
    void push(char z)
    {
    top++;
    a[top] = z;
    }
    void firstelenow()
    {
    printf("%c",a[top]);
    }
    void pop()
    {
    top = top-1;
    printf("%c",a[top]);
    }
    int main()
    {
    int n,i;
    char c[20];
    printf("
    enter the string
    ");
    gets(c);
    n = strlen(c);
    for(i= 0 ; i

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

    Frnds if you are doing code in C Plz include
    I have tried approx 2 days for this question so plz note this
    #include for string length
    #include

  • @theilluminati77
    @theilluminati77 8 лет назад +36

    how can you switch to C++,I am following C from the start?

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

      theilluminati77 I have the same problem

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

      go through book :- balaguruswamy for c++

    • @beastyt2510
      @beastyt2510 5 лет назад +6

      @@forbiddenumbrella I think he had learnt c++ in last two years😂

    • @matplotlib4670
      @matplotlib4670 5 лет назад +2

      Both are almost same

    • @BeastMaster-gc5zg
      @BeastMaster-gc5zg 4 года назад

      C++ is just the Incremented version of C. Both possess some similarities

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

    have u made any videos related to time and space complexities separately ?

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

      ruclips.net/video/D6xkbGLQesk/видео.html

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

    Sir please give the source code of the reversal of stack usong linked list

  • @IndianDesiTennis
    @IndianDesiTennis 6 лет назад +3

    this is my code with first inserting values in the linked list and then printing them to check if they get reverse or not
    #include
    #include
    using namespace std;
    struct Node {
    int data;
    Node* next;
    };
    Node* head;
    void reverse(){
    if(head==NULL){
    return;
    }
    else {
    stack s;
    Node* temp = head;
    while(temp!=NULL){
    s.push(temp);
    temp = temp->next;
    }
    Node* temp2 = s.top();
    head = temp2;
    s.pop();
    while(!s.empty()){
    temp2->next = s.top();
    s.pop();
    temp2=temp2->next;
    }
    temp2->next = NULL;
    }
    }
    Node* insert(Node* head ,int x){
    Node* temp = new Node();
    temp->data = x;
    temp->next = NULL;
    if(head==NULL){
    head = temp;
    }
    else {
    Node* temp2 = head;
    while(temp2->next!=NULL){
    temp2=temp2->next;
    }
    temp2->next = temp;
    }
    return head;
    }
    void print(){
    Node * temp = head;
    while(temp!=NULL){
    cout

  • @pryakks
    @pryakks 9 лет назад +21

    Code in C:
    #include
    #include
    #define MAX_SIZE 101
    int A[MAX_SIZE]; //global var
    int top=-1;//represent empty stack
    void Push(int x){
    if(top == MAX_SIZE-1){
    printf("Error: stack overflow
    ");
    return;
    }
    A[++top]= x;
    printf("%c",x);
    }
    void Pop(){
    if(top == -1){
    printf("Error: stack overflow
    ");
    return;
    }
    printf("%c",A[top--]);
    }
    int main(){
    char str[] ="mycodeschool";
    int len = strlen(str),i;
    printf("Original String : ");
    for(i=0;i

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

      Priya Kokas
      Good Job.....!!!

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

      Great!

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

      Dream On tell me one thing,how can you fetch elements of stack that was pushed very first in stack? Its last in first out.

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

      if(top == -1){
      printf("Error: stack is empty
      ");
      return;
      }
      mistake fixed :)

    • @AmrendraKumar-ko8yf
      @AmrendraKumar-ko8yf 7 лет назад +1

      correct code in c to reverse string using stack:
      #include
      #include
      #include
      void push(char a);
      void pop();
      void print();
      struct node{
      char data;
      struct node *next;
      };
      struct node *top=NULL;
      char a[10];
      void push(char a)
      {
      struct node *temp=(node *)malloc(sizeof(struct node));
      temp->data=a;
      temp->next=NULL;
      if(top==NULL)
      {
      top=temp;
      return;
      }
      temp->next=top;
      top=temp;
      }
      void pop()
      {
      printf("Reverse string:");
      while(top!=NULL)
      {
      struct node *temp=top;
      static int i=0;
      a[i]=top->data;
      i++;
      top=top->next;
      free(temp);
      }

      }
      void print()
      {
      struct node *temp=top;
      printf("string on stack:");
      while(temp!=NULL)
      {
      printf("%c",temp->data);
      temp=temp->next;
      }
      printf("
      ");
      }
      int main()
      {
      int i;
      printf("Enter string: ");
      gets(a);
      for(i=0;a[i]!='\0';i++)
      {
      push(a[i]);
      }
      print();
      pop();
      puts(a);
      }

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

    u are ruling on ytube when no any good content where uploaded . Omg this vid is 8 yrs old...can't really think such old and such informative content.

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

    where is sourecode you said in video

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

    so ,both the Time and space when using stack explictily are O(n)? But what the advantage of using stack explictily ?

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

    In previous classes, you said that address of nth member in array will be address of first member + 4*(n-1) but now in this video you said it is directly first member's address + 4 at around 10:38sec. Could you please clarify this

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

      pointer arithmetic in C for arrays are based on the type of that array. I haven't watched the previous video you are referring to but I can make an educated guess and say that he was talking about an integer array. Int's in C are typically 4 bytes long, which is why it is ..+4*(n-1). In this video, he is talking about char, which are 1 byte in size. Thus, 400, 401, ... 404. Hope that helps.

  • @ManishKumar-fb2yx
    @ManishKumar-fb2yx 6 лет назад +2

    Code for reversal of string using array
    // Reversal of string using stack
    #include
    #include
    #define MAX_SIZE 100
    char A[MAX_SIZE];
    int top = -1;
    void push(char c){
    A[++top] = c;
    }
    char Top(){
    if(top==-1)
    return;
    else
    return A[top];
    }
    void pop(){
    if(top==-1)
    printf("Stack Empty:
    ");
    else{
    top = top-1;
    }
    }
    void reverse(char *c ,int n){
    int i;
    for(i=0;i

  • @evagan2448
    @evagan2448 8 лет назад +2

    do you have a java programme one?

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

    At 1:09 in C a string must be terminated with a null character.... My question is do we need to have a null character even in c++ for termination??

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

    I have looked everywhere for the source code. I cannot find it. Can you tell me where it is?

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

      This is not his source code, but feel free to see mine : repl.it/EbXh/7

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

      It is Present in His Grave...Go and Get also share with Us!!

    • @VijayKumar-kk2yi
      @VijayKumar-kk2yi 4 года назад +1

      @@muhammadbilalmalik7292 he is not head ...his name is animesh ..and his friend harsha died in a car accident ...animesh is in san francisco working for google and has stopped making videos

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

    Why don't you use cout for output in your c++ programs??
    It makes me a little confused

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

      He's using C in this video.

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

      @@FatmaYousuf lol

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

      Sai Nivedh Vallala yeah totally funny

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

    Space complexity?

  • @ashwinaman12977
    @ashwinaman12977 8 лет назад +24

    Upload Dynamic programming videos

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

      do you know where can i find dynamic programming videos?

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

      Its on his channel
      watch the pointers and dynamic allocation series

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

      He's dead

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

    what is happening to the null character?

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

      Naveena Null Character is Attacked by Corona Virus ,,,Pray for it's health!!

  • @shivampandey-vv4wy
    @shivampandey-vv4wy 6 лет назад +1

    Why is it not working ?
    #include
    #include
    using namespace std;
    struct node
    {
    int data;
    node *next;
    };
    node *head;
    void reversal()
    {

    if(head==NULL)
    {
    return;
    }
    stack < struct *node > s;
    node *temp=head;
    while(temp!=NULL)
    {
    s.push(temp);
    temp=temp->next;
    }
    temp=s.top();
    head=temp;
    s.pop();
    while(!s.empty())
    {
    temp->next=s.top();
    s.pop();
    temp=temp->next;
    }
    temp->next=NULL;
    }
    void insert(int x, int n)
    {
    node *temp=new node();
    temp->data=x;
    temp->next=NULL;
    if(n==1)
    {
    temp->next=head;
    head=temp;
    return;
    }
    node *temp1=new node();
    temp1=head;
    for(int i=0;inext;
    }
    temp->next=temp1->next;
    temp1->next=temp;
    }
    void print()
    {
    node *temp=head;
    while(temp!=NULL)
    {
    coutnext;
    }
    }
    int main()
    {
    head=NULL;
    insert(2,1);
    insert(3,2);
    insert(4,3);
    insert(5,1);
    insert(8,5);
    print();
    reversal();
    print();
    }

  • @hridaynathp
    @hridaynathp 3 года назад +1

    0:11 🔥

  • @mayanovarini5757
    @mayanovarini5757 7 лет назад +4

    C++ Code for Reverse Function :
    void reverse() { // Time O(N), Space O(1)
    if (top == NULL) return;
    std::stack S; //create a stack called S
    Node* temp = top;
    while (temp != NULL) {
    S.push(temp); //LIFO
    temp = temp->link;
    }
    temp = S.top(); //now temp is pointing at the last inserted element on the stack or the TOP
    top = temp; // head points at the same element
    S.pop(); //clear up the element from top of the stack
    while(!S.empty()) {
    temp->link = S.top(); //point temp to the previous element in the linked list, or the element at the top
    S.pop(); // clear up that element fromt the stack
    temp = temp->link;
    }
    temp->link = NULL;
    }

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

      // C++ Full code Push at the head/tail, Pop at the head/tail, Reverse, Print
      // LInk : repl.it/EbXh/7
      #include
      #include
      #include
      #define MAX_SIZE 50
      int A[MAX_SIZE];
      struct Node {
      int data;
      struct Node* link;
      };
      struct Node* top = NULL;
      void push_head(int x) {
      struct Node* temp = new Node();
      temp->data = x;
      temp->link = top;
      top = temp;
      }
      void push_tail(int x) {
      struct Node* temp = top;
      while (temp->link != NULL) {
      temp = temp->link;
      }
      temp->link = new Node; // returns a pointer to the new node
      temp->link->data = x;
      temp->link->link = NULL;
      }
      void pop_head() {
      struct Node* temp; //no node necessary
      if (top == NULL) return;
      temp = top;
      top = top->link;
      free(temp);
      }
      void pop_tail() {
      struct Node* temp = top;
      struct Node* prev = NULL;
      while (temp->link != NULL) {
      prev = temp;
      temp = temp->link;
      }
      free(temp);
      prev->link = NULL;
      }
      void print() {
      Node* temp = top;
      while (temp != NULL) {
      printf("%d ", temp -> data);
      temp = temp -> link;
      }
      printf("
      ");
      }
      void reverse() { // Time O(N), Space O(1)
      if (top == NULL) return;
      std::stack S; //create a stack called S
      Node* temp = top;
      while (temp != NULL) {
      S.push(temp); //LIFO
      temp = temp->link;
      }
      temp = S.top(); //now temp is pointing at the last inserted element on the stack or the TOP
      top = temp; // head points at the same element
      S.pop(); //clear up the element from top of the stack
      while(!S.empty()) {
      temp->link = S.top(); //point temp to the previous element in the linked list, or the element at the top
      S.pop(); // clear up that element fromt the stack
      temp = temp->link;
      }
      temp->link = NULL;
      }
      int main() {
      push_head(2);
      print(); // output 2
      push_head(6);
      print(); // output 6 2
      push_head(9);
      print(); // output 9 6 2
      pop_head();
      print(); // output 6 2
      push_tail(8);
      print(); // output 6 2 8
      pop_head();
      print(); // output 2 8
      pop_tail();
      push_tail(3);
      print(); // output 2 3
      push_tail(5);
      print(); // output 2 3 5
      pop_tail();
      print(); // output 2 3
      push_head(6);
      push_tail(19);
      push_tail(55);
      print(); // output 6 2 3 19 55
      reverse();
      print(); // output 55 19 3 2 6
      }

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

      What is the use of the array A in your code?

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

    can someone share the link list code in c?

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

      here u go :)
      -->
      #include
      #include
      #include
      #define max 20
      char a[max];
      int top = -1;
      void push(char z)
      {
      top++;
      a[top] = z;
      }
      void firstelenow()
      {
      printf("%c",a[top]);
      }
      void pop()
      {
      top = top-1;
      printf("%c",a[top]);
      }
      int main()
      {
      int n,i;
      char c[20];
      printf("
      enter the string
      ");
      gets(c);
      n = strlen(c);
      for(i= 0 ; i

  • @dheerendraarc2515
    @dheerendraarc2515 4 года назад +2

    C Implementation
    #include
    #include
    #define MAX_SIZE 10 //Maximum Size for Array.
    int top = -1;
    char str[MAX_SIZE], reverseStr[MAX_SIZE]; // Global Array Name “Str” and "reverseStr"
    // Define Push Operation for Stack.
    void push(char x){
    if(top == MAX_SIZE){
    printf("Stack Overflow
    ");
    }
    reverseStr[++top] = x;
    }
    // Define Pop Operation for Stack.
    void pop(){
    if(top == -1){
    printf("Stack underflow
    ”);
    }
    printf("%c", reverseStr[top]);
    --top;
    }
    int main(){
    int i = 0;
    scanf("%s", str);
    for(i=0;str[i]!='\0';i++){
    push(str[i]);
    }
    for(i=0;str[i]!='\0';i++){
    pop();
    }
    printf("
    ");
    return 0;
    }

  • @md-ayaz
    @md-ayaz 8 лет назад

    how do I find out when to use Cases like "while(temp!=NULL)" and "while(temp->next!=NULL)"

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

      i also want to know that :(

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

      I'm not sure my answer is 100% correct but i'll try... I'm taking the simplest example which is our linked list identity HEAD..We firstly initialize the variable HEAD as Node* head = NULL...After inserting a new node in the list we will have head = temp ( temp being also a pointer variable Node* temp (c++)/ struct Node* temp (c) )...Our output til' now is HEAD - > TEMP - > NULL .. In my opinion temp and temp->next in this case are the same thing and both statements are correct. Through the initialization of temp as Node* this variable will take in consideration JUST the own address ( IN CASE YOU DON'T DEREFERENCE IT such as: (*temp).data )..in our case being the last in the linked list is NULL which is equal if i'm saying temp->next which also is pointing to NULL ( conclusion: both are pointing to NULL or making a reference to NULL )...hope you understand it.

    • @Arunkumar-md2lc
      @Arunkumar-md2lc 7 лет назад +1

      naah!! they are not same we use temp!=null to go to nth position or in case of traverse (we use this to go to last node) while temp->next!=null is used to go to n-1th node or in case of traversing(it is used to go to second-last node) hope u understand:)

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

      Thanks...

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

      Hope I am not too late. If you use the first while condition by the end of the loop your temp variable will have address pointing to 0 i.e it will travel through the whole linked list. But if use the second condition the temp variable will point to the last node and it won't travel the last node for display function that he has been writing in his examples

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

    RIP Harsha

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

    will u like to do a job in microsoft usa, sir, i m from microsoft , reply fast , we like your job animesh

    • @pup_lover
      @pup_lover 5 лет назад +2

      unfortunately, Animesh is no more with us. RIP .

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

      @@pup_lover not true. Its the other co-founder Harsha S who dies in an unfortunate accident.
      tiptechtales.com/2017/08/13/humblefool-greatest-coder-india-has-ever-produced/

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

      @@pup_lover Half knowledge is dangerous.

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

      I am His Close Friend...Availaible for Service on Behalf of my Best Friend!!!

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

      @@muhammadbilalmalik7292 Hello sir I am Rishabh from IIT Mandi

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

    It's not working..
    #include
    #include
    #include
    using namespace std;
    int main()
    {
    char a[100];
    cin>>a;
    int l;
    l=sizeof(a);
    stack s;
    int i;
    for(i=0;i

    • @x_rayapu3005
      @x_rayapu3005 6 лет назад +1

      use strlen instead of sizeof , happy coding

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

    Thank a lot !

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

    🙌🙌

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

    Where is the source code ?

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

      ruclips.net/p/PL7Lpn-y30qDvSaj0DxrS5_5-3dEdL_3zw check here for code if you like please subscribe to this channel

  • @1610Hunny
    @1610Hunny 9 лет назад

    Can someone tell the source code for reversing a linked list using stack in C??

    • @friend1310
      @friend1310 9 лет назад +5

      Hitesh Singhal I used C++, but it's not a big difference. I don't use stack from standard template library, I've written my own implementation instead. I am a newbie, too. So, it would be great if somebody could check my code.
      #include
      using namespace std;
      struct node { // node has 2 fields
      int data; // this field stores data
      node* link; // this field stores address of the next node in the list
      };
      struct stack { // my stack implementation using linked list
      node* node; // stores address of a node
      stack* next; // used for getting back to the previous stack node when popping
      };
      stack* top = NULL; // pointer to the top of stack
      node* head; // pointer to the head node in the linked list, initialized in main()
      void Insert(int x) { // Inserts a node at the end of the linked list
      node* temp = new node;
      temp->data = x;
      temp->link = NULL;
      if (head == NULL) {
      head = temp;
      return;
      }
      node* temp1 = head;
      while (temp1->link != NULL) {
      temp1 = temp1->link;
      }
      temp1->link = temp;
      }
      void Print() { // prints nodes in the linked list
      node* temp = head;
      while (temp != NULL) {
      cout data link;
      }
      cout node = current; // saving address of the node from the linked list
      temp->next = top;
      top = temp;
      }
      void Pop() {
      if (top == NULL) {
      cout link = Top();
      temp = temp->link;
      Pop();
      }
      temp->link = NULL;
      }
      int main() {
      head = NULL;
      Insert(2);
      Insert(3);
      Insert(7);
      Insert(9);
      Insert(11);
      cout

    • @AmrendraKumar-ko8yf
      @AmrendraKumar-ko8yf 7 лет назад

      Peter Masica .. great work man

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

    sir can you pls code in c?

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

      ruclips.net/p/PL7Lpn-y30qDvSaj0DxrS5_5-3dEdL_3zw check here for coding in C if you like please subscribe to this channel

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

    C code:
    #include
    #include
    #include
    struct Node{
    char data;
    struct Node *link;
    };
    struct Node *top = NULL;
    void Push(char c);
    void Pop();
    void Push(char c)
    {
    struct Node *temp = (struct Node*)malloc(sizeof(struct Node*));
    temp->data = c;
    temp->link = top;
    top = temp;
    printf("%c", temp->data);
    }
    void Pop()
    {
    struct Node *temp;
    if(top==NULL)
    {
    return;
    }
    temp = top;
    top = top->link;
    printf("%c", temp->data);
    }
    int main()
    {
    char *str = (char*)malloc(sizeof(char));
    printf("Enter String: ");
    scanf("%s", str);
    int m = strlen(str);
    for(int i=0; i

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

    reverse linkedlist- 09:17

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

    java implementation you can find here:
    github.com/sandeepmekala/datastructures-and-algorithms/commit/5f7c0f0e10b46f24b45bb0df047904b2fb68bc36

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

    print reverse linked list using stack
    void printReverseUsingStack()
    {
    struct Node* temp = head;
    stack S;
    if (head == NULL) return;
    while (temp != NULL)
    {
    S.push(temp);
    temp = temp -> next;
    }
    while (!S.empty())
    {
    printf("%d \t", S.top() -> data);
    S.pop();
    }
    printf ("
    ");
    }

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

    code for reverse of LL using Stack without top() method in java
    import java.util.Scanner;
    class TestReverseLL
    {
    Node head;
    int count;
    Stack top;
    class Node
    {
    int data;
    Node next;
    Node(int info)
    {
    data=info;
    }
    }
    class Stack
    {
    Node object;
    Stack s_next;
    public Stack(Node object)
    {
    this.object=object;
    }
    }
    public void push(Node temp)
    {
    Stack snode = new Stack(temp);
    if(top == null)
    {
    top=snode;
    }
    else
    {
    snode.s_next=top;
    top=snode;
    }
    }
    public Stack pop()
    {
    return top;
    }
    public void insert(int value)
    {
    Node newNode = new Node(value);
    if(head==null)
    {
    head=newNode;
    }
    else
    {
    Node temp;
    temp=head;
    while(temp.next!=null)
    {
    temp=temp.next;
    }
    temp.next=newNode;
    }
    }
    public void reverseLL()
    {
    Node temp = head;
    while(temp!=null)
    {
    push(temp);
    temp=temp.next;
    }
    System.out.println("Insertion completed");
    //Insertion into the stack done
    Stack stemp1=pop();
    top=top.s_next;
    Node temp1 = stemp1.object;
    head=temp1;
    Node temp2=null;
    Stack stemp2=null;
    while(top!=null)
    {
    stemp2=pop();
    top=top.s_next;
    temp2=stemp2.object;
    temp1.next=temp2;
    temp1=temp2;
    }
    temp1.next=null;
    }
    public void display()
    {
    Node temp=head;
    if(head==null)
    {
    System.out.println("List is empty");
    }
    else
    {
    while(temp!=null)
    {
    System.out.println(temp.data);
    temp=temp.next;
    }
    }
    }
    public static void main(String []args)
    {
    int val,info,n;
    Scanner sc = new Scanner(System.in);
    TestReverseLL list = new TestReverseLL();
    while(true)
    {
    System.out.println("Choose 1: to insert
    Choose 2: to display n
    Choose 3: to reverse");
    val=sc.nextInt();
    switch(val)
    {
    case 1:
    System.out.println("Enter the number
    ");
    info=sc.nextInt();
    list.insert(info);
    break;
    case 2:
    list.display();
    break;
    case 3:
    list.reverseLL();
    break;
    default:
    System.out.println("Invalid choice");
    break;
    }
    }
    }
    }

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

    c implementation
    #include
    #include
    #include
    struct node{
    int data;
    struct node *link;
    };
    struct node*top=NULL;
    void push(int data){
    struct node*temp=(struct node*)malloc(sizeof(struct node));
    temp->data=data;
    temp->link=top;
    top=temp;
    }
    void pop(){
    if(top==NULL){
    return;
    }
    top=top->link;
    }
    int Top(){
    struct node*temp=top;
    return temp->data;
    }
    void reverse(char*c,int n){
    for (int i=0;i

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

    Isn't pushing data to stack much better? In that way you don't have to worry about reversing links!!!!!

  • @aptitudepointiit-bhu5358
    @aptitudepointiit-bhu5358 2 года назад

    C++ code to reverse a Linked List using Stack!! (Used inbuilt stack of STL)
    stack st;
    Node* temp = head;
    while((*temp).next != NULL)
    {
    st.push(temp);
    temp = temp->next;
    }
    st.push(temp);
    if(head==NULL) return 0;
    temp = st.top();
    head = temp;
    st.pop();

    while(st.size()!=0)
    {
    (*temp).next = st.top();
    st.pop();
    temp = (*temp).next;
    }
    (*temp).next = NULL;
    print();

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

    can someone write it in C?

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

      Long tee see my post above

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

      Priya Kokas Thank you a lot,i was looking for this:)

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

      i cant find your post

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

    #include
    #include
    struct node
    {
    char c;
    struct node *link;
    };
    struct node *head=NULL;
    void push()
    {
    struct node* temp;
    temp=(struct node*)(malloc(sizeof(struct node)));
    printf("enter a character : ");
    scanf("%c",&temp->c);
    fflush(stdin);
    temp->link=NULL;
    if(head==NULL)
    {
    head=temp;
    }
    else
    {
    temp->link=head;
    head=temp;
    }
    }
    void show()
    {
    struct node *p;
    p=head;
    while(p!=NULL)
    {
    printf("%c",p->c);
    p=p->link;
    }
    }
    int main()
    {
    push();
    push();
    push();
    push();
    push();
    show();
    }
    we dont need to perform pop operation to reverse a strig . the above code works fine.

  • @pprrnnk
    @pprrnnk 7 лет назад +4

    can you please control your obsession with c++ and continue in c.... oh i think you can not ..........videos already uploaded ...fingers crossed for future videos i am going to watch ....he hehe

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

    blog.mycodeschool.com/ he's not dead!!! But these guys lost their teammate to a hit and run case back when I was in bangalore.

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

    your channel name is school and you are explaining things so complexly, not a good teache .

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

    my code here:
    #include
    #include
    typedef struct node
    {
    int data;
    void *link;
    } NODE;
    NODE *Push(NODE *top, int x)
    {
    NODE *newNode = (NODE *)malloc(sizeof(NODE));
    newNode->data = x;
    newNode->link = top;
    top = newNode;
    return top;
    }
    NODE *Pop(NODE *top)
    {
    NODE *temp = top;
    if (temp == NULL)
    {
    printf("No more elements to pop!
    ");
    return 0;
    }
    top = temp->link;
    free(temp);
    return top;
    }
    NODE *TopStack(NODE *top)
    {
    if (top == NULL)
    {
    printf("Stack is empty!
    ");
    return 0;
    }
    return top;
    }
    void print(NODE *head)
    {
    printf("Stack: ");
    while (head != NULL)
    {
    printf("%d->", head->data);
    head = head->link;
    }
    printf("
    ");
    }
    int IsEmpty(NODE *top)
    {
    if (top == NULL)
    {
    return 1;
    }
    else
    {
    return 0;
    }
    }
    NODE *PushRef(NODE *top)
    {
    NODE *newNode = (NODE *)malloc(sizeof(NODE));
    newNode->link = top;
    newNode = top;
    return newNode;
    }
    NODE *rev(NODE *top)
    {
    if (top == NULL)
    return 0;
    NODE *temp = top;
    while (temp != NULL)
    {
    temp = PushRef(temp);
    temp = temp->link;
    }
    temp = TopStack(temp);
    top = temp;
    Pop(top);
    while (!IsEmpty)
    {
    temp->link = Pop(top);
    Pop(top);
    temp = temp->link;
    }
    temp->link = NULL;
    return top;
    }
    int main()
    {
    NODE *top = (NODE *)malloc(sizeof(NODE));
    top = NULL;
    top = Push(top, 3);
    top = Push(top, 6);
    top = Push(top, 1);
    top = Push(top, 12);
    top = Push(top, 9);
    print(top);
    top = rev(top);
    print(top);
    }