-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_list_nth_position_DS.txt
112 lines (90 loc) · 1.71 KB
/
Linked_list_nth_position_DS.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
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
#include<stdio.h>
#include<malloc.h>
typedef struct list
{
int data;
struct list *next;
} list;
void nth_position(list **head)
{
int i,n;
list *node=(list *)malloc(sizeof(list));
printf("Enter data : ");
scanf("%d",&node->data);
printf("Enter position : ");
scanf("%d",&n);
if(n==1)
{
node->next=*head;
*head=node;
}
else
{
list *p=*head;
for(i=1; i<n-1; i++)
{
p=p->next;
}
node->next=p->next;
p->next=node;
}
}
void insert_end(list **head,int value)
{
list *node=(list *)malloc(sizeof(list));
list *p;
node->data=value;
node->next=NULL;
if(*head==NULL)
{
*head=node;
}
else
{
p=*head;
while(p->next !=NULL)
{
p=p->next;
}
p->next=node;
}
}
void print(list *head)
{
if(head==NULL)
{
printf("List is Empty");
}
else
{
while(head !=NULL)
{
printf("%d -> ",head->data);
head=head->next;
}
printf("NULL\n");
}
}
int main()
{
list *head1=NULL;
list *head2=NULL;
int i,n,value;
char ch;
printf("Enter total number : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d",&value);
insert_end(&head1,value);
}
print(head1);
do
{
nth_position(&head1);
print(head1);
printf("Do you want to insert another node or if you want quit the program then Enter 'e': ");
ch=getche();
}while(ch !='e');
return 0;
}