forked from Sai-02/Data-Structures-using-C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelete_from_last_in_DLL.c
More file actions
97 lines (90 loc) · 1.97 KB
/
Delete_from_last_in_DLL.c
File metadata and controls
97 lines (90 loc) · 1.97 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
struct node *deleteFromLast(struct node *, int);
struct node
{
int info;
struct node *next;
struct node *prev;
};
void main()
{
int n;
scanf("%d", &n);
struct node *head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
head->prev = NULL;
struct node *current = head;
// struct node* previous=NULL;
for (int i = 0; i < n; i++)
{
int value;
scanf("%d", &value);
if (i == 0)
{
current->info = value;
current->prev = NULL;
}
else
{
struct node *new = (struct node *)malloc(sizeof(struct node));
new->info = value;
new->prev = current;
new->next = NULL;
current->next = new;
current = current->next;
}
}
void value;
scanf("%d", &value);
head = deleteFromLast(head, value);
struct node *temp = head;
while (temp->next != NULL)
{
printf("%d ", temp->info);
temp = temp->next;
}
printf("%d\n", temp->info);
while (temp != NULL)
{
printf("%d ", temp->info);
temp = temp->prev;
}
}
struct node *deleteFromLast(struct node *head, int value)
{
struct node *last = head;
if (head->info == value)
{
last = last->next;
last->prev = NULL;
free(head);
return last;
}
while (last->next != NULL)
{
last = last->next;
}
struct node *next = NULL;
if (last->info == value)
{
next = last->prev;
next->next = NULL;
free(last);
return head;
}
//Now last is the last node of the linked list
while (last != NULL)
{
if (last->info == value)
{
next->prev = last->prev;
last->prev->next = next;
free(last);
return head;
}
next = last;
last = last->prev;
}
return head;
}