看配套的PPT
链表的创建
链表的按序号查找
链表尾部插入
一个链表中要包含:数据和节点
段错误:指针非法访问,内存越界
链表按位置取值:
链表插入:
链表的删除:
链表的释放:
linklist.h
typedef int data_t;
typedef struct node{
data_t data;
struct node * next;
}listnode, * linklist;
linklist list_create();
int list_tail_insert(linklist H, data_t value);//Head
int list_show(linklist H);
int list_insert(linklist H, data_t value, int pos);//链表在新的位置插入一个节点
linklist list_get(linklist H, int pos);//得到特定位置节点的值
int list_delete(linklist H, int pos);//删除某一位置上的节点
linklist list_free(linklist H);//链表的释放
linklist.c
#include <stdio.h>
#include "linklist.h"
#include <stdlib.h>
linklist list_create()
{
linklist H;
H = (linklist)malloc(sizeof(listnode));
if(H == NULL)
{
printf("malloc failed\n");
return H;
}
H->data = 0;
H->next = NULL;
return H;
}
int list_tail_insert(linklist H, data_t value)//Head
{
linklist p;
linklist q;
if(H == NULL)
{
printf("H is empty\n");
return -1;
}
//1 new node p
if((p = (linklist)malloc(sizeof(listnode))) == NULL)
{
printf("malloc failed\n");
return -1;
}
p->data = value;
p->next = NULL;
//2 locate tail node
q = H;
while(q->next != NULL)
{
q = q->next;
}
//3 insert
q->next = p;
return 0;
}
int list_show(linklist H)
{
linklist p;
if(H == NULL)
{
printf("H is NULL\n");
return -1;
}
p = H;
while(p->next != NULL)
{
printf("%d ", p->next->data);
p = p->next;
}
puts(" ");
return 0;
}
int list_insert(linklist H, data_t value, int pos)
{
linklist p;
linklist q;
if(H == NULL)
{
printf("H is NULL\n");
}
//1 locate node p (pos-1)
p = list_get(H, pos-1);
if(p == NULL)
{
return -1;
}
//2 new node q
if((q = (linklist)malloc(sizeof(listnode))) == NULL)
{
printf("malloc failed\n");
return -1;
}
q->data = value;
q->next = NULL;
//3 insert
q->next = p->next;
p->next = q;
return 0;
}
linklist list_get(linklist H, int pos)
{
linklist p;
int i;
if(H == NULL)
{
printf("H is NULL\n");
return NULL;
}
if(pos == -1)
{
return H;
}
if(pos < -1)
{
printf("pos is invalid\n");
return NULL;
}
p = H;
i = -1;
while(i < pos)
{
p = p->next;
if(p == NULL)
{
printf("pos is invalid\n");
return NULL;
}
i++;
}
return p;
}
int list_delete(linklist H, int pos)
{
linklist p;
linklist q;
//1
if(H == NULL)
{
printf("H is NULL\n");
return -1;
}
//2 locate prior找到上一个节点
p = list_get(H, pos-1);
if(p == NULL)
return -1;
if(p->next == NULL)
{
printf("delete pos is invalid\n");
return -1;
}
//3 update list
q = p->next;
p->next = q->next;
//4 free
printf("free:%d\n",q->data);
free(q);
q = NULL;
return 0;
}
linklist list_free(linklist H)
{
linklist p;
if(H == NULL)
{
printf("H is NULL\n");
return NULL;
}
p = H;
printf("free:");
while(H != NULL)
{
p = H;
printf("%d ",p->data);
H = H->next;
free(p);
}
puts("");
return NULL;
}
test.c
#include "linklist.h"
#include <stdio.h>
int test_get(void);
int test_insert(void);
int test_delete(void);
int test_free(void);
int main(int argc, char *argv[])
{
test_free();
return 0;
}
int test_get(void)
{
linklist H;
linklist p;
int value;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
p = list_get(H, 0);
if(p != NULL)
{
printf("value:%d\n", p->data);
}
}
int test_insert(void)
{
linklist H;
linklist p;
int value;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
list_insert(H, 100, 4);
printf("*******************************\n");
list_show(H);
if(p != NULL)
{
printf("value:%d\n", p->data);
}
}
int test_delete(void)
{
linklist H;
linklist p;
int value;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
list_delete(H, 2);
printf("*******************************\n");
list_show(H);
}
int test_free(void)
{
linklist H;
linklist p;
int value;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
list_delete(H, 2);
printf("*******************************\n");
list_show(H);
printf("free before:H's position:%p\n",H);
H = list_free(H);
printf("free after:H's position:%p\n",H);
}