-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
174 lines (157 loc) · 3.44 KB
/
functions.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
/* the functionsof the linked list */
#include<stdio.h>
#include"linked.h"
// the function to display the list.
void display(struct node *head){
struct node *temp=head;
while(temp!=NULL){
printf("%c",temp->data);
temp=temp->next; };
}
// the function to count the number of elements in the list.
void count(struct node *head){
int x=0;
while(head!=NULL){
x++;
head=head->next;
}
printf("the number of elements is %d:",x-1);
}
// the function to insert from the begining.
struct node* insert_in_begining(struct node *head,char x){
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if (temp!=NULL){
temp->data=x;
temp->next=head;
}
return temp;
}
//the function to insert at the position.
struct node* insert_after_position(struct node *head,char x,int pos){
int c=1;
struct node *temp;
struct node *t=head;
temp=(struct node *)malloc(sizeof(struct node));
if(head ==NULL&&pos>0)
return 0;
if(pos==0){
temp->next=head;
temp->data=x;
return temp;}
while(c<pos&&head->next!=NULL) {
head=head->next;
c++;
}
if(c==pos){
temp->next=head->next;
temp->data=x;
head->next=temp->next;}
return t;
}
// Delete the word from the list.
struct node* delete(struct node*head,char x){
struct node *prev=NULL,*current=head;
while(current!=NULL){
if(current->data==x){
if(current==head)
head=head->next;
else
prev->next=current->next;
free(current);}
else { prev=current;
current=current->next;
}
return head;
}
return head;
}
// The function to insert in the ascending order.
struct node* insert_ascending_order(struct node *head,char x){
struct node *temp,*t=head;
temp=(struct node *)malloc(sizeof(struct node));
if(temp!=NULL) {
temp->data=x;
if(head->data==NULL||head->data>x){
temp->next=head;
return temp;}
else
{ while(t->next!=NULL){
if(t->data<=x&&t->next->data>x){
temp->next=t->next;
t->next=temp;
return head;
}
else
t=t->next;
}
t->next=temp;
return head;
}
}
else
{ printf("Not able to allocate the memory.");
return head;
}
}
// The function to delete the list.
struct node* delete_list(struct node*head){
while(head!=NULL){
head=head->next; }
return head;
}
// The function to trim the list.
struct node* trim(struct node*head){
if(head!=NULL)
head=head->next;
return head;
}
// The function to swap two adjacent nodes.
//struct node* swap_adjacent_nodes(struct node *head){
// The function for the union of the two linked lists.
struct node* un_ion(struct node *H1,struct node *H2){
struct node *H,*t;
struct node *head=NULL;
if(H1==NULL&&H2==NULL)
return NULL;
while(H1!=NULL&&H2!=NULL){
if(t==NULL){
t=malloc(sizeof(struct node*));
head=t;
}
else {
t->next=malloc(sizeof(struct node*));
t=t->next;
}
if(H1->data < H2->data){
t->data=H1->data;
H1=H1->next;
}
else if(H1->data>H2->data){
t->data=H2->data;
H2=H2->next;
}
else if(H1->data==H2->data){
t->data=H1->data;
H1=H1->next;
H2=H2->next;
}
}
if(H1!=NULL)
H=H1;
else if(H2!=NULL)
H=H2;
while(H!=NULL){
if(head==NULL){
t=malloc(sizeof(struct node*));
head=t;
}
else {
t->next=malloc(sizeof(struct node *));
t=t->next;
}
t->data=H->data;
H=H->next;
}
return head;
}