-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedcharacterstuff.c
115 lines (89 loc) · 2.49 KB
/
linkedcharacterstuff.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
#include<stdio.h>
#include<string.h> //for string length
#include<stdlib.h> //for malloc function
//define structure of the node
struct node{
char c;
struct node *next;
};
int main()
{
int count,length,i;
struct node* head =NULL;
struct node* tail = NULL;
char p[50];
printf("\n Enter a string : ");
scanf("%s",p);
length = strlen(p);
//define a # node for stuffing it at the beginning of the string
struct node* temp1 = (struct node *)malloc(sizeof(struct node));
temp1->c = '#';
temp1->next = NULL;
head = temp1; // initialise head and tail to it
tail=temp1;
i=0;
while(i < length)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
tail->next = temp;
temp->c = p[i];
temp->next = NULL;
tail = temp;
i++;
}
//define a # node for stuffing it at the ending of the string
struct node *temp2 = (struct node *)malloc(sizeof(struct node));
temp2->c = '#';
temp2->next = NULL;
tail->next = temp2;
tail=temp2;
// now we will stuff $ infront of $ or # tht might be present in the data
struct node *train, *prev;
train= head;
prev = head;
//first traverse the first node then we will cross the first #
train = train->next;
count = 1 ; //since first node is traversed we add 1 to count
while(train->next != NULL)
{
if(train->c == '$' || train->c == '#' )
{
struct node* insert = (struct node * )malloc(sizeof(struct node));
insert->c = '$';
prev->next = insert;
insert->next = train;
prev = insert;
train = train->next;
prev = prev->next;
count += 2; // if $ or # we insert another $ before it so count is increased by 2
}
else
{ prev = prev->next;
train = train->next;
count += 1;
}
}
printf("\n Before the packet is handed over to data link layer the data is : ");
printf("%s",p);
printf("\n After the packet is handed over to data link layer the data is stuffed as : ");
struct node* printit;
printit = head;
i = 0;
while(i <= count)
{
printf("%c",printit->c);
printit = printit->next;
i++;
}
// free all the nodes
struct node * freeit;
freeit = head;
while(head != NULL)
{
head = head->next;
free(freeit);
freeit = head;
}
free(head);
return 0;
}