Skip to content

Commit a3cd495

Browse files
Doubly Linked List Traversal.cpp
1 parent 86d0470 commit a3cd495

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

Doubly Linked List Traversal.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

0 commit comments

Comments
 (0)