-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlistdeletion.cpp
More file actions
63 lines (56 loc) · 931 Bytes
/
Copy pathlinkedlistdeletion.cpp
File metadata and controls
63 lines (56 loc) · 931 Bytes
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
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
void push(struct node** head_ref,int data)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=*head_ref;
*head_ref=newnode;
return;
}
void deletenode(struct node** head_ref,int key)
{
struct node* temp=*head_ref,*prev;
while(temp!=NULL&&temp->data==key)
{
*head_ref=temp->next;
free(temp);
return;
}
while(temp!=NULL&&temp->data!=key)
{
prev=temp;
temp=temp->next;
}
if(temp==NULL)
return;
prev->next=temp->next;
free(temp);
return;
}
void printlist(struct node* noe)
{
while(noe!=NULL)
{
cout<<noe->data<<endl;
noe=noe->next;
}
}
int main() {
// your code goes here
struct node* head=NULL;
push(&head,8);
push(&head,1);
push(&head,9);
push(&head,0);
printlist(head);
deletenode(&head,1);
deletenode(&head,8);
printlist(head);
return 0;
}