-
Notifications
You must be signed in to change notification settings - Fork 137
/
Delete at beginning in linked list
76 lines (58 loc) · 1.68 KB
/
Delete at beginning in linked list
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
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
void deleteStart(struct Node** head){
struct Node* temp = *head;
// if there are no nodes in Linked List can't delete
if(*head == NULL){
printf("Linked List Empty, nothing to delete");
return;
}
// move head to next node
*head = (*head)->next;
printf("Deleted: %d\n",temp->data);
free(temp);
}
void display(struct Node* node){
// as linked list will end when Node is Null
while(node!=NULL){
printf("%d ",node->data);
node = node->next;
}
printf("\n");
}
int main()
{
//creating 4 pointers of type struct Node
//So these can point to address of struct type variable
struct Node* head = NULL;
struct Node* node2 = NULL;
struct Node* node3 = NULL;
struct Node* node4 = NULL;
// allocate 3 nodes in the heap
head = (struct Node*)malloc(sizeof(struct Node));
node2 = (struct Node*)malloc(sizeof(struct Node));
node3 = (struct Node*)malloc(sizeof(struct Node));
node4 = (struct Node*)malloc(sizeof(struct Node));
head->data = 22; // data set for head node
head->next = node2; // next pointer assigned to address of node2
node2->data = 30;
node2->next = node3;
node3->data = 24;
node3->next = node4;
node4->data = 20;
node4->next = NULL;
/*No need for & i.e. address as we do not
need to change head address
*/
printf("Linked List Before Operations : ");
display(head);
deleteStart(&head);
deleteStart(&head);
printf("Linked List After Operations : ");
display(head);
return 0;
}