-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly_create_display.c
More file actions
83 lines (67 loc) · 1.52 KB
/
doubly_create_display.c
File metadata and controls
83 lines (67 loc) · 1.52 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
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
} *head, *tail;
struct node *CreateNode(int data)
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
if(n==NULL)
{
printf("memory not allocated");
return NULL;
}
n->data=data;
n->next=NULL;
n->prev=NULL;
return n;
}
struct node *CreateList(int n)
{
int data;
printf(" Input data for node 1 : ");
scanf("%d", &data);
struct node *newNode=CreateNode(data);
if (newNode == NULL) return NULL;
head=newNode;
tail = head;
for (int i=2; i<=n; i++)
{
printf(" Input data for node %d : ", i);
scanf("%d", &data);
struct node *newNode=CreateNode(data);
if (newNode == NULL) return head;
newNode->prev = tail;
tail->next = newNode;
tail = newNode;
}
return head;
}
void DisplayList() {
struct node *temp=head;
if (head == NULL)
{
printf("List is empty. \n");
return;
}
printf("The list is: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
head = NULL;
tail = NULL;
int n;
printf(" Input the number of nodes for doubly linked list : ");
scanf("%d", &n);
head=CreateList(n);
DisplayList();
return 0;
}