-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec4_1.cpp
62 lines (55 loc) Β· 998 Bytes
/
lec4_1.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
// insertion in doubly linked list
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *prev;
Node *next;
Node(int value)
{
data = value;
prev = NULL;
next = NULL;
}
};
int main()
{
Node *head = NULL;
//insert at start
// linkedllst doesnt exist
if(head == NULL)
{
head = new Node(5);
}
// already existing linked list
else{
Node *temp = new Node(5);
temp->next = head;
head->prev = temp;
head = temp;
}
// insertion at end
if(head == NULL )
{
head = new Node(15);
}
else{
Node *curr = head;
while(curr->next!=NULL)
{
curr = curr->next;
}
Node *temp = new Node(15);
curr->next = temp;
temp->prev = curr;
}
// Traversing
Node *trav = head ;
while(trav)
{
cout<<trav->data<<" ";
trav = trav->next;
}
}