-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllrandominsert.cpp
More file actions
77 lines (71 loc) · 1.25 KB
/
Copy pathllrandominsert.cpp
File metadata and controls
77 lines (71 loc) · 1.25 KB
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 randominsert(int);
void create(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
int main()
{
int choice,item,loc;
do
{
printf("\n Enter the item which you want to insert ? \n");
scanf ("%d", &item);
if(head == NULL)
{
create(item);
}
else {
randominsert (item);
}
printf ("\n Press 0 to insert more ? \n");
scanf("%d", &choice);
}
while (choice == 0);
}
void create (int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if (ptr == NULL)
{
printf (" \n OVERFLOW \n");
}
else {
ptr -> data = item;
ptr -> next = head;
head = ptr;
printf ("\n Node Inserted \n");
}
}
void randominsert (int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
struct node *temp;
int i, loc;
if (ptr==NULL)
{
printf ("\n OVERFLOW");
} else
{
printf ("Enter the location");
scanf("%d", &loc);
ptr -> data=item;
temp = head;
for (i=0; i<loc; i++)
{
temp = temp->next;
if (temp == NULL)
{
printf("\n can't insert \n");
return;
}
}
ptr -> next = temp -> next;
temp -> next = ptr;
printf("\n Node inserted");
}
}