-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
133 lines (129 loc) · 2.31 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
void search();
void reverselist();
struct node {
int data;
struct node *next;
};
int c = 0;
struct node *head;
int b=0;
void insert();
void deletes();
void display();
int main() {
int n, a, p, ch;
head = NULL;
while(1) {
scanf("%d", &ch);
switch (ch) {
case 1:insert();
break;
case 2:deletes();
break;
case 3: display();
break;
case 4: search();
break;
case 5: reverselist(head);
break;
case 6: exit (0);
default:printf("invalid choice");
}
}
return 0;
}
void insert() {
int p, i,n ;
struct node *list1 = (struct node *)malloc(sizeof(struct node));
struct node *list2;
scanf("%s%d", &n, &p);
if (p <= 0 || p > c + 1)
{
printf("Enter a valid position\n");
exit (0);
}
else {
c++;
list1->data = n;
list1->next = NULL;
if (p == 1) {
list1->next = head;
head = list1;
}
else {
list2 = head;
for (i = 0; i < p - 2; i++) {
list2 = list2->next;
}
list1->next = list2->next;
list2->next = list1;
}
}
}
void deletes() {
int p, i;
struct node *temp1 = head, *temp2;
scanf("%d", &p);
if ((p <= 0) || p > c) {
printf("Enter a valid position\n");
}
else {
c--;
if (p == 1) {
head = temp1->next;
free(temp1);
}
else {
for (i = 0; i < p - 2; i++) {
temp1 = temp1->next;
}
temp2 = temp1->next;
temp1->next = temp2->next;
free(temp2);
}
}
}
void display() {
struct node *x = head;
if (x == NULL) {
printf("LinkedList is empty\n");
return;
}
while (x != NULL) {
printf("%d->", x->data);
x = x->next;
}
}
void search()
{
int n, p=0;
struct node *x;
scanf("%d", &n);
x= head;
while (x != head)
{
p++;
if(x -> data =n)
{
b=1;
break;
}
x= x->next;
}
if ( b == 1)
{
printf("%d element found", n);
}
else
printf("%d element not found", n);
}
void reverselist(struct node *head)
{
if (head == NULL)
return;
else
reverselist(head -> next);
printf("%d->", head -> data);
}