forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublylinkedlist.c
83 lines (71 loc) · 1.76 KB
/
doublylinkedlist.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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *prev;
struct node *next;
};
void addAtBeg(struct node **head, int newData){
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = newData;
if(*head == NULL){
*head = newNode;
newNode->next = NULL;
newNode->prev = NULL;
}
else{
newNode->prev = NULL;
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}
void addAfter(struct node **head, int look, int newData){
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = newData;
struct node *link = *head;
while(link->data != look){
link = link->next;
}
newNode->prev = link;
newNode->next = link->next;
link->next = newNode;
}
void removeNode(struct node **head, int key){
struct node *link = *head;
while(link->next->data != key){
link = link->next;
}
link->next->next->prev = link;
link->next = link->next->next;
}
void reverse(struct node **head){
struct node *current = *head;
struct node *temp = NULL;
while(current != NULL){
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if(temp != NULL )
*head = temp->prev;
}
void print(struct node *head){
while(head != NULL){
printf("%d ", head->data);
head = head->next;
}
}
int main(){
struct node *head = NULL;
struct node *tail = NULL;
addAtBeg(&head, 5);
addAtBeg(&head, 4);
addAfter(&head, 5, 6);
addAfter(&head, 6, 7);
removeNode(&head, 6);
reverse(&head);
print(head);
return 0;
}