-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathorder_list.cpp
More file actions
107 lines (94 loc) · 2.17 KB
/
order_list.cpp
File metadata and controls
107 lines (94 loc) · 2.17 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node()
{
next=NULL;
}
};
class O_linked_list
{
public:
Node* head;
Node* tail;
O_linked_list()
{
head=NULL;
tail=NULL;
}
void add_node(int d)
{
Node* new_node=new Node();
new_node->data=d;
if (head==NULL)
{
head=new_node;
tail=new_node;
}
else if (head->data > d)
{
new_node->next=head;
head=new_node;
}
else
{
Node* temp=head;
while (temp->data > d)
{
temp=temp->next;
}
new_node->next=temp->next;
temp->next=new_node;
}
}
void delete_node(int d)
{
Node* temp=head;
if (head==NULL)
{
cout<<"list is empty";
}
else if (head->data==d)
{
Node* temp=head;
head=head->next;
delete temp;
}
else
{
Node* temp=head;
while ( temp->next!=NULL && temp->next->data!=d)
{
temp=temp->next;
}
Node* p=temp->next;
temp->next=p->next;
delete p;
}
}
void display()
{
Node* temp=head;
cout<<"Ordered list is: ";
while (temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
};
int main()
{
O_linked_list list;
list.add_node(8);
list.add_node(2);
list.add_node(4);
list.add_node(3);
list.add_node(1);
list.delete_node(4);
list.display();
}