-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist_002.cpp
More file actions
53 lines (50 loc) · 1.05 KB
/
linkedlist_002.cpp
File metadata and controls
53 lines (50 loc) · 1.05 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
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node* next;
};
void insertNode(struct node* head,int d){
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=d;
temp->next=NULL;
struct node* ptr=head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=temp;
}
void deleteNode(struct node* head){
struct node *ptr1=head;
struct node *ptr2=head;
while(ptr1->next!=NULL){
ptr2=ptr1;
ptr1=ptr1->next;
}
ptr2->next=NULL;
}
void display(struct node* head){
cout<<"Linked list:";
while(head!=NULL){
cout<<head->data<<" -> ";
head=head->next;
}
cout<<" Null "<<endl;
}
int main()
{
struct node *head=(struct node*)malloc(sizeof(struct node));
head->data=12;
head->next=NULL;
insertNode(head,14);
insertNode(head,24);
insertNode(head,34);
insertNode(head,44);
display(head);
insertNode(head,46);
display(head);
deleteNode(head);
display(head);
return 0;
}