-
Notifications
You must be signed in to change notification settings - Fork 83
/
.c
98 lines (81 loc) · 1.88 KB
/
.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head, *temp, *newnode;
void input_node(struct node *head, int size)
{
printf("enter the elements of the node\n");
scanf("%d",&head->data);
head -> next = NULL;
temp = head;
for(int i=1; i<size; i++)
{
newnode = (struct node *)malloc(sizeof(struct node *));
scanf("%d", &newnode->data);
newnode->next = NULL;
temp->next = newnode;
temp = temp->next;
}
}
void display (struct node *head, int size)
{
temp = head;
for(int i=0; i<size; i++)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void insert_at_beginning(struct node *head)
{
struct node *freshnode;
freshnode = (struct node *)malloc(sizeof(struct node *));
if(freshnode == NULL)
printf("memory not allocated\n");
else
{
printf("enter data you want to input\n");
scanf("%d",&freshnode->data);
freshnode->next = head;
head = freshnode;
}
temp = head;
while(temp!= NULL)
{
printf("%d ->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
int size, option, cont = 1;
head = (struct node *)malloc(sizeof(struct node *));
while(cont != 0)
{
printf("choose between the two options\n 1. for creation of node\n 2. for display of node\n 3. for inserting at beginning\n");
scanf("%d",&option);
switch(option)
{
case 1:
printf("enter the size of the node\n");
scanf("%d", &size);
input_node(head, size);
break;
case 2:
display(head, size);
break;
case 3:
insert_at_beginning(head);
break;
}
printf("1 to contiue and 0 to exit\n");
scanf("%d",&cont);
}
return 0;
}