-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrev_linklist.c
63 lines (60 loc) · 1.28 KB
/
rev_linklist.c
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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node;
node * rev_linklist(node * head){
node * p=head;
node * t=head;
node * ptr=NULL;
while (p!=NULL)
{
p=p->next;
t->next = ptr;
ptr = t;
t = p;
}
head = ptr;
return head;
}
int main()
{
node *head, *newNode, *temp;
head = NULL;
int choice;
while (choice)
{
newNode = (node *)malloc(sizeof(node));
printf("Enter data: ");
scanf("%d", &newNode->data);
newNode->next = NULL;
if (head == NULL)
{
head = temp = newNode;
}
else
{
temp->next = newNode;
temp = newNode;
}
printf("Do you want to continue (1/0) : ");
scanf("%d", &choice);
}
temp = head;
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
head = rev_linklist(head);
temp = head;
printf("\nrev is : \n");
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
return 0;
}