File tree Expand file tree Collapse file tree 1 file changed +118
-0
lines changed Expand file tree Collapse file tree 1 file changed +118
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ /* Link list node */
5
+ struct Node {
6
+ int data;
7
+ struct Node *next;
8
+ Node (int x) {
9
+ data = x;
10
+ next = NULL ;
11
+ }
12
+ }*head;
13
+
14
+ Node *findNode (Node* head, int search_for)
15
+ {
16
+ Node* current = head;
17
+ while (current != NULL )
18
+ {
19
+ if (current->data == search_for)
20
+ break ;
21
+ current = current->next ;
22
+ }
23
+ return current;
24
+ }
25
+
26
+
27
+ void insert ()
28
+ {
29
+ int n,i,value;
30
+ Node *temp;
31
+ scanf (" %d" ,&n);
32
+
33
+ for (i=0 ; i<n; i++)
34
+ {
35
+ scanf (" %d" ,&value);
36
+ if (i==0 )
37
+ {
38
+ head=new Node (value);
39
+ temp=head;
40
+ continue ;
41
+ }
42
+ else
43
+ {
44
+ temp->next = new Node (value);
45
+ temp=temp->next ;
46
+ temp->next =NULL ;
47
+ }
48
+ }
49
+ }
50
+
51
+ /* Function to print linked list */
52
+ void printList (Node *node)
53
+ {
54
+ while (node != NULL )
55
+ {
56
+ printf (" %d " , node->data );
57
+ node = node->next ;
58
+ }
59
+ cout << endl;
60
+ }
61
+
62
+
63
+ void deleteNode (Node *node_ptr);
64
+
65
+ /* Drier program to test above function*/
66
+ int main (void )
67
+ {
68
+ /* Start with the empty list */
69
+
70
+ int t,k,n,value;
71
+
72
+ scanf (" %d" ,&t);
73
+ while (t--)
74
+ {
75
+ insert ();
76
+ scanf (" %d" ,&k);
77
+ Node *del = findNode (head, k);
78
+ if (del != NULL && del->next != NULL )
79
+ {
80
+ deleteNode (del);
81
+ }
82
+ printList (head);
83
+ }
84
+ return (0 );
85
+ }
86
+
87
+
88
+ // } Driver Code Ends
89
+
90
+
91
+ /*
92
+ struct Node {
93
+ int data;
94
+ struct Node *next;
95
+ Node(int x) {
96
+ data = x;
97
+ next = NULL;
98
+ }
99
+ }*head;
100
+ */
101
+
102
+ // This function should delete node from linked list. The function
103
+ // may assume that node exists in linked list and is not last node
104
+ // node: reference to the node which is to be deleted
105
+ void deleteNode (Node *node)
106
+ {
107
+ // Your code here
108
+ Node *cur=node;
109
+ if (cur->next ==NULL )
110
+ {
111
+ free (cur);
112
+ return ;
113
+ }
114
+ cur->data =cur->next ->data ;
115
+ Node *temp=cur->next ;
116
+ cur->next =cur->next ->next ;
117
+ free (temp);
118
+ }
You can’t perform that action at this time.
0 commit comments