Compare commits
6 Commits
90bdecd365
...
a290788bd3
Author | SHA1 | Date | |
---|---|---|---|
a290788bd3 | |||
e13be3a0b6 | |||
cd08400d5f | |||
c005c34103 | |||
c8f250e9d6 | |||
579fa56ae8 |
@ -485,7 +485,10 @@ again:
|
|||||||
printf("\nRecord saved successfully!\n");
|
printf("\nRecord saved successfully!\n");
|
||||||
|
|
||||||
// reload data into linked list again
|
// reload data into linked list again
|
||||||
read_file();
|
// read_file();
|
||||||
|
|
||||||
|
// show back the all results
|
||||||
|
display_students();
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_file()
|
void read_file()
|
||||||
|
79
real/q2.c
Normal file
79
real/q2.c
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include "stdio.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
#define STUDENT_ID_SIZE 10
|
||||||
|
#define CHAR_INT '0'
|
||||||
|
|
||||||
|
struct studentID
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
struct studentID *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct studentID STUDENTID;
|
||||||
|
typedef STUDENTID *STUDENTIDPtr;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
const char student_id[] = "1001245345";
|
||||||
|
|
||||||
|
// 1. creation nodes
|
||||||
|
size_t student_id_len = sizeof(student_id) / sizeof(student_id[0]) - 1;
|
||||||
|
|
||||||
|
STUDENTIDPtr student_ptr_1 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
STUDENTIDPtr student_ptr_2 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
STUDENTIDPtr student_ptr_3 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
STUDENTIDPtr student_ptr_4 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
STUDENTIDPtr student_ptr_5 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
|
||||||
|
student_ptr_1->next = student_ptr_2;
|
||||||
|
student_ptr_2->next = student_ptr_3;
|
||||||
|
student_ptr_3->next = student_ptr_4;
|
||||||
|
student_ptr_4->next = student_ptr_5;
|
||||||
|
student_ptr_5->next = NULL;
|
||||||
|
|
||||||
|
// 2. fill the node's values
|
||||||
|
STUDENTIDPtr temp = student_ptr_1;
|
||||||
|
int last_len = 5;
|
||||||
|
while (temp != NULL)
|
||||||
|
{
|
||||||
|
temp->value = student_id[student_id_len - last_len] - CHAR_INT;
|
||||||
|
temp = temp->next;
|
||||||
|
last_len--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. removes dup
|
||||||
|
STUDENTIDPtr temp1, temp2, duplicate;
|
||||||
|
temp1 = student_ptr_1;
|
||||||
|
|
||||||
|
while (temp1 != NULL && temp1->next != NULL)
|
||||||
|
{
|
||||||
|
temp2 = temp1;
|
||||||
|
|
||||||
|
while (temp2->next != NULL)
|
||||||
|
{
|
||||||
|
if (temp1->value == temp2->next->value)
|
||||||
|
{
|
||||||
|
duplicate = temp2->next;
|
||||||
|
temp2->next = temp2->next->next;
|
||||||
|
free(duplicate);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
temp2 = temp2->next;
|
||||||
|
}
|
||||||
|
temp1 = temp1->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// output the result
|
||||||
|
while (student_ptr_1 != NULL)
|
||||||
|
{
|
||||||
|
printf("%d ", student_ptr_1->value);
|
||||||
|
student_ptr_1 = student_ptr_1->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
2
run
2
run
@ -7,4 +7,4 @@
|
|||||||
|
|
||||||
rm -r single.bin
|
rm -r single.bin
|
||||||
|
|
||||||
gcc work1/single.c -o single.bin && ./single.bin
|
gcc work1/q2.c -o single.bin && ./single.bin
|
@ -485,7 +485,9 @@ again:
|
|||||||
printf("\nRecord saved successfully!\n");
|
printf("\nRecord saved successfully!\n");
|
||||||
|
|
||||||
// reload data into linked list again
|
// reload data into linked list again
|
||||||
read_file();
|
// read_file();
|
||||||
|
|
||||||
|
display_students();
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_file()
|
void read_file()
|
83
work1/q2.c
Normal file
83
work1/q2.c
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
#include "stdio.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
#define STUDENT_ID_SIZE 10
|
||||||
|
#define CHAR_INT '0'
|
||||||
|
|
||||||
|
struct studentID
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
struct studentID *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct studentID STUDENTID;
|
||||||
|
typedef STUDENTID *STUDENTIDPtr;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
const char student_id[] = "1001245345";
|
||||||
|
|
||||||
|
// printf("Student ID: %s\n", student_id);
|
||||||
|
|
||||||
|
// memcpy(five_digits, &student_id[5], 5);
|
||||||
|
// five_digits[5] = '\0';
|
||||||
|
|
||||||
|
// creation nodes
|
||||||
|
size_t student_id_len = sizeof(student_id) / sizeof(student_id[0]) - 1;
|
||||||
|
|
||||||
|
STUDENTIDPtr student_ptr_1 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
STUDENTIDPtr student_ptr_2 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
STUDENTIDPtr student_ptr_3 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
STUDENTIDPtr student_ptr_4 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
STUDENTIDPtr student_ptr_5 = (STUDENTIDPtr)malloc(sizeof(STUDENTID));
|
||||||
|
|
||||||
|
student_ptr_1->next = student_ptr_2;
|
||||||
|
student_ptr_2->next = student_ptr_3;
|
||||||
|
student_ptr_3->next = student_ptr_4;
|
||||||
|
student_ptr_4->next = student_ptr_5;
|
||||||
|
student_ptr_5->next = NULL;
|
||||||
|
|
||||||
|
// fill the node's values
|
||||||
|
STUDENTIDPtr temp = student_ptr_1;
|
||||||
|
int last_len = 5;
|
||||||
|
while (temp != NULL)
|
||||||
|
{
|
||||||
|
temp->value = student_id[student_id_len - last_len] - CHAR_INT;
|
||||||
|
temp = temp->next;
|
||||||
|
last_len--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// removes dup
|
||||||
|
STUDENTIDPtr temp1, temp2, duplicate;
|
||||||
|
temp1 = student_ptr_1;
|
||||||
|
|
||||||
|
while (temp1 != NULL && temp1->next != NULL)
|
||||||
|
{
|
||||||
|
temp2 = temp1;
|
||||||
|
|
||||||
|
while (temp2->next != NULL)
|
||||||
|
{
|
||||||
|
if (temp1->value == temp2->next->value)
|
||||||
|
{
|
||||||
|
duplicate = temp2->next;
|
||||||
|
temp2->next = temp2->next->next;
|
||||||
|
free(duplicate);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
temp2 = temp2->next;
|
||||||
|
}
|
||||||
|
temp1 = temp1->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (student_ptr_1 != NULL)
|
||||||
|
{
|
||||||
|
printf("%d ", student_ptr_1->value);
|
||||||
|
student_ptr_1 = student_ptr_1->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
14
work1/q3.c
Normal file
14
work1/q3.c
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#include "stdio.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
struct Bus {
|
||||||
|
int BusID;
|
||||||
|
int RouteID;
|
||||||
|
time_t schedule;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user