-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-ReversedLinkedList.cpp
More file actions
56 lines (56 loc) · 1.04 KB
/
stack-ReversedLinkedList.cpp
File metadata and controls
56 lines (56 loc) · 1.04 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
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <string.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
struct Node* head2;
void insert(int x) {
struct Node* temp = (struct Node*) malloc(sizeof(struct Node));
temp->data = x;
temp->next = head2;
head2 = temp;
}
void Reverse() {
stack<struct Node*>s;
struct Node* temp = head2;
while(temp != NULL) {
s.push(temp);
temp = temp->next;
}
struct Node* temp2 = s.top();
head2 = temp2;
s.pop();
while(!s.empty()) {
temp2->next = s.top();
s.pop();
temp2 = temp2->next;
}
temp2->next = NULL;
}
void print() {
struct Node* temp3 = head2;
while(temp3!= NULL) {
printf("%d ", temp3->data - '0');
temp3 = temp3->next;
}
printf("\n");
}
int main(void) {
head2 = NULL;
char c[20];
printf("Enter any LinkedList: ");
scanf("%s",c);
for(int i=0;i<strlen(c);i++) {
insert(c[i]);
}
printf("The provided LinkedList is: ");
print();
Reverse();
printf("The Reversed LinkedList is: ");
print();
return 0;
}