-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlinkedlist3.cpp
72 lines (66 loc) · 1.05 KB
/
linkedlist3.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
// deleting at nth position
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
Node *head;
void Insert(int data)
{
Node *temp = new Node();
temp->data = data;
temp->next = NULL;
if (head == NULL)
{
temp->next = head;
head = temp;
return;
}
Node *itr = head;
while (itr->next != NULL)
{
itr = itr->next;
}
itr->next = temp;
}
void Print()
{
Node *temp = new Node();
temp = head;
while (temp != NULL)
{
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL\n";
}
void Delete(int n)
{
Node* temp = head;
if (n==1){
head = temp->next;
delete temp;
return;
}
for(int i=0;i<n-2;i++)
temp = temp->next;
Node* temp2 = temp->next;
temp->next = temp2->next;
delete temp2;
}
int main()
{
Insert(3);
Insert(5);
Insert(1);
Insert(9);
Print();
int n;
cout << "Enter Position : ";
cin >> n;
Delete(n);
Print();
}