-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathArrayReversal.cpp
169 lines (138 loc) · 2.7 KB
/
ArrayReversal.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
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct Node{
int data ;
Node* p;
};
class LinkedList{
public :
Node* head;
int max_index;
LinkedList();
void print();
void DeleteFromEnd();
void DeleteFromFront();
void InsertAtEnd(int v);
void InsertAtFront(int v);
void InsertAtIndex(int v,int index);
void DeleteFromIndex(int index);
};
LinkedList::LinkedList(){
head=NULL;
max_index=-1;
}
void LinkedList::print(){
if(head!=NULL){
Node* temp= head;
while(temp!=NULL)
{
cout<<temp->data<<",";
temp=temp->p;
}
cout<<endl;
} else cout<<"List is empty"<<endl;
}
void LinkedList::InsertAtFront(int v){
Node* x = new Node;
x->p=head;
x->data=v;
head=x;
max_index++;
}
void LinkedList::InsertAtIndex(int v,int index){
if(index==0)
InsertAtFront(v);
else if(index>0 && index<=max_index)
{
Node* new_node = new Node;
new_node->data=v;
Node * temp = head;
for(int i=1;i<index;i++){
temp = temp->p;
}
new_node->p=temp->p;
temp->p=new_node;
max_index++;
}
else cout<<"Index out of range";
}
void LinkedList::InsertAtEnd(int v){
Node* temp = head;
if(temp!=NULL)
{
while(temp->p!=NULL)
temp=temp->p;
Node* x = new Node;
temp->p=x;
x->data=v;
x->p=NULL;
max_index++;
}
else InsertAtFront(v);
}
void LinkedList::DeleteFromEnd(){
if(head!=NULL){
if(head->p==NULL)
{
Node * tempP=head;
head=NULL;
delete tempP;
max_index--;
}
else{
Node* temp = head;
Node* temp1=temp->p ;
while(temp1->p!=NULL) {
temp=temp->p;
temp1=temp->p;
}
temp->p=NULL; delete temp1;
max_index--;}}
else cout<<"Nothing to delete!\n";}
void LinkedList::DeleteFromIndex(int index){
if(head!=NULL){
if(index==0)
DeleteFromFront();
else if(index==max_index)
DeleteFromEnd();
else if(index>0 && index<=max_index){
Node* temp = head;
Node* temp1=temp->p ;
for(int i=1;i<index;i++) {
temp=temp->p;
temp1=temp->p;
}
temp->p=temp1->p; delete temp1;
max_index--;}
else cout<<"Index out of range\n";
}
else cout<<"Nothing to delete!\n";}
void LinkedList::DeleteFromFront(){
if(head!=NULL){
Node * temp = head;
head=head->p;
delete temp;
max_index--;}
else cout<<"Nothing to delete!\n";
}
int main(int argc, char** argv) {
LinkedList L;
L.InsertAtFront(1);
L.print();
L.InsertAtEnd(2);
L.print();
L.InsertAtIndex(3,0);
L.print();
L.InsertAtIndex(2,1);
L.print();
L.DeleteFromIndex(3);
L.print();
L.DeleteFromIndex(1);
L.print();
L.DeleteFromEnd();
L.print();
L.DeleteFromFront();
L.print();
return 0;
}