-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteNode.cpp
58 lines (45 loc) · 1.09 KB
/
deleteNode.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
#include<iostream>
using namespace std;
int data;
struct node{
int data;
struct node * next;
};
void linkedListTraversal(struct node *ptr){
while (ptr != NULL){
cout<<ptr->data<<endl;
ptr = ptr->next;
}
}
struct node * deleteNode(struct node * head){
struct node * ptr = head;
head = head->next;
free(ptr);
return head;
}
int main(){
struct node * head;
struct node * second;
struct node * third;
struct node * fourth;
// memory allocation of linked list is in heap
head = (struct node*)malloc(sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node*)malloc(sizeof(struct node));
fourth = (struct node*)malloc(sizeof(struct node));
// first node
head->data=10;
head->next=second;
// second node
second->data=20;
second->next=third;
//third node
third->data=30;
third->next=fourth;
fourth->data=40;
fourth->next=NULL;
linkedListTraversal(head);
head = deleteNode(head);
linkedListTraversal(head);
return 0;
}