-
Notifications
You must be signed in to change notification settings - Fork 14
/
singly insertion at end
105 lines (93 loc) · 2.19 KB
/
singly insertion at end
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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void CreateNode(struct node **p, int value)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
ptr->data = value;
ptr->next = *p;
*p = ptr;
}
void Disp(struct node *p)
{
struct node *temp = p;
while (temp != NULL)
{
printf("Element is : %d\n", temp->data);
temp = temp->next;
}
printf("NULL");
}
void InsertAtEnd(struct node **head, int value)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
ptr->data = value;
struct node *p = *head;
while (p->next != NULL)
{
p = p->next;
}
p->next = ptr;
ptr->next = NULL;
}
void InsertAtIndex(struct node **head, int value, int index)
{
int i = 0;
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
struct node *p = *head;
while (i != index - 1)
{
p = p->next;
i++;
}
ptr->data = value;
ptr->next = p->next;
p->next = ptr;
}
int main()
{
int i = 0, choice, value, index;
struct node *head = NULL;
CreateNode(&head, 12);
CreateNode(&head, 13);
CreateNode(&head, 14);
printf("Enter 1.Create a linked list \n2.To create a node \n3.To insert node at the end \n4.Insert node at index \n5.To display linked list \n6.To execute program : ");
do
{
printf("\nEnter the choice : ");
scanf("%d", &choice);
switch (choice)
{
case 2:
printf("Enter the data : ");
scanf("%d", &value);
CreateNode(&head, value);
break;
case 3:
printf("Enter the data : ");
scanf("%d", &value);
InsertAtEnd(&head, value);
break;
case 4:
printf("Enter the data : ");
scanf("%d", &value);
printf("Enter the index : ");
scanf("%d", &index);
InsertAtIndex(&head, value, index);
break;
case 5:
Disp(head);
break;
case 6:
exit(0);
break;
default:
break;
}
} while (choice != 6);
return 0;
}