forked from bamsarts/DS-ALGO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete_without_headPointer.cpp
101 lines (84 loc) · 1.77 KB
/
Delete_without_headPointer.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
#include<bits/stdc++.h>
using namespace std;
/* Link list node */
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
}*head;
Node *findNode(Node* head, int search_for)
{
Node* current = head;
while (current != NULL)
{
if (current->data == search_for)
break;
current = current->next;
}
return current;
}
void insert()
{
int n,i,value;
Node *temp;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d",&value);
if(i==0)
{
head=new Node(value);
temp=head;
continue;
}
else
{
temp->next= new Node(value);
temp=temp->next;
temp->next=NULL;
}
}
}
/* Function to print linked list */
void printList(Node *node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
cout << endl;
}
void deleteNode(Node *node_ptr);
int main(void)
{
/* Start with the empty list */
int t,k,n,value;
scanf("%d",&t);
while(t--)
{
insert();
scanf("%d",&k);
Node *del = findNode(head, k);
if (del != NULL && del->next != NULL)
{
deleteNode(del);
}
printList(head);
}
return(0);
}
// This function should delete node from linked list. The function
// may assume that node exists in linked list and is not last node
// node: reference to the node which is to be deleted
void deleteNode(Node *node)
{
// Your code here
Node *temp = node->next;
node->data = node->next->data;
node->next = temp->next;
delete temp;
}