-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedListwInput.c
106 lines (90 loc) · 2.19 KB
/
LinkedListwInput.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person{
int age;
struct Person *next;
};
struct Person *createNewPerson (int age){
struct Person *newPerson = NULL;
newPerson = malloc(sizeof(struct Person));
if (newPerson != NULL)
{
newPerson->next = NULL;
newPerson->age = age;
printf("Created New Person at %p\n",newPerson);
} else{
printf("FAIL");
}
return newPerson;
};
void printPerson(struct Person *person,char *comment){
if (person==NULL)
printf("%s is NULL\n",comment);
else{
printf("%s : Age : %d\nAddress : %p\nNext : %p\n",comment,person->age,person,person->next);
printf("----------------------\n");
}
}
void cleanList(struct Person *list){
struct Person *next;
while(list)
{
next = list->next;
printf("Cleaning %d..",list->age);
free(list);
list = next;
}
}
void printList(struct Person *list){
printf("List :\n");
struct Person *temp;
temp = list;
if(temp == NULL)
printf("List is empty !");
else
while(temp){
printPerson(temp,"Temp");
temp = temp->next;
}
}
int main() {
struct Person *first = NULL;
struct Person *others = NULL;
char nick[100];
int age;
while(1){
printf("Enter a command or value (Q For Quit)");
fgets(nick,64,stdin);
if(strcmp("q\n",nick) == 0)
{
printf("Quitting..\n");
break;
}
else if(strcmp("print\n",nick) == 0)
{
printf("Printing..\n");
printList(first);
}
else if(sscanf(nick,"%d", &age) != 0)
{
printf("Adding %d..\n",age);
if(first == NULL)
{
first = createNewPerson(age);
if(first != NULL)
{
others = first;
}
} else{
others->next = createNewPerson(age);
if(others->next != NULL)
others = others->next;
}
}
}
cleanList(first);
first = NULL;
others = NULL;
return 0;
}