-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly.c
More file actions
162 lines (111 loc) · 2.72 KB
/
singly.c
File metadata and controls
162 lines (111 loc) · 2.72 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
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
154
155
156
157
158
159
160
161
162
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void insertAtEND(int data, struct Node **head){
struct Node *newnode = malloc(sizeof(struct Node));
newnode->data = data;
newnode->next = NULL;
// Case 1: If the list is empty, make the new node the head
if (*head == NULL) {
*head = newnode;
return;
}
//Travesrse
struct Node *temp = *head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newnode;
}
void insertAtStart(int data,struct Node **head){
struct Node *newnode = malloc(sizeof(struct Node));
newnode->data = data;
newnode->next = NULL;
if (*head == NULL) {
*head = newnode;
return;
}
newnode->next = *head;
*head = newnode;
}
void insertAtPosition(int data, struct Node **head, int index){
struct Node *newnode = malloc(sizeof(struct Node));
newnode->data = data;
newnode->next = NULL;
// Case 1: If the list is empty, make the new node the head
// if (*head == NULL) {
// *head = newnode;
// return;
// }
if (index == 0) {
newnode->next = *head;
*head = newnode;
return;
}
int counter=0;
struct Node *temp = *head;
while(temp->next != NULL && counter <index-1){
counter++;
temp = temp->next;
}
newnode->next = temp->next;
temp->next = newnode;
}
void display(struct Node *head){
struct Node *temp = head;
while(temp!= NULL){
printf("%d -> ",temp->data);
temp = temp->next;
}
}
void deleteAtFront(struct Node **head){
if (*head == NULL) return;
struct Node *old = *head;
*head = (*head)->next;
free(old);
}
void deleteAtEnd(struct Node **head){
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node *temp = *head;
while(temp->next->next != NULL){
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
void deleteAtIndex(struct Node **head, int index){
if (*head == NULL) return; // 1. Empty check
int counter = 0;
// Special Case: Deleting the head
if (index == 0) {
deleteAtFront(head);
return;
}
struct Node *temp = *head;
while(temp->next !=NULL && counter < index-1){
temp = temp->next;
counter++;
}
struct Node *old = temp->next;
temp->next = old->next;
free(old);
}
int main(){
struct Node *head = NULL;
insertAtEND(20,&head);
insertAtEND(30,&head);
insertAtEND(40,&head);
insertAtStart(35,&head);
insertAtPosition(25,&head,2);
display(head);
return 0;
}