-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_node_last.c
105 lines (90 loc) · 1.73 KB
/
delete_node_last.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
struct Node *addToEmpty(struct Node *head, int data)
{
if (head != NULL)
{
return head;
}
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = NULL;
temp -> prev = NULL;
return temp;
}
void traverse(struct Node *head)
{
struct Node *p;
if (head == NULL)
{
printf("No element present");
return;
}
p = head;
while(p -> next != NULL)
{
printf("%d ",p -> data);
p = p -> next;
}
printf("%d \n",p->data);
while(p != NULL)
{
printf("%d ",p -> data);
p = p -> prev;
}
}
struct Node *addEnd(struct Node *head, int data)
{
if (head == NULL)
{
return addToEmpty(head, data);
}
struct Node *trav = head;
while(trav -> next != NULL)
{
trav = trav -> next;
}
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = NULL;
trav -> next = temp;
temp -> prev = trav;
return head;
}
struct Node *solution(struct Node *head, int data)
{
//Write your code here
struct Node*t=head;
if(head==NULL){
return NULL;
}
if(head->next==NULL){
return NULL;
}
while(t->next->next!=NULL){
t=t->next;
}
t->next->prev=NULL;
t->next=NULL;
return head;
}
int main()
{
int n,x;
scanf("%d",&n);
struct Node *head = NULL;
for(int i = 0; i < n; i++)
{
scanf("%d",&x);
head = addEnd(head,x);
}
scanf("%d",&x);
head = solution(head,x);
traverse(head);
return 0;
}