-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_any_position.c
More file actions
99 lines (99 loc) · 1.57 KB
/
insert_any_position.c
File metadata and controls
99 lines (99 loc) · 1.57 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node* next;
};
typedef struct node * nodep;
nodep create();
nodep add();
void display(nodep);
nodep ins_any_pos(nodep);
int main()
{
nodep start=NULL,t;
start=add();
t=ins_any_pos(start);
display(t);
getch();
return 0;
}
nodep create()
{
nodep t=(nodep)malloc(sizeof(struct node));
if(t==NULL)
{
printf("overflow");
exit(0);
}
t->next=NULL;
printf("Enter data\n");
scanf("%d",&t->info);
return t;
}
nodep add()
{
char ch;
nodep start=NULL,t,t1;
printf("Creation of list\n");
do{
t=create();
if(start==NULL)
start=t;
else
t1->next=t;
t1=t;
printf("Do you want to continue(y/Y):");
scanf(" %c",&ch);
}while(ch=='y' || ch=='Y');
return start;
}
nodep ins_any_pos(nodep start)
{
int n,count=2;
nodep temp,temp1;
printf("Enter data to insert into list\n");
temp1=create();
printf("Enter position where you want to Insert\n");
scanf("%d",&n);
temp=start;
if(n==1)
{
start=NULL;
free(temp);
}
else if(n>1)
{
while(temp->next!=NULL && count<n)
{
temp=temp->next;
count++;
}
if(n==count)
{
temp1->next=temp->next;
temp->next=temp1;
}
else
printf("position is greate than size\n");
}
return start;
}
void display(nodep start)
{
nodep t;
printf("List is: ");
if(start==NULL)
{
printf("list is empty\n");
exit(0);
}
t=start;
while(t!=NULL)
{
printf("%d\t",t->info);
t=t->next;
}
}