-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstring_reverse_using_stack.c
More file actions
80 lines (64 loc) · 1.07 KB
/
Copy pathstring_reverse_using_stack.c
File metadata and controls
80 lines (64 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Reversing a string using stack datastructure
// Author: Siddhartha Sadhukhan
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
struct node
{
char data[1];
struct node* next;
};
struct node* top=NULL; // top always point at the top of the stack
// push function
push(char x)
{
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
if(top==NULL)
{
(*temp).data[0] = x;
(*temp).next = NULL;
top=temp;
}
else
{
(*temp).data[0] = x;
(*temp).next = top;
top=temp;
}
}
// pop function
char* pop(void)
{
struct node* temp;
temp=top;
if(temp==NULL)
{
exit;
}
else
{
top=(*temp).next;
return (*temp).data;
}
}
// Main function
int main()
{
char bucket[50];
int length,n;
printf("Enter a string to reverse: ");
scanf("%s",bucket);
length=strlen(bucket);
for(n=0;n<length;n++)
{
push(bucket[n]);
}
printf("\n\n Reverse String: ");
for(n=0;n<length;n++)
{
bucket[n] = *pop();
printf("%c", bucket[n]);
}
}