jelasin 的学生作业:
//Slist.h
#ifndef __SLIST_H__
#define __SLIST_H__
#include
#include
#include
#include
#include
#define RED "\033[31m"
#define NONE "\033[0m"
typedef int datatype_t;
typedef struct slist_node
{
datatype_t data;
struct slist_node * next;
} slist_node;
typedef struct slist_head
{
uint32_t n;
slist_node *next;
} slist_head;
extern slist_head* create_empyt_slist();
extern int insert_head_slist(slist_head* head, datatype_t value);
extern void print_slist(const slist_head* head);
extern int insert_tail_slist(slist_head* head, datatype_t value);
extern int insert_order_slist(slist_head* head, datatype_t value);
extern void delete_node_slist(slist_head* head, datatype_t value);
extern void destyroy_slist(slist_head* head);
extern bool is_empty_slist(slist_head* head);
extern uint num_of_node_slist(slist_head* head);
extern slist_node* find_node_slist(const slist_head* head, datatype_t value);
#endif
//SList.c
#include "SList.h"
slist_head* create_empyt_slist()
{
slist_head * l = (slist_head*)malloc(sizeof(slist_head));
if (NULL == l)
{
perror("Failed to allocate memory for slist_head");
return NULL;
}
memset(l, 0, sizeof(slist_head));
l->n = 0;
l->next = NULL;
return l;
}
int insert_head_slist(slist_head* head, datatype_t value)
{
slist_node* new_node = (slist_node*)malloc(sizeof(slist_node));
if (NULL == new_node)
{
perror("Failed to allocate memory for new node");
return -1;
}
new_node->data = value;
new_node->next = head->next;
head->next = new_node;
head->n++;
return 0;
}
int insert_tail_slist(slist_head* head, datatype_t value)
{
slist_node* new_node = (slist_node*)malloc(sizeof(slist_node));
if (NULL == new_node)
{
perror("Failed to allocate memory for new node");
return -1;
}
new_node->data = value;
slist_node* temp = head->next;
if (NULL != temp)
{
while(NULL != temp->next)
{
temp = temp->next;
}
}
else // 只有首个元素会调用,放在else block
{
head->next = new_node;
new_node->next = NULL;
head->n++;
return 0;
}
temp->next = new_node;
new_node->next = NULL;
head->n++;
return 0;
}
int insert_order_slist(slist_head* head, datatype_t value)
{
slist_node* new_node = (slist_node*)malloc(sizeof(slist_node));
if (NULL == new_node)
{
perror("Failed to allocate memory for new node");
return -1;
}
new_node->data = value;
slist_node* temp = head->next;
if (NULL != temp)
{
while (NULL != temp->next && temp->next->data next;
}
}
else // 只有首个元素会调用,放在else block
{
head->next = new_node;
new_node->next = NULL;
head->n++;
return 0;
}
new_node->next = temp->next;
temp->next = new_node;
head->n++;
return 0;
}
slist_node* find_node_slist(const slist_head* head, const datatype_t value)
{
slist_node* current = head->next;
if (NULL != current)
{
while (value != current->data)
{
if (NULL != current->next)
{
current = current->next;
}
else
{
return NULL;
}
}
}
return current;
}
void delete_node_slist(slist_head* head, datatype_t value)
{
while (NULL != find_node_slist(head, value))
{
slist_node * p = head->next;
slist_node * q = NULL;
if (NULL != p)
{
while (p->data != value)
{
if (NULL != p->next)
{
q = p;
p = p->next;
}
else
{
return;
}
}
if (NULL != q)
{
q->next = p->next;
free(p);
p = NULL;
head->n--;
}
else
{
head->next = p->next;
free(p);
p = NULL;
head->n--;
if (head->n == 0)
{
head->next = NULL;
}
}
}
}
}
void destyroy_slist(slist_head* head)
{
slist_node* current = head->next;
slist_node* p = NULL;
if (NULL != current)
{
while (NULL != current->next) {
p = current;
current = current->next;
printf("%d ", p->data);
free(p);
p = NULL;
}
// current != NULL && current->next == NULL
printf("%d \n", current->data);
free(current);
current = NULL;
}
free(head);
head = NULL;
}
bool is_empty_slist(slist_head* head)
{
return head->next == NULL ? true : false;
}
uint32_t num_of_node_slist(slist_head* head)
{
return head->n;
}
void print_slist(const slist_head* head)
{
const slist_node* current = head->next;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
//main.c
#include "SList.h"
int test()
{
slist_head * l = create_empyt_slist();
if (NULL == l)
{
perror("Failed to create list");
return -1;
}
// 创建一个单向链表,把1,5,3,7,9,5,8,5,3无序数据要求按从大到小的方式利用有序插入的方式插入链表,并输出。然后删除链表中所有值为3的数据
insert_order_slist(l, 1);
insert_order_slist(l, 5);
insert_order_slist(l, 3);
insert_order_slist(l, 7);
insert_order_slist(l, 9);
insert_order_slist(l, 5);
insert_order_slist(l, 8);
insert_order_slist(l, 5);
insert_order_slist(l, 3);
print_slist(l);
delete_node_slist(l, 3);
print_slist(l);
destyroy_slist(l);
}
int main(int argc, char const *argv[])
{
return test();
}
➜ 2 ./slist
1 -> 3 -> 3 -> 5 -> 5 -> 5 -> 7 -> 8 -> 9 -> NULL
1 -> 5 -> 5 -> 5 -> 7 -> 8 -> 9 -> NULL
1 5 5 5 7 8 9