-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlinked_lists_5.c
199 lines (157 loc) · 3.08 KB
/
linked_lists_5.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sPerson {
int age;
struct sPerson *nextInLine;
};
void printPerson(const struct sPerson *person, const char *comment)
{
if (person == NULL)
{
printf("%s is NULL\n", comment);
}
else
{
printf("%s: age:%d address:%p nextInLine:%p\n",
comment,
person->age,
person,
person->nextInLine);
}
}
void PrintList(const struct sPerson *list)
{
printf("Printing List:\n");
const struct sPerson *t;
t = list;
if(t == NULL)
{
printf("list is empty\n");
}
else
{
while(t)
{
printPerson(t, "t");
t = t->nextInLine;
}
}
}
struct sPerson *getNewPerson(const int age)
{
struct sPerson *newPerson = NULL;
newPerson = malloc(sizeof(struct sPerson));
if(newPerson != NULL)
{
newPerson->nextInLine = NULL;
newPerson->age = age;
printf("new person at %p\n", newPerson);
}
else
{
printf("Allocation Failure!! \n");
}
return newPerson;
}
struct sPerson *getPlacePointer(struct sPerson *list, const int place)
{
struct sPerson *t;
t = list;
int count = 1;
do
{
count++;
if (count == place)
{
break;
}
t = t->nextInLine;
}
while (t->nextInLine != NULL);
return t;
}
void CleanUp(struct sPerson *list)
{
struct sPerson *next;
while(list)
{
next = list->nextInLine;
printf("Cleaning %d\n", list->age);
free(list);
list = next;
}
}
int main()
{
printf("** START **\n");
struct sPerson *first = NULL;
struct sPerson *pTemp1 = NULL;
struct sPerson *pTemp2 = NULL;
char command[64];
int place;
int age;
first = getNewPerson(21);
pTemp1 = first;
pTemp1->nextInLine = getNewPerson(22);
pTemp1 = pTemp1->nextInLine;
pTemp1->nextInLine = getNewPerson(23);
pTemp1 = pTemp1->nextInLine;
pTemp1->nextInLine = getNewPerson(12);
pTemp1 = pTemp1->nextInLine;
pTemp1->nextInLine = getNewPerson(65);
pTemp1 = pTemp1->nextInLine;
while(1)
{
printf( "Enter a command or value : ");
fgets(command, 64, stdin);
if (strcmp("q\n", command) == 0)
{
printf("Quitting..\n");
break;
}
else if (strcmp("print\n", command) == 0)
{
printf("Printing..\n");
PrintList(first);
}
else
{
if (sscanf(command, "%d %d", &place, &age) != 0)
{
printf("Adding %d at place %d\n", age, place);
if(first == NULL)
{
first = getNewPerson(age);
}
else if(place == 1)
{
pTemp1 = getNewPerson(age);
if(pTemp1 != NULL)
{
pTemp1->nextInLine = first;
first = pTemp1;
pTemp1 = NULL;
}
}
else
{
pTemp1 = getPlacePointer(first,place);
pTemp2 = getNewPerson(age);
if(pTemp2 != NULL)
{
pTemp2->nextInLine = pTemp1->nextInLine;
pTemp1->nextInLine = pTemp2;
pTemp2 = NULL;
}
pTemp1 = NULL;
}
}
}
}
CleanUp(first);
first = NULL;
pTemp1 = NULL;
pTemp2 = NULL;
return 0;
}