File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < stdio.h>
2+ #include < stdlib.h>
3+
4+ struct Node {
5+ int data;
6+ struct Node *prev;
7+ struct Node *next;
8+ };
9+
10+ void DoublylinkedListTraversal (struct Node *ptr){
11+ while (ptr != NULL ){
12+ printf (" Elements: %d\n " ,ptr->data );
13+ ptr=ptr->next ;
14+ }
15+ }/*
16+ void DoublylinkedListTraversal(struct Node *head)
17+ {
18+ struct Node * ptr = head;
19+ while (ptr->next != NULL)
20+ {
21+ printf("Elements: %d\n",ptr->data);
22+ ptr = ptr->next;
23+ }
24+ printf("Elements: %d\n",ptr->data);
25+ printf("\n");
26+ while (ptr != NULL)
27+ {
28+ printf("Elements: %d\n",ptr->data);
29+ ptr = ptr->prev;
30+ }
31+ }
32+ */
33+ int main (){
34+ struct Node *head;
35+ struct Node *second;
36+ struct Node *third;
37+ struct Node *fourth;
38+
39+ // Allocate memory to Nodes in heap
40+ head=(struct Node *)malloc (sizeof (struct Node ));
41+ second=(struct Node *)malloc (sizeof (struct Node ));
42+ third=(struct Node *)malloc (sizeof (struct Node ));
43+ fourth=(struct Node *)malloc (sizeof (struct Node ));
44+
45+ // Link first to second as well prev to NULL
46+ head->data =23 ;
47+ head->prev =NULL ;
48+ head->next =second;
49+
50+ // Link second to third as well prev to first
51+ second->data =34 ;
52+ second->prev =head;
53+ second->next =third;
54+
55+ // Link third to fourth as well prev to second
56+ third->data =56 ;
57+ third->prev =second;
58+ third->next =fourth;
59+
60+ // Link fourth to NULL as well prev to third
61+ fourth->data =52 ;
62+ fourth->prev =third;
63+ fourth->next =NULL ;
64+
65+ DoublylinkedListTraversal (head);
66+ // printf("\n");
67+ // DoublylinkedListTraversal(fourth);
68+
69+ return 0 ;
70+ }
You can’t perform that action at this time.
0 commit comments