linklist.h
linklist.c
#ifndef LINK_LIST_H
#define LINK_LIST_H 1
#include <stdio.h>
#include <string.h>
#include <malloc.h>
struct stu
{
char name[32];
char stu_num[32];
char sex[16];
int age;
struct stu *next;
struct stu *pre;
};
struct stu *initlist(struct stu **head);
void destroylist(struct stu *head);
void clearlist(struct stu *head);
int listempty(const struct stu *head);
int listlength(const struct stu *head);
struct stu *getelem(const struct stu *head, int i);
void listinsert(struct stu *head, struct stu *new);
void listdelete(struct stu *node);
#endif /* LINK_LIST_H */
linklist.c
#include <linklist.h>
struct stu *initlist(struct stu **head)
{
*head = (struct stu *)malloc(sizeof(struct stu));
if(!*head) {
return NULL;
}
memset(*head, 0, sizeof(struct stu));
(*head)->next = *head;
(*head)->pre = *head;
return *head;
}
void destroylist(struct stu *head)
{
clearlist(head);
free(head);
return;
}
void clearlist(struct stu *head)
{
struct stu *n;
for(n=head->next; head->next!=head; n=head->next) {
head->next = n->next;
n->next->pre = head;
free(n);
}
return;
}
int listempty(const struct stu *head)
{
return head->next == head;
}
int listlength(const struct stu *head)
{
int i = 0;
struct stu *n = head->next;
while(n != head) {
n = n->next;
++i;
}
return i;
}
struct stu *getelem(const struct stu *head, int i)
{
struct stu *n = (struct stu *)head;
for(; i--&&((n=n->next)!=head);)/* nothing*/;
return n==head ? NULL:n;
}
void listinsert(struct stu *head, struct stu *new)
{
new->next = head;
new->pre = head->pre;
head->pre->next = new;
head->pre = new;
return;
}
void listdelete(struct stu *node)
{
node->pre->next = node->next;
node->next->pre = node->pre;
free(node);
return;
}