You are on page 1of 2

8/22/23, 1:29 AM insertionatend.

insertionatend.c

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 struct node {
5 int data;
6 struct node *next;
7 };
8
9 void main() {
10 struct node *head;
11 struct node *newnode;
12 struct node *temp;
13 head = NULL;
14
15 int choice = 1, count = 0;
16
17 while (choice) {
18 newnode = (struct node *)malloc(sizeof(struct node));
19 printf("Enter Data:\n");
20 scanf("%d", &newnode->data);
21 newnode->next = NULL;
22
23 if (head == NULL) {
24 head = temp = newnode;
25 } else {
26 temp->next = newnode;
27 temp = newnode;
28 }
29
30 printf("Do you want to continue (0, 1)?\n");
31 scanf("%d", &choice);
32 }
33
34 printf("--------------------------------\n");
35
36 temp = head;
37 while (temp != NULL) {
38 printf("\n%d\n", temp->data);
39 temp = temp->next;
40 count++;
41 }
42 printf("\nCount=%d \n", count);
43
44 struct node *endnode;
45 endnode=(struct node*)malloc(sizeof(struct node));
46
47 printf("Enter the element: ");
48
49 //data
50 scanf("%d",&endnode->data);
51
52 //right link
53 endnode->next=NULL;
54
55 //left link
56 temp=head;

localhost:64626/1dc58eb2-1b16-488e-8083-3576f48234f5/ 1/2
8/22/23, 1:29 AM insertionatend.c

57 while(temp->next!=0)
58 {
59 temp=temp->next;
60 }
61 temp->next=endnode;
62
63 //print
64 temp = head;
65 count=0;
66 while (temp != NULL) {
67 printf("\n%d\n", temp->data);
68 temp = temp->next;
69 count++;
70 }
71 printf("\nCount=%d \n", count);
72 }

localhost:64626/1dc58eb2-1b16-488e-8083-3576f48234f5/ 2/2

You might also like