-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLL.c
96 lines (93 loc) · 1.9 KB
/
DLL.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
typedef struct node node;
//to create a list or add front of the list
node *addFront(node *head, int x)
{
node *temp = (node *)malloc(sizeof(node));
temp->data = x;
temp->next = head;
temp->prev = NULL;
if (head == NULL)
head = temp;
else
{
head->prev = temp;
head = temp;
}
temp = NULL;
free(temp);
return head;
}
void printList(node *head)
{
if (head == NULL)
printf("the list is empty!!\n");
else
{
while (head != NULL)
{
printf("%d\t", head->data);
head = head->next;
}
}
}
//to delete the first node of the list
node* delfirst(node* head)
{
if (head == NULL) {
printf("List is Empty");
return NULL;
}
node* temp = head;
head = head->next;
if (head != NULL)
head->prev = NULL;
free(temp);
return head;
}
// to delete a node by entering the value of a node
node *deleteNode(node *head, int x)
{
if (head != NULL)
{
node *temp = head;
if (temp->data == x)
{
head = head->next;
head->prev = NULL;
free(temp);
}
else
{
while (temp != NULL && temp->data != x)
temp = temp->next;
if (temp == NULL)
return head;
temp->prev->next = temp->next;
if (temp->next != NULL)
temp->next->prev = temp->prev;
free(temp);
}
}
return head;
}
int main()
{
node* head = NULL;
head = addFront(head, 3);
head = addFront(head, 9);
head = addFront(head, 1);
head = addFront(head, 10);
printList(head);
head = deleteNode(head,3);
printf("\n");
printList(head);
return 0;
}