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:
For practice problems and more, visit:
Like us on Facebook:
Follow us on twitter:
Nguồn: https://freecode.com.vn
Xem thêm bài viết khác: https://freecode.com.vn/mobile
You are just amazing! You are truly, honestly, clearly deserved to work at Google!
Much Easier C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node{
int data;
struct Node *link;
};
struct Node *head;
void push(int x)
{
struct Node *nod=(struct Node*)malloc(sizeof(struct Node));
nod->data=x;
nod->link=head;
head=nod;
}
char pop()
{
char n;
struct Node *temp=head;
if(head==NULL) return;
n=temp->data;
head=head->link;
free(temp);
return n;
}
int main()
{
head=NULL;
char A[100000],n;
printf("Enter the string : ");
gets(A);
for(int i=0;i<strlen(A);i++)
{
push(A[i]);
}
for(int i=0;i<strlen(A);i++)
{
A[i]=pop();
}
for(int i=0;i<strlen(A);i++)
{
printf("%c",A[i]);
}
}
How can I copy the stack’s elements to another new stack?
C code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
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<m; i++)
{
Push(str[i]);
}
printf("n");
for (int i=0; i<m; i++)
{
Pop();
}
printf("n");
}
Reverse a linked list using stack in CPP source code link –
https://github.com/tridibsamanta/Data-Structures-and-Algorithms/blob/master/DS/Stack_LinkedListReverse.cpp
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!!!
C Implementation
#include<stdlib.h>
#include<stdio.h>
#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 Overflown");
}
reverseStr[++top] = x;
}
// Define Pop Operation for Stack.
void pop(){
if(top == -1){
printf("Stack underflown”);
}
printf("%c", reverseStr[top]);
–top;
}
int main(){
int i = 0;
scanf("%s", str);
for(i=0;str[i]!='