Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lab0
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ramael Odisho
lab0
Commits
cd1db091
Commit
cd1db091
authored
5 years ago
by
Ramael Odisho
Browse files
Options
Downloads
Patches
Plain Diff
Trying another approach from scratch
parent
27fdcdbc
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.c
+182
-60
182 additions, 60 deletions
main.c
with
182 additions
and
60 deletions
main.c
+
182
−
60
View file @
cd1db091
...
@@ -15,6 +15,178 @@ struct student
...
@@ -15,6 +15,178 @@ struct student
struct
list_elem
elem
;
struct
list_elem
elem
;
};
};
//=============================================================================
void
insert
(
struct
list
*
student_list
)
{
char
temp
[
40
];
// struct list_elem *it;
printf
(
"
\n
Enter the name of the student: "
);
scanf
(
"%s"
,
temp
);
char
*
entered_name
=
(
char
*
)
malloc
((
strlen
(
temp
)
+
1
)
*
sizeof
(
char
));
strcpy
(
entered_name
,
temp
);
struct
student
*
student_node
=
list_entry
(
&
student_list
->
tail
,
struct
student
,
elem
);
student_node
->
name
=
(
char
*
)
malloc
((
strlen
(
entered_name
))
*
sizeof
(
char
));
// strcpy(student_node->name, entered_name);
student_node
->
name
=
entered_name
;
printf
(
"Student node name is: %s
\n
"
,
student_node
->
name
);
// printf("The list_elem has this value: %s\n", student_node->elem);
printf
(
"The list_elem has this address: %p
\n
"
,
&
student_node
->
elem
);
printf
(
"The list_elem next has this address: %p
\n
"
,
student_node
->
elem
.
next
);
printf
(
"The list_elem prev has this address: %p
\n
"
,
student_node
->
elem
.
prev
);
printf
(
"The list tail next has this address: %p
\n
"
,
student_list
->
tail
.
next
);
printf
(
"The list tail prev has this address: %p
\n
"
,
student_list
->
tail
.
prev
);
printf
(
"The list head next has this address: %p
\n
"
,
student_list
->
head
.
next
);
printf
(
"The list head prev has this address: %p
\n
"
,
student_list
->
head
.
prev
);
list_push_back
(
student_list
,
&
student_node
->
elem
);
printf
(
"Address of list head: %p
\n
"
,
&
student_list
->
head
);
// printf("The list tail has this address: %s\n", student_list->tail);
// printf("The list head has this address: %s\n", student_list->head);
printf
(
"The list tail next has this address: %p
\n
"
,
student_list
->
tail
.
next
);
printf
(
"The list tail prev has this address: %p
\n
"
,
student_list
->
tail
.
prev
);
printf
(
"The list head next has this address: %p
\n
"
,
student_list
->
head
.
next
);
printf
(
"The list head prev has this address: %p
\n
"
,
student_list
->
head
.
prev
);
// struct list_elem *e;
// bool name_already_exists = false;
// for (e = list_begin (&student_list); e != list_end (&student_list);
// e = list_next (e))
// {
// struct student *student_node = list_entry(e, struct student, elem);
// if (entered_name == student_node->name) {
// name_already_exists = true;
// }
// }
// if (name_already_exists)
// {
// printf("Another student with that name already exists. No new student was added to the list.");
// }
// else
// {
// list_push_back(student_list, student_node);
// }
}
//=============================================================================
void
delete
(
struct
list
*
student_list
)
{
}
//=============================================================================
void
list
(
struct
list
*
student_list
)
{
if
(
list_empty
(
student_list
))
{
printf
(
"THE LIST IS EMPTY
\n
"
);
// return;
}
else
{
struct
list_elem
*
e
;
for
(
e
=
list_begin
(
student_list
);
e
!=
list_end
(
student_list
);
e
=
list_next
(
e
))
{
struct
student
*
student_node
=
list_entry
(
e
,
struct
student
,
elem
);
printf
(
"%s
\n
"
,
student_node
->
name
);
}
}
}
//=============================================================================
void
quit
(
struct
list
*
student_list
)
{
/*
for (e = list_begin (&list); e != list_end (&list); e = list_remove (e))
{
...do something with e...
}
If you need to free() elements of the list then you need to be
more conservative. Here's an alternate strategy that works
even in that case:
*/
while
(
!
list_empty
(
student_list
))
{
struct
list_elem
*
e
=
list_pop_front
(
student_list
);
free
(
e
);
}
exit
(
1
);
}
//=============================================================================
int
main
()
{
struct
list
student_list
;
list_init
(
&
student_list
);
int
opt
;
do
{
printf
(
"Menu:
\n
"
);
printf
(
"1 - Insert student
\n
"
);
printf
(
"2 - Delete student
\n
"
);
printf
(
"3 - List students
\n
"
);
printf
(
"4 - Exit
\n\n
"
);
printf
(
"Enter your choice: "
);
scanf
(
"%d"
,
&
opt
);
switch
(
opt
)
{
case
1
:
{
insert
(
&
student_list
);
break
;
}
case
2
:
{
delete
(
&
student_list
);
break
;
}
case
3
:
{
list
(
&
student_list
);
break
;
}
case
4
:
{
quit
(
&
student_list
);
break
;
}
default:
{
printf
(
"Quit? (1/0):
\n
"
);
scanf
(
"%d"
,
&
opt
);
if
(
opt
)
quit
(
&
student_list
);
break
;
}
}
}
while
(
1
);
return
0
;
}
/*
void checkIfMallocFailed(void* ptr)
void checkIfMallocFailed(void* ptr)
{
{
printf("\nAddress of ptr INSIDE FUNC is: %p\n", ptr);
printf("\nAddress of ptr INSIDE FUNC is: %p\n", ptr);
...
@@ -169,10 +341,11 @@ void list(struct list *student_list)
...
@@ -169,10 +341,11 @@ void list(struct list *student_list)
// struct student student_node;
// struct student student_node;
struct list_elem *it;
struct list_elem *it;
struct student *student_node = (struct student*) malloc(sizeof(student_node));
for (it = list_begin(student_list); it != list_end(student_list); it = list_next(it))
for (it = list_begin(student_list); it != list_end(student_list); it = list_next(it))
{
{
struct
student
*
student_node
=
list_entry
(
it
,
struct
student
,
elem
);
student_node = list_entry(it, struct student, elem);
printf("%s\n", student_node->name);
printf("%s\n", student_node->name);
}
}
...
@@ -184,16 +357,14 @@ void list(struct list *student_list)
...
@@ -184,16 +357,14 @@ void list(struct list *student_list)
void quit(struct list *student_list)
void quit(struct list *student_list)
{
{
/*
// for (e = list_begin (&list); e != list_end (&list); e = list_remove (e))
for (e = list_begin (&list); e != list_end (&list); e = list_remove (e))
// {
{
// ...do something with e...
...do something with e...
// }
}
If you need to free() elements of the list then you need to be
// If you need to free() elements of the list then you need to be
more conservative. Here's an alternate strategy that works
// more conservative. Here's an alternate strategy that works
even in that case:
// even in that case:
*/
while (!list_empty (student_list))
while (!list_empty (student_list))
{
{
struct list_elem* e = list_pop_front (student_list);
struct list_elem* e = list_pop_front (student_list);
...
@@ -204,54 +375,5 @@ void quit(struct list *student_list)
...
@@ -204,54 +375,5 @@ void quit(struct list *student_list)
//=============================================================================
//=============================================================================
int
main
()
{
struct
list
student_list
;
list_init
(
&
student_list
);
int
opt
;
do
{
printf
(
"Menu:
\n
"
);
printf
(
"1 - Insert student
\n
"
);
printf
(
"2 - Delete student
\n
"
);
printf
(
"3 - List students
\n
"
);
printf
(
"4 - Exit
\n\n
"
);
printf
(
"Enter your choice: "
);
scanf
(
"%d"
,
&
opt
);
switch
(
opt
)
{
case
1
:
{
insert
(
&
student_list
);
break
;
}
case
2
:
{
delete
(
&
student_list
);
break
;
}
case
3
:
{
list
(
&
student_list
);
break
;
}
case
4
:
{
quit
(
&
student_list
);
break
;
}
default:
{
printf
(
"Quit? (1/0):
\n
"
);
scanf
(
"%d"
,
&
opt
);
if
(
opt
)
quit
(
&
student_list
);
break
;
}
}
}
while
(
1
);
return
0
;
*/
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment