list.h
#include <stdio.h>
#include <stdlib.h>
typedef char DATA;
struct linked_list{
DATA d;
struct linked_list *next;
};
typedef struct linked_list ELEMENT;
typedef ELEMENT *LINK;
编写一个函数创建一个链表,这函数返回一个指针,指向被创建的链表的头部
#include <stdlib.h>
#include "list.h"
LINK string_to_list(char s[])
{
LINK head;
if(s[0] == '/0')
{
return NULL;
}
else
{
head = malloc(sizeof(ELEMENT));
head->d = s[0];
head->next = string_to_list(s+1);
return head;
}
}
编写两个函数,一个计算链表的元素个数,另一个对链表的元素进行打印
int count(LINK head)
{
if(NULL == head)
{
return 0;
}
else
{
return (1+count(head->next));
}
}
void print_list(LINK head)
{
if(NULL == head)
{
printf("NULL");
}
else
{
printf("%c --> ", head->d);
print_list(head->next);
}
}
对两个链表进行连接,假设有两个链表a和b,其中a不为空。需要把b连接到a的尾部
void concatenate(LINK a, LINK b)
{
assert(NULL != a);
if(a->next == NULL)
{
a->next = b;
}
else
{
concatenate(a->next, b);
}
}
将q所指向的元素放在p1和p2所指向的元素之间
void insert(LINK p1, LINK p2, LINK q)
{
assert(p2 == p1->next);
p1->next = q;
q->next =p2;
}
使用free(),把存储空间返回给系统
void delete_list(LINK head)
{
if(NULL != head)
{
delete_list(head->next);
free(head);
}
}