单链表也是一种链式存取的线性表,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,以next指针指向下一个节点而连接起来,相比于顺序表,链表有着快速增长,删除节点的优点,其节点的随机访问效率较低。ide
头文件:函数
/***************************************************************************************************** *Copyright:Yue Workstation * *FileName:LineTable.h * *Function:单链表相关数据定义和函数声明 * *Author:Abel Lee * *CreateOn:2011-5-3 * *Log:2011-5-3 由Abel Lee建立 *****************************************************************************************************/ #ifndef SINGLE_LIST_H #define SINGLE_LIST_H #include "global.h" typedef struct LNode { ElemType data; struct LNode *next; }LNode,*LinkList; int CreateSingleList(LinkList *L,int n); void PrintSingleList(LinkList L); int InsertSingleList(LinkList *L,int i,ElemType e); int DeleteSingleList(LinkList *L,int i,ElemType *e); int GetSingleListLength(LinkList L); void DestroySingleList(LinkList *L); #endif
源文件:指针
/***************************************************************************************************** *Copyright:Yue Workstation * *FileName:SingleList.c * *Function:单链表基本操做 * *Author:Abel Lee * *CreateOn:2011-5-3 * *Log:2011-5-3 由Abel Lee建立 *****************************************************************************************************/ #include "../inc/SingleList.h" /**************************************************************************************************** *Function Name:CreateSingleList * *Function:建立一个单链表 * *Parameter: L:单链表表头, * n:单链表长度 * *Return Value:成功返回0,失败返回-1 * *Author:Abel Lee * *Log:2011-5-24 ***************************************************************************************************/ int CreateSingleList(LinkList *L,int n) { int i = 0; LinkList p1,p2; *L = (LinkList)malloc(sizeof(LNode)); if(*L == NULL) { perror("malooc error\n"); return -1; } (*L)->data = 0; (*L)->next = NULL; p1 = *L; p2 = *L; for(i=1; i<=n; i++) { p1 = (LinkList)malloc(sizeof(LNode)); if(p1 == NULL) { perror("malooc error\n"); return -1; } p1->data = i; p2->next = p1; p2 = p1; } p2->next = NULL; (*L)->data = n; return 0; } /**************************************************************************************************** *Function Name:PrintSingleList * *Function:打印单链表表中的元素 * *Parameter: L:单链表表头 * *Return Value:无 * *Author:Abel Lee * *Log:2011-5-24 ***************************************************************************************************/ void PrintSingleList(LinkList L) { L = L->next; while(L) { printf("%d---",L->data); L = L->next; } putchar('\n'); return ; } /**************************************************************************************************** *Function Name:InsertSingleList * *Function:在i位置插入一个元素e * *Parameter: L:单链表表头, * i:元素位置 * e:要插入的元素 * *Return Value:成功返回0,失败返回-1 * *Author:Abel Lee * *Log:2011-5-24 ***************************************************************************************************/ int InsertSingleList(LinkList *L,int i,ElemType e) { LinkList p1 = (*L)->next; LinkList p2 = (*L)->next; int j = 1; while(p1 && j<i-1) { p1 = p1->next; ++j; } if(!p1 || j>i-1) { perror("Insert position error,the parameter i is error!\n"); return -1; } p2 = (LinkList)malloc(sizeof(LNode)); p2->data = e; p2->next = p1->next; p1->next = p2; (*L)->data += 1; return 0; } /**************************************************************************************************** *Function Name:DeleteSingleList * *Function:在制定位置删除单链表中的元素 * *Parameter: L:单链表表头, * i:元素位置 * e:要插入的元素 * *Return Value: 成功返回0,失败返回-1 * *Author:Abel Lee * *Log:2011-5-24 ***************************************************************************************************/ int DeleteSingleList(LinkList *L,int i,ElemType *e) { LinkList p1 = (*L)->next; LinkList p2 = NULL; int j = 1; while(p1 && j<i-1) { p1 = p1->next; ++j; } if(!p1 || j>=i) { perror("Delete position error,the parameter i is error!\n"); return -1; } p2 = p1->next; p1->next = p2->next; *e = p2->data; free(p2); (*L)->data -= 1; return 0; } /**************************************************************************************************** *Function Name:GetSingleListLength * *Function:获取单链表长度 * *Parameter: L:单链表表头 * *Return Value:单链表长度 * *Author:Abel Lee * *Log:2011-5-24 ***************************************************************************************************/ int GetSingleListLength(LinkList L) { if(L == NULL) { return -1; } return L->data; } /**************************************************************************************************** *Function Name:DestroySingleList * *Function:销毁一个单链表 * *Parameter: L:单链表表头 * *Return Value:无 * *Author:Abel Lee * *Log:2011-5-24 ***************************************************************************************************/ void DestroySingleList(LinkList *L) { LinkList p1 = *L; LinkList p2 = *L; while(p1 != NULL) { p2 = p1; p1 = p1->next; free(p2); } *L = NULL; return; }