线性表的链式存储实现(无头结点)(陈越数据结构)

//线性表的链式存储实现(无头结点)
#include <stdio.h>
struct node{
    int element;
    struct node* next;
};
void Find2(struct node* head,int k)
{
    struct node* p;
    p=head;
    int location=1;
    while(p&&location<k){
        p=p->next;
        location++;
    }
    if(p&&location==k)
        printf("%d\n",p->element);
    else
        printf("Not Found\n");
    return;
}
struct node* Insert(struct node* head,int i,int number)
{
    struct node* t;
    t=(struct node*)malloc(sizeof(struct node));
    t->element=number;
    t->next=NULL;
    if(i==1){
      head=t;
      return head;

    }
    int j=1;
    struct node* p;
    p=head;
    while(p&&j<i-1){
        p=p->next;
        j++;
    }
    if(p==NULL){
        printf("Error\n");
        free(t);
        return head;
    }
    t->next=p->next;
    p->next=t;
    return head;

}
struct node* Delete(struct node* head,int i)
{
     if(i==1){
        struct node* temp;
        temp=head;
        head=head->next;
        free(temp);
        return head;
     }
     struct node* p;
     int location=1;
     while(location<i-1&&p){
         p=p->next;
         location++;
     }
     if(location!=i-1||p->next==NULL){//元素不到i或者i位置为NULL
        printf("Error\n");
        return head;
     }
     struct node* temp=p->next;
     p->next=temp->next;
     free(temp);
     return head;
};
struct node*Init(struct node* head)
{
    return NULL;
};
int main()
{
    struct node* head;
    //初始化
    head=Init(head);
    //按值查找
//    Find1(head,number);//略
    //序号查找
    Find2(head,1);
    //插入
    head=Insert(head,1,10);//插到第i个位置
    //删除
    head=Delete(head,1);//删除第i个元素

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值