一般创建表有两种方式:基于数组、基于结点。由于表经常需要实现在表的中间某个位置实现插入或者删除的操作,而且表的大小很多时候是未知的。但数组大小是创建表的时候就指定了,而且插入删除是十分耗时的(由于数组是连续存储的,所以需要整体移动表中的数据),所以我们一般采用第二种方式来实现表(这种方式中,数据的存储是不连续的,数据的存取是通过指针的索引来实现定位的)。
一、创建一个链表
我们在写任何涉及指针的数据结构的时候都最好画出一张图来,如下是创建链表的过程图
- initialize 完成后要实现的状态
- 按下面的循环添加结点
我们要准备三个指针:head,previous,pioneer,第一个头指针,链表被创建以后,它起着对链表的唯一标识作用,previous指针和pioneer指针用来在创建链表的过程中使用,具体使用方法,看下面代码,(代码中我采用了表头结点,个人觉得这要看兴趣)
//node struct
typedef struct Node
{
int data;
struct Node *next;
}Node;
node *create_list(int n)
{
/********************initialize*********************/
int a, i;
scanf("%d", &a);
node *head, *pioneer, *previous;//create 3 pointer
head = (node *)malloc(sizeof(node));//create a node
head->data = a; head->next = NULL;//initialize the node
pioneer = head;//initialize the pioneer
previous = head;//initialize the previous
/***********************************************/
/***************add node***********************/
for(i = 0; i < n - 1; i++){
scanf("%d", &a);
// 1:malloc space for the new node and let the pioneer point to it
pioneer = (node *)malloc(sizeof(node));
if(pioneer == NULL)
exit(1);
// 2:initiate the new node
pioneer->data = a;
pioneer->next = NULL;
// 3:use the previous to link the previous to the new node
previous->next = pioneer;
// 4:update the previous
previous = pioneer;
}
/***********************************************/
return head;
}
上面的代码是一个函数,这个函数的作用是创建一个包含n个结点的链表并返回头指针来标识这个链表供以后使用,整体框架在通过代码一目了然,previous和pioneer(是探路,不是过去,有向前进的意味)的意思与其作用相对应。
二、链表反转
void list_reverse(Node *head)
{
Node *p1, *p2, *p3;
p1 = head->next;
p2 = head->next->next;
head->next->next = NULL;
while(p2 != NULL){
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = p1;
}
三、表尾追加数据
void list_append(Node *head, int n)
{
Node *pioneer;
while(1){
if(head->next == NULL){
pioneer = (Node *)malloc(sizeof(Node));
if(pioneer == NULL)
exit(1);
pioneer->data = n;
pioneer->next = NULL;
head->next = pioneer;
break;
}
head = head->next;
}
}
四、表头追加数据
void list_add(Node *head, int n)
{
Node *pioneer;
pioneer = malloc(sizeof(Node));
if(pioneer == NULL)
exit(1);
pioneer->data = n;
pioneer->next = head->next;
head->next = pioneer;
}
五、删除表头表尾数据
void list_delete(Node *head)
{
head->next = head->next->next;
while(1){
if(head->next->next == NULL){
head->next = NULL;
break;
}
head = head->next;
}
}
六、在某个特定数后面插入数据
//Find the first data which is bigger than the given data,and insert the given data after the found data.
void list_insert(Node *head, int n)
{
Node *previous, *pioneer;
previous = head;
while(previous != NULL){
if(previous->data > n){
pioneer = (Node *)malloc(sizeof(Node));
if(pioneer == NULL)
exit(1);
pioneer->data = n;
pioneer->next = previous->next;
previous->next = pioneer;
previous = previous->next;
break;
}
previous = previous->next;
}
}
七、链表打印(递归实现)
void list_print(Node *head)
{
if(head->next == NULL){
printf("%d\n", head->data);
return;
} else {
printf("%d\n", head->data);
list_print(head->next);
}
}
八、链表逆序打印(递归实现)
void list_print_reverse(Node *head)
{
if(head->next == NULL){
printf("%d\n", head->data);
return;
} else {
list_print_reverse(head->next);
printf("%d\n", head->data);
}
}
九、求链表大小
int list_size(Node *head)
{
int count = 0;
while(1){
if(head->next == NULL){
break;
}
count++;
head = head->next;
}
return count;
}