Reverse a string or linked list using stack.

preview_player
Показать описание
See complete series on data structures here:

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:

Reversal of linked list using recursion:


Рекомендации по теме
Комментарии
Автор

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

yokedici
Автор

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

mycodeschool
Автор

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.

suyash.
Автор

Here is the complete code for 2nd program:


#include<iostream>
#include <stack>
using namespace std;


struct Node
{
int value;
Node *next;
};




void reverse(Node** head){


Node* temp = *head;


if(*head == NULL) return;


stack<struct Node*> 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 << temp->value << "\n";
temp = temp->next;
}
}


int main(){


Node *head = NULL;


push(&head, 1);
push(&head, 2);
push(&head, 3);


print(head);


reverse(&head);

cout << "rev: "<< "\n";
print(head);


return 0;
}

vigneshwarm
Автор

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

nirajabcd
Автор

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.

kalyanroy
Автор

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

factwithhunny
Автор

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!

MinhazulArefinAbtahi
Автор

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...

PratikShende
Автор

just in case you have any error for implementing the 1st example, check out my code in c++ :

#include <iostream>
#include <stack>
#include <cstring>

using namespace std;

void Reverse(char *C, int n)
{
stack<char> 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 << "\nEnter a string: ";
cin >> C;
Reverse(C, strlen(C));
cout << "Reversed output: " << C;
cout << "\n";
}

danhle
Автор

for using strlen(c), include <cstring> headerfile:
#include <cstring>

aswinvishnua
Автор

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

junegirly
Автор

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

mycodeschool
Автор

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

vijaydwivedi
Автор

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

Stacy
Автор

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

emrekorkmaz
Автор

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

stunner
Автор

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.

smitasingh
Автор

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

theilluminati
Автор

Code in C:

#include<stdio.h>
#include<string.h>
#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\n");
        return;
    }
    A[++top]= x;
    printf("%c", x);
}
void Pop(){
    if(top == -1){
        printf("Error: stack overflow\n");
        return;
    }
    printf("%c", A[top--]);
}

int main(){
 char str[] ="mycodeschool";
    int len = strlen(str), i;
    printf("Original String : ");
    for(i=0;i<len;i++)
        Push(str[i]);
        printf("\n");
 printf("Reversed String : ");

     for(i=0;i<len;i++)
        Pop();
}

pryakks