Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

绿色网站无广告
会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

链表学习笔记之2

来源: 未知 分享至:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//结点类型
typedef struct student
{
void *data;
struct student* next;
}student;
//链表类型
typedef struct studentlist
{
student* head;
student* current;
student* tail;
int size;
}studentlist;

studentlist* linked_list_construct()
{
studentlist* list=NULL;
list=calloc(1,sizeof(studentlist));
if(list==NULL)return NULL;
return list;
}
void linked_list_prepend_node(studentlist *list, student *node)
{
if (list == NULL || node == NULL) return;
student* head=NULL;
head=list->head;
if(head==NULL)
{
list->head=list->tail=node;
}
else
{
node->next=head;
list->head=node;
}
list->size++;
}
void linked_list_append_node(studentlist *list, student *node)
{
if (list == NULL || node == NULL) return;
student* tail=NULL;
tail=list->tail;
if(tail==NULL)
{
list->head=list->tail=node;
}
else
{
node->next=tail;
list->tail=node;
}
list->size++;
}
student*linked_list_node_construct(const void *data)
{
student* node=NULL;
node=call(1,sizeof(student));
if(node==NULL)return NULL;
node->data=data;
return node;
}
student *linked_list_get_next_node(studentlist *list)
{
if (list == NULL || list->current==NULL) return NULL;
student *node=NULL;
node=list->current;
list->current=list->current->next;
return node;
}
void linked_list_destroy(studentlist *list)
{
if (list == NULL) return;
student *node = NULL, *next = NULL;
linked_list_seek_start(list);
node = linked_list_get_next_node(list);
while(node!=NULL)
{
next = linked_list_get_next_node(list);
linked_list_node_destroy(node);
node=next;
}
list->head = NULL;
list->tail = NULL;
list->current = NULL;
list->size = 0;
free(list);
}
void linked_list_remove_node(studentlist *list, student *node)
{
if (list == NULL || node == NULL)
{
return;
}
if (node == list->head)
{
list->head = node->next;
}
if (node == list->tail)
{
list->tail = list->head;
}
node->next = NULL;
list->size--;
}
void linked_list_node_destroy(student *node)
{
if (node == NULL) return;
free(node);
}
void linked_list_seek_start(studentlist *list)
{
if (list == NULL) return;
list->current=list->head;
}
void linked_list_seek_end(studentlist *list)
{
if (list == NULL) return;
list->current=list->tail;
}
int linked_list_length(studentlist *list)
{
if (list == NULL) return 0;
return list->size;
}
int linked_list_is_empty(studentlist *list)
{
if (list == NULL) return 1;
return list->head=NULL;
}

Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史