-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlistdelete.c
94 lines (94 loc) · 2.23 KB
/
linkedlistdelete.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<stdlib.h>
void display();
int delete(int);
struct node
{
int data;
struct node *next;
}*head;
int main()
{
int n,i,a;
typedef struct node Node;
head=(Node*)malloc(sizeof(Node));
Node *tem;
Node *new;
printf("enter no of nodes:");
scanf("%d",&n);
if(n>0) //gets the first node of the linked list
{
printf("enter the values:");
scanf("%d",&a);
head->data=a;
head->next=NULL;
tem=head;
}
for(i=1;i<n;i++) //get the remaining nodes of the linked list
{
new=(Node*)malloc(sizeof(Node));
scanf("%d",&a);
new->data=a;
new->next=NULL;
tem->next=new;
tem=tem->next;
}
if(n>0)
{
printf("enter the element to delete:");
scanf("%d",&a);
printf("List before deletion:");
display(); //display function can display the node in list
i=delete(a);
if(i==1)
{
printf("List after deletion:");
display(); //display after deletion
}
}
free(head); //free memory which is allocated
free(new);
}
void display()
{
struct node *temp;
temp=head;
if(temp==NULL) //when list has no nodes
printf("list is empty");
else
{
while(temp!=NULL) //print all the nodes
{
printf("%d ",temp->data);
temp=temp->next;
}printf("\n");
}
}
int delete(int a)
{
struct node *temp,*prev;
temp=head;
if(temp->data==a) //deleting element is found at head
{
temp=temp->next;
head=temp;
return 1;
}
if(head!=NULL)
{
while(temp!=NULL&&temp->data!=a) //gets previous node and next of deleting element
{
prev=temp;
temp=temp->next;
}
if(temp==NULL) //deleting element not found
{
printf("Deleting element was not found in the list");
return 0;
}
prev->next=temp->next; //changes the next of previous
return 1;
}
printf("list is empty");
return 0;
}