无头链表
2017-03-07 本文已影响9人
76577fe9b28b
头文件定义
#include "head.h"
typedef struct Student{
int age;
struct Student *next;
} Stu;
//初始化链表
int init_link_list(Stu **head);
//创建节点
int create_node(int age, Stu **node);
//释放节点
int free_node(Stu **node);
//在表尾部插入
int insert_node_to_foot(Stu *node, Stu **head);
//在表头部插入
int insert_node_to_head(Stu *node, Stu **head);
//在local位置插入node节点
int insert_node_to_any_location(Stu *node, Stu **head, int local);
//找到age对应的node
Stu * find_node(int age, Stu *head);
//删除对应节点
int delete_node(Stu *node,Stu **head);
//打印link
void print_link(Stu *head);
//销毁链表
int free_link(Stu **head);
//获取链表长度
int get_link_length(Stu *head);
#endif /* Link_list_h */
链表实现
//初始化链表
int init_link_list(Stu **head){
if (head != NULL) {
*head = NULL;
}else{
return -1;
}
return 0;
}
//创建节点
int create_node(int age, Stu **node){
if (node == NULL) {
return -1;
}
Stu *node_in = calloc(1, sizeof(Stu));
if (node == NULL) {
return -1;
}
node_in->age = age;
node_in->next = NULL;
*node = node_in;
return 0;
}
//释放节点
int free_node(Stu **node){
if (node == NULL || *node == NULL) {
return -1;
}
free(*node);
*node = NULL;
return 0;
}
//在表尾部插入
int insert_node_to_foot(Stu *node, Stu **head){
if (node == NULL || head== NULL ) {
return -1;
}
if (*head == NULL) {
*head = node;
return 0;
}
Stu *p = *head;
while (p->next) {
p = p->next;
}
p->next = node;
return 0;
}
//在表头部插入
int insert_node_to_head(Stu *node, Stu **head){
if (node == NULL || head == NULL) {
return -1;
}
Stu *p = *head;
*head = node;
node->next = p;
return 0;
}
//任何位置插入
int insert_node_to_any_location(Stu *node, Stu **head, int local){
if (node == NULL || head== NULL ) {
return -1;
}
if ( local < 1 || local > get_link_length(*head)) {
fprintf(stderr, "local参数错误");
return -1;
}
if (local == 1) {
Stu *p = (*head);
*head = node;
node->next = p;
return 0;
}
Stu *p = *head;
Stu *q = p;
int index = 0;
while (p) {
index++;
if (index == local) {
break;
}
q = p;
p = p->next;
}
if (p) {
Stu *temp = q->next;
q->next = node;
node->next = temp;
}
return 0;
}
int get_link_length(Stu *head){
int length = 0;
Stu *p = head;
while (p) {
length++;
p = p->next;
}
return length;
}
//找到age对应的node
Stu * find_node(int age, Stu *head){
if (head == NULL) {
return NULL;
}
Stu *p = head;
while (p != NULL) {
if (age == p->age) {
return p;
}
p = p->next;
}
return NULL;
}
int delete_node(Stu *node,Stu **head){
if (node == NULL || head == NULL || *head == NULL) {
return -1;
}
//这里要改变头指针,要单独判断
if (node == *head) {
*head = (*head)->next;
return 0;
}
Stu *head_in = *head;
Stu *p = *head;
while (head_in) {
if (head_in == node) {
p->next = head_in->next;
free_node(&head_in);
return 0;
}
p = head_in;
head_in = head_in->next;
}
return 0;
}
//打印link
void print_link(Stu *head){
Stu *p = head;
while (p) {
printf("%d\n", p->age);
p = p->next;
}
}
//销毁链表
int free_link(Stu **head){
if (head == NULL || *head == NULL) {
return -1;
}
Stu *p = *head;
Stu *q = NULL;
while (p) {
q = p;
p = p->next;
free_node(&q);
}
*head = NULL;
return 0;
}
测试
void link_list_test(){
Stu *head ;
init_link_list(&head);
int age = 0;
while (1) {
printf("请输入年龄:");
scanf("%d", &age);
if (age == -1) {
break;
}
Stu *node = NULL;
if (create_node(age, &node) == 0) {
insert_node_to_head(node, &head);
// insert_node_to_foot(node, &head);
}
}
print_link(head);
// while (1) {
//
// printf("请输入要删除的节点:");
// scanf("%d", &age);
// if (age <= 0) {
//
// break;
// }
// Stu *find = find_node(age, head);
// if (find !=NULL) {
//
// delete_node(find, &head);
// }
//
//
// print_link(head);
//
// }
while (1) {
int age, local;
printf("请输入要插入的节点,以及插入的位置:");
scanf("%d,%d",&age,&local);
if (age <= 0) {
break;
}
Stu *node = NULL;
create_node(age, &node);
insert_node_to_any_location(node, &head, local);
print_link(head);
}
free_link(&head);
}