Repository navigation
Expand file tree
/
Copy pathinsertnode.cpp
More file actions
121 lines (100 loc) · 1.87 KB
/
Copy pathinsertnode.cpp
File metadata and controls
121 lines (100 loc) · 1.87 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
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
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *end;
void begin(struct node **head,int data){
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
if(*head==NULL){
*head=newnode;
return;
}
newnode->next=*head;
*head=newnode;
return;
}
void append(struct node **head,int data){
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
if(*head==NULL){
*head=newnode;
end=newnode;
return;
}
end->next=newnode;
end=end->next;
return;
}
void display(struct node *head){
struct node *temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
void deletenode(struct node **head,int data){
struct node *temp=*head;
if(*head==NULL)
return;
if(temp->data==data){
struct node *acg=temp;
temp=temp->next;
*head=temp;
free(acg);
return;
}
while(temp->next!=NULL && temp->next->data!=data)
temp=temp->next;
if(temp==NULL)
return;
else{
struct node *acd=temp->next;
temp->next=temp->next->next;
free(acd);
}
//return;
}
void length(struct node **head){
int lngth=0;
struct node *temp=*head;
while(temp!=NULL){
lngth++;
temp=temp->next;
}
cout<<lngth<<endl;
}
int searchnode(struct node **head,int d){
struct node *temp=*head;
while(temp!=NULL && temp->data!=d){
temp=temp->next;
}
if(temp==NULL)
return false;
else
return true;
}
int main(){
struct node *head=NULL;
int n;
cin>>n;
for(int i=0;i<n;i++){
int d;
cin>>d;
append(&head,d);
}
display(head);
length(&head);
// deletenode(&head,1);
//display(head);
if(searchnode(&head,6))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
return 0;
}