-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.c
More file actions
136 lines (132 loc) · 3.46 KB
/
linkedList.c
File metadata and controls
136 lines (132 loc) · 3.46 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
}* main_list = NULL;
void reverse(struct node ** main_list) {
struct node * prev = NULL;
struct node * now = * main_list;
struct node * next = NULL;
while (now != NULL) {
next = now -> next;
now -> next = prev;
prev = now;
now = next;
}* main_list = prev;
}
void insert(struct node ** main_list, int i, int k) {
struct node * trav, * last, * t, * k_list = NULL;
k_list = (struct node * ) malloc(sizeof(struct node));
int temp;
printf("Enter data:\n");
scanf("%d", & temp);
k_list -> data = temp;
k_list -> next = NULL;
last = k_list;
for (int _ = 0; _ < k - 1; _++) {
t = (struct node * ) malloc(sizeof(struct node));
printf("Enter data:\n");
scanf("%d", & temp);
t -> data = temp;
t -> next = NULL;
last -> next = t;
last = t;
}
trav = * main_list;
for (int _ = 0; _ < i; _++) {
trav = trav -> next;
}
struct node * nex;
nex = trav -> next;
trav -> next = k_list;
last -> next = nex;
}
void del(struct node ** main_list, int i, int k) {
struct node * trav;
trav = * main_list;
for (int _ = 0; _ < i; _++) {
trav = trav -> next;
}
struct node * tempdel;
tempdel = trav -> next;
for (int _ = 0; _ < k; _++) {
tempdel = tempdel -> next;
}
trav -> next = tempdel -> next;
}
void printList(struct node * main_list, int i, int k) {
struct node * trav = main_list;
for (int _ = 0; _ < i; _++) {
trav = trav -> next;
}
for (int _ = 0; _ < k; _++) {
printf("%d", trav -> data);
trav = trav -> next;
}
printf("\n");
}
void printFullList(struct node * main_list) {
struct node * trav = main_list;
while (trav != NULL) {
printf("%d ", trav -> data);
trav = trav -> next;
}
printf("\n");
}
int main() {
struct node * last, * t;
main_list = (struct node * ) malloc(sizeof(struct node));
int n, tempdata;
printf("Enter no. Of Elements");
scanf("%d", & n);
printf("Enter data:\n");
scanf("%d", & tempdata);
main_list -> data = tempdata;
main_list -> next = NULL;
last = main_list;
for (int i = 0; i < n - 1; i++) {
t = (struct node * ) malloc(sizeof(struct node));
printf("Enter data:\n");
scanf("%d", & tempdata);
t -> data = tempdata;
t -> next = NULL;
last -> next = t;
last = t;
}
printFullList(main_list);
while (1 > 0) {
printf("Choose:\n 1.Insert Values\n2.Delete Values\n3.Reverse List\n4.Display List\n5.Display Full list.");
int ch;
scanf("%d", & ch);
if (ch == 1) {
int i, k;
printf("Enter the position at which you want to add new elements\n");
scanf("%d", & i);
printf("Enter the number of elements\n");
scanf("%d", & k);
insert( & main_list, i, k);
} else if (ch == 2) {
int id, kd;
printf("Enter the position at which you want to start deleting elements\n");
scanf("%d", & id);
printf("Enter the number of elements to delete");
scanf("%d", & kd);
del( & main_list, id, kd);
} else if (ch == 3) {
reverse( & main_list);
printf("Reversed");
} else if (ch == 4) {
int ip, kp;
printf("Enter the position to at which you want to start printing elements\n");
scanf("%d", & ip);
printf("Enter the number of elements to print\n");
scanf("%d", & kp);
printList(main_list, ip, kp);
} else if (ch == 5) {
printFullList(main_list);
} else {
printf("Invalid Choice");
}
}
}