-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinglylinkedlist_class.cpp
More file actions
117 lines (104 loc) · 2.4 KB
/
singlylinkedlist_class.cpp
File metadata and controls
117 lines (104 loc) · 2.4 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
108
109
110
111
112
113
114
115
116
117
#include <iostream>
using namespace std;
class node
{
private:
static int count;
int data;
node *ptr_next;
node *head; // bahi yhn pe pointer node nami(type)
// ko save krega isliya yhn pr node type rakhi h...
public:
node()
{
ptr_next = NULL;
count = count + 1;
}
node(int data, node *node_addr)
{
head = node_addr;
this->data = data;
node_addr->ptr_next = NULL;
}
void preappend(int data)
{
node *n = new node();
node *temp = head;
head = n;
n->ptr_next = temp;
n->data = data;
}
void append(int data)
{
node *n = new node();
node *temp = head;
while (temp->ptr_next != NULL)
{
temp = temp->ptr_next;
}
temp->ptr_next = n;
n->data = data;
n->ptr_next = NULL;
}
void atyourdesire(int data)
{
int pos;
node *temp = head;
cout << "Enter node number to whom after you want to insert number...";
cin >> pos;
if (pos <= 0)
{
cout << "YOU SHOULD HAVE PREAPPEND INSTEAD OF INSERT..." << endl;
}
else
{
for (int i = 1; i < pos; i++)
{
if (temp->ptr_next == NULL)
{
break;
}
temp = temp->ptr_next;
}
node *n = new node();
n->data = data;
node *temp2;
temp2 = temp->ptr_next;
temp->ptr_next = n;
n->ptr_next = temp2;
}
}
void display()
{
node *temp = head;
while (temp != NULL)
{ // y akhir tk gaya h..
cout << temp->data << "->";
temp = temp->ptr_next;
}
cout << "NULL" << endl;
}
static void nodescount()
{
cout << "no.of nodes: " << count << endl;
}
};
int node::count = count + 1;
int main()
{
node n1(5, &n1);
n1.preappend(6);
n1.append(19);
n1.append(30);
n1.preappend(0);
n1.preappend(-1);
n1.append(29);
n1.display();
n1.nodescount();
n1.atyourdesire(77);
n1.display();
n1.nodescount();
n1.preappend(100);
n1.display();
n1.nodescount();
}