No video

Reverse a string or linked list using stack.

  Рет қаралды 384,309

mycodeschool

mycodeschool

Күн бұрын

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.mycodeschoo...
Like us on Facebook: / mycodeschool
Follow us on twitter: / mycodeschool

Пікірлер: 184
@yokedici90
@yokedici90 9 жыл бұрын
"...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 жыл бұрын
bhai bhai bhai bhai bhai bhai
@IndianDesiTennis
@IndianDesiTennis 6 жыл бұрын
did you create this meme "bhai bhai bhai bhai" :o
@vikrant4666
@vikrant4666 6 жыл бұрын
sonu sonu you are the creater of arey bhai bhai meme , salute respeccc
@rishabhvarshney7522
@rishabhvarshney7522 4 жыл бұрын
Vinod
@hasinanjum6282
@hasinanjum6282 3 жыл бұрын
@@rishabhvarshney7522 O MY Binod!!!! Binod is also here............im fainting
@mycodeschool
@mycodeschool 10 жыл бұрын
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 жыл бұрын
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.
@paula19335
@paula19335 Жыл бұрын
But he is here..
@vigneshwarm
@vigneshwarm 5 жыл бұрын
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 жыл бұрын
I really learned a lot from your videos. Thank you so much for your time and effort.
@kalyanroy6687
@kalyanroy6687 8 жыл бұрын
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!
@PratikShende91
@PratikShende91 6 жыл бұрын
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 жыл бұрын
blog.mycodeschool.com/ he's not dead his teammate died in a hit and run case in bangalore in 2014!!
@danhle7999
@danhle7999 3 жыл бұрын
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
@mycodeschool
@mycodeschool 10 жыл бұрын
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 жыл бұрын
@@HARIHaran-ks7wp Complete series is finished go and check their playlist
@Stacy8209
@Stacy8209 7 жыл бұрын
8:33 closed caption says "space complexity O(n)" while you're correctly saying O(1), just letting you know
@Stacy8209
@Stacy8209 7 жыл бұрын
same thing at 11:05
@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
@aswinvishnua8330
@aswinvishnua8330 3 жыл бұрын
for using strlen(c) ,include headerfile: #include
@burakbosluk1297
@burakbosluk1297 7 жыл бұрын
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 7 жыл бұрын
@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.
@pryakks
@pryakks 9 жыл бұрын
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 жыл бұрын
Priya Kokas Good Job.....!!!
@pij6277
@pij6277 9 жыл бұрын
Great!
@gurdyalsingh8228
@gurdyalsingh8228 7 жыл бұрын
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 жыл бұрын
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); }
@regachoaliyahkhaet6577
@regachoaliyahkhaet6577 Ай бұрын
you're such a life-saver.
@theilluminati77
@theilluminati77 8 жыл бұрын
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 жыл бұрын
@@forbiddenumbrella I think he had learnt c++ in last two years😂
@MeethiNeend
@MeethiNeend 5 жыл бұрын
Both are almost same
@BeastMaster-gc5zg
@BeastMaster-gc5zg 5 жыл бұрын
C++ is just the Incremented version of C. Both possess some similarities
@emrekorkmaz3359
@emrekorkmaz3359 5 жыл бұрын
I wish you could be my data structure teacher :) thanks for the videos , it helped me a lot :)
@kartikey_Kq
@kartikey_Kq 5 жыл бұрын
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
@vijaydwivedi823
@vijaydwivedi823 4 жыл бұрын
Precious Gift to the world , thanks buddy , CS DOJO approved , BULLDOG MINDSET approved etc etc .
@junegirly2718
@junegirly2718 9 жыл бұрын
Urs tutorial is too good nd pfcorse ...of ur voice. .it's easy to understand everything..post more videos..on c
@ashwinaman12977
@ashwinaman12977 8 жыл бұрын
Upload Dynamic programming videos
@amateurbeginner7538
@amateurbeginner7538 7 жыл бұрын
do you know where can i find dynamic programming videos?
@kanishkjain2767
@kanishkjain2767 7 жыл бұрын
Its on his channel watch the pointers and dynamic allocation series
@jayant9151
@jayant9151 5 жыл бұрын
He's dead
@2222stunner
@2222stunner 6 жыл бұрын
Thanks for all the effort you people have put in to make these videos. These are the best. ☺
@thestarinthesky_
@thestarinthesky_ 4 жыл бұрын
You are just amazing! You are truly, honestly, clearly deserved to work at Google!
@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
@IndianDesiTennis
@IndianDesiTennis 6 жыл бұрын
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
@smitasingh9095
@smitasingh9095 2 жыл бұрын
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.
@gauravdotiyal7171
@gauravdotiyal7171 Жыл бұрын
best explanation ever
@shalikasinha9025
@shalikasinha9025 5 жыл бұрын
awesome teaching...cleared my concepts
@ManishKumar-fb2yx
@ManishKumar-fb2yx 7 жыл бұрын
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
@ahmetertugrulkaya7134
@ahmetertugrulkaya7134 3 жыл бұрын
now i want a double like button on yt bc this video explains shit great
@dasemaw1862
@dasemaw1862 10 жыл бұрын
Awesome video,you make it very easy to understand, need more,linked lists,with previous and next, thank you very much.
@rohanreddy01
@rohanreddy01 6 жыл бұрын
Love you man! Next level teaching.
@radhikaravi1379
@radhikaravi1379 5 жыл бұрын
thank you so much sir... thx for motivating us in DSA !! wish u good luck :)
@soldierofislam194
@soldierofislam194 11 жыл бұрын
when will you upload nore videos on data structures.best lessons ever guys.thank you so much.
@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.
@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!!!
@PavanKumar-mr9td
@PavanKumar-mr9td 3 жыл бұрын
what an awesome explanation bro....
@vijayendrasdm
@vijayendrasdm 10 жыл бұрын
very precise and helpful video. Thank you very much
@jimwang4582
@jimwang4582 9 жыл бұрын
so ,both the Time and space when using stack explictily are O(n)? But what the advantage of using stack explictily ?
@muhammadbilalmalik7292
@muhammadbilalmalik7292 4 жыл бұрын
Because Knowledge Is Power
@pranshul..
@pranshul.. 3 жыл бұрын
@@muhammadbilalmalik7292 😮
@ayusharyan161
@ayusharyan161 7 жыл бұрын
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..
@redlamaravind104
@redlamaravind104 4 жыл бұрын
could you share the code ones I can't find anywhere..
@cybrarybr1558
@cybrarybr1558 5 жыл бұрын
As always , like before watching :)
@ayoubelmardi9970
@ayoubelmardi9970 6 жыл бұрын
Ongoing charity love this playlist
@sriramkasu5286
@sriramkasu5286 4 жыл бұрын
as said at last where is the code link in the description : (
@karthickraja6419
@karthickraja6419 6 жыл бұрын
it throws me a error of "Fatal error directive " how it can be cleared
@tusharbarman1924
@tusharbarman1924 5 жыл бұрын
RIP Harsha
@diobrando2209
@diobrando2209 Жыл бұрын
is there a code to reverse link list using stack in C? I tried but it doesn't work
@saptarshideysikder755
@saptarshideysikder755 6 жыл бұрын
In the loop for pop function you could have done it in one line like c[i]=S.pop(); right??
@mayanovarini5757
@mayanovarini5757 7 жыл бұрын
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?
@jyotichhabra25
@jyotichhabra25 6 жыл бұрын
Can u please upload reverse function code using C
@abhrajyotikirtania2275
@abhrajyotikirtania2275 10 жыл бұрын
nice .. thanks
@gadipallikrishnaprasad2967
@gadipallikrishnaprasad2967 7 жыл бұрын
have u made any videos related to time and space complexities separately ?
@noumanmalik960
@noumanmalik960 6 жыл бұрын
kzbin.info/www/bejne/emfbnJV9gbaYqc0
@dheerendraarc2515
@dheerendraarc2515 4 жыл бұрын
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; }
@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!!
@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 жыл бұрын
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.
@hridaynathp
@hridaynathp 3 жыл бұрын
0:11 🔥
@nguaial8490
@nguaial8490 8 жыл бұрын
I have looked everywhere for the source code. I cannot find it. Can you tell me where it is?
@mayanovarini5757
@mayanovarini5757 7 жыл бұрын
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 жыл бұрын
@@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
@ntlzz93
@ntlzz93 9 жыл бұрын
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
@naveenav4470
@naveenav4470 8 жыл бұрын
what is happening to the null character?
@muhammadbilalmalik7292
@muhammadbilalmalik7292 4 жыл бұрын
Naveena Null Character is Attacked by Corona Virus ,,,Pray for it's health!!
@GauravKumar-dw2ml
@GauravKumar-dw2ml 4 жыл бұрын
In linked list pop operation why you call pop outside while...?
@boodywolverine
@boodywolverine 5 жыл бұрын
the link to the practice problems is not working says "502 Bad Gateway"
@user-qy2yu4ky6c
@user-qy2yu4ky6c 4 жыл бұрын
How can I copy the stack’s elements to another new stack?
@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
@sunithafrancis7091
@sunithafrancis7091 7 жыл бұрын
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??
@YourCRTube
@YourCRTube 4 жыл бұрын
Yes
@vikrant4666
@vikrant4666 6 жыл бұрын
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 6 жыл бұрын
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 жыл бұрын
I am His Close Friend...Availaible for Service on Behalf of my Best Friend!!!
@rishabhranjan2437
@rishabhranjan2437 4 жыл бұрын
@@muhammadbilalmalik7292 Hello sir I am Rishabh from IIT Mandi
@onepercentswe
@onepercentswe 6 жыл бұрын
java implementation you can find here: github.com/sandeepmekala/datastructures-and-algorithms/commit/5f7c0f0e10b46f24b45bb0df047904b2fb68bc36
@evagan2448
@evagan2448 8 жыл бұрын
do you have a java programme one?
@adarshverma3372
@adarshverma3372 4 жыл бұрын
yes i have added check top comment
@urrahman196
@urrahman196 3 жыл бұрын
reverse linkedlist- 09:17
@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
@shivampandey-vv4wy
@shivampandey-vv4wy 6 жыл бұрын
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(); }
@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 (" "); }
@ashishkumarverma8065
@ashishkumarverma8065 4 жыл бұрын
Sir please give the source code of the reversal of stack usong linked list
@narayansharma256
@narayansharma256 6 жыл бұрын
where is sourecode you said in video
@madhavirathi6004
@madhavirathi6004 3 жыл бұрын
Space complexity?
@jayant696
@jayant696 8 жыл бұрын
Can anyone give a link for STL function Signature and how to use them
@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.
@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 8 жыл бұрын
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 8 жыл бұрын
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 8 жыл бұрын
Thanks...
@nirmalkc1234
@nirmalkc1234 7 жыл бұрын
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
@varunraokadaparthi3565
@varunraokadaparthi3565 6 жыл бұрын
Isn't pushing data to stack much better? In that way you don't have to worry about reversing links!!!!!
@varunraokadaparthi3565
@varunraokadaparthi3565 6 жыл бұрын
I meant for linked lists
@shivammina1009
@shivammina1009 3 жыл бұрын
🙌🙌
@sukruthipattabi3004
@sukruthipattabi3004 5 жыл бұрын
Where is the source code ?
@devarapallisrilakshmi8445
@devarapallisrilakshmi8445 4 жыл бұрын
kzbin.info/aero/PL7Lpn-y30qDvSaj0DxrS5_5-3dEdL_3zw check here for code if you like please subscribe to this channel
@ihiteshsinghal
@ihiteshsinghal 9 жыл бұрын
Can someone tell the source code for reversing a linked list using stack in C??
@friend1310
@friend1310 9 жыл бұрын
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
@serenabuarra6931
@serenabuarra6931 8 жыл бұрын
Thank a lot !
@bharatprakashparakh9601
@bharatprakashparakh9601 7 жыл бұрын
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 жыл бұрын
use strlen instead of sizeof , happy coding
@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();
@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
@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; } } } }
@technologydig3394
@technologydig3394 4 жыл бұрын
sir can you pls code in c?
@devarapallisrilakshmi8445
@devarapallisrilakshmi8445 4 жыл бұрын
kzbin.info/aero/PL7Lpn-y30qDvSaj0DxrS5_5-3dEdL_3zw check here for coding in C if you like please subscribe to this channel
@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
@pprrnnk
@pprrnnk 7 жыл бұрын
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
@kyssl
@kyssl 6 жыл бұрын
Priyank Bhardwaj he's dead
@mcgil8891
@mcgil8891 6 жыл бұрын
Priyank Bhardwaj ikr
@himanshusomani7
@himanshusomani7 6 жыл бұрын
he's dead...
@e2bvideos
@e2bvideos 6 жыл бұрын
You mean he is no more?
@vishnu_bhatt
@vishnu_bhatt 6 жыл бұрын
yes he is no more.
@longtee1937
@longtee1937 9 жыл бұрын
can someone write it in C?
@pryakks
@pryakks 9 жыл бұрын
Long tee see my post above
@longtee1937
@longtee1937 9 жыл бұрын
Priya Kokas Thank you a lot,i was looking for this:)
@ayush2445
@ayush2445 6 жыл бұрын
i cant find your post
@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.
@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.
@rahul5959able
@rahul5959able 4 жыл бұрын
your channel name is school and you are explaining things so complexly, not a good teache .
@koulicksadhu7679
@koulicksadhu7679 4 жыл бұрын
Are you really out of your mind?
@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); }
Check for balanced parentheses using stack
14:13
mycodeschool
Рет қаралды 472 М.
Evaluation of Prefix and Postfix expressions using stack
14:10
mycodeschool
Рет қаралды 629 М.
My Cheetos🍕PIZZA #cooking #shorts
00:43
BANKII
Рет қаралды 26 МЛН
Пройди игру и получи 5 чупа-чупсов (2024)
00:49
Екатерина Ковалева
Рет қаралды 3,2 МЛН
Reverse a linked list using recursion
8:55
mycodeschool
Рет қаралды 606 М.
Is THIS Python's MOST Underrated Operator? (Walrus Operator)
5:45
Data Structures: Linked List implementation of stacks
10:58
mycodeschool
Рет қаралды 634 М.
Introduction to linked list
17:13
mycodeschool
Рет қаралды 1,6 МЛН
Pointers and dynamic memory - stack vs heap
17:26
mycodeschool
Рет қаралды 1,4 МЛН
Quicksort algorithm
20:39
mycodeschool
Рет қаралды 1,8 МЛН
Merge sort algorithm
18:20
mycodeschool
Рет қаралды 2,2 МЛН