You are on page 1of 2

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

insertionbeg.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 *begnode;
45 begnode=(struct node*)malloc(sizeof(struct node));
46
47 printf("Enter the element: ");
48
49 //data
50 scanf("%d",&begnode->data);
51
52 //right link
53 begnode->next=head;
54
55 //left link
56 head=begnode;

localhost:64626/8c6eef25-9c33-479c-9cd4-b22d2fc49c2b/ 1/2
8/22/23, 1:29 AM insertionbeg.c

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

localhost:64626/8c6eef25-9c33-479c-9cd4-b22d2fc49c2b/ 2/2

You might also like