-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathollie57.cpp
84 lines (68 loc) · 1.61 KB
/
ollie57.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
#include<iostream>
using namespace std;
// INSERTION OF A NODE IN BETWEEN A DOUBLY LINKED LIST !!!
struct Node
{
int data;
struct Node* prev;
struct Node* next;
};
void linkedlisttraversal(struct Node* a)
{
while(a != NULL)
{
cout<<"Element: "<<a->data<<endl;
a = a->next;
}
}
void reverselisttraversal(struct Node* b)
{
while(b != NULL)
{
cout<<"Element: "<<b->data<<endl;
b = b->prev;
}
}
void InsertInBetween(struct Node* head , int data , int index)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data = data;
int i = 1;
while(i != index-1)
{
head = head->next;
i++;
}
newnode->prev = head;
newnode->next = head->next;
(head->next)->prev = newnode;
head->next = newnode;
}
int main()
{
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));
struct Node* fourth = (struct Node*)malloc(sizeof(struct Node));
head->data = 11;
head->prev = NULL;
head->next = second;
second->data = 13;
second->prev = head;
second->next = third;
third->data = 15;
third->prev = second;
third->next = fourth;
fourth->data = 19;
fourth->prev = third;
fourth->next = NULL;
linkedlisttraversal(head);
cout<<endl;
reverselisttraversal(fourth);
InsertInBetween(head , 17 , 4);
cout<<endl;
linkedlisttraversal(head);
cout<<endl;
reverselisttraversal(fourth);
return 0;
}