-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsixth.cpp
66 lines (66 loc) · 1.27 KB
/
sixth.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
#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* temp = getNewNode(x);
node* temp1 = head;
if (n==1) {
temp->next = head;
head = temp;
}
else
{
for (int i = 0; i < n - 2; i++) {
temp1 = temp1->next;
}
temp->next = temp1->next;
temp1->next = temp;
}
}
node* problem(int n) {
node* temp = head;
node* tempen = head;
while (--n) {
temp=temp->next;
}
while (temp->next != NULL) {
tempen = tempen->next;
temp = temp -> next;
}
return tempen;
}
void display() {
node* temp = head;
while (temp != NULL) {
std::cout << temp->data << '\n';
temp = temp -> next;
}
}
int main(int argc, char const *argv[]) {
head = NULL;
int n;
insert(2,1);
insert(4,2);
insert(6,3);
insert(8,4);
insert(10,5);
insert(12,6);
insert(14,7);
insert(16,8);
display();
std::cout <<"Enter the value of n"<< '\n';
std::cin >> n;
std::cout << "The nth number is "<< problem(n)->data << '\n';
return 0;
}