https://blog.csdn.net/endeavor_g/article/details/80552680

https://blog.csdn.net/qq_39993896/article/category/7319745

这个详细

顺序表:https://blog.csdn.net/lady_killer9/article/details/82695770

//链表
typedef struct lnode *list;
struct lnode{
    elementtype data;
    list next;
};
struct lnode l;
list ptrl;

//建有头结点链表
list creatlist(int n){
    list head,node,end;
    head = (list)malloc(sizeof(struct lnode));
    end = head;
    for(int i=0; i<n; i++){
        node = (list)malloc(sizeof(struct lnode));
        scanf("%d",&node->data);
        end->next = node;
        end = node;
    }
    end->next = null;
    return head;
}

//建立无头结点链表
list creat0list(int n){
    list head,end;
    elementtype x;
    while(n--){
        scanf("%d",&x);
        list node;
        node = (list)malloc(sizeof(struct lnode));
        node->data = x;
        node->next = null;
        if(head == null){
            head = node;
            end = head;
        }
        else{
            end->next = node;
            end = node;
        }
    }
    return head;
}

//求表长
int length(list ptrl){
    list p = ptrl;
    int j = 0;
    while(p){
        p = p->next;
        j++;
    }
    return j;
}
//查找,按序号第k个
list findkth(int k,list ptrl){
    list p = ptrl;
    int i = 1;
    while(p && i<k){
        p = p->next;
        i++;
    }
    if(i == k)
        return p;
    else
        return null;
}
//查找,按值查找
list find(elementtype x,list ptrl){
    list p = ptrl;
    while(p && p->data != x){
        p = p->next;
    }
    return p;
}
//插入,插在第i-1结点的后面
list insert(elementtype x,int i,list ptrl){
    list s,p;
    if(i == 1){
        s=(list)malloc(sizeof(struct lnode));
        s->data=x;
        s->next = ptrl;
        return s;
    }
    p = findkth(i-1,ptrl);
    if(p == null){
        printf("位置错误");
        return null:
    }
    else{
        s = (list)malloc(sizeof(struct lnode));
        s->data = x;
        s->next = p->next;
        p->next = s;
        return ptrl;
    }
}
//删除,删第i个结点
list delete(int i,list ptrl){
     list s,p;
     if(i == 1){
        s = ptrl;
        if(ptrl != null)
            ptrl = ptrl->next;
        else 
            return null;
        free(s);
        return ptrl;
     }
     p = findkth(i-1,list ptrl);
     if(p == null){
        printf("位置错误");//删的前面的没有
        return null;    
     }
     else if(p->next == null){
        printf("位置错误");//要删的没有
        return null;
     }
     else{
        s = p->next;
        p->next = s->next;
        free(s);
        return ptrl; 
     }
}

 双向链表

typedef struct lnode *list;
struct lnode{
    int data;
    list next;
    list prex;
};
struct lnode l;
list ptrl;

//建无表头
list creatlist(int n){
    list head=null,node=null,end=null;
    int x;
    while(n--){
        scanf("%d",&x);
        list node;
        node = (list)malloc(sizeof(struct lnode));
        node->data = x;
        node->next = null;
        node->prex = end;
        if(head == null){
            head = node;
            end = head;
        }
        else{
            end->next = node;
            end = node;
        }
    }
    return head;
}

list findkth(int k,list ptrl){
    list p = ptrl;
    int j = 1;
    while(p && j<k){
        p = p->next;
        j++;
    }
    if(j == k){
        return p;
    }
    else
        return null;
}


//前遍历
void printlist(list ptrl){
    list p = ptrl;
    while(p != null){
        printf("%dt",p->data);
        p = p->next;
    }
}

//插入,把x插在i-1结点后
list insert(int x,int i,list ptrl){
    list s,p;
    if(i == 1){
        s = (list)malloc(sizeof(struct lnode));
        s->data = x;
        s->next = ptrl;
        s->prex = null;
        return s;
    }
    p =  findkth(i-1,ptrl);
    if(p == null){
        printf("位置错误n");
        return null;
    }
    else if(p->next == null){
        s = (list)malloc(sizeof(struct lnode));
        s->data = x;
        s->next = p->next;
        s->prex = p;
        p->next = s;
        return ptrl;
    }
    else{
        s = (list)malloc(sizeof(struct lnode));
        s->data = x;
        p->next->prex = s;
        s->next = p->next;
        s->prex = p;
        p->next = s;
        return ptrl;
    }
}

//删除,删第i个
list deletelist(int i,list ptrl){
    list p;
    p = ptrl;
    i--;
    while(i--){
        p = p->next;
    }
    p->prex->next = p->next;
    p->next->prex = p->prex;
    free(p);
    return ptrl;
}
分类: 未分类

0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注