-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
80 lines (72 loc) · 1.66 KB
/
test.c
File metadata and controls
80 lines (72 loc) · 1.66 KB
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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *link;
};
void append(struct Node** head_ref, int new_data)
{
/*New-Node Creation*/
struct Node* new_node = (struct Node*)malloc(sizeof (struct Node));
/*Adding Data to the New Node*/
new_node -> data = new_data;
/*The address section of the new_node is set to null*/
new_node -> link = NULL;
/*If the list is empty, th new_node is declared as the new_node*/
if(*head_ref == NULL)
{
*head_ref = new_node;
return;
}
/*Else traverse from the head to the end and add the new_node*/
struct Node* last = *head_ref;
while(last -> link != NULL)
last = last -> link;
last -> link = new_node;
return;
}
void deleteAlt(struct Node *head)
{
if (head == NULL)
return;
/* Initialize prev and node to be deleted */
struct Node *prev = head;
struct Node *node = head->link;
while (prev != NULL && node != NULL)
{
/* Change next link of previous node */
prev->link = node->link;
/* Update prev and node */
prev = prev->link;
if (prev != NULL)
node = prev->link;
}
free(node);
}
void show(struct Node* node)
{
if(node == NULL)
printf("Empty!\n");
else {
while (node != NULL) {
printf("%d ", node->data);
node = node->link;
}
printf("\n");
}
return;
}
int main(void)
{
struct Node *head = NULL;
int n;
scanf("%d", &n);
for(int i =0; i<n; i++) {
int temp;
scanf("%d", &temp);
append(&head, temp);
}
deleteAlt(head);
show(head);
}