单链表的初始化,插入,遍历,长度,倒序,删除等等

学习内容:单链表的初始化,插入,遍历,长度,倒序,获取元素,定位,删除清空等等。

主要函数实现:

#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;
}

 

链表这部分不是太懂,明天继续研究。 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值