-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.cpp
71 lines (69 loc) · 1.45 KB
/
SinglyLinkedList.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
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
};
Node* head = NULL;
void Front(struct Node** head, int new_data)
{
Node *newNode = new Node;
newNode->data = new_data;
newNode->next = (*head);
newNode->prev = NULL;
if ((*head) != NULL)
(*head)->prev = newNode;
(*head) = newNode;
}
void After(struct Node* prev_node, int new_data)
{
if (prev_node == NULL) {
cout<<"previous node Null";
return;
}
Node* newNode = new Node;
newNode->data = new_data;
newNode->next = prev_node->next;
prev_node->next = newNode;
newNode->prev = prev_node;
if (newNode->next != NULL)
newNode->next->prev = newNode;
}
void End(struct Node** head, int new_data)
{
Node* newNode = new Node;
Node* last = *head;
newNode->data = new_data;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
while (last->next != NULL)
last = last->next;
last->next = newNode;
newNode->prev = last;
return;
}
void traverse(Node* node) {
Node* last;
while (node != NULL) {
cout<<node->data<<" ";
last = node;
node = node->next;
}
if(node == NULL)
cout<<node->data<<" ";
cout<<node->next==head;
}
int main() {
End(&head, 40);
Front(&head, 20);
Front(&head, 10);
End(&head, 50);
After(head->next, 30);
traverse(head);
return 0;
}