linux c 单链表

头文件:

#ifndef _LINKLIST_H //有头结点
#define _LINKLIST_H
#define SUCCESS 10000
#define FAILURE 10001
#define TURE    10003
#define FALSE   10002

typedef int ElemType;
struct node //定义
{
    int data;
    struct node *next;
};
typedef struct node SNode;
int IinkInsert(SNode l,int n,ElemType e);//函数声明
int LinkInit(SNode **l);
int LinkTraver(SNode *l, void (*p)(ElemType));
int LinkLength(SNode *l);
int LinkGetelem(SNode *l,int p,ElemType *e);
int LinkEmpty(SNode *l);
int LocatElem(SNode *l,ElemType e,int (*p)(ElemType,ElemType));
int LinkDelete(SNode *l,int p,ElemType *e);
int LinkClear(SNode *l);
int LinkDestory(SNode **l);
int LinkRever(SNode *l);

#endif

 函数文件:

#include "LinkList.h"//有头结点
#include <stdlib.h>

int LinkInit(SNode **l)
{
    *l = (SNode *)malloc(sizeof(SNode)*1);
    if(NULL == *l)
    {
        return FAILURE;
    }
    (*l)->next = NULL;
    return SUCCESS;
}
int LinkInsert(SNode *l,int n,ElemType e)//插入元素
{
	SNode *p = l;
	int  k = 1;
	if(NULL == l)
	{
		return FAILURE;
	}
	while (k < n && NULL != p)
	{
		p = p->next;
		k++;
	}
	if (k > n || p == NULL)
	{
		return FAILURE;
	}
	SNode *q = (SNode *)malloc (sizeof(SNode)*1);
	q->data = e;
	q->next= p->next;
	p->next = q;
	return SUCCESS;
}
int LinkTraver(SNode *l, void (*p)(ElemType))//遍历
{
	if (NULL == l)
	{
		return FAILURE;
	}	
	SNode *q = l;
	while (q->next)
	{
		q = q -> next;
		p(q->data);
	}
	return SUCCESS;	
}
int LinkLength(SNode *l)//查看长度
{
	if(NULL == l)
	{
		return FAILURE;
	}
	SNode *q = l;
	int length = 0;
	while (q->next != NULL)
	{
		length++;
		q = q->next;
	}
	return length;
}
int LinkEmpty(SNode *l) //查看是否为空
{
	if(l == NULL)
	{
		return TURE;
	}
	if (l->next != NULL)
	{
		return FALSE;
	}
}	
int LinkGetelem(SNode *l,int p,ElemType *e)//显示p位置上的元素
{
	if(l == NULL || p < 1)
	{
		return FAILURE;
	}
	int i;
	SNode *q = l;
	for(i = 0;i < p && q != NULL;i++)
	{
		q = q->next;
	}
	if (!q)
	{
		return FAILURE;
	}
	*e = q->data;
	
	return SUCCESS;
}
int LocatElem(SNode *l,ElemType e,int (*p)(ElemType,ElemType))
{
	if(NULL == l)
	{
		return FAILURE;
	}
	int len = 1;
	SNode *q = l->next;
	while (q)
	{
		if (p(q->data, e) == TURE)
		{
			return len;
		}
		q = q->next;
		len++;
	}
	return FAILURE;
	
}
int LinkDelete(SNode *l,int p,ElemType *e)
{
	int k = 1;
	if(NULL == l)
	{
		return FAILURE;
	}
	SNode *q = l;
	while (k < p && q != NULL)
	{
		q = q -> next;
		k++;
	}
	if(k > p || q == NULL)
	{
		return FAILURE;
	}
	SNode *n = q -> next;
	*e = n -> data;
	q -> next = n -> next;
	free (n);
	return SUCCESS;
}
	
int LinkClear(SNode *l)
{
	if(NULL == l)
	{
		return FAILURE;
	}
	SNode *p = l -> next;
	while (p)
	{
		l -> next = p -> next;
		free(p);
		p = l -> next;
	}
	return SUCCESS;
}
int LinkDestory(SNode **l)
{
	if(NULL == l)
	{
		return FAILURE;
	}
	free(*l);
	(*l) = NULL;
	return SUCCESS;
}
int LinkRever(SNode *l)
{
	if(NULL == l)
	{
		return FAILURE;
	}
	SNode *p = l->next;
	l->next = NULL;
	while (p !=NULL)
	{
		SNode *q = p;
		p = p->next;
		q->next = l->next;
		l->next = q;
	}
	return SUCCESS;
}
	

	
	
	
	


 测试文件:

#include"LinkList.h"// 有头节点
#include <time.h>
#include <stdio.h>

void print(ElemType e)  //函数指针
{
	printf("%d ",e);
}

int equal(ElemType e1,ElemType e2) //函数指针
{
	return (e1 == e2)? TURE : FALSE;
}

int Large (ElemType e1,ElemType e2)
{
	return (e1 > e2)? TURE : FALSE;
}
int main()
{
    int ret;
    SNode *first = NULL;  //头指针
    ret = LinkInit(&first);
    if(ret == FAILURE)
    {
        printf("Init Failure!\n");
    }
    else
    {
        printf("Init Success!\n");
    }
	int i;
	srand(time(NULL));
	int p;
	ElemType e;
	for(i = 0;i < 10; i++)
	{
		p= LocatElem(first,rand()%20,Large); //插入
		if (p == FAILURE)
		{
			LinkInsert(&first,i + 1,
	}
	if ( ret == SUCCESS)	
	{
		printf("LinkInsert success!\n");
	}
	else 
	{
		printf("LinkInsert failure!\n");
	}
	ret = LinkTraver(first, print); //遍历
	if (ret == FAILURE)
	{
		printf("\n Traverfailure!\n");
	}
	else 
	{
		printf("\n Traver success!\n");
	}
	ret = LinkRever(first);// 反转
	if (ret == SUCCESS)
	{
		printf("Rever success!\n");
	}
	else 
	{
		printf("Rever failure!\n");
	}
	
	ret = LinkTraver(first, print); //遍历
	if (ret == FAILURE)
	{
		printf("\n Traverfailure!\n");
	}
	else 
	{
		printf("\n Traver success!\n");
	}
	
	ret = LinkLength(first);//查看长度
	if (ret == FAILURE)
	{
		printf("get length failure!\n");
	}
	else 
	{
		printf("%d ",ret);
	}
	ret = LinkEmpty(first);//检查是否为空
	if (ret == TURE)
	{
		printf("The list is emoty!\n");
	}
	else 
	{
		printf("The list is not empty!\n");
	}
	p = 5;
	ret = LinkGetelem(first,p,&e);//获取p位置上的元素
	if(ret == FAILURE)
	{
		printf("Get element failure!\n");
	}
	else 
	{
		printf("The %dth elem is %d!\n",p,e);
	}
	e = 10;
	ret = LocatElem(first,e,equal); //寻找
	if (ret == FAILURE)
	{
		printf("%d is not exist!\n",e);
	}
	else
	{
		printf("%d is %dth element!\n",e,ret);
	}
	p = 7;
	ret = LinkDelete(first,p,&e);//删除
	if (ret == SUCCESS)
	{
		printf("Delete %d success!\n",e);
	}
	else 
	{
		printf("Delete failure\n");
	}
	
	ret = LinkClear(first); //清空
	if (ret == SUCCESS)
	{
		printf("Clear success!\n");
	}
	else 
	{
		printf("Clear Failure!\n");
	}
	
	ret = LinkDestory(&first);//销毁
	if (ret == FAILURE)
	{
		printf("Destory failure!\n");
	}
	else 
	{
		printf("Destory success!\n");
	}
	
	for (i = 0; i < 10; i++)
	{
		ret = LinkInsert(first, i + 1, rand() % 20);
		if (ret == FAILURE)
		{
			printf("Insert Failure!\n");
		}
		else
		{
			printf("Insert Success!\n");
		}
	}
    return 0;
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值