Reverse a Sentence using Stacks | Stack Challenge | C++ Placement Course | Lecture 23.2

preview_player
Показать описание
Notes of this Lecture:
Рекомендации по теме
Комментарии
Автор

T(n) = O(n) solution below
string sentence = "Hey, how are you doing?";
int i=0;
string temp = " ";
stack <string> s;

if(sentence[i]==' ' or sentence[i]=='\0'){
s.push(temp);
temp = "";
}
temp = temp + sentence[i];
i++;
}
while(!s.empty()){
cout << s.top();
s.pop();
}

aayushvyas
Автор

Bhaiaya Stack using Linked List ka video miss ho gaya kya ? Kaha par hai ?

snehilsinha
Автор

0:45 Stack implementation using linked list kab hua ??

chirag-sgkd
Автор

Where is implementation of stack using linked list ??

ritikprasad
Автор

For Some persons who is thinking that mam is doing this question in more time complexity
Then guys you are wrong
Take a pen and paper and visualise mam code then you get to know the value of i which we are is the increment vallue of i thats why this code is running same as in one loop or using two loops !!!!

webseriesworld
Автор

Mujhe janna hai stack ka implementation kab kiya linked list mai ???

sachinbairi
Автор

At 3:23, I got the point that here we are iterating over the string unless we encounter a space but I didn't understand how this while loop is working and what's the need of i<s.length()

saketkashyap
Автор

Reverse a sentence in "O(n)" time complexity....


void reverseSentence(string s){
stack<string> st;
string word = "";

for (int i=0; i<s.length(); i++){
if (s[i] == ' '){
st.push(word);
word = "";
i++;
}
word += s[i];
}
st.push(word);

while (!st.empty()){
cout << st.top() << " ";
st.pop();
}
cout << endl;
}

Please let me know if you have better approach than this....
And is this program correct or need any modification....
Thank you!

akashrathod
Автор

string s = "hey, how are you doing?" ;
int a = s.length();
while (a--) {
cout<<s[a] ;
}

psychoticgamer
Автор

// We can used one loop also but it saves the character by character
// so we have to make a character array

StackADT* st = new StackADT();

string str = "i am Gourav Khurana";
for(int i = 0; i < str.length(); i++){
st->push(str[i]);
}

string rev;

while(!st->empty()){
char ch = st->top();
rev = rev + ch;
st->pop();
}

cout << rev << endl;

webseriesworld
Автор

why don't you upload notes of the lectures ?

atulraj
Автор

std::reverse(Sentence.begin(), Sentence.end());

😳

II_xD_II
Автор

Best way of learning


Thank You sooo much

toxicboy
Автор

ALTERNATIVE CODE:

#include <bits/stdc++.h>
using namespace std;
void solve(string s)
{
stack<string>st;
string words="";
for(int i=0;i<s.size();i++)
{
if(s[i]!=' ')
words=words+s[i];
else
{
st.push(words);
words="";
}
}
st.push(words);
while(!st.empty()){

cout<<st.top()<<endl;
st.pop();
}

}
int main() {

string s="hello what are you doing";
solve(s);
}

sunnykakrani
Автор

Link list jb pdhaya gya tha tb insert at head and delete at head pdhaya gya tha..
Whi hota h push pop🙂
There us no need for specific video of implementation of stack using ll

devansh
Автор

i think in while condition (i<s.length() would come first then s.charAt(i)!=' ')

tushardixit
Автор

void solve(string &str) {
stack<char> st;

for(const auto &ch : str) {
if(ch != ' ') st.push(ch);
else {
while(!st.empty()) {
putchar(st.top());
st.pop();
}

putchar(' ');
}
}

while(!st.empty()) {
putchar(st.top());
st.pop();
}

}

mdanishossain
Автор

where is stack using linked list video ? didi app nay Stack LL wali video upload nh ki ?

pMuhammadJawadKhan
Автор

I understand this code, thanks for explaining this :)

nandyxotaku
Автор

stack<string> st
why this line has used in the code?

faizahmad