-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletion in linked list.c
71 lines (70 loc) · 1.47 KB
/
deletion in linked list.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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void traverse(struct node* head){
struct node* current=head;
int count=0;
printf("\n traversing the list\n");
while(current!=NULL)
{
count++;
printf("%d ",current->data);
current=current->next;
}
printf("\ntotal no of nodes : %d\n",count);
}
struct node* creatnode(int d){
struct node* temp=malloc(sizeof(struct node));
temp->data=d;
temp->next=NULL;
return temp;
}
int main(){
printf("Insertion elements in linked list\n");
printf("enter 0 to stop building the list, else enter any integer\n");
int k,count=1,x;
struct node* curr,*temp,*prev;
scanf("%d",&k);
struct node* head=creatnode(k);
scanf("%d",&k);
while(k){
curr=creatnode(k);
curr->next=head;
head=curr;
scanf("%d",&k);
}
traverse(head);
printf("\ndeleting the first node.\n");
temp=head;
head=head->next;
free(temp);
traverse(head);
printf("\nfirst node deleted.\n");
printf("\ndeleting the last node.\n");
temp=head->next;
prev=head;
while(temp->next!=NULL){
temp=temp->next;
prev=prev->next;
}
prev->next=NULL;
free(temp);
traverse(head);
printf("\last node deleted.\n");
printf("\n enter the position of the node you want to delete\n");
scanf("%d",&x);
temp=head->next;
prev=head;
while(count!=x-1){
temp=temp->next;
prev=prev->next;
count++;
}
prev->next=temp->next;
free(temp);
traverse(head);
return 0;
}