-
Notifications
You must be signed in to change notification settings - Fork 163
/
04_insertion_end.cpp
77 lines (54 loc) · 1.29 KB
/
04_insertion_end.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
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<stdlib.h>
void trevers(struct node *ptr);
struct node* insertionAtEnd(struct node*head,int data);
struct node{
int data;
struct node *next;
};
int main()
{
struct node *head;
struct node *second;
struct node *third;
struct node *fourth;
head=(struct node*)malloc(sizeof(struct node));
second=(struct node*)malloc(sizeof(struct node));
third=(struct node*)malloc(sizeof(struct node));
fourth=(struct node*)malloc(sizeof(struct node));
head->data=1;
head->next=second;
second->data=2;
second->next=third;
third->data=3;
third->next=fourth;
fourth->data=4;
fourth->next=NULL;
trevers(head);
printf("then");
head=insertionAtEnd(head,67);
trevers(head);
}
struct node* insertionAtEnd(struct node*p,int data)
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct node));
ptr->data=data;
while(p!=0)
{
if(p->next=NULL)
{
p->next=ptr;
ptr->next=NULL;
}
p=p->next;
}return p;
}
void trevers(struct node *ptr)
{
while (ptr!=0)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}