-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4.c
117 lines (85 loc) · 2.05 KB
/
p4.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
typedef struct node node;
void create(node *,int);
void display(node *);
struct node *insert_begin(node *,int);
struct node *insert_middle(node *,int);
int main()
{
struct node *head;
head=(struct node *)malloc(sizeof(struct node));
create(head,5);
head=insert_middle(head,45);
display(head);
return 0;
}
void create(struct node * l,int n)
{
int i;
printf("enter value of 1 node ");
scanf("%d",&l->data);
for(i=0;i<n-1;i++)
{
l->next=(struct node *)malloc(sizeof(struct node));
l=l->next;
printf("enter value for %d node",i+2);
scanf("%d",&l->data);
l->next=NULL;
}
}
struct node *insert_begin(struct node *l,int data)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=l;
l=temp;
return l;
}
struct node *insert_middle(struct node *c,int d)
{
struct node *temp,*l;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=d;
l=c;
int count=1,len=5;
while(count<=len/2)
{
l=l->next;
count++;
}
temp->next=l->next;
l->next=temp;
return c;
}
struct node *insert_pos(struct node *c,int n)
{
struct node *temp,*l,*prev;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=45;
l=c;
int count=1,len=6;
while(count<k)
{
prev=l;
l=l->next;
count++;
}
prev->next=temp;
temp->next=l;
return c;
}
void display(struct node * l)
{
while(l!=NULL)
{
printf("%d->",l->data);
l=l->next;
}
printf("NULL\n");
}