-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly_linked_list.c
More file actions
122 lines (118 loc) · 2.04 KB
/
Copy pathsingly_linked_list.c
File metadata and controls
122 lines (118 loc) · 2.04 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void printlist(struct Node *n)
{
if(n==NULL)
{
printf("list is empty\n");
return;
}
while(n!=NULL)
{
printf("%d->",n->data);
n=n->next;
}
printf("\n");
}
void push(struct Node **head_ref,int new_data)
{
struct Node *new_node=(struct Node *)malloc(sizeof(struct Node));
new_node->data=new_data;
new_node->next=NULL;
struct Node *temp=*head_ref;
if(temp==NULL)
{
*head_ref=new_node;
return;
}
while(temp->next!=NULL)
temp=temp->next;
temp->next=new_node;
return;
}
void append(struct Node **head_ref,int new_data)
{
struct Node *new_node=(struct Node *)malloc(sizeof(struct Node));
new_node->data=new_data;
struct Node *temp=*head_ref;
if(temp==NULL)
{
*head_ref=new_node;
return;
}
new_node->next=temp;
*head_ref=new_node;
return;
}
void insertafter(struct Node **head_ref,int prev_data,int new_data)
{
struct Node *new_node=(struct Node *)malloc(sizeof(struct Node));
new_node->data=new_data;
struct Node *temp=*head_ref;
if(temp==NULL)
{
printf("Cant be inserted");
return;
}
while(temp &&temp->data!=prev_data)
temp=temp->next;
if(temp!=NULL)
{
new_node->next=temp->next;
temp->next=new_node;
}
}
void delete(struct Node **head_ref,int dlt_data)
{
struct Node *temp=*head_ref;
struct Node *curr=NULL;
if(temp==NULL)
{
printf("Cant be deleted");
return;
}
if(temp->data==dlt_data)
{
temp=temp->next;
*head_ref=temp;
}
while(temp &&temp->data!=dlt_data)
{
curr=temp;
temp=temp->next;
}
if(temp==NULL)
{
printf("Cant be deleted");
return;
}
curr->next=temp->next;
return;
}
int main()
{
struct Node *head=NULL;
printf("Created list is:");
printlist(head);
push(&head,30);
push(&head,40);
push(&head,50);
push(&head,20);
printf("Created list is:");
printlist(head);
append(&head,70);
printf("Created list is:");
printlist(head);
insertafter(&head,30,200);
printf("Created list is:");
printlist(head);
delete(&head,30);
printf("Created list is:");
printlist(head);
return 0;
}