-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsert at a Position.txt
81 lines (78 loc) · 1.33 KB
/
Insert at a Position.txt
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
//insert node at position
#include<stdio.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void create()
{
char ch;
do
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
printf("Make the entry in the Linked List\n");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("Make another entry:\n");
ch=getche();
}while(ch!='n');
}
void insert_pos()
{
int pos,i;
struct node *temp,*new_node;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter the node to be inserted\n");
scanf("%d",&temp->data);
printf("Enter the position to insert the position\n");
scanf("%d",&pos);
new_node=start;
for(i=1;i<pos-1;i++)
{
if(new_node->next==NULL)
{
printf("it has less elements");
return;
}
new_node=new_node->next;
}
temp->next=new_node->next;
new_node->next=temp;
return 0;
}
void display()
{
struct node *new_node;
new_node=start;
while(new_node!=NULL)
{
if(start==NULL)
{
printf("\nNULL");
return;
}
else
{
printf("%d---->",new_node->data);
new_node=new_node->next;
}
}
}
void main()
{
create();
insert_pos();
display();
}