-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
122 lines (111 loc) · 2.19 KB
/
code.cpp
File metadata and controls
122 lines (111 loc) · 2.19 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
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
#include<bits/stdc++.h>
#include <numeric>
#define pb push_back
#define pob pop_back()
#define mp make_pair
#define all(v) v.begin(),v.end()
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fin(i,n1,n2);for(ll i=n1;i<n2;i++)
#define fde(i,n1,n2);for(ll i=n1;i>n2;i--)
#define M 1000000007
using ll=long long;
using ld=long double;
using namespace std;
//Arpcoder
bool comp(pair <ll,ll> &a,pair <ll,ll> &b)
{
if(a.first!=b.first)
return a.first>b.first;
else return a.second<b.second;
}
class Node
{
public:
ll data;
Node *next;
Node(ll Data)
{
data=Data;
next=NULL;
}
};
void InsertAtPosition(Node *&head,Node *&tail,ll pos,ll d)
{
if(pos==1)
{
Node *temp=new Node(d);
temp->next=head;
head=temp;
return;
}
Node *temp=head;
ll cnt=1;
while(cnt<pos-1)
{
temp=temp->next;
cnt++;
}
if(temp->next==NULL)
{
Node *NodeToInsert=new Node(d);
temp->next=NodeToInsert;
NodeToInsert->next=NULL;
tail=NodeToInsert;
return;
}
Node *NodeToInsert=new Node(d);
NodeToInsert->next=temp->next;
temp->next=NodeToInsert;
}
void print(Node *&head)
{
Node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"\n";
}
void Delete(Node *&head,ll pos)
{
if(pos==1)
{
Node *q=head;
head=head->next;
free(q);
return;
}
Node *p=head;
Node *q=head->next;
ll cnt=1;
while(cnt<pos-1)
{
p=p->next;
q=q->next;
cnt++;
}
p->next=q->next;
free(q);
}
int main()
{
IOS;
//we can do the following also-
//Node *node1=new Node(data);
//Node *head=node1;
//Node *tail=node1;
Node *head=new Node(3);
Node *node1=new Node(4);
Node *tail=new Node(5);
head->next=node1;
node1->next=tail;
tail->next=NULL;
print(head);
InsertAtPosition(head,tail,4,6);
print(head);
Delete(head,1);
print(head);
//cout<<head->data<<" "<<tail->next<<" "<<head->next;
return 0;
}