学习内容:单链表的初始化,插入,遍历,长度,倒序,获取元素,定位,删除清空等等。
主要函数实现:
#include <stdlib.h>
#include "LinkList.h"
int LinkInit(Node **l)
{
*l = (Node *)malloc(sizeof(Node)*1);
if(NULL == *l)
{
return FAILURE;
}
(*l)->next = NULL;
return SUCCESS;
}
int LinkInsert(Node *l ,int n,ElemType e)
{
Node *p=l;
int k=1;
if(NULL == l)
{
return FAILURE;
}
while(k < n || p == NULL)
{
p = p->next;
k++;
}
if(k > n || p ==NULL)
{
return FAILURE;
}
Node *q = (Node *)malloc(sizeof(Node)*1);
if(NULL == q)
{
return FAILURE;
}
q->data=e;
q->next=p->next;
p->next=q;
}
int LinkTraverse(Node *l,void (*p)(ElemType))
{
if(NULL == l)
{
return FAILURE;
}
Node *q = l;
while(q->next)
{
q = q->next;
p(q->data);
}
return SUCCESS;
}
int LinkLength(Node *l)
{
if(NULL == l)
{
return FAILURE;
}
int len = 0;
Node *p = l->next;
while(p)
{
len++;
p = p->next;
}
return len;
}
int LinkReverse(Node *l)
{
if(NULL == l)
{
return FAILURE;
}
Node *p =l->next;
l->next=NULL;
while(p !=NULL)
{
Node *q=p;
p=p->next;
q->next=l->next;
l->next=q;
}
return SUCCESS;
}
int LinkEmpty(Node *l)
{
return (l->next == NULL) ? TRUE : FALSE;
}
int GetElem(Node *l,int p,ElemType *e)
if(NULL == l || p<1)
{
return FAILURE;
}
Node *q = l;
int i;
for(i = 0; i < p && q != NULL; i++) {
q = q->next;
}
if(!q)
{
return FAILURE;
}
*e = q->data; return SUCCESS;
}
int LocateElem(Node *l,ElemType e,int (*p)(ElemType,ElemType))
{
if(NULL == l)
{
return FAILURE;
}
Node *q=l->next;
int len = 1;
while(q)
{
if(p(e,q->data) == TRUE)
{
return len;
}
q = q->next;
len++;
}
return FAILURE;
}
int LinkDelete(Node *l,int p,ElemType *e)
{
int k;
Node *q = l;
if(NULL == l)
{
return FAILURE;
}
while(k < p && q != NULL)
{
q =q->next;
k++;
}
if(k > p || q == NULL)
{
return FAILURE;
}
Node *n =q->next;
*e = n->data;
q->next = n->next;
free(n);
return SUCCESS;
}
int LinkClear(Node *l)
{
if(NULL == l)
{
return FAILURE;
}
Node *p = l->next;
while(p)
{
l->next = p->next;
free(p);
p=l->next;
}
return SUCCESS;
}
int LinkDestroy(Node **l)
{
if(NULL == l)
{
return FAILURE;
}
free(*l);
(*l)=NULL;
return SUCCESS;
}
主函数部分:
#include <stdio.h>
#include "LinkList.h"
void print(ElemType e)
{
printf("%d ",e);
}
int equal(ElemType e1,ElemType e2)
{
return (e1==e2) ? TRUE :FALSE;
}
int main()
{
int ret,i;
Node *first = NULL;
ret = LinkInit(&first);
srand(time(NULL));
if(ret == FAILURE)
{
printf("Init Failure!\n");
}
else
{
printf("Init 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");
}
}
ret=LinkTraverse(first,print);
if(ret == FAILURE)
{
printf("\nTraverse Failure!\n");
}
else
{
printf("\nTraverse Success!\n");
}
ret=LinkLength(first);
if(ret == FAILURE)
{
printf("\nGet Length Failure!\n");
}
else
{
printf("length:%d\n",ret);
}
ret=LinkReverse(first);
{
if(ret == FAILURE)
{
printf("\nReverse Failure!\n");
}
else
{
printf("Reverse Success!");
}
}
ret=LinkTraverse(first,print);
if(ret == FAILURE)
{
printf("\nTraverse Failure!\n");
}
else
{
printf("\nTraverse Success!\n");
}
ret=LinkEmpty(first);
if(ret == FAILURE)
{
printf("List is empty!\n");
}
else
{
printf("List is not empty!\n");
}
int p = 3;
ElemType e;
ret = GetElem(first, p ,&e);
if(ret == FAILURE)
{
printf("Get element failure!\n");
}
else
{
printf("The %dth element is %d\n",p,e);
}
e=4;
ret=LocateElem(first,e,equal);
if(ret == FAILURE)
{
printf(" %d is not exist!\n",e);
}
else
{
printf(" %dth element is %d\n",ret,e);
}
p=3;
ret=LinkDelete(first,p,&e);
if(ret == FAILURE)
{
printf("Delete Failure!\n");
}
else
{
printf("Delete %d Success!\n",e);
}
ret=LinkClear(first);
if(ret == FAILURE)
{
printf("Clear Failure!\n");
}
else
{
printf("Clear Success!\n",e);
}
ret=LinkDestroy(&first);
if(ret == FAILURE)
{
printf("Destroy Failure!\n");
}
else
{
printf("Destroy %d Success!\n",e);
}
return 0;
}
链表这部分不是太懂,明天继续研究。