forked from kingroryg/DSA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinkedList_funcs.c
199 lines (176 loc) · 3.36 KB
/
linkedList_funcs.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//git username->DravitLochan
#include<stdio.h>
#include<stdlib.h>
/*
In this cpp file, all the basic functions of a linked list have been implemented.
make a call according to your needs.
cheers!
*/
struct node{
int data;
struct node *next;
}*head,*ptr,*t,*temp;
struct node *init()
{
return (struct node *)(malloc(sizeof(struct node)));
}
struct node *create_node()
{
return (struct node *)(malloc(sizeof(struct node)));
}
//to push a node in a LL
void push(struct node *t,int n)
{
while(t->next!=NULL)
{
t=t->next;
}
struct node *ptr=create_node();
ptr->data=n;
ptr->next=NULL;
t->next=ptr;
}
//print a list
print_list(struct node *t)
{
printf("the final list is \n\n\t");
t=t->next;
while(t!=NULL)
{
printf("%d->",t->data);
t=t->next;
}
printf("NULL\n");
}
int count(struct node *t) //counts and returns the number of nodes in the linked list
{
int ctr=0;
t=t->next;
while(t!=NULL)
{
++ctr;
t=t->next;
}
return ctr;
}
//deletes the first element from the list
struct node * del_first(struct node *t)
{
t=t->next->next;
return t;
}
//deletes the last element from the list
int del_last(struct node *t)
{
t=t->next;
if(t->next->next==NULL)
{
t=NULL;
printf("The only node in the list deleted successfully\n");
return 0;
}
while(t->next->next!=NULL)
{
t=t->next;
}
free(t->next);
printf("The last node in the list deleted successfully\n");
return 0;
}
//prints the linked list in the reverse order(using recursive calls).
void print_reverse(struct node *t)
{
if(t->next!=NULL)
print_reverse(t->next);
printf("%d->",t->data);
}
//sorts a given list
void sort_list(struct node *head)
{
struct node *t=(struct node *)malloc(sizeof(struct node));
struct node *temp=(struct node *)malloc(sizeof(struct node));
int tempint;
t=head->next;
while(t!=NULL)
{
temp=t->next;
while(temp!=NULL)
{
if(t->data>temp->data)
{
tempint=t->data;
t->data=temp->data;
temp->data=tempint;
}
temp=temp->next;
}
t=t->next;
}
print_list(head);
}
//merges 2 sorted linked lists
struct node* merge_2_lists(struct node *head1,struct node *head2)
{
struct node *mllhead=init();
temp=head1->next;
t=head2->next;
while(temp!=NULL&&t!=NULL)
{
if(temp->data<t->data)
{
push(mllhead,temp->data);
temp=temp->next;
}
else if(temp->data>t->data)
{
push(mllhead,t->data);
t=t->next;
}
else
{
push(mllhead,t->data);
push(mllhead,temp->data);
temp=temp->next;
t=t->next;
}
}
while(t!=NULL)
{
push(mllhead,t->data);
t=t->next;
}
while(temp!=NULL)
{
push(mllhead,temp->data);
temp=temp->next;
}
printf("\n\n\nThe final merged list is :\n");
print_list(mllhead);
return mllhead;
}
/*
In the main function, only one linked list has been created.
to use any thing else, make the function calls suitably.
*/
main()
{
int ctr,i,n,*freq;
struct node *head=init();
printf("\t\t\tWELCOME\n\n\nHead created successfully...!!\n"); //head does not have data value. It just points to the first node containing the data value
printf("\nEnter the number of nodes in the list : ");
scanf("%d",&ctr);
struct node *ptr=create_node();
head->next=ptr;
printf("\n\nEnter the data for node 1\n");
scanf("%d",&(ptr->data));
ptr->next=NULL;
--ctr;
for(i=0;i<ctr;++i)
{
printf("Enter the data for node %d\n",i+2);
scanf("%d",&n);
push(head,n);
}
print_list(head);
printf("\n");
}