forked from daction/Basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLL_insert_nth_position2_withMod_testcase.c
More file actions
70 lines (54 loc) · 1.25 KB
/
LL_insert_nth_position2_withMod_testcase.c
File metadata and controls
70 lines (54 loc) · 1.25 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
//Mar 22, 2015 - Pupil
//Linked List: Insert in nth Position
#include <stdio.h>
#include <stdlib.h>
struct Node* head;
struct Node{
int data;
struct Node* next;
};
void Insert(int data, int n){// n is position
struct Node* temp1 = (struct Node*) malloc(sizeof(struct Node*));
temp1->data = data;
temp1->next = NULL;
if(n == 1){
temp1->next = head;
head = temp1;
return;
}
struct Node* temp2 = head;
int i;
for(i=0; i<n-2; i++){
temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp1;
}
void Print(){
struct Node* temp = head;
while(temp != NULL){
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
//Modified test case
//By using root and ask user to insert
//#scanf, &(pointer), for loop
head = NULL;
int data, position, num, i;
printf("How many number you want to Insert? ");
scanf("%d", &num);
//int i;
for(i=0; i<num; i++){
printf("The number want to Insert is \n");
scanf("%d", &data);
printf("The position of number want to Insert is \n");
scanf("%d", &position);
Insert(data, position);
Print();
printf("\n");
}
}