forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_nodes.c
More file actions
82 lines (82 loc) · 1.42 KB
/
delete_nodes.c
File metadata and controls
82 lines (82 loc) · 1.42 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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int val;
struct node*next;
}NODE;
NODE*createlist(NODE*head,int n)
{
NODE*p,*temp;
int i;
for(int i=0;i<n;i++)
{
temp=(NODE*)malloc(sizeof(NODE));
if(temp==NULL)
{
printf("Memory allocation failed");
exit(0);
}
printf("Enter element: ");
scanf("%d",&temp->val);
if(head==NULL)
{
head=temp;
}
else
{
p->next=temp;
}
p=temp;
}
p->next=NULL;
return head;
}
NODE*deletenth(NODE*head, int key)
{
NODE*temp,*p;
int c=-1;
for(p=head;p!=NULL;p=p->next)
{
c++;
if(key==0)
{
temp=p->next;
return temp;
}
if(c==key-1)
{
if(p->next!=NULL)
{
p->next=p->next->next;
}
else
{
p->next=NULL;
}
}
}
return head;
}
void display(NODE*head)
{
NODE*temp;
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("%d",temp->val);
}
}
void main()
{
NODE*head=NULL;
int n;
printf("Enter the number of nodes: ");
scanf("%d",&n);
head=createlist(head,n);
display(head);
int key;
printf("Enter the key: ");
scanf("%d", &key);
head=deletenth(head,key);
display(head);
}