-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
171 lines (146 loc) · 2.36 KB
/
linked_list.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/**----------------------creates linked list remembering position that require changes -------------*/
struct node
{
unsigned int index;
int org_size;
char* string;
struct node *next;
}*start=NULL;
unsigned int total_size;
void insert(unsigned int ind,char * str,int size )
{
struct node *p=start,*new,*prev=NULL;
new=(struct node *)malloc(sizeof(struct node));
new->string=NULL;
new->string=(char *)calloc((strlen(str)+10),sizeof(char));
total_size+=strlen(str);
if(start==NULL)
{
start=new;
new->index=ind;
strcpy(new->string,str);
new->org_size=size;
new->next=NULL;
return;
}
else
{
while(p!=NULL )
{
if((p->index)>ind)
break;
else
{
prev=p;
p=p->next;
}
}
new->next=p;
if(prev!=NULL)
prev->next=new;
else
start=new;
new->index=ind;
new->org_size=size;
strcpy(new->string,str);
}
return;
}
void remove_front()
{
struct node *ptr=NULL;
if(start==NULL)
{
return ;
}
else
{
ptr=start;
start=start->next;
free(ptr);
return;
}
}
void remove_end()
{
struct node *ptr=start,*prev=NULL;
if(start==NULL)
{
return;
}
else if(start->next==NULL)
{
ptr=start;
start=start->next;
total_size-=strlen(ptr->string);
free(ptr);
return;
}
else
{
while(ptr->next!=NULL)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=NULL;
total_size-=strlen(ptr->string);
free(ptr);
return;
}
}
void print()
{
struct node *ptr=start;
if(start==NULL)
return;
while(ptr->next!=NULL)
{
printf("\n\nindex is %d and length = %d string is %s",ptr->index,strlen(ptr->string),ptr->string);
ptr=ptr->next;
}
printf("\n\nindex is %d and length = %d string is %s",ptr->index,strlen(ptr->string),ptr->string);
}
unsigned int get_index()
{
if(start==NULL)
return (-1);
return (start->index);
}
unsigned int get_last_index()
{
struct node *ptr=start;
if(start==NULL)
return -1;
while(ptr->next!=NULL)
ptr=ptr->next;
return ptr->index;
}
char * get_string()
{
if(start==NULL)
return NULL;
return (start->string);
}
char *get_last_string()
{
struct node *ptr=start;
if(start==NULL)
return NULL;
while(ptr->next!=NULL)
ptr=ptr->next;
return (ptr->string);
}
unsigned int get_org_size()
{
if(start==NULL)
return -1;
return start->org_size;
}
unsigned int get_total_size()
{
return total_size;
}