a3/a3.c
#include
#include
#include
#include
struct snode{
int id;
char * name;
struct snode* next;
};
struct slist{
struct snode* front;
};
struct slist* create_list(){
struct slist* root = (struct slist*)malloc(sizeof(struct slist));
root->front = NULL;
return root;
}
bool insert_student(int id, char name[], struct slist* lst){
struct snode* temp = lst->front;
struct snode* prev = NULL;
if(!temp){
struct snode* head = (struct snode*)malloc(sizeof(struct snode));
head->id = id;
head->name = name;
head->next = NULL;
lst->front = head;
return true;
}
while(temp){
if(temp->id == id){
return false;
}
if(temp->id > id){
struct snode* node = (struct snode*)malloc(sizeof(struct snode));
node->id = id;
node->name = name;
node->next = NULL;
prev->next = node;
node->next = temp;
return true;
}
prev = temp;
temp = temp->next;
}
struct snode* node = (struct snode*)malloc(sizeof(struct snode));
node->id = id;
node->name = name;
node->next = NULL;
prev->next = node;
return true;
}
bool remove_student(int id, struct slist* lst){
struct snode* temp = lst->front;
struct snode* prev = NULL;
while(temp && temp->id <= id){
if(temp->id == id){
if(prev == NULL){
lst->front = temp->next;
free(temp);
return true;
}
else{
prev->next = temp->next;
temp->next = NULL;
free(temp);
return true;
}
}
prev = temp;
temp = temp->next;
}
return false;
}
char* find_student(int id, struct slist* lst){
struct snode* temp = lst->front;
while(temp && temp->id <= id){
if(temp->id == id){
return temp->name;
}
temp = temp->next;
}
return NULL;
}
void free_list(struct slist* lst){
lst->front = NULL;
}
void merge_list(struct slist* out, struct slist* s1, struct slist* s2){
struct snode* temp1 = s1->front;
struct snode* temp2 = s2->front;
struct snode* head = NULL;
struct snode* temp = NULL;
while(temp1 && temp2){
if(temp1->id < temp2->id){
if(head == NULL){
head = temp1;
temp = head;
temp1 = temp1->next;
}
else{
temp->next = temp1;
temp = temp->next;
temp1= temp1->next ;
}
}
else{
if(head == NULL){
head = temp2;
temp = head;
temp2 = temp2->next;
}
else{
temp->next = temp2;
temp = temp->next;
...