-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_ReverseDeletionCircularLL.cpp
165 lines (135 loc) · 2.87 KB
/
GFG_ReverseDeletionCircularLL.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
https://practice.geeksforgeeks.org/problems/deletion-and-reverse-in-linked-list/1#
Deletion and Reverse in Linked List
*/
// { Driver Code Starts
// C program to delete a given key from
// linked list.
#include<bits/stdc++.h>
using namespace std;
/* structure for a node */
struct Node
{
int data;
struct Node *next;
Node(int x){
data = x;
next = NULL;
}
};
Node *pre=NULL;
/* Function to delete a given node from the list */
//void deleteNode(struct Node *head, int key);
void deleteNode(Node **head,int data);
void reverse(Node **head);
/* Function to reverse the linked list */
//static void reverse(struct Node** head_ref);
/* Function to insert a node at the beginning of
a Circular linked list */
void push(struct Node **head_ref, int data)
{
// Create a new node and make head as next
// of it.
struct Node *curr = new Node(data);
curr->next=curr;
if(*head_ref==NULL)
{
*head_ref=curr;
curr->next=*head_ref;
pre=*head_ref;
}
else
{
curr->next=*head_ref;
pre->next=curr;
pre=pre->next;
}
}
/* Function to print nodes in a given
circular linked list */
void printList(struct Node *head)
{
struct Node *temp = head;
if (head != NULL)
{
do
{
printf("%d ", temp->data);
temp = temp->next;
}
while (temp != head);
}
else
cout<<"empty"<<endl;
printf("\n");
}
/* Driver program to test above functions */
int main()
{
/* Initialize lists as empty */
int t,a,delnode;
cin>>t;
while(t--)
{
pre=NULL;
Node *head=NULL;
int n;
cin>>n;
while(n--)
{
cin>>a;
push(&head,a);
}
/* Created linked list will be 2->5->7->8->10 */
cin>>delnode;
deleteNode(&head, delnode);
reverse(&head);
printList(head);
}
return 0;
}
// } Driver Code Ends
/* structure for a node
struct Node
{
int data;
struct Node *next;
};
*/
/* Function to delete a given node from the list */
void deleteNode(struct Node **head, int key)
{
// struct Node *ptr = (*head)->next, *prev=*head;
// while(ptr->data != key && ptr->next != *head)
// {
// prev = ptr;
// ptr = ptr->next;
// }
// if(ptr->data == key)
// {
// prev->next = ptr->next;
// }
struct Node *ptr = (*head);
while(ptr->next != *head)
{
if(ptr->next->data == key)
ptr->next = ptr->next->next;
ptr = ptr->next;
}
}
/* Function to reverse the linked list */
void reverse(struct Node** head_ref)
{
struct Node* prev = nullptr, *cnxt = nullptr;
struct Node* ptr = *head_ref;
while(ptr->next != *head_ref)
{
cnxt = ptr->next;
ptr->next = prev;
prev = ptr;
ptr = cnxt;
}
ptr->next = prev;
(*head_ref)->next = ptr;
(*head_ref) = ptr;
}