-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprac.cpp
More file actions
70 lines (63 loc) · 1.3 KB
/
prac.cpp
File metadata and controls
70 lines (63 loc) · 1.3 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
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int data1,Node* next1){
data=data1;
next=next1;
}
Node(int data1){
data=data1;
next=nullptr;
}
};
Node* deletevalue(Node* head,int value1){
if(head==nullptr)return head;
if(head->data==value1){
Node* temp=head;
head=head->next;
free(temp);
return head;
}
Node* temp=head;
while(temp->next!=nullptr){
if(temp->next->data==value1){
Node* tobedeleted=temp->next;
temp->next=temp->next->next;
free(tobedeleted);
break;
}
temp=temp->next;
}
return head;
}
int main(){
cout<<"enetr the length";
int n;
cin>>n;
Node* head=nullptr,*temp=nullptr,*newnode=nullptr;
for(int i=0;i<n;i++){
int value;
cin>>value;
newnode=new Node(value);
if(head==nullptr){
head=newnode;
temp=head;
}
else{
temp->next=newnode;
temp=newnode;
}
}
int value1;
cout<<"enetr the value you want to be deleted";
cin>>value1;
head=deletevalue(head,value1);
temp=head;
while(temp!=nullptr){
cout<<temp->data<<" ";
temp=temp->next;
}
}