-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist7.c
74 lines (60 loc) · 1.22 KB
/
linkedlist7.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
68
69
70
71
72
73
74
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node*next;
};
struct Node*head,*temp;
void printlist(struct Node*n)
{
while(n!=NULL){
printf("%d \t",n->data);
n=n->next;
}
}
void main()
{
struct Node*second=NULL;
struct Node*third=NULL;
struct Node*previous;
int n,i=1,count=1,j=1;
head=(struct Node*)malloc(sizeof(struct Node));
second=(struct Node*)malloc(sizeof(struct Node));
third=(struct Node*)malloc(sizeof(struct Node));
head->data=98;
head->next=second;
second->data=99;
second->next=third;
third->data=100;
third->next=NULL;
printf("linked list before deleting the last node:\n");
printlist(head);
printf("\n");
printf("Enter the position of the node to delete:");
scanf("%d",&n);
temp=head;
while(temp->next!=NULL){
count =count+1;
temp=temp->next;
}
if(n>count){
printf("Enter position is not valid");
}
else{
temp=head;
while(i<n){
temp=temp->next;
i++;
}
previous=head;
while(j<n-1){
previous=previous->next;
j++;
}
previous->next=temp->next;
temp->next=NULL;
printf("Linked list after deletion is:\n");
printlist(head);
}
}