-
Notifications
You must be signed in to change notification settings - Fork 0
/
double.c
65 lines (57 loc) · 1.53 KB
/
double.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
#include <stdio.h>
#include <stdlib.h>
typedef struct new_node
{
int data;
struct new_node* prev;
struct new_node* next;
}*node;
node head=NULL;
node tail=NULL;
void insert(int data) {
//Create a new node
node newNode = (node)malloc(sizeof(node));
newNode->data = data;
//If list is empty
if(head == NULL) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to NULL
head->prev = NULL;
//tail's next will point to NULL, as it is the last node of the list
tail->next = NULL;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode's previous will point to tail
newNode->prev = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to NULL
tail->next = NULL;
}
}
void display() {
//Node current will point to head
node current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of doubly linked list: \n");
while(current != NULL) {
//Prints each node by incrementing pointer.
printf("%d ", current->data);
current = current->next;
}
}
int main(int argc, char const *argv[])
{
insert(2);
insert(32);
insert(4);
insert(5);
display();
return 0;
}