-
Notifications
You must be signed in to change notification settings - Fork 22
/
insertation_any.c
66 lines (65 loc) · 1.34 KB
/
insertation_any.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
#include <stdio.h>
#include <stdlib.h>
void main()
{
struct node
{
int data;
struct node *next;
};
struct node *head, *temp, *newnode;
head = 0;
int choice = 1, count = 0;
while (choice)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("enter data\n");
scanf("%d", &newnode->data);
newnode->next = 0;
if (head == 0)
{
head = temp = newnode;
}
else
{
temp->next = newnode;
temp = newnode;
}
printf("wanna continue(0,1)?\n");
scanf("%d", &choice);
}
temp = head;
while (temp != 0)
{
count++;
temp = temp->next;
}
// at any pos
int pos,i=1;
printf("enter the pos");
scanf("%d",&pos);
if (pos>count)
{
printf("invalid");
}
else
{
temp=head;
while(i<pos-1)
{
temp=temp->next;
i++;
}
newnode = (struct node *)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=temp->next;
temp->next=newnode;
}
temp=head;
while(temp!=0)
{
printf("%d ",temp->data);
temp=temp->next;
}
}