forked from krimanisha/Hacktoberfest21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode1.c
119 lines (117 loc) · 2.3 KB
/
node1.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int x;
struct node *next;
} * start;
void add_node();
void display();
void move();
void search();
void main()
{
int op;
do
{
printf("\n_____menu_____");
printf("\n1.add_node 2.display 3.exit 4.move 5.search\n");
printf("\nEnter your choice");
scanf("%d", &op);
switch (op)
{
case 1:
add_node();
break;
case 2:
display();
break;
case 3:
printf("\nBye");
break;
case 0:
move();
break;
case 5:
search();
break;
default:
printf("\ninvalid choice");
break; /* code */
}
} while (op != 3);
}
void add_node()
{
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nMemory is full");
}
else
{
printf("\nEnter the value of x\n");
scanf("%d", &ptr->x);
ptr->next = start;
start = ptr;
}
}
void display()
{
struct node *ptr;
ptr = start;
while (ptr != NULL)
{
printf("x=%d", ptr->x);
ptr = ptr->next;
}
}
void move()
{
struct node *last, *second_last;
last = second_last = start;
while (last->next != NULL)
{
second_last = last;
last = last->next;
}
last->next = start;
start = last;
second_last->next = NULL;
}
void search()
{
struct node *ptr, *new, *temp;
int s, flag = 0;
printf("\nEnter the element");
scanf("%d", &s);
ptr = start;
while (ptr != NULL)
{
if (ptr->x == s)
{
flag = 1;
break;
}
ptr = ptr->next;
}
ptr = start;
if (flag == 0)
{
printf("\nNot found");
}
else
{
new = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the value of x in the new node");
scanf("%d", &new->x);
while (ptr->x != s)
{
temp = ptr;
ptr = ptr->next;
}
new->next = temp->next;
temp->next = new;
}
}