-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathSinglyLinkedList.cpp
98 lines (98 loc) · 2.06 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
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int data){
this->data=data;
this->next=NULL;
}
~Node(){
int value=this->data;
if(this->next!=NULL){
delete next;
this->next=NULL;
}
cout<<value<<endl;
}
};
void insertiontop(Node* &head,int d){
Node* temp=new Node(d);
temp->next=head;
head=temp;
}
void insertiontail(Node* &tail,int d){
Node* temp=new Node(d);
tail->next=temp;
tail=temp;
}
void insertposition(Node* &head,Node* &tail,int position,int d){
if(position==1){
insertiontop(head,d);
return;
}
Node* temp=head;
int cnt=1;
while(cnt<position-1){
temp=temp->next;
cnt++;
}
if(temp->next==NULL){
insertiontail(tail,d);
return;
}
Node* Nodetoinsert=new Node(d);
Nodetoinsert->next=temp->next;
temp->next=Nodetoinsert;
}
void deleteposition(Node* &head,Node* &tail,int position){
if(position==1){
Node* temp=head;
head=head->next;
temp->next=NULL;
delete temp;
}
else{
Node* current=head;
Node* previous=NULL;
int cnt=1;
while(cnt<position){
previous=current;
current=current->next;
cnt++;
}
if(current->next==NULL){
tail=previous;
}
Node* temp=current;
previous->next=temp->next;
temp->next=NULL;
delete temp;
}
}
void print(Node* &head){
Node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main()
{
Node* Node1=new Node(10);
Node* head=Node1;
Node* tail=Node1;
insertiontail(tail,12);
insertiontail(tail,15);
insertiontail(tail,20);
//insertposition(head,tail,1,22);
//insertposition(head,tail,3,22);
//insertposition(head,tail,5,22);
deleteposition(head,tail,4);
print(head);
cout<<head->data<<endl;
cout<<tail->data<<endl;
return 0;
}