-
Notifications
You must be signed in to change notification settings - Fork 0
/
h4q1.c
79 lines (76 loc) · 1.61 KB
/
h4q1.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
# include <stdio.h>
# include <stdlib.h>
struct nodeh41
{
int data;
struct nodeh41* next;
}*head = NULL,*tail = NULL;
void printh41(struct nodeh41 *h)
{
if(h == NULL)
{
printf("LIST IS EMPTY\n");
}
while(h != NULL)
{
printf("%d\t", h->data);
h = h-> next;
}
}
int h4q1()
{
struct nodeh41 *curr;
printf("\t\tCREATING THE LIST\n\n");
int n;
printf("ENTER NUMBER OF NODEh41S : ");
scanf("%d",&n);
printf("ENTER THE ELEMENTS : ");
for(int i=0;i<n;i++)
{
curr= malloc(sizeof(struct nodeh41));
curr -> next = NULL;
scanf("%d",&curr->data);
if(head == NULL)
{
head = tail = curr;
}
else
{
tail -> next = curr;
tail = curr;
}
}
// printh41(head);
struct nodeh41 *ptr = head,*prev;
printf("ENTER THE VALUE OF N : ");
int m;
scanf("%d",&m);
while(ptr != NULL)
{
if(ptr == head && ptr->data == n)
{
printf("ELEMENT ALREADY PRESENT AT BEGINNING\n");
break;
}
else
{
prev = ptr;
ptr = ptr->next;
if(ptr->data == m)
{
prev->next = ptr->next;
ptr->next = head;
head = ptr;
// free(ptr);
// add(n);
break;
}
}
}
if(ptr == NULL)
{
printf("ELEMENT NOT FOUND\n");
}
printh41(head);
return 0;
}