单链表

来源:互联网 发布:淘宝教育平台 编辑:程序博客网 时间:2024/05/29 16:24
#include<stdio.h>typedef struct{    int data;    int *next;}Lnode ,*Linklist;//头插法创建单链表//特点,每次插入一个节点需要的时间为O(1),插入n个节点所需要的时间为O(n) Linklist Creat(Linklist &L){    Lnode *s,int x;    L=(LinkList )malloc(sizeof(Lnode));    L->next = NULL;    scanf("%d",&x);    while(x!=9999){        s=(Lnode*)malloc(sizeof(Lnode));        s->data = x;        s->next = L->next;        L->next = s;        scanf("%d",s->data);    }    return L;}//尾插法创建单链表//时间复杂度和头插法相同 Linklist Creat1(Linklist &L){    int x;    L = (Linklist)malloc(sizeof(Lnode));    Lnode *s;    Lnode *r=L;    scanf("%d",&x);    while(x!=9999)    {        s= (Lnode *)malloc(sizeof(Lnode));        s->data = x;        r->next = s;        r=s;    }    r->next = NULL;    return L;}//按序号查找结点值Lnode *Getelem(Linklist L,int i){    if(i<1)    return false;    Lnode *p=L;    while(i==0)    {        p=p->next;        i--;        if(p->next == NULL)        return false;    }    i = p->data;    return i; } //按值查找表结点Lnode *Locate(Linklist L,int e){    int i=0;    Lnode *p = L;    while(p->data == e)    {        i++;        p = p->next;        if(p->data == NULL)        return false;    }    return i; }  //插入结点(伪) p = GetElem(L,i-1); //GetElem函数功能:得到插入位置的前驱结点 s->next = p->next; p->next = s; //删除结点(伪) p->GetElem(L,i=1); s = p->next; p->next = s->next; free(s);int mian(){}
原创粉丝点击