-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinglyLinkedList.cpp
More file actions
103 lines (100 loc) · 2.12 KB
/
Copy pathsinglyLinkedList.cpp
File metadata and controls
103 lines (100 loc) · 2.12 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
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int data, Node *next = NULL){
this->data = data;
this->next = next;
}
};
class linkedList{
Node *head;
public:
linkedList(){
head = NULL;
}
int get(int inx){
Node *temp;
temp = head;
int c =0;
while(temp !=NULL){
if(c == inx){
return (temp->data);
}
else{
c++;
temp = temp->next;
}
}
}
void push_front(int data){
Node *newnode = new Node(data);
if(head = NULL){
head = newnode;
}
else{
newnode->next = head;
head = newnode;
}
}
void push_back(int data){
Node *newnode = new Node(data);
if(head==NULL){
head = newnode;
}
else{
Node *temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newnode;
newnode->next = NULL;
}
}
void insert(int data, int inx){
Node *newnode = new Node(data);
if(head==NULL){
head = newnode;
}
else{
Node *i = find(inx-1);
newnode->next = i->next;
i->next = newnode;
}
}
Node *find(int inx){
Node *temp = head;
int c = 0;
while(temp != NULL){
if(c == inx){
return temp;
}
else{
c++;
temp = temp->next;
}
}
}
void print(){
Node *temp = head;
while (temp !=NULL)
{
cout<<temp->data<<endl;
temp = temp->next;
}
}
};
int main(){
linkedList *list1 = new linkedList();
list1->push_front(5);
list1->push_front(12);
list1->push_back(25);
list1->push_back(455);
list1->push_back(322);
list1->print();
list1->insert(16, 2);
cout<<"after insertion"<<endl;
list1->print();
}