-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion of linked list.cpp
67 lines (67 loc) · 942 Bytes
/
insertion of linked list.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
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *next;
};
void add_node(node ** head,int n)//inserts at the front
{
node *temp=new node;
temp->data=n;
temp->next=*head;
*head=temp;
}
void add(node ** head,int n)
{
node *temp=new node();
node *l=*head;
temp->data=n;
temp->next=NULL;
if(*head==NULL)
{
*head=temp;
}
else
{
while(l->next!=NULL)
{
l=l->next;
}
l->next=temp;
}
}
void display(node ** k)
{
while((*k)!=NULL)
{
cout<<(*k)->data<<" ";
(*k)=(*k)->next;
}
}
void display2(node * k)//another way to display(pass only head)
{
while((k)!=NULL)
{
cout<<(k)->data<<" ";
(k)=(k)->next;
}
}
int main()
{
int n,i,x;
node *head=NULL;
cin>>n;
for(i=0;i<n;i++)
{
cin>>x;
add(&head, x);
}
display(&head);
int y;
cin>>y;
//del(&head,y);
display(&head);
//display2(head);(another way)
}