目录
linklist.c
功能函数
#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
linklist list_create()
{
linklist H;
H = (linklist)malloc(sizeof(linklist));
if(H == NULL)
{
printf("malloc default\n");
return NULL;
}
H->data = 0;
H->next = NULL;
return H;
}
int list_tail_insert(linklist H,data_t value)
{
linklist p,q;
if(H == NULL)
{
printf("this list is NULL\n");
return -1;
}
if((p = (linklist)malloc(sizeof(linklist))) == NULL)
{
printf("malloc default\n");
return -1;
}
q = H;
while(q->next != NULL)
q = q->next;
p->data = value;
p->next = NULL;
q->next = p;
return 0;
}
#if 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;
}
#endif
#if 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;
}
p = H;
i = -1;
while (i < pos) {
p = p->next;
if (p == NULL) {
printf("pos is invalid\n");
return NULL;
}
i++;
}
return p;
}
#endif
#if 1
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;
for(i = -1;i < pos;i++)
{
p = p->next;
if (p == NULL)
{
printf("pos is invalid\n");
return NULL;
}
}
return p;
}
#endif
#if 1
int list_insert(linklist H, data_t value, int pos)
{
linklist p,q;
if(H == NULL)
{
printf("H is NULL\n");
return -1;
}
//list_tail_insert(H, 0);
p = list_get(H, pos-1);
if (p == NULL) {
return -1;
}
if ((q = (linklist)malloc(sizeof(listnode))) == NULL) {
printf("malloc failed\n");
return -1;
}
q->next = list_get(H, pos);
q->data = value;
p->next = q;
}
#endif
#if 1
int list_delete(linklist H, int pos)
{
linklist p,q;
p = list_get(H, pos-1);
q = list_get(H, pos);
if (H == NULL) {
printf("H is NULL\n");
return -1;
}
if (p == NULL)
return -1;
if (q == NULL) {
printf("delete pos is invalid\n");
return -1;
}
p->next = q->next;
printf("%d\n",q->data);
free(q);
q = NULL;
return 0;
}
#endif
#if 1
linklist list_free(linklist H)
{
linklist p;
if (H == NULL)
return NULL;
p = H;
printf("free:");
for(;H->next != NULL;)
{
p = H;
printf("%d ",p->data);
H = H->next;
free(p);
}
puts("");
return NULL;
}
#endif
#if 1
int list_show(linklist H)
{
if(H == NULL)
{
printf("H is NULL\n");
return -1;
}
linklist p;
p = H;
for(;p->next != NULL;p = p->next)
{
printf("%d ",p->next->data);
}
//printf("%d",p->data);
puts("");
return 0;
}
#endif
linklist.h
对功能函数的声明以及结构体的定义
typedef int data_t;
typedef struct node{
struct node * next;
data_t data;
}listnode,* linklist;
linklist list_create();
int list_tail_insert(linklist H,data_t value);
linklist list_get(linklist H,int pos);
int list_insert(linklist H, data_t value, int pos);
int list_delete(linklist H, int pos);
linklist list_free(linklist H);
int list_show(linklist H);
test.c
主函数和验证函数
#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
void test_1();
void test_2();
int main()
{
//test_2();
linklist H;
int value;
H = list_create();
if (H == NULL)
return -1;
printf("input:");
while (1) {
scanf("%d", &value);
if (value == -1)
break;
list_tail_insert(H, value);
printf("input:");
}
list_show(H);
printf("H=%p\n", H);
H = list_free(H);
printf("H=%p\n", H);
list_delete(H, -4);//1 3 5 7
list_show(H);
list_free(H);
return 0;
}
void test_1()
{
linklist H;
H = list_create();
list_tail_insert(H, 1);
list_tail_insert(H, 2);
list_tail_insert(H, 3);
list_tail_insert(H, 4);
list_show(H);
printf("%d\n",list_get(H, 2)->data);
printf("*****************************\n");
list_show(H);
list_insert(H, 20, 1);
list_show(H);
}
void test_2()
{
linklist H;
H = list_create();
int n;
printf("input:");
while(1){
scanf("%d",&n);
if(n == -1){
break;
}
list_tail_insert(H, n);
}
list_show(H);
printf("**********************************\n");
list_delete(H, 2);
list_show(H);
}
运行结果