链表学习笔记之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;
}

备注:#pragma comment(lib,"PrivateProtocol.lib") 调用静态库

posted @ 2011-10-10 21:46  火腿骑士  阅读(232)  评论(0编辑  收藏  举报