-
Notifications
You must be signed in to change notification settings - Fork 0
/
del-at-end.c
67 lines (66 loc) · 1.26 KB
/
del-at-end.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;
}*head,*new,*prev,*p;
void create(int n);
void deleteend(int data);
void displaylist();
int main()
{
printf("enter total no of nodes");
int n,data;
scanf("%d",&n);
create(n);
printf("data in the list\n");
displaylist();
printf("enter data to insert at begini\n");
scanf("%d",&data);
deleteatend(data);
printf("data in the list:");
displaylist();
}
void create(int n)
{
int i,data;
head=(struct node*)malloc(sizeof(struct node));
printf("enter value of node1");
scanf("%d",&data);
head->data=data;
head->next=NULL;
p=head;
for(i=2;i<=n;i++)
{
new=(struct node *)malloc(sizeof(struct node));
printf("enter data of node %d",i);
scanf("%d",&data);
new->data=data;
new->next=NULL;
p->next=new;
p=p->next;
}
printf("singly linked list created successfully\n");
}
void deleteatend(int data)
{
prev=head;
p=prev->next;
while(p->next!=NULL)
{
p=p->next;
prev=prev->next;
}
prev->next=NULL;
free(p);
}
void displaylist()
{
p=head;
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
}