-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathdelete_head_node_of_LL.cpp
85 lines (69 loc) · 1.17 KB
/
delete_head_node_of_LL.cpp
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node* delete_head_node(node *head)
{
node* p;
if (head->next !=NULL)
{
p = head;
head = p->next;
free(p);
}
else
{
free(head);
head = NULL;
}
return head;
}
int main()
{
int a , i = 1 , n ,r , node_data , position; // i = 0 is also ok....
node *p,*q,*head;
printf("Enter the number of nodes");
scanf("%d",&n);
printf("Enter node %d \n",i); // you can also start with i = 0
p = (node*)malloc(sizeof(node));
scanf("%d",&a);
p->data = a;
p->next = NULL;
head = p;
for(i=2;i<=n;i++) //if i=0 , then for ( i = 1; i < n; i++ )
{
printf("Enter node %d \n",i);
q = (node*)malloc(sizeof(node));
scanf("%d",&a);
q->data = a;
q->next = NULL;
p->next = q;
p = p->next;
}
p = head;
while(p!=NULL)
{
printf("\t %d", p->data);
p = p->next;
}
printf("\nDeleting the head node.......");
head = delete_head_node(head);
printf("\n");
if(head==NULL)
{
printf("The linked is empty");
}
else
{
p=head;
while(p!=NULL)
{
printf("\t%d",p->data);
p = p->next;
}
}
return 0;
}