forked from hariom20singh/data-structure-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOUBLE_linklist008703.cpp
90 lines (74 loc) · 1.99 KB
/
DOUBLE_linklist008703.cpp
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
84
85
86
87
88
89
90
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *prev;
struct node *next;
};
// void trivarsal(struct node *head){
// struct node*ptr=head;
// while(ptr->next!= NULL){
// cout<<"Elements are:- "<<ptr->data<<endl;
// ptr=ptr->next;
// }
// cout << "Elements are:- " << ptr->data << endl;
// }
void trivarsalprev(struct node*head){
struct node *ptr = head;
while (ptr->next != NULL)
{
ptr=ptr->next;
}
do
{
cout << "Elements are:- " << ptr->data << endl;
ptr=ptr->prev;
} while (ptr->next!=NULL);
}
int main()
{
struct node *head;
struct node *second;
struct node *third;
struct node *fourth;
struct node *fifth;
struct node *six;
struct node *seven;
struct node *eight;
head = (struct node *)malloc(sizeof(struct node));
second = (struct node *)malloc(sizeof(struct node));
third = (struct node *)malloc(sizeof(struct node));
fourth = (struct node *)malloc(sizeof(struct node));
fifth = (struct node *)malloc(sizeof(struct node));
six = (struct node *)malloc(sizeof(struct node));
seven = (struct node *)malloc(sizeof(struct node));
eight = (struct node *)malloc(sizeof(struct node));
head->data = 1;
head->prev = NULL;
head->next = second;
second->data = 2;
second->prev = head;
second->next = third;
third->data = 3;
third->prev = second;
third->next = fourth;
fourth->data = 4;
fourth->prev = third;
fourth->next = fifth;
fifth->data = 5;
fifth->prev = fourth;
fifth->next = six;
six->data = 6;
six->prev = fifth;
six->next = seven;
seven->data = 7;
seven->prev = six;
seven->next = eight;
eight->data = 8;
eight->prev = seven;
eight->next = NULL;
// trivarsal(head);
trivarsalprev(head);
return 0;
}