forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteatN2.c
67 lines (64 loc) · 1.1 KB
/
DeleteatN2.c
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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head;
struct Node* end;
void insertatend(int data)
{
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp->data=data;
temp->next=NULL;
if(head==NULL)
{
head=temp;
end=temp;
return;
}
end->next=temp;
end=temp;
}
void Delete(int n)
{
struct Node* temp1 = head;
if(n==1){
head = temp1->next;
free(temp1);
return;
}
int i;
for(i=0;i<n-2;i++)
{
temp1 = temp1->next;
}
struct Node* temp2 = temp1->next;
temp1->next = temp2->next;
free(temp2);
}
void Print()
{
struct Node* temp = head;
printf("List is:");
while(temp != NULL)
{
printf(" %d",temp->data);
temp= temp->next;
}
printf("\n");
}
int main(){
head = NULL;
end = NULL;
insertatend(4);
insertatend(2);
insertatend(3);
insertatend(5);
Print();
int n;
printf("Enter a position\n");
scanf("%d",&n);
Delete(n);
Print();
}