-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_ll.c
101 lines (85 loc) · 1.46 KB
/
reverse_ll.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<stdio.h>
#include<stdlib.h>
typedef struct Node_def {
int data;
struct Node_def* next;
} Node;
Node* head;
int counter;
void Insert(int dat) {
Node* temp1=(Node*)malloc(sizeof(Node));
temp1->data=dat;
temp1->next=NULL;
Node* temp2=head;
while(temp2->next != NULL) temp2=temp2->next;
temp2->next=temp1;
}
void Print() {
counter = 0;
Node* temp=head;
printf("List: ");
while(temp != NULL) {
printf("%d ", temp->data);
counter+=1;
temp=temp->next;
}
printf("\n");
}
void Delete(int x) {
Node* temp1 = head;
int i;
if (x > counter) {
printf("Invalid position!\n");
return;
}
if (x == 1) {
head = temp1->next;
free(temp1);
return;
}
Node* temp2;
for(i=0;i<x-2;i+=1) {
temp1 = temp1->next;
}
temp2=temp1->next;
temp1->next=temp2->next;
free(temp2);
}
void Reverse() {
Node *current, *prev, *next;
int i;
current = head;
prev = NULL;
while(current != NULL) {
next = current->next;
current->next=prev;
prev=current;
current=next;
}
head = prev;
}
int main() {
head=NULL;
int n, i, dat, x;
char r;
printf("Enter number of elements: \n");
scanf("%d", &n);
printf("Enter the elements: \n");
scanf("%d", &dat);
head = (Node*)malloc(sizeof(Node));
head->data=dat;
head->next=NULL;
for(i=0;i<n-1;i+=1) {
scanf("%d", &dat);
Insert(dat);
}
Print();
printf("Reversing...\n");
Reverse();
Print();
printf("Enter the position of the element to delete: \n");
scanf("%d", &x);
Delete(x);
Print();
return 0;
}