-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse.cpp
67 lines (65 loc) · 1.08 KB
/
reverse.cpp
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
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}node;
void create(node ** head,int n)
{
node *temp;
if(*head == NULL) {
temp = (node *)malloc(sizeof(node));
temp->data = n;
*head = temp;
(*head)->next = NULL;
}else {
create((&(*head)->next),n);
}
}
void print(node *head)
{
while(head != NULL) {
cout << head->data << "-->";
head = head->next;
}
}
static void reverse(node **head)
{
int count = -1;
node *next;
node *temp = *head;
node *prev = NULL;
count++;
while(temp->next != NULL) {
next = (temp)->next;
//cout << "next is " << next->data << endl;
(temp)->next = prev;
prev = (temp);
//cout << "prev is " << prev->data <<endl;
temp = next;
//cout << "new temp is " << temp->data << endl;
}
temp->next = prev;
*head = temp;
//cout << "head value is " << (*head)->data << endl;
}
int main()
{
node *head = NULL;
while(1) {
//create(&head,n);
int n;
cin >> n;
if(n == 999) {
break;
}else {
create(&head,n);
}
}
print(head);
reverse(&head);
print(head);
return 0;
}