-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecond.cpp
62 lines (62 loc) · 1.13 KB
/
second.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
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* head;
node* getNewNode(int x){
node* temp = new node();
temp->data = x;
temp->next = NULL;
return temp;
}
void insert(int x,int n)
{
node* newNode = getNewNode(x);
node* temp2;
if(n == 1)
{
newNode->next = head;
head = newNode;
}
else
{
node* temp1 = head;
for (int i = 0; i < n-1; i++) {
temp2 = temp1;
temp1 = temp1->next;
}
newNode->next = temp1;
temp2->next = newNode;
}
}
void remove(int n) {
node* tempt;
node* temp = head;
for (int i = 0; i < n-1; i++) {
tempt = temp;
temp = temp->next;
}
tempt->next = temp->next;
delete temp;
}
void display() {
node* temp3 = head;
while (temp3 != NULL) {
std::cout << temp3->data << '\n';
temp3 = temp3->next;
}
}
int main(int argc, char const *argv[]) {
head = NULL;
insert(3,1);
insert(6,2);
insert(4,2);
insert(5,3);
display();
std::cout << '\n';
remove(3);
display();
return 0;
}