-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion.c
More file actions
111 lines (106 loc) · 2.63 KB
/
Insertion.c
File metadata and controls
111 lines (106 loc) · 2.63 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
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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
struct node* create_node(){
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->next=NULL;
return newnode;
}
void push(int data){
struct node *newnode=create_node();
newnode->data=data;
if(head==NULL){
head=newnode;
return;
}
else{
newnode->next=head;
head=newnode;
return;
}
}
void insertend(int data){
struct node *newnode=create_node();
newnode->data=data;
struct node *temp=head;
if(head==NULL){
head=newnode;
return;
}
else{
while(temp->next != NULL){
temp = temp->next;
}
temp->next=newnode;
return;
}
}
void insertn(int data, int n){
struct node *newnode=create_node();
newnode->data=data;
struct node *temp=head;
if(head==NULL){
head=newnode;
return;
}
else{
for(int i=1;i<n-1;i++){
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
return;
}
}
void display(){
struct node *temp=head;
while(temp != NULL){
printf("%d\t",temp->data);
temp = temp->next;
}
}
int main(){
int ch,n,ele;
while(1)
{
printf("\nEnter Options\n");
printf("1-Inserting at start\n");
printf("2-Inserting at the end\n");
printf("3-Inserting at specific position\n");
printf("4-Display the list\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the element to be inserted at the head\n");
scanf("%d",&ele);
push(ele);
printf("Successfully inserted at the head\n");
break;
case 2:
printf("Enter the element to be inserted at the end\n");
scanf("%d",&ele);
insertend(ele);
printf("Successfully inserted at the end\n");
break;
case 3:
printf("Enter the position in which element to be inserted\n");
scanf("%d",&n);
printf("Enter the element to be inserted at %d :",n);
scanf("%d",&ele);
insertn(ele,n);
printf("Successfully inserted at the head\n");
break;
case 4:
printf("Elements in the list are \n");
display();
break;
default:
exit(0);
}
}
return 0;
}