-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_delete.c
49 lines (41 loc) · 1.16 KB
/
03_delete.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
/**************************************************************************
write a function DeleteList() that takes a list, deallocates all of its
memory and sets its head pointer to NULL(the empty list)
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
/*--------Build One Two Three ---------*/
struct node* buildonetwothree(){
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node));
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head -> data = 1;
head -> next = second;
second -> data = 2;
second -> next = third;
third -> data = 3;
third -> next = NULL;
return head;
}
/*-------------Delete List---------------*/
void deletelist(struct node** headRef){
struct node* current = *headRef;
struct node* next;
for(current = *headRef; current != NULL; current = next){
next = current->next;
free(current);
}
*headRef = NULL;
}
/*----------------Test Driver---------------------*/
void main(){
struct node* mylist = buildonetwothree();
deletelist(&mylist);
}