-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10.c
154 lines (141 loc) · 3.09 KB
/
10.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//insertion to a doubly linked list
#include<stdio.h>
#include<stdlib.h>
typedef struct node1
{
int data;
struct node1 *rlink,*llink;
}node;
void main()
{
node *ptr,*header,*temp,*new;
header=(node*)malloc(sizeof(node));
header->data=0;
header->rlink=NULL;
int i,k,n,num,c,a;
char y;
printf("Enter no.of elements : ");
scanf("%d",&n);
ptr=(node*)malloc(sizeof(node));
ptr=header;
for(i=0;i<n;i++)
{
temp=(node*)malloc(sizeof(node));
printf("Enter : ");
scanf("%d",&num);
temp->data=num;
temp->rlink=NULL;
ptr->rlink=temp;
temp->llink=ptr;
ptr=ptr->rlink;
}
printf("Created List : ");
ptr=header->rlink;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->rlink;
}
printf("INSERTION \n\n1.At Beginning \n2.Anywhere \n3.At end \n");
do
{
printf("Enter choice(1/2/3): ");
scanf("%d",&c);
switch(c)
{
case 1:ptr=header->rlink;
new=(node *)malloc(sizeof(node));
if(new==NULL)
{
printf("memory not allocated.");
}
else
{
printf("Enter element to insert : ");
scanf("%d",&a);
new->data=a;
new->llink=header;
new->rlink=ptr;
if(ptr!=NULL)
{
ptr->llink=new;
}
header->rlink=new;
}
break;
case 2:ptr=header->rlink;
node *ptr1;
ptr1=(node *)malloc(sizeof(node));
new=(node *)malloc(sizeof(node));
if(new==NULL)
{
printf("memory not allocated.");
}
else
{
printf("Enter key and element to insert : ");
scanf("%d%d",&k,&a);
if(ptr==NULL)
{
printf("List Empty.");
}
else
{
while(ptr!=NULL && ptr->data!=k )
{
ptr=ptr->rlink;
}
if(ptr==NULL)
{
printf("Key not found.");
}
else
{
new->data=a;
ptr1=ptr->rlink;
new->llink=ptr;
new->rlink=ptr1;
if(ptr1!=NULL)
{
ptr1->llink=new;
}
ptr->rlink=new;
}
}
}
break;
case 3: //insert at end
new=(node *)malloc(sizeof(node));
if(new==NULL)
{
printf("memory not allocated.");
}
else
{
ptr=header->rlink;
while(ptr!=NULL)
{
temp=ptr;
ptr=ptr->rlink;
}
printf("Enter element to insert : ");
scanf("%d",&a);
new->data=a;
temp->rlink=new;
new->rlink=NULL;
new->llink=temp;
}
break;
}
printf("\nDo you want to continue(y/n): ");
scanf(" %c",&y);
}
while(y=='y');
printf("New list : \n");
ptr=header->rlink;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->rlink;
}
}