forked from shriyajalana/programming1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummation.cpp
More file actions
65 lines (60 loc) · 1.13 KB
/
Copy pathsummation.cpp
File metadata and controls
65 lines (60 loc) · 1.13 KB
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
// Inserting a new node at nth position of the list
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Node
{
int data;
Node *next;
};
Node *head;
void Insert(int data, int n)
{
Node *temp1 = new Node;
temp1->data = data;
temp1->next = NULL;
if (n == 1)
{
temp1->next = head;
head = temp1;
return;
}
Node *temp2 = head;
for (int i = 0; i < n - 2; i++)
{
temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp1;
}
void Print()
{
Node *temp = head;
cout << "the list is... ";
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main()
{
head = NULL;
int n, data, num;
int k = n;
cout << "how many number do you wanted to insert?\n";
cin >> num;
while (n > 0)
{
cout << "enter number\n";
cin >> data;
cout << "at which position\n";
cin >> n;
Insert(data, n);
n--;
}
Print();
return 0;
}