-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
106 lines (94 loc) · 1.76 KB
/
code.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
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
#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 InsertNode(Node *&tail,ll element,ll d)
{
if(tail==NULL)
{
Node *temp=new Node(d);
tail=temp;
temp->next=temp;
return;
}
Node *temp=tail;
while(temp->data!=element)
{
temp=temp->next;
}
Node *NodeToInsert=new Node(d);
NodeToInsert->next=temp->next;
temp->next=NodeToInsert;
}
void print(Node *tail)
{
if(tail==NULL)
{cout<<"List is empty\n";
return;}
Node *temp=tail;
do {
cout<<temp->data<<" ";
temp=temp->next;
}
while(temp!=tail);
cout<<"\n";
}
void DeleteNode(Node *&tail,ll value)
{
Node *p=tail;
Node *q=tail->next;
if(p->next==p)
{
free(p);
return;
}
while(q->data!=value)
{
q=q->next;
}
p->next=q->next;
//q->next=NULL;
free(q);
}
int main()
{
Node *node1=new Node(3);
Node *tail=node1; //creating circular ll
tail->next=tail;
print(tail);
InsertNode(tail,3,4);
print(tail);
InsertNode(tail,4,5);
print(tail);
DeleteNode(tail,4);
print(tail);
DeleteNode(tail,5);
print(tail);
}