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.
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!
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...
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
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
@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.
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
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!!!
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
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.
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!!
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; }
@@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
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.
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:)
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
@@Mountain-Cola 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/
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
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
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
"...a warrior should not just possess a weapon, but he must also know when and how to use it." well said bro
bhai bhai bhai bhai bhai bhai
did you create this meme "bhai bhai bhai bhai" :o
sonu sonu you are the creater of arey bhai bhai meme , salute respeccc
Vinod
@@rishabhvarshney7522 O MY Binod!!!! Binod is also here............im fainting
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.
But he is here..
Hi Shadman
Data structures is our priority. We publish 2 to 3 videos in a week. So the complete series will take some time.
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
I really learned a lot from your videos. Thank you so much for your time and effort.
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.
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!
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
In print function can't we declare a stack and pick all the elements form linked list into stack & print them
@@ravenx You could dereference the address of each pointer in the stack and print the data field of it
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...
blog.mycodeschool.com/ he's not dead his teammate died in a hit and run case in bangalore in 2014!!
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
Thanks for all the effort you people have put in to make these videos. These are the best. ☺
I wish you could be my data structure teacher :) thanks for the videos , it helped me a lot :)
for using strlen(c) ,include headerfile:
#include
you're such a life-saver.
Precious Gift to the world , thanks buddy , CS DOJO approved , BULLDOG MINDSET approved etc etc .
Urs tutorial is too good nd pfcorse ...of ur voice. .it's easy to understand everything..post more videos..on c
8:33 closed caption says "space complexity O(n)" while you're correctly saying O(1),
just letting you know
same thing at 11:05
best explanation ever
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
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
Priya Kokas
Good Job.....!!!
Great!
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.
if(top == -1){
printf("Error: stack is empty
");
return;
}
mistake fixed :)
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);
}
java implementation you can find here:
github.com/sandeepmekala/datastructures-and-algorithms/commit/5f7c0f0e10b46f24b45bb0df047904b2fb68bc36
awesome teaching...cleared my concepts
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("
");
}
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?
@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.
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
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.
Hi Shadman,
Data structures is our priority. We publish 2-3 videos in a week. So, the complete series will take some time.
its been a while...
@@HARIHaran-ks7wp Complete series is finished go and check their playlist
what an awesome explanation bro....
thank you so much sir... thx for motivating us in DSA !! wish u good luck :)
You are just amazing! You are truly, honestly, clearly deserved to work at Google!
Awesome video,you make it very easy to understand, need more,linked lists,with previous and next, thank you very much.
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
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
when will you upload nore videos on data structures.best lessons ever guys.thank you so much.
Love you man! Next level teaching.
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!!!
now i want a double like button on yt bc this video explains shit great
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
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.
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??
Yes
You should verify the input first. Before dereferencing C, you should check if C is not
a null pointer. cool video outside of that.
reverse linkedlist- 09:17
very precise and helpful video. Thank you very much
is there a code to reverse link list using stack in C? I tried but it doesn't work
how can you switch to C++,I am following C from the start?
theilluminati77 I have the same problem
go through book :- balaguruswamy for c++
@@ikashyap5 I think he had learnt c++ in last two years😂
Both are almost same
C++ is just the Incremented version of C. Both possess some similarities
could you share the code ones I can't find anywhere..
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..
In the loop for pop function you could have done it in one line like
c[i]=S.pop();
right??
so ,both the Time and space when using stack explictily are O(n)? But what the advantage of using stack explictily ?
Because Knowledge Is Power
@@muhammadbilalmalik7292 😮
Ongoing charity love this playlist
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!!
In linked list pop operation why you call pop outside while...?
Upload Dynamic programming videos
do you know where can i find dynamic programming videos?
Its on his channel
watch the pointers and dynamic allocation series
He's dead
have u made any videos related to time and space complexities separately ?
th-cam.com/video/D6xkbGLQesk/w-d-xo.html
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;
}
// 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
}
What is the use of the array A in your code?
as said at last where is the code link in the description : (
Can u please upload reverse function code using C
As always , like before watching :)
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;
}
How can I copy the stack’s elements to another new stack?
Where do I can search source code in tutorial? Please I'm implementing stack use linked list that i implement on your previous lesson.
yes as mentioned it is not available in description
Why don't you use cout for output in your c++ programs??
It makes me a little confused
He's using C in this video.
@@FatmaYousuf lol
Sai Nivedh Vallala yeah totally funny
I have looked everywhere for the source code. I cannot find it. Can you tell me where it is?
This is not his source code, but feel free to see mine : repl.it/EbXh/7
It is Present in His Grave...Go and Get also share with Us!!
@@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
it throws me a error of "Fatal error directive " how it can be cleared
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
Space complexity?
what is happening to the null character?
Naveena Null Character is Attacked by Corona Virus ,,,Pray for it's health!!
nice .. thanks
Sir please give the source code of the reversal of stack usong linked list
the link to the practice problems is not working says "502 Bad Gateway"
0:11 🔥
where is sourecode you said in video
do you have a java programme one?
yes i have added check top comment
Can anyone give a link for STL function Signature and how to use them
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 ("
");
}
how do I find out when to use Cases like "while(temp!=NULL)" and "while(temp->next!=NULL)"
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.
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:)
Thanks...
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
RIP Harsha
will u like to do a job in microsoft usa, sir, i m from microsoft , reply fast , we like your job animesh
unfortunately, Animesh is no more with us. RIP .
@@Mountain-Cola 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/
@@Mountain-Cola Half knowledge is dangerous.
I am His Close Friend...Availaible for Service on Behalf of my Best Friend!!!
@@muhammadbilalmalik7292 Hello sir I am Rishabh from IIT Mandi
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();
}
does anyone have the code for Java, please i am very stuck on this idk what to do anymore its so frustrating.
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();
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
Where is the source code ?
th-cam.com/play/PL7Lpn-y30qDvSaj0DxrS5_5-3dEdL_3zw.html check here for code if you like please subscribe to this channel
can someone share the link list code in c?
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
sir can you pls code in c?
th-cam.com/play/PL7Lpn-y30qDvSaj0DxrS5_5-3dEdL_3zw.html check here for coding in C if you like please subscribe to this channel
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;
}
}
}
}
Thank a lot !
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
use strlen instead of sizeof , happy coding
Can someone tell the source code for reversing a linked list using stack in C??
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
Peter Masica .. great work man
Isn't pushing data to stack much better? In that way you don't have to worry about reversing links!!!!!
I meant for linked lists
🙌🙌
#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.
can someone write it in C?
Long tee see my post above
Priya Kokas Thank you a lot,i was looking for this:)
i cant find your post
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.
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
Priyank Bhardwaj he's dead
Priyank Bhardwaj ikr
he's dead...
You mean he is no more?
yes he is no more.
your channel name is school and you are explaining things so complexly, not a good teache .
Are you really out of your mind?
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);
}