forked from AyanSharma1001/Hacktoberfest22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlyLinkedList.cpp
169 lines (166 loc) · 3.61 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//SINGLY LINKED LIST
#include<bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
//Default Constructor
Node()
{
data=0;
next=NULL;
}
//Parametrised constructor
Node(int data){
this->data = data;
this->next = NULL;
}
};
class LinkedList {
Node* head;
public:
//Default constructor
LinkedList()
{
head=NULL;
}
void traversal();
void insertAtbeginning(int);
void insertAtlast(int);
void insertAtindex(int , int);
void deleteAtbeginning();
void deleteAtlast();
void deleteAtindex(int);
};
void LinkedList::traversal(){
Node* temp= head;
if(temp != NULL){
cout<<"The list contains: ";
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
else {
cout<<"The list is empty"<<endl;
}
}
void LinkedList::insertAtbeginning(int new_data){
Node* newNode = new Node(new_data); // allocate node
//Assign to head
if(head==NULL){
head = newNode;
return;
}
//Insert at beginning
newNode->next = head;
head= newNode;
}
void LinkedList::insertAtlast(int new_data){
Node* newNode = new Node(new_data);
if(head==NULL){
head = newNode;
return;
}
Node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newNode;
newNode->next=NULL;
}
void LinkedList::insertAtindex(int new_data , int index){
Node* newNode = new Node(new_data);
if(head==NULL){
head = newNode;
return;
}
// Node* temp=head;
// for(int i=1;i<index;i++){
// if(temp->next!=NULL){
// temp=temp->next;
// }
// }
// newNode->next=temp->next;
// temp->next=newNode;
int i=0;
Node* p=head;
Node* q=head->next;
while(i!=index-1){
i++;
p=p->next;
q=q->next;
}
p->next=newNode;
newNode->next=q;
}
void LinkedList::deleteAtbeginning(){
Node* temp=head;
if(head==NULL){
return;
}
head=head->next;
free(temp);
}
void LinkedList::deleteAtlast(){
if(head==NULL){
return;
}
Node* p=head;
Node* q=head->next;
while(q->next!=NULL){
p=p->next;
q=q->next;
}
p->next=q->next;
free(q);
}
void LinkedList::deleteAtindex(int index){
//*************FIRST METHOD******************//
// if(head==NULL){
// return;
// }
// Node* temp=head;
// int i=0;
// while(i!=index-1){
// i++;
// temp=temp->next;
// }
// temp->next=temp->next->next;
if(head==NULL){
return;
}
Node* p=head;
Node* q=head->next;
for(int i=0;i<index-1;i++){
p=p->next;
q=q->next;
}
p->next=q->next;
free(q);
}
int main(){
LinkedList list;
//inserting nodes
list.insertAtbeginning(1);
list.insertAtbeginning(2);
list.insertAtbeginning(3);
list.insertAtbeginning(4);
list.insertAtlast(5);
list.insertAtlast(6);
list.insertAtindex(7,2);
list.insertAtindex(8,4);
list.insertAtbeginning(9);
list.insertAtbeginning(10);
list.insertAtbeginning(11);
list.insertAtbeginning(12);
list.deleteAtbeginning();
// list.deleteAtlast();
// list.deleteAtindex(3);
// list.deleteAtindex(2);
cout<<"Element of the list are: ";
list.traversal();
return 0;
}